Files
souli-web/src/components/InputMethod/assets/dicUtil.js
T
2026-04-29 18:29:59 +08:00

42 lines
1.1 KiB
JavaScript

import { dict } from './dic.js'
let SimpleInputMethod = {
dict: {}
}
SimpleInputMethod.initDict = function() {
this.dict.py2hz = dict;
this.dict.py2hz2 = {};
this.dict.py2hz2['i'] = 'i'; // i比较特殊,没有符合的汉字,所以特殊处理
for (let key in this.dict.py2hz) {
let ch = key[0];
if (!this.dict.py2hz2[ch]) {
this.dict.py2hz2[ch] = this.dict.py2hz[key];
}
}
};
SimpleInputMethod.getSingleHanzi = function(pinyin){
return this.dict.py2hz2[pinyin] || this.dict.py2hz[pinyin] || '';
}
SimpleInputMethod.getHanzi = function(pinyin) {
let result = this.getSingleHanzi(pinyin);
if (result) return [result.split(''), pinyin];
let temp = '';
let start = Math.min(pinyin.length, 6);
for (let i = start; i >= 1; i--) {
let str = pinyin.substr(0, i);
let rs = this.getSingleHanzi(str);
if (rs) return [rs.split(''), str];
}
return [[], '']; // 理论上一般不会出现这种情况
};
SimpleInputMethod.initDict();
export { SimpleInputMethod } //换成export default SimpleInputMethod;不能用