Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 自定义词典词 length 适配双字节字符 #247

Merged
merged 1 commit into from
Jun 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/common/segmentit/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,10 @@ export class AC {
}

addNodeToQueues(trieNode: TrieNode) {
if (!this.queues[trieNode.prefix.length]) {
this.queues[trieNode.prefix.length] = [];
if (!this.queues[stringLength(trieNode.prefix)]) {
this.queues[stringLength(trieNode.prefix)] = [];
}
this.queues[trieNode.prefix.length].push(trieNode);
this.queues[stringLength(trieNode.prefix)].push(trieNode);
}

// 按照优先级插入 pattern
Expand Down
3 changes: 2 additions & 1 deletion lib/data/special.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
getPinyinWithoutTone,
} from '@/core/pinyin/handle';
import DICT1 from './dict1';
import { stringLength } from '@/common/utils';

export const InitialList = [
'zh',
Expand Down Expand Up @@ -167,7 +168,7 @@ export const PatternNumberDict: Pattern[] = Object.keys(NumberDict).map(
zh: key,
pinyin: NumberDict[key],
probability: Probability.Rule,
length: key.length,
length: stringLength(key),
priority: Priority.Normal,
dict: Symbol('rule'),
})
Expand Down
6 changes: 4 additions & 2 deletions lib/data/surname.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Probability, Priority } from '@/common/constant';
import { stringLength } from '@/common/utils';
import type { Pattern } from '../common/segmentit';

const Surnames: { [key: string]: string } = {
南宫: 'nán gōng',
第五: 'dì wǔ',
Expand Down Expand Up @@ -498,8 +500,8 @@ export default Surnames;
export const PatternSurname: Pattern[] = Object.keys(Surnames).map((key) => ({
zh: key,
pinyin: Surnames[key],
probability: Probability.Surname + key.length,
length: key.length,
probability: Probability.Surname + stringLength(key),
length: stringLength(key),
priority: Priority.Surname,
dict: Symbol('surname'),
}));
Loading