-
-
Notifications
You must be signed in to change notification settings - Fork 111
/
Copy pathtest-tokenization.js
679 lines (559 loc) · 20.9 KB
/
test-tokenization.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
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
/*
* Unit tests for common utility functions/methods.
*/
import { assert } from 'chai';
import * as models from "@keymanapp/models-templates";
import * as wordBreakers from "@keymanapp/models-wordbreakers";
import { customWordBreakerFormer, customWordBreakerProper } from './custom-breakers.def.js';
function asProcessedToken(text) {
// default wordbreaker emits these at the end of each context half if ending with whitespace.
// Indicates a new spot for non-whitespace text.
if(text == '') {
return {
text: text
};
} else if(text.trim() == '') {
// Simple cases using standard Latin-script patterns - can be handled via trim()
return {
text: text,
isWhitespace: true
};
}
// could add simple check for other, non-default cases here.
return {
text: text
};
}
describe('Tokenization functions', function() {
describe('tokenize', function() {
it('tokenizes English using defaults, pre-whitespace caret', function() {
let context = {
left: "The quick brown fox",
right: " jumped over the lazy dog",
startOfBuffer: true,
endOfBuffer: true
};
let tokenization = models.tokenize(wordBreakers.default, context);
let expectedResult = {
left: ['The', ' ', 'quick', ' ', 'brown', ' ', 'fox'].map(asProcessedToken),
right: [' ', 'jumped', ' ', 'over', ' ', 'the', ' ', 'lazy', ' ', 'dog'].map(asProcessedToken),
caretSplitsToken: false
};
assert.deepEqual(tokenization, expectedResult);
});
it('tokenizes English using defaults, pre-whitespace caret, partial context', function() {
let context = {
left: "quick brown fox", // No "The"
right: " jumped over the lazy ", // No "dog"
startOfBuffer: false,
endOfBuffer: false
};
let tokenization = models.tokenize(wordBreakers.default, context);
let expectedResult = {
left: ['quick', ' ', 'brown', ' ', 'fox'].map(asProcessedToken),
right: [' ', 'jumped', ' ', 'over', ' ', 'the', ' ', 'lazy', ' ', ''].map(asProcessedToken),
caretSplitsToken: false
};
assert.deepEqual(tokenization, expectedResult);
});
it('tokenizes English using defaults, post-whitespace caret', function() {
let context = {
left: "The quick brown fox ",
right: "jumped over the lazy dog",
startOfBuffer: true,
endOfBuffer: true
};
let tokenization = models.tokenize(wordBreakers.default, context);
// Technically, we're editing the start of the first token on the right
// when in this context.
let expectedResult = {
left: ['The', ' ', 'quick', ' ', 'brown', ' ', 'fox', ' ', ''].map(asProcessedToken),
right: ['jumped', ' ', 'over', ' ', 'the', ' ', 'lazy', ' ', 'dog'].map(asProcessedToken),
caretSplitsToken: true
};
assert.deepEqual(tokenization, expectedResult);
});
it('tokenizes English using ascii-breaker, post-whitespace caret', function() {
let context = {
left: "The quick brown fox ",
right: "jumped over the lazy dog ",
startOfBuffer: true,
endOfBuffer: true
};
let tokenization = models.tokenize(wordBreakers.ascii, context);
// Technically, we're editing the start of the first token on the right
// when in this context.
let expectedResult = {
left: ['The', ' ', 'quick', ' ', 'brown', ' ', 'fox', ' '].map(asProcessedToken),
right: ['jumped', ' ', 'over', ' ', 'the', ' ', 'lazy', ' ', 'dog', ' '].map(asProcessedToken),
caretSplitsToken: false
};
assert.deepEqual(tokenization, expectedResult);
});
it('tokenizes English using defaults, post-whitespace caret, partial context', function() {
let context = {
left: "quick brown fox ",
right: "jumped over the lazy",
startOfBuffer: false,
endOfBuffer: false
};
let tokenization = models.tokenize(wordBreakers.default, context);
// Technically, we're editing the start of the first token on the right
// when in this context.
let expectedResult = {
left: ['quick', ' ', 'brown', ' ', 'fox', ' ', ''].map(asProcessedToken),
right: ['jumped', ' ', 'over', ' ', 'the', ' ', 'lazy'].map(asProcessedToken),
caretSplitsToken: true
};
assert.deepEqual(tokenization, expectedResult);
});
it('tokenizes English using defaults, splitting caret, complete context (start, end == true)', function() {
let context = {
left: "The quick brown fox jum",
right: "ped over the lazy dog",
startOfBuffer: true,
endOfBuffer: true
};
let tokenization = models.tokenize(wordBreakers.default, context);
let expectedResult = {
left: ['The', ' ', 'quick', ' ', 'brown', ' ', 'fox', ' ', 'jum'].map(asProcessedToken),
right: ['ped', ' ', 'over', ' ', 'the', ' ', 'lazy', ' ', 'dog'].map(asProcessedToken),
caretSplitsToken: true
};
assert.deepEqual(tokenization, expectedResult);
});
it('tokenizes English using defaults, splitting caret, incomplete context (start, end == false)', function() {
let context = {
left: "The quick brown fox jum",
right: "ped over the lazy dog",
startOfBuffer: false,
endOfBuffer: false
};
let tokenization = models.tokenize(wordBreakers.default, context);
let expectedResult = {
left: ['The', ' ', 'quick', ' ', 'brown', ' ', 'fox', ' ', 'jum'].map(asProcessedToken),
right: ['ped', ' ', 'over', ' ', 'the', ' ', 'lazy', ' ', 'dog'].map(asProcessedToken),
caretSplitsToken: true
};
assert.deepEqual(tokenization, expectedResult);
});
it('properly handles empty-context cases', function() {
// Wordbreaking on a empty space => no word.
let context = {
left: '', startOfBuffer: true,
right: '', endOfBuffer: true
};
let tokenization = models.tokenize(wordBreakers.default, context);
let expectedResult = {
left: [],
right: [],
caretSplitsToken: false
};
assert.deepEqual(tokenization, expectedResult);
});
it('properly handles null context cases', function() {
// Wordbreaking on a empty space => no word.
let tokenization = models.tokenize(wordBreakers.default, null);
let expectedResult = {
left: [],
right: [],
caretSplitsToken: false
};
assert.deepEqual(tokenization, expectedResult);
});
it('properly handles a near-empty context: one space before caret', function() {
// Wordbreaking on a empty space => no word.
let context = {
left: ' ', startOfBuffer: true,
right: '', endOfBuffer: true
};
let tokenization = models.tokenize(wordBreakers.default, context);
let expectedResult = {
left: [' ', ''].map(asProcessedToken),
right: [],
caretSplitsToken: false
};
assert.deepEqual(tokenization, expectedResult);
});
it('properly tokenizes partial English contractions - default setting', function() {
let context = {
left: "I can'",
right: "",
startOfBuffer: true,
endOfBuffer: true
};
let tokenization = models.tokenize(wordBreakers.default, context);
// Technically, we're editing the start of the first token on the right
// when in this context.
let expectedResult = {
left: ['I', ' ', 'can\''].map(asProcessedToken),
right: [].map(asProcessedToken),
caretSplitsToken: false
};
assert.deepEqual(tokenization, expectedResult);
});
it('overly tokenizes partial English contractions when (default) apostrophe rejoin is disabled', function() {
let context = {
left: "I can'",
right: "",
startOfBuffer: true,
endOfBuffer: true
};
let tokenization = models.tokenize(wordBreakers.default, context, { rejoins: [] });
// Technically, we're editing the start of the first token on the right
// when in this context.
let expectedResult = {
left: ['I', ' ', 'can' , '\''].map(asProcessedToken),
right: [].map(asProcessedToken),
caretSplitsToken: false
};
assert.deepEqual(tokenization, expectedResult);
});
it('properly tokenizes English contractions', function() {
// Note: a 'context0' with the caret before the `'` actually
// is not supported well yet; a leading `'` is broken from
// following text when in isolation.
let context1 = {
left: "I can'",
right: "t",
startOfBuffer: true,
endOfBuffer: true
};
let context2 = {
left: "I can't",
right: "",
startOfBuffer: true,
endOfBuffer: true
}
let tokenization1 = models.tokenize(wordBreakers.default, context1);
let tokenization2 = models.tokenize(wordBreakers.default, context2);
let expectedResult1 = {
left: ['I', ' ', 'can\''].map(asProcessedToken),
right: ['t'].map(asProcessedToken),
caretSplitsToken: true
};
let expectedResult2 = {
left: ['I', ' ', 'can\'t'].map(asProcessedToken),
right: [].map(asProcessedToken),
caretSplitsToken: false
};
assert.deepEqual(tokenization1, expectedResult1);
assert.deepEqual(tokenization2, expectedResult2);
});
// For the next few tests: a mocked wordbreaker for Khmer, a language
// without whitespace between words.
let mockedKhmerBreaker = function(text) {
// Step 1: Build constants for spans that a real wordbreaker would return.
let srok = { // Khmer romanization of 'ស្រុក'
text: 'ស្រុក',
start: 0,
end: 5, // ្រ = ្ + រ
length: 5
};
let sro = { // Khmer romanization of 'ស្រុ'
text: 'ស្រុ',
start: 0,
end: 4,
length: 4
};
let k = { // Not the proper Khmer romanization; 'k' used here for easier readability.
text: 'ក',
start: 0,
end: 1,
length: 1
};
let khmer = { // Khmer romanization of 'ខ្មែរ'
text: 'ខ្មែរ',
start: 0,
end: 5, // ្ម = ្ + ម
length: 5
}
// Step 2: Allow shifting a defined 'constant' span without mutating the definition.
let shiftSpan = function(span, delta) {
// Avoid mutating the parameter!
let shiftedSpan = {
text: span.text,
start: span.start + delta,
end: span.end + delta,
length: span.length
};
return shiftedSpan;
}
// Step 3: Define return values for the cases we expect to need mocking.
switch(text) {
case 'ស្រុ':
return [sro];
case 'ក':
return [k];
case 'ស្រុក':
return [srok];
case 'ខ្មែរ':
return [khmer];
case 'ស្រុកខ្មែរ':
return [srok, shiftSpan(khmer, srok.length)]; // array of the two.
case 'កខ្មែរ':
// I'd admittedly be at least somewhat surprised if a real wordbreaker got this
// and similar situations perfectly right... but at least it gives us what
// we need for a test.
return [k, shiftSpan(khmer, k.length)];
default:
throw "Dummying error - no return value specified for \"" + text + "\"!";
}
}
it('tokenizes Khmer using mocked wordbreaker, caret between words', function() {
// The two words:
// - ស្រុក - 'land'
// - ខ្មែរ - 'Khmer'
// Translation: Cambodia (informal), lit: "Khmer land" / "land of [the] Khmer"
let context = {
left: "ស្រុក",
right: "ខ្មែរ",
startOfBuffer: true,
endOfBuffer: true
};
let tokenization = models.tokenize(mockedKhmerBreaker, context);
let expectedResult = {
left: ['ស្រុក'].map(asProcessedToken),
right: ['ខ្មែរ'].map(asProcessedToken),
caretSplitsToken: false
};
assert.deepEqual(tokenization, expectedResult);
});
it('tokenizes Khmer using mocked wordbreaker, caret within word', function() {
// The two words:
// - ស្រុក - 'land'
// - ខ្មែរ - 'Khmer'
// Translation: Cambodia (informal), lit: "Khmer land" / "land of [the] Khmer"
let context = {
left: "ស្រុ",
right: "កខ្មែរ",
startOfBuffer: true,
endOfBuffer: true
};
let tokenization = models.tokenize(mockedKhmerBreaker, context);
let expectedResult = {
left: ['ស្រុ'].map(asProcessedToken),
right: ['ក', 'ខ្មែរ'].map(asProcessedToken),
caretSplitsToken: true
};
assert.deepEqual(tokenization, expectedResult);
});
let midLetterNonbreaker = (text) => {
let customization = {
rules: [{
match: (context) => {
if(context.propertyMatch(null, ["ALetter"], ["MidLetter"], ["eot"])) {
return true;
} else {
return false;
}
},
breakIfMatch: false
}],
propertyMapping: (char) => {
let hyphens = ['\u002d', '\u2010', '\u058a', '\u30a0'];
if(hyphens.includes(char)) {
return "MidLetter";
} else {
return null;
}
}
};
return wordBreakers.default(text, customization);
}
it('treats caret as `eot` for pre-caret text tokenization', function() {
let context = {
left: "don-", // We use a hyphen here b/c single-quote is hardcoded.
right: " worry",
endOfBuffer: true,
startOfBuffer: true
};
let tokenization = models.tokenize(wordBreakers.default, context);
assert.deepEqual(tokenization, {
left: ["don", "-"].map(asProcessedToken),
right: [" ", "worry"].map(asProcessedToken),
caretSplitsToken: false
});
tokenization = models.tokenize(midLetterNonbreaker, context);
assert.deepEqual(tokenization, {
left: ["don-"].map(asProcessedToken),
right: [" ", "worry"].map(asProcessedToken),
caretSplitsToken: false
});
});
it('handles mid-contraction tokenization (via wordbreaker customization)', function() {
let context = {
left: "don:",
right: "t worry",
endOfBuffer: true,
startOfBuffer: true
};
let tokenization = models.tokenize(wordBreakers.default, context);
assert.deepEqual(tokenization, {
// This particular case feels like a possible issue.
// It'd be a three-way split token, as "don:t" would
// be a single token were it not for the caret in the middle.
left: ["don", ":"].map(asProcessedToken),
right: ["t", " ", "worry"].map(asProcessedToken),
caretSplitsToken: false
})
tokenization = models.tokenize(midLetterNonbreaker, context);
assert.deepEqual(tokenization, {
left: ["don:"].map(asProcessedToken),
right: ["t", " ", "worry"].map(asProcessedToken),
caretSplitsToken: true
});
});
it('mitigates effects of previously-distributed malformed wordbreaker output', function () {
const text = 'the quick brown fox jumped over the lazy dog ';
/** @type { Context } */
const context = {
left: text,
right: '',
startOfBuffer: true,
endOfBuffer: true
}
const tokenized = models.tokenize(customWordBreakerFormer, context);
// Mitigation aims to prevent the _worst_ side-effects that can result from invalidating the
// underlying assumption of a monotonically-increasing index within the context -
// assigning repeated or blank entries the text that preceded them!
assert.notExists(tokenized.left.find((token) => token.text == text));
assert.notExists(tokenized.left.find((token) => token.text.startsWith(text.substring(0, 25))));
// 'the' appears twice in the context, which should result in two separate 'the' tokens here.
// This was improperly handled when we didn't check that assumption.
assert.equal(tokenized.left.filter((token) => token.text == 'the').length, 2);
// Does not address multiple blank-token ('') entries that result from intervening spaces;
// that would add too much extra complexity to the method... and it can already be
// handled decently by the predictive-text engine.
assert.deepEqual(
tokenized.left
.filter((entry) => !entry.isWhitespace)
.filter((entry) => entry.text != '')
.map((entry) => entry.text),
['the', 'quick', 'brown', 'fox', 'jumped', 'over', 'the', 'lazy', 'dog']
);
});
it('properly works with well-formed custom wordbreaker output', function () {
const text = 'the quick brown fox jumped over the lazy dog ';
/** @type { Context } */
const context = {
left: text,
right: '',
startOfBuffer: true,
endOfBuffer: true
}
const tokenized = models.tokenize(customWordBreakerProper, context);
// Easier-to-parse version
assert.deepEqual(
tokenized.left
.filter((entry) => !entry.isWhitespace)
.map((entry) => entry.text),
['the', 'quick', 'brown', 'fox', 'jumped', 'over', 'the', 'lazy', 'dog']
);
// This time, with whitespaces.
assert.deepEqual(
tokenized.left.map((entry) => entry.text), [
'the',
' ',
'quick',
' ',
'brown',
' ',
'fox',
' ',
'jumped',
' ',
'over',
' ',
'the',
' ',
'lazy',
' ',
'dog',
' '
]
);
});
//
});
describe('getLastPreCaretToken', function() {
it('operates properly with pre-whitespace caret', function() {
let context = {
left: "The quick brown fox",
right: " jumped over the lazy dog",
startOfBuffer: true,
endOfBuffer: true
};
let tokenization = models.getLastPreCaretToken(wordBreakers.default, context);
assert.equal(tokenization, 'fox');
});
it('operates properly with post-whitespace caret', function() {
let context = {
left: "The quick brown fox ",
right: "jumped over the lazy dog",
startOfBuffer: true,
endOfBuffer: true
};
let tokenization = models.getLastPreCaretToken(wordBreakers.default, context);
assert.equal(tokenization, '');
});
it('operates properly with post-whitespace caret, ascii breaker', function() {
let context = {
left: "The quick brown fox ",
right: "jumped over the lazy dog",
startOfBuffer: true,
endOfBuffer: true
};
let tokenization = models.getLastPreCaretToken(wordBreakers.ascii, context);
assert.equal(tokenization, '');
});
it('operates properly within a token', function() {
let context = {
left: "The quick brown fox jum",
right: "ped over the lazy dog",
startOfBuffer: true,
endOfBuffer: true
};
let tokenization = models.getLastPreCaretToken(wordBreakers.default, context);
assert.equal(tokenization, 'jum');
});
it('operates properly with no context', function() {
let tokenization = models.getLastPreCaretToken(wordBreakers.default, null);
assert.equal(tokenization, '');
});
});
describe('wordbreak', function() {
it('operates properly with pre-whitespace caret', function() {
let context = {
left: "The quick brown fox",
right: " jumped over the lazy dog",
startOfBuffer: true,
endOfBuffer: true
};
let tokenization = models.wordbreak(wordBreakers.default, context);
assert.equal(tokenization, 'fox');
});
it('operates properly with post-whitespace caret', function() {
let context = {
left: "The quick brown fox ",
right: "jumped over the lazy dog",
startOfBuffer: true,
endOfBuffer: true
};
let tokenization = models.wordbreak(wordBreakers.default, context);
assert.equal(tokenization, '');
});
// This version is subject to change. In the future, we may wish the wordbreak
// operation to include "the rest of the word" - the post-caret part.
it('operates properly within a token', function() {
let context = {
left: "The quick brown fox jum",
right: "ped over the lazy dog",
startOfBuffer: true,
endOfBuffer: true
};
let tokenization = models.wordbreak(wordBreakers.default, context);
assert.equal(tokenization, 'jum');
});
});
});