-
Notifications
You must be signed in to change notification settings - Fork 39
/
data.ts
319 lines (293 loc) · 9.59 KB
/
data.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
const fs = require('fs');
import Dictionary from '../src/data';
// Mock browser.extension.getURL
global.browser = { extension: { getURL: jest.fn(url => url) } };
// Mock fetch
window.fetch = jest.fn().mockImplementation(
url =>
new Promise((resolve, reject) => {
fs.readFile(`${__dirname}/../${url}`, function(err, data) {
if (err) {
reject(err);
return;
}
resolve({ text: () => data.toString() });
});
})
);
// Common dictionary instance to save loading each time
const sharedDict = new Dictionary({ loadNames: true });
describe('Dictionary', () => {
it('loads', async () => {
await sharedDict.loaded;
// Nothing to check
});
it('finds an exact match', async () => {
const result = await sharedDict.wordSearch('蛋白質');
expect(result.matchLen).toBe(3); // 3 characters long
expect(result.data.length).toBeGreaterThanOrEqual(1);
expect(result.data[0][0]).toMatch(/protein/);
});
it('finds a match partially using katakana', async () => {
const result = await sharedDict.wordSearch('タンパク質');
expect(result.matchLen).toBe(5);
});
it('finds a match partially using half-width katakana', async () => {
const result = await sharedDict.wordSearch('タンパク質');
expect(result.matchLen).toBe(6);
});
it('finds a match partially using hiragana', async () => {
const result = await sharedDict.wordSearch('たんぱく質');
expect(result.matchLen).toBe(5);
});
it('finds a match fully using katakana', async () => {
const result = await sharedDict.wordSearch('タンパクシツ');
expect(result.matchLen).toBe(6);
});
it('finds a match fully using half-width katakana', async () => {
const result = await sharedDict.wordSearch('タンパクシツ');
expect(result.matchLen).toBe(7);
});
it('finds a match fully using hiragana', async () => {
const result = await sharedDict.wordSearch('たんぱくしつ');
expect(result.matchLen).toBe(6);
});
it('finds a partial match', async () => {
const result = await sharedDict.wordSearch('蛋白質は');
expect(result.matchLen).toBe(3);
});
it('reports character lengths for half-width katakana normalization', () => {
const result = sharedDict.normalizeInput('ガーデン');
expect(result).toEqual(['がーでん', [0, 2, 3, 5, 6]]);
});
it('performs de-inflection', () => {
const result = sharedDict.deinflect('走ります');
const match = result.find(candidate => candidate.word === '走る');
expect(match).toEqual({ reason: 'polite', type: 2, word: '走る' });
});
it('chooses the right de-inflection for potential and passives', async () => {
// Ichidan/ru-verb -- られる ending could be potential or passive
let result = await sharedDict.wordSearch('止められます');
let match = result.data.find(
([item, reason]) => item.indexOf('[とめる]') !== -1
);
expect(match[1]).toEqual('< potential or passive < polite');
// Godan/u-verb -- られる ending is passive
result = await sharedDict.wordSearch('止まられます');
match = result.data.find(
([item, reason]) => item.indexOf('[とまる]') !== -1
);
expect(match[1]).toEqual('< passive < polite');
// Godan/u-verb -- れる ending is potential
result = await sharedDict.wordSearch('止まれます');
match = result.data.find(
([item, reason]) => item.indexOf('[とまる]') !== -1
);
expect(match[1]).toEqual('< potential < polite');
});
it('performs de-inflection recursively', () => {
const result = sharedDict.deinflect('踊りたくなかった');
const match = result.find(candidate => candidate.word === '踊る');
expect(match).toEqual({
reason: '-tai < negative < past',
type: 2,
word: '踊る',
});
});
it('translates sentences', async () => {
const result = await sharedDict.translate('期間限定発売 秋の膳');
expect(result.textLen).toBe(10); // 10 characters including the space
expect(result.data.length).toBe(5);
expect(result.more).toBe(false);
const kana = result.data
.map(word => {
const matches = word[0].match(/^(.+?)\s+(?:\[(.*?)\])?/);
return matches[2] || matches[1];
})
.join('');
expect(kana).toBe('きかんげんていはつばいあきのぜん');
});
it('looks up kanji', () => {
const result = sharedDict.kanjiSearch('峠');
expect(result).toEqual({
kanji: '峠',
misc: {
B: '46',
DK: '265',
E: '1663',
F: '1941',
G: '8',
H: '358',
I: '3o6.3',
IN: '1351',
L: '773',
N: '1416',
P: '1-3-6',
S: '9',
U: '5CE0',
V: '1464',
},
miscDisplay: [
{ abbrev: 'H', name: 'Halpern' },
{ abbrev: 'L', name: 'Heisig' },
{ abbrev: 'E', name: 'Henshall' },
{ abbrev: 'DK', name: 'Kanji Learners Dictionary' },
{ abbrev: 'N', name: 'Nelson' },
{ abbrev: 'V', name: 'New Nelson' },
{ abbrev: 'Y', name: 'PinYin' },
{ abbrev: 'P', name: 'Skip Pattern' },
{ abbrev: 'IN', name: 'Tuttle Kanji & Kana' },
{ abbrev: 'I', name: 'Tuttle Kanji Dictionary' },
{ abbrev: 'U', name: 'Unicode' },
],
onkun: ['とうげ'],
nanori: [],
bushumei: [],
radical: '山',
eigo: 'mountain peak, mountain pass, climax, crest, (kokuji)',
});
});
it('looks up kanji with nanori and bushumei', () => {
const options = {
includedReferences: new Set([
'H',
'L',
'E',
'DK',
'N',
'V',
'Y',
'P',
'IN',
'I',
'U',
]),
includeKanjiComponents: false,
};
const result = sharedDict.kanjiSearch('士', options);
expect(result).toEqual({
kanji: '士',
misc: {
B: '33',
DK: '2129',
E: '494',
F: '526',
G: '4',
H: '3405',
I: '3p0.1',
IN: '572',
L: '319',
N: '1160',
P: '4-3-2',
S: '3',
U: '58EB',
V: '1117',
Y: 'shi4',
},
miscDisplay: [
{ abbrev: 'H', name: 'Halpern' },
{ abbrev: 'L', name: 'Heisig' },
{ abbrev: 'E', name: 'Henshall' },
{ abbrev: 'DK', name: 'Kanji Learners Dictionary' },
{ abbrev: 'N', name: 'Nelson' },
{ abbrev: 'V', name: 'New Nelson' },
{ abbrev: 'Y', name: 'PinYin' },
{ abbrev: 'P', name: 'Skip Pattern' },
{ abbrev: 'IN', name: 'Tuttle Kanji & Kana' },
{ abbrev: 'I', name: 'Tuttle Kanji Dictionary' },
{ abbrev: 'U', name: 'Unicode' },
],
onkun: ['シ'],
nanori: ['お', 'ま'],
bushumei: ['さむらい'],
radical: '士',
eigo: 'gentleman, samurai, samurai radical (no. 33)',
});
});
it('looks up kanji with multiple readings', () => {
const result = sharedDict.kanjiSearch('与');
expect(result).toEqual({
kanji: '与',
misc: {
B: '1',
DK: '2138',
E: '1873',
F: '308',
G: '8',
H: '3421',
I: '0a3.23',
IN: '539',
L: '1246',
N: '6',
P: '4-3-4',
S: '3',
U: '4E0E',
V: '6',
Y: 'yu3 yu4',
},
miscDisplay: [
{ abbrev: 'H', name: 'Halpern' },
{ abbrev: 'L', name: 'Heisig' },
{ abbrev: 'E', name: 'Henshall' },
{ abbrev: 'DK', name: 'Kanji Learners Dictionary' },
{ abbrev: 'N', name: 'Nelson' },
{ abbrev: 'V', name: 'New Nelson' },
{ abbrev: 'Y', name: 'PinYin' },
{ abbrev: 'P', name: 'Skip Pattern' },
{ abbrev: 'IN', name: 'Tuttle Kanji & Kana' },
{ abbrev: 'I', name: 'Tuttle Kanji Dictionary' },
{ abbrev: 'U', name: 'Unicode' },
],
onkun: ['ヨ', 'あた.える', 'あずか.る', 'くみ.する', 'ともに'],
nanori: ['とも', 'ゆ'],
bushumei: [],
radical: '一',
eigo:
'bestow, participate in, give, award, impart, provide, cause, gift, godsend',
});
});
it('looks up kanji with kanji components', () => {
const options = {
includedReferences: new Set(['H', 'U']),
includeKanjiComponents: true,
};
const result = sharedDict.kanjiSearch('鯖', options);
expect(result).toEqual({
kanji: '鯖',
misc: {
B: '195',
L: '2820',
N: '5301',
P: '1-11-8',
U: '9BD6',
S: '19',
V: '6883',
Y: 'qing1 zheng1',
},
miscDisplay: [
{ abbrev: 'H', name: 'Halpern' },
{ abbrev: 'U', name: 'Unicode' },
],
components: [
{ radical: '魚', yomi: 'うお', english: 'fish' },
{ radical: '二', yomi: 'に', english: 'two' },
{ radical: '亠', yomi: 'なべぶた', english: 'lid' },
{ radical: '土', yomi: 'つち', english: 'earth' },
{ radical: '月', yomi: 'つき', english: 'moon' },
{ radical: '田', yomi: 'た', english: 'rice field' },
{ radical: '青', yomi: 'あお', english: 'blue' },
{ radical: '灬', yomi: 'れっか', english: 'fire' },
],
onkun: ['セイ', 'ショウ', 'さば'],
nanori: [],
bushumei: [],
radical: '魚',
eigo: 'mackerel',
});
});
// TODO: Test names dictionary handling
// e.g. create a new Dictionary specifying 'false' for loadNames but then
// do a word lookup where 'doNames' is true.
// e.g.(2) do a word look up where 'doNames' is true and check we
// *don't* match names
});