|
OpenHarmony**中文输入法**
1. 项目介绍
在系统输入法中加上中文输入法,首先了解中文输入法的开发需要什么功能,中英文切换入口、单字、词语、分词、词语联想及九宫格键盘等功能。
当前针对单字、词语、分词、词语联想所使用的数据库,先进行读写后词语时进行分库,将23个声母分开(此处因为只有应用层技术)后续需要封装词库
项目实现逻辑:
把不同的键盘分别封装,各自处理对应的逻辑及响应点击事件
https://forums.openharmony.cn/file:///C:/Users/zhang/AppData/Local/Temp/msohtmlclip1/01/clip_image002.jpg
https://forums.openharmony.cn/file:///C:/Users/zhang/AppData/Local/Temp/msohtmlclip1/01/clip_image004.jpg
import windowManager from '@ohos.window'- 将KeyboardController 设置为窗口最上层控件
复制代码- 用于处理隐藏、显示窗口,监听键盘点击事件,处理响应的逻辑后的回调
复制代码- [/code][code]记录用户的每一次字母键盘事件点击,通过getSelectNotes查询对应的单字中文
复制代码- 通过getSelectWords查询选中单词的关联词组
复制代码 export function getChoiceWords(pinyin:string): [string]{- let allWords: [string] = [];
复制代码- let choiceWord: [string] =[];
复制代码- if(pinyin.indexOf('a',0)==0){
复制代码- choiceWord = getSelectWords3(pinyin,allWords)
复制代码- if(choiceWord.length != 0){
复制代码- console.info(logTag + "getChoiceWords_6: " + JSON.stringify(choiceWord))
复制代码 2. 遇到问题
A. api8、9两个版本的接口有很多不同,api9中弃用了很多api8的接口,导致工程无法唤起,而且studio中没有报错信息,将项目用api9实现
B. 词组量大,某些词组合后编码过长,导致工程无法唤起,util.TextDecoder("utf-8") 将问题解决但解决不彻底,可能个别词组的文字无法进行解析,分词逻辑缩小词库的大小
C. 使用++来进行赋值,导致工程无法唤起
D. 循环方式,导致工程无法唤起
for(let i = 0;i < charList.length;i++){} 改为for(var i in charList.length) {}可解决
3. 目前进度
现在进行分词逻辑,将23个声母添加到数组中,其余韵母加上组合放到数组中,用户输入后进行声母韵母的组合后进行匹配 然后将组合好的拼音传到词库位置查找词组返回到用户的输入法页面。const TAG = 'SpellTool'
var result: string = '';
var ym: Array = [- [/code]export function trimSpecll(spell: string) {
- [code] result = '';
复制代码- console.info(TAG + ' trimSpecll')
复制代码- console.info(TAG + ' trimSpecll_0 ' + spell.length)
复制代码- [/code]function findpy(py: string) {
- [code] console.info(TAG + ' findpy_01 ' + py)
复制代码- let py2: [string] = py.split("");
复制代码- console.info(TAG + ' findpy_02 ' + py2.toString())
复制代码- for (var i = 0; i < ym.length; i++) {
复制代码- for (var j = 0; j 2){[/b]
- [b]// [/b][b]多字,两个或两个字以上的情况[/b]
- [b] console.info(logTag + "getChoiceWords_10: need fc " + selStr +' cici '+ cici.toString())[/b]
- [b] section = getChoiceWords(selStr);[/b]
- [b] console.info(logTag + "getChoiceWords_10: need fc2 "+ selStr +' ' + (section == null) + 'length ' + section.length)[/b]
- [b] if(section!=null&§ion.length==0){[/b]
- [b]// [/b][b]不能一次完成搜索的分词词汇,比如拼音anbomo[/b]
- [b] curFenciPy = fenci[/b]
- [b] console.info(logTag + "getChoiceWords_10: need fd " + fenci)[/b]
- [b] let fenci1:[string] = cici[/b]
- [b] console.info(logTag + "getChoiceWords_10: need fe " + fenci1[0])[/b]
- [b] for (var index = 0; index < fenci1.length-1; index++) {[/b]
- [b] console.info(logTag + "getChoiceWords_10: need ff " + fenci1[index])[/b]
- [b] }[/b]
- [b] curPosition = 0;[/b]
- [b] curFenci = fenci1;[/b]
- [b]// [/b][b]不能一次完成搜索的分词词汇,比如拼音anbomo,则从第一个开始搜索[/b]
- [b] section = getChoiceWords(fenci1[0]);[/b]
- [b] }else{[/b]
- [b]// [/b][b]能一次完成搜索的分词词汇,比如拼音baicai,无需特殊处理,需要把当前分词的拼音数组置为[][/b]
- [b] console.info(logTag + "getChoiceWords_10: need fc2a "+ selStr +' ' + (section == null) + ', fenci ' + fenci)[/b]
- [b] curFenciPy = fenci;[/b]
- [b] curFenci = [][/b]
- [b] }[/b]
- [b] } else{[/b]
- [b]// [/b][b]单字,正常操作[/b]
- [b] curFenciPy = fenci;[/b]
- [b] curFenci = [];[/b]
- [b] section = getChoiceWords(selStr);[/b]
- [b] }[/b]
- [b] console.info(logTag + "getChoiceWords_10: need fg " + section)[/b]
- [b] AppStorage.SetOrCreate('selTexList',section)[/b]
- [b] if(curFenciPy.length>0){[/b]
- [b] AppStorage.SetOrCreate('promptText',curFenciPy)[/b]
- [b] }[/b]
- 分词选取处理逻辑:
- [list=1]判断是否是未处理完全的分词,如果则从指定的position进行查询,将查询到的结果进行存储;正常的词汇不用特殊处理;对于还未查询选择完的分词,使用混合排版的方式,放入到输入框。比如:安博mu (分词拼音为: an bo mu)
- [/list][b]下面是根据分词情况进行字词选取逻辑[/b]
- // 判断是是分词,词汇和单字类型的已被过滤掉
- if(curFenci.length>0){
- [code] curPosition ++;
复制代码- console.log('aaa-->listItem fcP '+curPosition)
复制代码- let section1 = getChoiceWords(curFenci[curPosition]);
复制代码- console.log('aaa-->listItem fcP '+section1)
复制代码- AppStorage.SetOrCreate('selTexList',section1);
复制代码- AppStorage.SetOrCreate('inputSelTexts',selText)
复制代码- console.log('aaa-->listItem fcP_01 ' + curFenci.length + "textlength "+curFenci.toString()+ ', selText ' + selText.length + " , " + selText.toString() )
复制代码 if(curFenci.length - 1>selText.length){- console.log('aaa-->listItem fcP_02 ')
复制代码- for(var i = 0; i< selText.length; i++){
复制代码- textFenci.push(selText[i])
复制代码- console.log('aaa-->listItem fcP_03 ')
复制代码- for (var j = selText.length; j < curFenci.length-1; j++) {
复制代码- textFenci.push(curFenci[j])
复制代码- console.log('aaa-->listItem fcP_04 ')
复制代码- AppStorage.SetOrCreate('inputSelTexts',textFenci)
复制代码- this.isClickShow = !this.isClickShow
复制代码- AppStorage.SetOrCreate('isMoreText',this.isClickShow)
复制代码 4.**后续想法**
首先是将分词进行完成 目前分词的词库已经写完,逻辑正在进行,后续进行九宫格的开发以及词库的封装。 |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?立即注册
x
|