-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
533 lines (511 loc) · 15.1 KB
/
cli.js
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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
var util = require('util');
var dict = require('./data/dictionary');
const htmlToText = require('html-to-text');
const chalk = require('chalk');
var fs = require('fs');
const strokeCount = require('./strokeCount' );
var hanzi = require("hanzi");
var hanziLoaded = false;
const blurb = require('./data/blurb.js');
const pinyin = require( 'chinese-to-pinyin' );
function addDictionaryItem() {
return new Promise( ( resolve ) => {
var chinese, english;
getUserInput( 'Enter Chinese:' ).then( ( chinese ) => {
getUserInput( 'Enter English:' ).then( ( english ) => {
dict.saveWord( chinese, english )
.then(()=>dict.save())
.then(() => {
resolve();
});
} );
} );
} );
}
function getUserInput( msg ) {
return new Promise( ( resolve, reject ) => {
feedback( msg, true );
process.stdin.setEncoding('utf8');
process.stdin.once('data', function (text) {
resolve( util.inspect(text).replace( /'([^\n]*)'/g, '$1' ).replace( '\\n', '' )
.replace(/\\/, '').trim() );
});
})
}
function deal(wordLength, max, _keys) {
max = max || 10;
if ( !_keys ) {
_keys = dict.getWords(wordLength, undefined, max);
}
if ( _keys.length ) {
var index = Math.floor(Math.random() * _keys.length);
var word = _keys[index];
_keys.splice( index, 1 );
getUserInput('What is: ' + word + '?').then((answer) => {
feedback( dict.translate(word) );
deal( wordLength, max, _keys );
});
} else {
game();
}
}
function feedback( msg, bold ) {
if ( bold ) {
console.log( chalk.bold( msg ) );
} else {
console.log( '\t\t' + msg );
}
}
function decomposeWord() {
return new Promise( ( resolve ) => {
var components = [];
getUserInput('What is the Chinese word you want to decompose?').then((answer) => {
getUserInput('What are the components?').then((radicals) => {
const components = Array.from(radicals.trim().replace(/ /g, ''));
if ( components.length ) {
dict.addDecomposition( answer, components );
dict.save().then(()=>resolve());
} else {
console.log('Decomposition cannot be empty');
resolve();
}
});
});
} );
}
function viewDecomposition() {
return getUserInput('Enter chinese character to decompose').then((word) => {
feedback( dict.decompose(word).join( '*' ) );
} );
}
function translate() {
return getUserInput('What is the Chinese word you want to translate?').then((answer) => {
feedback( dict.translate(answer) );
});
}
function changeDifficulty(delta) {
return getUserInput('Enter chinese character(s) to change difficulty for (' + delta + ')')
.then((word) => {
Array.from(word).forEach((char) => {
const diff = dict.getDifficultyRating(char, true) || 0;
dict.rateWord(char, diff + delta);
console.log(`${char} difficulty level = ${diff+delta}`)
});
} ).then(()=> {
return dict.save();
} );
}
function game() {
const options = [
'0: Back to menu',
'1: Deal radicals',
'2: Deal singles',
'3: Deal double cards',
'4: Deal triple cards'
];
return getUserInput( '**********************\n' + options.join('\n') + '\n**********************' )
.then( ( val ) => {
val = parseInt( val, 10 );
switch ( val ) {
case 0:
menu();
break;
case 1:
deal( 0 );
break;
case 2:
deal( 1 );
break;
case 3:
deal( 2 );
break;
case 4:
deal( 3 );
break;
default:
feedback('Huh?');
game();
}
});
}
function loadHanzi() {
if ( !hanziLoaded ) {
hanzi.start();
hanziLoaded = true;
}
}
function removeUnreadableCharacters(components) {
return components.filter((char) => char.match(/[㇕𥃭㇕𠮛]/) === null);
}
function batchAutoDecompose(words) {
loadHanzi();
words.filter((char) => char.length === 1).forEach((char) => {
const decomps = hanzi.decompose(char);
let components = decomps.components1.filter((comp) =>
comp !== 'No glyph available' );
if ( ( components.length === 1 && components[0] !== char ) || components.length > 1 ) {
const existingDecomps = dict.getDecompositions()[char] || [];
components = removeUnreadableCharacters(components).slice(0, 2);
if ( existingDecomps.length !== components.length ||
removeUnreadableCharacters(existingDecomps).length !== existingDecomps.length
) {
console.log('Add', char, components );
dict.addDecomposition( char, components );
}
}
})
}
function getEnglish(char) {
loadHanzi();
var inEnglish = hanzi.definitionLookup(char, 's');
return !inEnglish ? ['?'] : inEnglish.map((definition) => {
return definition.definition;
}).join('/').split('/')
.filter((def)=> {
const l = def.toLowerCase();
return l.indexOf('surname ') === -1 && l.indexOf('[') === -1 && l.length < 15;
});
}
function clean() {
loadHanzi();
const decomps = dict.getDecompositions();
Object.keys( decomps ).map((key) => {
const decomp = decomps[key];
if ( decomp.indexOf( '?' ) > -1 ) {
console.log(`Warning: ${key} has ? decomposition.`, decomp);
}
const indexFlesh = decomp.indexOf( '⺼' );
if ( indexFlesh > -1 ) {
// moon and flesh are very similar, so we merged them into one.
decomp[indexFlesh] = '月';
console.log(`Updated ⺼ to 月 for ${key}`);
}
const indexIce = decomp.indexOf( '⺀' );
if ( indexIce > -1 ) {
// moon and flesh are very similar, so we merged them into one.
decomp[indexIce] = '冫 ';
console.log(`Updated ⺀ to 冫 for ${key}`);
}
decomp.forEach((char) => {
Array.from(char).forEach((chinese)=> {
const p = dict.getPinyin(chinese);
if ( !p ) {
const _pinyin = pinyin(chinese);
if ( _pinyin ) {
dict.savePinyin(chinese, _pinyin);
console.log('Add pinyin', chinese, '=', _pinyin);
}
}
});
});
});
dict.all().forEach((char) => {
const decomp = dict.decompose(char);
if ( decomp.indexOf( '?' ) > -1 ) {
// reduce the hanzi decomposition to real characters
const hanz = hanzi.decompose(char).components1.filter((c) => {
return c !== 'No glyph available'
});
if ( hanz.length === 2 ) {
console.log('Add decomposition', char, decomp, hanz);
dict.addDecomposition(char, hanz);
} else {
const hanzUnique = hanz.filter((char) => decomp.indexOf(char) === -1);
if ( hanzUnique.length ) {
const newDecomp = decomp.map((char, i) => char === '?' ? hanzUnique[0] : char);
console.log('Decom issue', char, decomp, hanzUnique, newDecomp);
dict.addDecomposition(char, newDecomp);
}
}
}
})
dict.all().forEach((chinese) => {
const eng = dict.getWord(chinese);
if ( eng === '?' ) {
console.log('Remove from dictionary:', chinese);
dict.removeWord(chinese);
} else if ( eng.match(/\//) ) {
const newEng = eng.replace(/\//g, '; ');
console.log('Switched / for ; in ' + newEng);
dict.saveWord(chinese, newEng);
}
// update pinyin for one characters
Array.from(chinese).forEach((char) => {
const p = dict.getPinyin(char);
if ( !p ) {
const _pinyin = pinyin(char);
dict.savePinyin(char, _pinyin);
console.log('Add pinyin', char, '=', _pinyin);
}
});
})
dict.missing().forEach((chinese)=> {
var english = getEnglish(chinese).slice(0, 3).join('; ');
if ( english && english !== '?' ) {
dict.saveWord( chinese, english );
console.log('added', chinese, english);
}
});
var mapper = {
'⺮': '𥫗'
};
// Remove things that decompose to itself.
const decomp = dict.getDecompositions();
Object.keys(decomp).forEach((key) => {
var parts = decomp[key];
if ( parts.indexOf(key) > -1 ) {
console.log(`${key} decomposes to itself`);
parts.splice( parts.indexOf(key), 1 );
decomp[key] = parts;
}
if ( Object.keys(mapper).indexOf(key) > -1 ) {
console.log(`${key} is duplicated by ${mapper[key]}`);
decomp[key] = [];
}
Object.keys(mapper).forEach((dupeKey) => {
if ( parts.indexOf(dupeKey) > -1 ) {
console.log(`Substituting ${dupeKey} with ${mapper[dupeKey]}`)
parts[parts.indexOf(dupeKey)] = mapper[dupeKey];
}
})
});
Object.keys(decomp).forEach((key) => {
var parts = decomp[key];
if ( parts.length === 0 ) {
console.log(`Empty decomposition for ${key}`);
delete decomp[key];
}
});
}
function lookup(p) {
return dict.lookupPinyin(p).forEach(( words ) => {
console.log( words[0], words[1], words[2] || '' );
});
}
function menu() {
const options = [
'0: Lookup pinyin',
'1: Game',
'2: Lookup word',
'3: auto decompose difficulty level',
'4: Lookup character',
'5: Add to dictionary',
'6: Batch decompose',
'7: Decompose chinese word',
'8: Decrease difficulty of word(s)',
'9: Increase difficulty of word(s)',
'10: Missing definitions',
'11: Delete word',
'12: Auto-assign difficulty',
'13: Clean',
'14: Add blurb',
'15: View cards with difficulty range',
'16: Add pinyin',
'17: Clear decompositions',
'18: Batch delete characters',
'19: Show unused'
];
getUserInput( '**********************\n' + options.join('\n') + '\n**********************' )
.then( ( val ) => {
val = parseInt( val, 10 );
switch ( val ) {
case 0:
getUserInput('Enter chinese character').then((msg) => {
console.log(lookup(msg));
return menu();
});
break;
case 4:
getUserInput('Enter chinese character').then((msg) => {
console.log(getEnglish(msg));
return menu();
});
break;
case 1:
game();
break;
case 13:
clean();
return dict.save().then(() => menu());
break;
case 3:
getUserInput('Enter difficulty level').then((msg) => {
var words = dict.getWords(2,parseInt(msg, 10));
batchAutoDecompose(words);
return dict.save().then(() => menu());
});
break;
case 6:
getUserInput('Enter chinese character(s) separated by spaces').then((msg) => {
const words = msg.split( ' ' ).map((w) => w.trim());
batchAutoDecompose(words);
return dict.save().then(() => menu());
});
break;
case 5:
addDictionaryItem().then(() => menu());
break;
case 7:
decomposeWord().then(() => menu());
break;
case 8:
changeDifficulty(-1).then(()=>menu());
break;
case 2:
getUserInput('Enter chinese character').then((char) => {
const usedBy = dict.usedBy(char);
const translation = usedBy.
map((char)=>`${char} (${dict.getWord(char)})`).join('; ')
feedback( `Character: ${char}` );
feedback( `English: ${dict.getWord( char )}`)
feedback( `Pinyin: ${dict.getPinyin( char )}`);
feedback( `${dict.decompose( char ).join('+')}` );
feedback( dict.translate(char) );
feedback( `Standalone Difficulty=${dict.getDifficultyRating(char, true)}` );
feedback( `True difficulty=${dict.getDifficultyRating(char)}` );
feedback( `Length=${dict.getWordLength(char)}` );
feedback( `Used by=${usedBy.length ? translation : 'Nothing'}`)
feedback( `Blurb=\n${blurb.getBlurb(char)}`)
} ).then(() => menu());
break;
case 10:
const missing = dict.missing();
console.log( `There are ${missing.length} words missing definitions without decompositions` );
console.log( missing.sort(()=>Math.random() > 0.5 ? -1 : 1 ).join(' ' ) );
menu().then(()=> {
dict.reload();
return dict.save();
} );
break;
case 9:
changeDifficulty(1).then(()=>menu());
break;
case 11:
getUserInput('Enter chinese character to remove').then((word) => {
const deleted = dict.removeWord(word);
// remove pinyin
dict.deletePinyin(word);
if ( deleted ) {
console.log('Removed word (retained difficulty and decompositions)', word);
} else {
console.log('Word does not exist');
}
return dict.save().then(() => menu());
} );
break;
case 12:
getUserInput('Enter chinese character to count strokes for').then((word) => {
return rateWordsDifficultyByStrokeCount([word]).then(()=> menu());
} );
break
case 14:
getUserInput('Enter chinese character to add blurb for').then((word) => {
getUserInput('Enter blurb').then((text) => {
blurb.addBlurb(word, text);
fs.writeFileSync(blurb.JSON_FILE, blurb.getBlurbJSONString());
return menu();
} );
} );
break;
case 15:
getUserInput('Word length?').then((wl) => {
const wordL = parseInt(wl, 10);
getUserInput('What is the range? e.g. 0,10').then((rangeStr) => {
let range = rangeStr.split(',').map((str) => str ? parseInt(str, 10) : 0);
if ( range.length === 1) {
range.push(range[0]);
}
console.log(`Looking in range ${range[0]} - ${range[1]}`);
dict.all().filter((word) => {
const rating = dict.getDifficultyRating(word);
//console.log('giot', rating, rating > range[0], rating <= range[1]);
return rating >= range[0] && rating <= range[1];
}).filter(
(a)=>dict.getWordLength(a) === wordL
).sort(
(a,b)=>dict.getWordLength(a)<dict.getWordLength(b) ? -1 : 1
).forEach((word) => {
console.log(word, dict.getWord(word), dict.getPinyin(word), 'word length=', dict.getWordLength(word));
});
return menu();
} );
} );
break;
case 16:
return getUserInput('Word?').then((w) => {
return getUserInput('Pinyin?').then((p) => {
dict.savePinyin(w, p);
return dict.save();
}).then(()=>menu());
});
break;
case 17:
return getUserInput('Clear decompositions for what?').then((w) => {
dict.addDecomposition( w, [] );
return dict.save();
}).then(()=>menu());
break;
case 18:
return getUserInput('Batch delete what words?').then((w) => {
Array.from(w).forEach((word) => dict.removeWord(word));
return dict.save();
}).then(()=>menu());
break;
case 19:
dict.all().filter((word) => {
return Array.from( word ).length < 2 &&
dict.usedBy(word).length === 0 && !blurb.getBlurb(word);
} ).forEach((char) => {
console.log( char, dict.getWord( char ) );
} );
menu();
break;
default:
feedback('Huh?');
menu();
}
});
}
function rateWordsDifficultyByStrokeCount(words) {
return new Promise((resolve) => {
strokeCount(words).then((counts) => {
Object.keys(counts).forEach((key) => {
dict.rateWord(key, counts[key]);
});
resolve();
});
});
}
function importWords(json) {
let promises = [];
json.forEach((word) => {
if ( !dict.getWord( word ) &&
// blacklist of characters to ignore.
!word.match( /[\—\:\+\─\:\%\·\;\》\《\<\=\>\?\~\@\!\_\の\よ\う\な\だ\め\…\/\#\!\‧\ \’\‘\•]/ )
) {
let def = getEnglish(word).join(';');
def = def.trim();
if ( def && def !== '?' ) {
console.log(`Imported ${word}=${def}`, def === '?');
promises.push( dict.saveWord( word, def ) );
}
} else {
console.log( `skipped ${word}`)
}
})
console.log( 'Imported', promises.length, 'words');
promises.push( rateWordsDifficultyByStrokeCount( json ) );
return Promise.all(promises).then(()=>dict.save());
}
if ( process.argv[2] ) {
const filename = process.argv[2];
console.log(`reading ${filename}`)
fs.readFile(filename, 'utf-8', ( err, data ) => {
const json = JSON.parse( data );
dict.load().then(() => {
return importWords(json);
});
} );
} else {
dict.load().then(() => menu());
}