-
Notifications
You must be signed in to change notification settings - Fork 839
/
test.ts
396 lines (358 loc) · 13 KB
/
test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
import pinyin, { compare, compact, IPinyinStyle } from "../index";
describe("pinyin() without param", function() {
it("pinyin() => []", function() {
// @ts-ignore
expect(pinyin()).toEqual([]);
});
});
const cases: any[] = [
// 单音字
[ "我", {
STYLE_NORMAL: [["wo"]],
STYLE_PASSPORT: [["WO"]],
STYLE_TONE: [["wǒ"]],
STYLE_TONE2: [["wo3"]],
STYLE_TO3NE: [["wo3"]],
STYLE_INITIALS: [[""]],
STYLE_FIRST_LETTER: [["w"]],
} ],
[ "〇", {
STYLE_NORMAL: [["ling", "xing"]],
STYLE_PASSPORT: [["LING", "XING"]],
STYLE_TONE: [["líng", "xīng"]],
STYLE_TONE2: [["ling2", "xing1"]],
STYLE_TO3NE: [["li2ng", "xi1ng"]],
STYLE_INITIALS: [["l", "x"]],
STYLE_FIRST_LETTER: [["l", "x"]],
} ],
// 多音字
[ "中", {
STYLE_NORMAL: [["zhong"]],
STYLE_PASSPORT: [["ZHONG"]],
STYLE_TONE: [["zhōng", "zhòng"]],
STYLE_TONE2: [["zhong1", "zhong4"]],
STYLE_TO3NE: [["zho1ng", "zho4ng"]],
STYLE_INITIALS: [["zh"]],
STYLE_FIRST_LETTER: [["z"]],
} ],
// 元音字
["爱", {
STYLE_NORMAL: [["ai"]],
STYLE_PASSPORT: [["AI"]],
STYLE_TONE: [["ài"]],
STYLE_TONE2: [["ai4"]],
STYLE_TO3NE: [["a4i"]],
STYLE_INITIALS: [[""]],
STYLE_FIRST_LETTER: [["a"]],
} ],
["啊", {
STYLE_NORMAL: [["a"]],
STYLE_PASSPORT: [["A"]],
STYLE_TONE: [["ā", "á", "ǎ", "à", "a"]],
STYLE_TONE2: [["a1", "a2", "a3", "a4", "a"]],
STYLE_TO3NE: [["a1", "a2", "a3", "a4", "a"]],
STYLE_INITIALS: [[""]],
STYLE_FIRST_LETTER: [["a"]],
} ],
// YU
["吕", {
STYLE_NORMAL: [["lv"]],
STYLE_PASSPORT: [["LYU"]],
STYLE_TONE: [["lǚ"]],
STYLE_TONE2: [["lv3"]],
STYLE_TO3NE: [["lv3"]],
STYLE_INITIALS: [["l"]],
STYLE_FIRST_LETTER: [["l"]],
} ],
// 单音词
[ "我是谁", {
STYLE_NORMAL: [["wo"], ["shi"], ["shui"]],
STYLE_PASSPORT: [["WO"], ["SHI"], ["SHUI"]],
STYLE_TONE: [["wǒ"], ["shì"], ["shuí"]],
STYLE_TONE2: [["wo3"], ["shi4"], ["shui2"]],
STYLE_TO3NE: [["wo3"], ["shi4"], ["shui2"]],
STYLE_INITIALS: [[""], ["sh"], ["sh"]],
STYLE_FIRST_LETTER: [["w"], ["s"], ["s"]],
} ],
// 多音字,单音词。分词后可以准确识别读音。
[ "中国", {
STYLE_NORMAL: [["zhong"], ["guo"]],
STYLE_PASSPORT: [["ZHONG"], ["GUO"]],
STYLE_TONE: {
normal: [["zhōng", "zhòng"], ["guó"]],
segment: [["zhōng"], ["guó"]],
},
STYLE_TONE2: {
normal: [["zhong1", "zhong4"], ["guo2"]],
segment: [["zhong1"], ["guo2"]],
},
STYLE_TO3NE: {
normal: [["zho1ng", "zho4ng"], ["guo2"]],
segment: [["zho1ng"], ["guo2"]],
},
STYLE_INITIALS: [["zh"], ["g"]],
STYLE_FIRST_LETTER: [["z"], ["g"]],
} ],
[ "重心", {
STYLE_NORMAL: {
normal: [["zhong", "chong"], ["xin"]],
segment: [["zhong"], ["xin"]],
},
STYLE_PASSPORT: {
normal: [["ZHONG", "CHONG"], ["XIN"]],
segment: [["ZHONG"], ["XIN"]],
},
STYLE_TONE: {
normal: [["zhòng", "chóng"], ["xīn"]],
segment: [["zhòng"], ["xīn"]],
},
STYLE_TONE2: {
normal: [["zhong4", "chong2"], ["xin1"]],
segment: [["zhong4"], ["xin1"]],
},
STYLE_TO3NE: {
normal: [["zho4ng", "cho2ng"], ["xi1n"]],
segment: [["zho4ng"], ["xi1n"]],
},
STYLE_INITIALS: {
normal: [["zh", "ch"], ["x"]],
segment: [["zh"], ["x"]],
},
STYLE_FIRST_LETTER: {
normal: [["z", "c"], ["x"]],
segment: [["z"], ["x"]],
},
} ],
// 英文
[ "a", {
STYLE_NORMAL: [["a"]],
STYLE_TONE: [["a"]],
STYLE_TONE2: [["a"]],
STYLE_TO3NE: [["a"]],
STYLE_INITIALS: [["a"]],
STYLE_FIRST_LETTER: [["a"]],
} ],
[ "aa", {
STYLE_NORMAL: [["aa"]],
STYLE_TONE: [["aa"]],
STYLE_TONE2: [["aa"]],
STYLE_TO3NE: [["aa"]],
STYLE_INITIALS: [["aa"]],
STYLE_FIRST_LETTER: [["aa"]],
} ],
[ "a a", {
STYLE_NORMAL: [["a a"]],
STYLE_TONE: [["a a"]],
STYLE_TONE2: [["a a"]],
STYLE_TO3NE: [["a a"]],
STYLE_INITIALS: [["a a"]],
STYLE_FIRST_LETTER: [["a a"]],
} ],
[ "一 一", {
STYLE_NORMAL: [["yi"], [" "], ["yi"]],
STYLE_TONE: [["yī"], [" "], ["yī"]],
STYLE_TONE2: [["yi1"], [" "], ["yi1"]],
STYLE_TO3NE: [["yi1"], [" "], ["yi1"]],
STYLE_INITIALS: [[""], [" "], [""]],
STYLE_FIRST_LETTER: [["y"], [" "], ["y"]],
} ],
// 中英混合
[ "拼音(pinyin)", {
STYLE_NORMAL: [["pin"], ["yin"], ["(pinyin)"]],
STYLE_TONE: [["pīn"], ["yīn"], ["(pinyin)"]],
STYLE_TONE2: [["pin1"], ["yin1"], ["(pinyin)"]],
STYLE_TO3NE: [["pi1n"], ["yi1n"], ["(pinyin)"]],
STYLE_INITIALS: [["p"], [""], ["(pinyin)"]],
STYLE_FIRST_LETTER: [["p"], ["y"], ["(pinyin)"]],
} ],
// 中英混合,多音字,单音词。
[ "中国(china)", {
STYLE_NORMAL: [["zhong"], ["guo"], ["(china)"]],
STYLE_TONE: {
normal: [["zhōng", "zhòng"], ["guó"], ["(china)"]],
segment: [["zhōng"], ["guó"], ["(china)"]],
},
STYLE_TONE2: {
normal: [["zhong1", "zhong4"], ["guo2"], ["(china)"]],
segment: [["zhong1"], ["guo2"], ["(china)"]],
},
STYLE_TO3NE: {
normal: [["zho1ng", "zho4ng"], ["guo2"], ["(china)"]],
segment: [["zho1ng"], ["guo2"], ["(china)"]],
},
STYLE_INITIALS: [["zh"], ["g"], ["(china)"]],
STYLE_FIRST_LETTER: [["z"], ["g"], ["(china)"]],
} ],
[ "彷徨", {
STYLE_NORMAL: {
normal: [["pang", "fang"], ["huang"]],
segment: [["pang"], ["huang"]],
},
STYLE_TONE: {
normal: [["páng", "fǎng"], ["huáng"]],
segment: [["páng"], ["huáng"]],
},
STYLE_TONE2: {
normal: [["pang2", "fang3"], ["huang2"]],
segment: [["pang2"], ["huang2"]],
},
STYLE_TO3NE: {
normal: [["pa2ng", "fa3ng"], ["hua2ng"]],
segment: [["pa2ng"], ["hua2ng"]],
},
STYLE_INITIALS: {
normal: [["p", "f"], ["h"]],
segment: [["p"], ["h"]],
},
STYLE_FIRST_LETTER: {
normal: [["p", "f"], ["h"]],
segment: [["p"], ["h"]],
},
} ],
// 中英混合,多音字,单音词。
[ "0套价", {
STYLE_NORMAL: [["0"], ["tao"], ["jia", "jie"]],
STYLE_TONE: [["0"], ["tào"], ["jià", "jiè", "jie"]],
STYLE_TONE2: [["0"], ["tao4"], ["jia4", "jie4", "jie"]],
STYLE_TO3NE: [["0"], ["ta4o"], ["jia4", "jie4", "jie"]],
STYLE_INITIALS: [["0"], ["t"], ["j"]],
STYLE_FIRST_LETTER: [["0"], ["t"], ["j"]],
} ],
// 其他
[ "女流氓", {
STYLE_NORMAL: [["nv", "ru"], ["liu"], ["mang", "meng"]],
STYLE_TONE: [["nǚ", "rǔ"], ["liú"], ["máng", "méng"]],
STYLE_TONE2: [["nv3", "ru3"], ["liu2"], ["mang2", "meng2"]],
STYLE_TO3NE: [["nv3", "ru3"], ["liu2"], ["ma2ng", "me2ng"]],
STYLE_INITIALS: [["n", "r"], ["l"], ["m"]],
STYLE_FIRST_LETTER: [["n", "r"], ["l"], ["m"]],
} ],
];
describe("姓名模式", function() {
it("复姓", function() {
expect(pinyin("南宫世家")).toEqual([["nán"], ["gōng"], ["shì"], ["jiā"]]);
expect(pinyin("南宫世家", { mode: "SURNAME"})).toEqual([["nán"], ["gōng"], ["shì"], ["jiā"]]);
expect(pinyin("查某说:您是万俟先生?", { mode: "NORMAL"})).toEqual([["chá"], ["mǒu"], ["shuō"], [":"], ["nín"], ["shì"], ["wàn"], ["sì"], ["xiān"], ["shēng"], ["?"]]);
expect(pinyin("查某说:您是万俟先生?", { mode: "SURNAME"})).toEqual([["zhā"], ["mǒu"], ["shuō"], [":"], ["nín"], ["shì"], ["mò"], ["qí"], ["xiān"], ["shēng"], ["?"]]);
});
it("单姓", function() {
expect(pinyin("华夫人", { mode: "NORMAL"})).toEqual([["huá"], ["fū"], ["rén"]]);
expect(pinyin("华夫人", { mode: "SURNAME"})).toEqual([["huà"], ["fū"], ["rén"]]);
});
});
function getPinyinStyle(styleName: string): IPinyinStyle {
return styleName.replace(/^STYLE_/, "") as IPinyinStyle;
}
function makeTest(han: string, opt: any, style: string){
let py = opt[style];
let pys = py;
// 有多音字的词组。
if (py.normal && py.segment) {
pys = py.segment;
py = py.normal;
}
const single_pinyin = [];
for(let i = 0, l = py.length; i < l; i++){
single_pinyin[i] = [py[i][0]];
}
// 非多音字模式。
const _py = pinyin(han, {style: getPinyinStyle(style)});
it("pinyin(\"" + han + "\", " + style + ") : " +
JSON.stringify(_py) + " === " + JSON.stringify(single_pinyin), function() {
expect(_py).toEqual(single_pinyin);
});
// 普通多音字模式。
const _py2 = pinyin(han, {style: getPinyinStyle(style), heteronym:true});
it("pinyin(\"" + han + "\", " + style + ",heteronym) : " +
JSON.stringify(_py2) + " === " + JSON.stringify(py), function() {
expect(_py2).toEqual(py);
});
// 分词多音字模式。
const _py2s = pinyin(han, {
style: getPinyinStyle(style),
heteronym: true,
segment: true,
});
it("pinyin(\"" + han + "\", " + style + ",heteronym,segment) : " +
JSON.stringify(_py2s) + " === " + JSON.stringify(pys), function() {
expect(_py2s).toEqual(pys);
});
}
describe("pinyin", function() {
for (let i = 0, l = cases.length; i < l; i++) {
const han = cases[i][0];
const opt: Record<string, string[][]> = cases[i][1];
for(const style in opt) {
makeTest(han, opt, style);
}
}
});
describe("pinyin.compare", function() {
it("我,要,排,序 => 序,我,排,要", function() {
const data = "我要排序".split("");
const sortedData = data.sort(compare);
expect(sortedData).toEqual("排我序要".split(""));
});
});
describe("pinyin group", function() {
it("groups segments", function () {
const han = "我喜欢你";
const py = pinyin(han, {segment: true, group: true, heteronym: true});
expect(py).toEqual([["wǒ"], ["xǐhuān"], ["nǐ"]]);
});
it("groups segments with heteronyms", function() {
const han = "我都喜欢朝阳";
const py = pinyin(han, {segment: true, group: true, heteronym: true});
expect(py).toEqual([["wǒ"], ["dū", "dōu"], ["xǐhuān"], ["zhāoyáng", "cháoyáng"]]);
});
it("groups segments without heteronyms", function() {
const han = "我都喜欢朝阳";
const py = pinyin(han, {segment: true, group: true, heteronym: false});
expect(py).toEqual([["wǒ"], ["dū"], ["xǐhuān"], ["zhāoyáng"]]);
});
it("落叶落下着落", function() {
const han = "落叶落下着落";
const py = pinyin(han, {segment: true, group: true, heteronym: false});
expect(py).toEqual([["luòyè"], ["làxià"], ["zháoluò"]]);
});
});
describe("pinyin compact", function() {
it("compact without heternonyms, normal style", function() {
const han = "还钱";
const py = pinyin(han, { style: pinyin.STYLE_NORMAL, segment: "@node-rs/jieba", group: true, heteronym: false, compact: true });
expect(py).toEqual([["huan", "qian"]]);
});
it("compact with heternonyms, normal style", function() {
const han = "还钱";
const py = pinyin(han, { style: pinyin.STYLE_NORMAL, segment: "@node-rs/jieba", group: true, heteronym: true, compact: true });
expect(py).toEqual([["huan", "qian"], ["hai", "qian"]]);
});
it("compact with heternonyms, normal style", function() {
const han = "还钱";
const py = pinyin(han, { style: "NORMAL", segment: "segmentit", group: true, heteronym: true });
expect(compact(py)).toEqual([["huan", "qian"], ["hai", "qian"]]);
expect(pinyin.compact(py)).toEqual([["huan", "qian"], ["hai", "qian"]]);
});
it("compact with heternonyms, first letter, group false", function() {
const han = "还钱";
const py = pinyin(han, { style: "FIRST_LETTER", segment: "nodejieba", group: false, heteronym: true });
expect(compact(py)).toEqual([["h", "q"]]);
expect(pinyin.compact(py)).toEqual([["h", "q"]]);
});
it("compact with heternonyms, group true", function() {
const han = "还钱";
const py = pinyin(han, {segment: true, group: true, heteronym: true});
expect(compact(py)).toEqual([["huán", "qián"], ["hái", "qián"]]);
expect(pinyin.compact(py)).toEqual([["huán", "qián"], ["hái", "qián"]]);
});
it("compact with heternonyms, many words", function() {
const han = "我们都爱朝阳";
const py = pinyin(han, { style: "FIRST_LETTER", heteronym: true, compact: true });
expect(py).toEqual([["w", "m", "d", "a", "z", "y"], ["w", "m", "d", "a", "c", "y"]]);
});
it("行不行 compact without heternonyms, normal style", function() {
const han = "行不行";
const py = pinyin(han, { style: pinyin.STYLE_NORMAL, segment: true, heteronym: false, compact: true });
expect(py).toEqual([["xing", "bu", "xing"]]);
});
});