/
tool.ts
462 lines (431 loc) · 14.2 KB
/
tool.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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
import {spellInfo} from './spellToWord';
import {has} from './util';
import Dict from '../dict';
import {AllArgs, ICnChar, TypeProp, ToneType, SpellArg, StrokeArg, TypeValueObject} from 'cnchar-types/main/index';
import {Json, ITransformReturn} from 'cnchar-types/main/common';
import {TSpellArg, IDealUpLowFirst, IRemoveTone, IFunc, ICheckArgs, ITransformTone} from 'cnchar-types/main/tool';
import {_warn, isCnChar} from '@common/util';
export const tones: string = 'āáǎàōóǒòēéěèīíǐìūúǔùǖǘǚǜ*ńňǹ'; // * 表示n的一声
const noTones: string = 'aoeiuün';
export const arg: TSpellArg = {
array: 'array',
low: 'low',
up: 'up',
first: 'first',
poly: 'poly',
tone: 'tone',
simple: 'simple',
trad: 'trad',
flat: 'flat',
};
let _cnchar: ICnChar;
export function initCnchar (cnchar: ICnChar): void {
_cnchar = cnchar;
}
export function getCnChar () {
return _cnchar;
}
const NOT_CNCHAR: string = 'NOT_CNCHAR';
export function spell (dict: Json<string>, originArgs: Array<string>): string | Array<string> {
const strs = originArgs[0].split('');
const args = (originArgs.splice(1)) as Array<SpellArg>;
checkArgs('spell', args);
const poly = has(args, arg.poly);
const tone = has(args, arg.tone);
const res: Array<Array<string>> = [];
for (const sp in dict) { // 遍历拼音
const ds: string = dict[sp]; // 某个拼音的所有汉字字符串
const pos = parseInt(ds[0]); // 某个拼音的音调位置
for (let i = 0; i < strs.length; i++) { // 遍历字符数组
const ch: string = strs[i];
const addIntoPolyRes = (spell: string) => {
isDefaultSpell(ch, spell) ? res[i].unshift(spell) : res[i].push(spell);
};
if (isCnChar(ch)) { // 如果是汉字
let index = ds.indexOf(ch);
if (index !== -1) {
const ssp = getSpell(sp, ds, index, poly, tone, pos); // single spell
if (ssp.poly) { // 多音字模式 且 当前汉字是多音字
if (!res[i]) res[i] = [];
addIntoPolyRes(ssp.res);
let dsNew = ds;
const n = (dsNew.match(new RegExp(ch, 'g')) as Array<string>).length;
for (let k = 1; k < n; k++) {
dsNew = dsNew.substr(index + 2);
index = dsNew.indexOf(ch);
addIntoPolyRes(getSpell(sp, dsNew, index, poly, tone, pos).res);
}
} else {
if (ssp.isPolyWord) { // 是多音字 不是多音字模式
if (Dict.spellDefault[ch]) { // 设置了多音字的默认拼音
ssp.res = removeTone(Dict.spellDefault[ch], tone).spell; // 默认有音调
}
}
res[i] = [ssp.res];
strs[i] = '';
}
}
} else if (ch !== '') { // 如果不是汉字
res[i] = [NOT_CNCHAR, ch];
}
}
}
dealUpLowFirst(res, args);
// 从res中读取数据
const result: Array<string> = [];
for (let i = 0; i < strs.length; i++) {
const item = res[i];
if (typeof item === 'undefined') {
result[i] = strs[i]; // 查不到的汉字返回原字
} else if (item.length > 1) {
if (item[0] === NOT_CNCHAR) {
result[i] = item[1]; // 非汉字返回原字符
} else {
result[i] = `(${item.join('|')})`;
}
} else {
result[i] = item[0];
}
if (has(args, arg.flat) && has(args, arg.tone)) {
result[i] = shapeSpell(result[i], true);
}
}
if (!has(args, arg.array)) {
return result.join('');
}
return result;
}
export const dealUpLowFirst: IDealUpLowFirst = (
res: Array<Array<string>> | Array<string>,
args: Array<SpellArg>
): void => {
if (_cnchar._.poly) {
dealResCase(res, low);
// 当启用了 多音词时 需要强制默认小写
// 因为会被覆盖
}
if (has(args, arg.first)) {
dealResCase(res, first);
}
if (has(args, arg.up)) {
dealResCase(res, up);
} else if (!has(args, arg.low)) {
dealResCase(res, upFirst);
}
};
function dealResCase (
res: Array<Array<string>> | Array<string>,
func:(str: string) => string
): void {
res.forEach((item: Array<string> | string, index: number) => {
if (typeof item !== 'string') {
if (item[0] !== NOT_CNCHAR) {
item.forEach((s, j) => {item[j] = func(s);});
}
} else {
res[index] = func(item);
}
});
}
function first (s: string): string {
return s[0];
}
function up (s: string): string {
return s.toUpperCase();
}
function upFirst (s: string): string {
return up(s[0]) + s.substr(1);
}
function low (s: string): string {
return s.toLowerCase();
}
function getSpell (
spell: string,
str: string,
index: number,
isPoly: boolean,
isTone: boolean,
pos: number
): {
res: string,
poly: boolean,
isPolyWord: boolean
} {
let tone = parseInt(str[index + 1]);
const res = {res: spell, poly: false, isPolyWord: (tone >= 5)};
if (!isPoly && !isTone) {
return res;
}
if (res.isPolyWord) { // 是多音字
tone -= 5; // 正确的音调
if (isPoly) { // 既是多音字模式 又是 多音字
res.poly = true;
}
}
if (isTone) {
res.res = setTone(spell, pos, tone as ToneType);
}
return res;
}
function isDefaultSpell (word: string, spell: string) {
const def = Dict.spellDefault[word];
if (!def) return false;
return def === spell || (shapeSpell(def, true).replace(/[0-4]/, '') === spell);
}
// tone=false : 根据有音调的拼音获得无音调的拼音和音调
// tone=true : 返回原拼音
export const removeTone: IRemoveTone = (spell: string, tone: boolean): {
spell: string, tone?: ToneType, index?: number
} => {
if (tone) {
return {spell};
}
for (let i = 0; i < spell.length; i++) {
const index: number = tones.indexOf(spell[i]);
if (index !== -1) { // 命中
return {
spell: spell.substr(0, i) + noTones[Math.floor(index / 4)] + spell.substr(i + 1),
tone: ((index % 4) + 1) as ToneType,
index: i + 1
};
}
}
return {spell, tone: 0, index: -1};
};
/**
* 给拼音添加音调
* setTone('ni', 1, 3) = nǐ
*/
export function setTone (spell: string, index: number, tone: ToneType): string {
if (tone === 0) { // 轻声
return spell;
}
const p = spell[index];
const toneP = tones[noTones.indexOf(p) * 4 + (tone - 1)];
if (p !== toneP) {
return spell.replace(p, toneP);
}
return spell;
}
// 计算笔画基础方法
export function stroke (
dict: Json<string>,
originArgs: Array<string>
): number | Array<number> {
const strs = originArgs[0].split('');
const strokes: Array<number> = [];
const args = originArgs.splice(1) as Array<StrokeArg>;
checkArgs('stroke', args);
for (const i in dict) {
for (let j = 0; j < strs.length; j++) {
if (strs[j]) {
if (dict[i].indexOf(strs[j] as string) !== -1) {
strs[j] = '';
strokes[j] = parseInt(i);
}
}
}
}
strs.forEach((c: string, i: number): void => {
if (c) {strokes[i] = 0;}
});
if (!has(args, arg.array as StrokeArg)) {
return sumStroke(strokes);
}
return strokes;
}
export const sumStroke: IFunc<number, Array<number>> = (strs: Array<number>): number => {
let sum: number = 0;
strs.forEach(function (c) {
sum += c;
});
return sum;
};
/*
检查cnchar中某方法 传入的参数是否合法
spell 所有参数 ["array", "low", "up", "first", "poly", "tone", "simple"] // simple 禁用繁体字
stroke 所有参数 ["letter", "shape", "count", "name", "detail", "array", "order", "simple"]
*/
let _hasCheck: boolean = false;
export const checkArgs: ICheckArgs = (
type: TypeProp,
args: Array<AllArgs>,
jumpNext?: boolean
): void => {
if (!_cnchar.check) {
return;
}
if (_hasCheck) {
_hasCheck = false;
return;
}
if (jumpNext) { _hasCheck = true; }
const useless: Array<AllArgs> = [];
for (let i = args.length - 1; i >= 0; i--) {
const arg = args[i];
if (!(_cnchar.type[type] as TypeValueObject)[arg]) {
useless.push(arg);
args.splice(i, 1);
}
}
const ignore: Array<AllArgs> = [];
const redunt: Array<AllArgs> = [];
const check = (name: AllArgs | Array<AllArgs>, arr: Array<AllArgs> = ignore): void => {
if (name instanceof Array) {
name.forEach((item) => {
check(item, arr);
});
return;
}
if (has(args, name)) { arr.push(name); }
};
if (_cnchar.plugins.indexOf('trad') === -1) {
if (has(args, 'simple'))
ignore.push('simple');
if (has(args, 'trad'))
ignore.push('trad');
}
if (type === 'spell') {
if (has(args, 'up') && has(args, 'low')) {
ignore.push('low');
}
// t.spell.origin 表示启用了多音词
// !has(args,'origin') 表示没有禁用多音词
// 此时的 poly 就会被忽略
// if(t.spell.origin && !has(args,'origin') && has(args,'poly')){
// ignore.push('poly');
// }
} else if (type === 'stroke') { // stroke
if (has(args, 'order')) { // 笔画顺序模式
check('array', redunt);
// detail > shape > name > count > letter 默认值是 letter
if (has(args, 'letter')) {
check(['detail', 'shape', 'name', 'count']);
check('letter', redunt);
} else if (has(args, 'detail')) {
check(['shape', 'name', 'count']);
} else if (has(args, 'shape')) {
check(['name', 'count']);
} else if (has(args, 'name')) {
check(['count']);
}
} else { // 笔画数模式
check(['detail', 'shape', 'name', 'letter']);
check('count', redunt);
}
} else if (type === 'orderToWord') {
if (has(args, 'match')) {
check(['matchorder', 'contain', 'start']);
} else if (has(args, 'matchorder')) {
check(['contain', 'start']);
} else if (has(args, 'contain')) {
check(['start']);
}
} else if (type === 'strokeToWord') {
} else if (type === 'spellToWord') {
} else if (type === 'idiom') {
if (has(args, 'spell')) {
check(['stroke', 'char']);
} else if (has(args, 'stroke')) {
check(['tone', 'char']);
} else {
check(['tone']);
}
} else if (type === 'xhy') {
} else if (type === 'radical') {
}
warnArgs(useless, '无效', type, args);
warnArgs(ignore, '被忽略', type, args);
warnArgs(redunt, '冗余', type, args);
};
function warnArgs (
arr: Array<AllArgs>,
txt: string,
type: TypeProp,
args: Array<AllArgs>
): void {
if (arr.length > 0) {
let mes: string = `以下参数${txt}:${JSON.stringify(arr)};`;
if (txt === '被忽略' && type === 'stroke' && !has(args, 'order')) {
mes += ' 要启用笔顺模式必须使用 order 参数';
} else {
mes += ` 可选值:[${Object.keys((_cnchar.type[type] as TypeValueObject))}]`;
}
_warn(mes);
}
}
/*
将拼音转换成含有音调的拼音
lv2 => lǘ
reverse=true: lǘ => lv2
*/
export function shapeSpell (spell: string, reverse: boolean = false): string {
const tones: string = '01234';
const hasNumTone = tones.indexOf(spell[spell.length - 1]) !== -1;
if (!reverse) {
return hasNumTone ? transformTone(spell, true, 'low').spell : spell;
}
if (hasNumTone) {
return spell;
}
const info = transformTone(spell, false, 'low');
if (info.spell.indexOf('ü') !== -1) {
info.spell = info.spell.replace('ü', 'v');
}
return hasNumTone ? spell : info.spell + info.tone;
}
/*
将拼音转换成json数据
lv2 => {spell:'lü', tone: 2, index: 2, isTrans: true}
lǘ => {spell:'lü', tone: 2, index: 2, isTrans: false}
needTone = true: lv2 => {spell:'lǘ', tone: 2, index: 2, isTrans: true}
*/
export const transformTone: ITransformTone = (
spell: string,
needTone: boolean = false,
type: 'low' | 'up' = 'low'
): ITransformReturn => {
if (spell.indexOf('v') !== -1) {
spell = spell.replace('v', 'ü');
}
const lastStr: string = spell[spell.length - 1];
let tone: ToneType;
let index: number = -1;
let isTrans: boolean = false;
if (parseInt(lastStr).toString() === lastStr) { // lv2
spell = spell.substr(0, spell.length - 1);
const info = spellInfo(spell);
index = info.index;
tone = parseInt(lastStr) as ToneType;
isTrans = true;
if (needTone) {
spell = setTone(spell, index - 1, tone);
}
} else { // lǘ
const info = spellInfo(spell);
index = info.index;
tone = info.tone; // 声调已经带好了的情况
if (!needTone && tone !== 0) { // 需要去除音调并且有音调
spell = info.spell;
}
}
if (type === 'low') {
spell = spell.toLowerCase();
} else if (type === 'up') {
spell = spell.toUpperCase();
}
return {spell, tone, index, isTrans};
};
export function checkTrad (input: string|string[], args: string[]) {
if (args.indexOf('trad') === -1 || _cnchar.plugins.indexOf('trad') === -1) {
return input;
}
let isArr = false;
if (input instanceof Array) {
isArr = true;
input = input.join('');
}
input = _cnchar.convert.tradToSimple(input);
if (isArr) return input.split('');
return input;
}