You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

56 lines
1.2 KiB

if (!window.dicts) {
window.dicts = {}
}
function Segment() {
//初始化词库
let mappers = {}
for (let key in dicts) {
let array = dicts[key];
array.map((value, index) => {
mappers[value.toUpperCase()] = key;
});
}
this.getMappers = function () {
return mappers;
}
this.cut = function (sentence) {
let start = 0, end = sentence.length - 1;
while (start !== end) {
let str = [];
for (let i = start; i <= end; i++) {
let s = sentence.substring(i, i + 1);
str.push(s);
// 如果在字典中,则添加到分词结果集
let val = mappers[str.join('').toUpperCase().replace(/ /g, '')];
if (val) {
return val;
}
}
start++;
}
}
}
const segment = new Segment();
function getIcon(name, icon) {
if(!name){
return;
}
let value = 'far fa-circle';
if (icon) {
//有默认图标的
if (icon !== value) {
return icon;
}
}
return segment.cut(name) || value;
}