-
Notifications
You must be signed in to change notification settings - Fork 953
Expand file tree
/
Copy pathSentenceTokenizerSpec.js
More file actions
452 lines (401 loc) · 19.4 KB
/
SentenceTokenizerSpec.js
File metadata and controls
452 lines (401 loc) · 19.4 KB
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
import SentenceTokenizer from "../../../../src/languageProcessing/helpers/sentence/SentenceTokenizer";
const mockTokenizer = new SentenceTokenizer();
const doubleQuotePairs = [ { start: "\"", end: "\"" },
{ start: "〝", end: "〞" },
{ start: "“", end: "”" },
{ start: "‟", end: "„" },
{ start: "『", end: "』" },
{ start: "«", end: "»" } ];
describe( "A test for tokenizing a (html) text into sentences", function() {
it( "returns whether the sentenceBeginning beginning is a valid beginning.", function() {
expect( mockTokenizer.isValidSentenceBeginning( "Text with duplicate spaces." ) ).toBe( true );
expect( mockTokenizer.isValidSentenceBeginning( "2 texts with duplicate spaces." ) ).toBe( true );
expect( mockTokenizer.isValidSentenceBeginning( "'" ) ).toBe( true );
expect( mockTokenizer.isValidSentenceBeginning( "¿" ) ).toBe( true );
expect( mockTokenizer.isValidSentenceBeginning( "<" ) ).toBe( true );
expect( mockTokenizer.isValidSentenceBeginning( "أ" ) ).toBe( true );
} );
it( "returns an array of sentences for a given array of tokens started and ended with block with the " +
" start and end of the quote pair", function() {
doubleQuotePairs.forEach( ( quotePair ) => {
const tokens = [
{ type: "block-start", src: "<form>" },
{ type: "html-start", src: "<p>" },
{ type: "sentence", src: "First sentence" },
{ type: "sentence-delimiter", src: ":" },
{ type: "sentence", src: " second sentence" },
{ type: "full-stop", src: "." },
{ type: "sentence", src: " Third sentence" },
{ type: "full-stop", src: "." },
{ type: "sentence-delimiter", src: quotePair.start },
{ type: "sentence", src: " Fourth sentence" },
{ type: "sentence-delimiter", src: quotePair.end },
{ type: "html-end", src: "<br> element.</p>" },
{ type: "block-end", src: "</form>" },
];
expect( mockTokenizer.getSentencesFromTokens( tokens ) ).toEqual( [
"<form><p>First sentence:",
"second sentence.",
"Third sentence." + quotePair.start,
"Fourth sentence" + quotePair.end,
"</form>",
] );
} );
} );
it( "returns an array of sentences for a given array of tokens that include quotation marks", function() {
const tokens = [
{ type: "sentence", src: "Hello", line: 1, col: 1 },
{ type: "full-stop", src: ".", line: 1, col: 6 },
{ type: "sentence", src: " ", line: 1, col: 7 },
{ type: "sentence-delimiter", src: "\"", line: 1, col: 8 },
{ type: "sentence", src: "How are you", line: 1, col: 9 },
{ type: "sentence-delimiter", src: "\"", line: 1, col: 20 },
{ type: "sentence", src: " Bye", line: 1, col: 21 },
];
expect( mockTokenizer.getSentencesFromTokens( tokens ) ).toEqual( [
"Hello.",
"\"How are you\" Bye",
] );
} );
it( "returns an array of sentences for a given array of tokens that include quotation marks preceded by" +
"period", function() {
const tokens = [
{ type: "sentence", src: "Hello", line: 1, col: 1 },
{ type: "full-stop", src: ".", line: 1, col: 6 },
{ type: "sentence", src: " ", line: 1, col: 7 },
{ type: "sentence-delimiter", src: "\"", line: 1, col: 8 },
{ type: "sentence", src: "How are you", line: 1, col: 9 },
{ type: "full-stop", src: ".", line: 1, col: 20 },
{ type: "sentence-delimiter", src: "\"", line: 1, col: 21 },
{ type: "sentence", src: " Bye", line: 1, col: 22 },
];
expect( mockTokenizer.getSentencesFromTokens( tokens ) ).toEqual( [
"Hello.",
"\"How are you.\"",
"Bye",
] );
} );
it( "returns an array of sentences for a given array of tokens started and ended with html tags.", function() {
const tokens = [
{ type: "html-start", src: "<p>" },
{ type: "sentence", src: "First sentence" },
{ type: "sentence-delimiter", src: ":" },
{ type: "sentence", src: " second sentence" },
{ type: "full-stop", src: "." },
{ type: "sentence", src: " 5 is " },
{ type: "smaller-than-sign-content", src: "< than 7." },
{ type: "html-end", src: "</p>" },
];
expect( mockTokenizer.getSentencesFromTokens( tokens ) ).toEqual( [
"First sentence:",
"second sentence.",
"5 is < than 7.",
] );
} );
it( "returns an array of sentences for a given array of tokens when there is no next token after sentence delimiter.", function() {
const tokens = [
{ type: "sentence", src: "First sentence" },
{ type: "sentence-delimiter", src: ":" },
];
expect( mockTokenizer.getSentencesFromTokens( tokens ) ).toEqual( [ "First sentence:" ] );
} );
it( "returns an array of sentences for a given array of tokens when there is no next token after full stop.", function() {
const tokens = [
{ type: "sentence", src: "One cat is special" },
{ type: "full-stop", src: "." },
];
expect( mockTokenizer.getSentencesFromTokens( tokens ) ).toEqual( [ "One cat is special." ] );
} );
it( "does not split sentences if 'block-end' is followed by a token started with a number or " +
"if it is preceded by a valid sentence ending, but not followed by a valid sentence beginning.", function() {
let tokens = [
{ type: "block-start", src: "<form>" },
{ type: "sentence", src: "One cat is special" },
{ type: "full-stop", src: "." },
{ type: "block-end", src: "</form>" },
{ type: "sentence", src: "5 is perfect!" },
];
expect( mockTokenizer.getSentencesFromTokens( tokens ) ).toEqual( [ "<form>One cat is special.</form>5 is perfect!" ] );
tokens = [
{ type: "block-start", src: "<form>" },
{ type: "sentence", src: "One cat is special" },
{ type: "full-stop", src: "." },
{ type: "block-end", src: "</form>" },
{ type: "sentence", src: "five is perfect!" },
];
expect( mockTokenizer.getSentencesFromTokens( tokens ) ).toEqual( [ "<form>One cat is special.</form>five is perfect!" ] );
} );
it( "does not split sentences if it contains an abbreviation", function() {
const tokens = [
{ type: "sentence", src: "Our feline teacher is called Prof" },
{ type: "full-stop", src: "." },
{ type: "sentence", src: " Felix the cat" },
{ type: "full-stop", src: "." },
];
expect( mockTokenizer.getSentencesFromTokens( tokens ) ).toEqual( [ "Our feline teacher is called Prof. Felix the cat." ] );
} );
it( "does not split sentences if last word is No. and a number. ", function() {
const tokens = [
{ type: "sentence", src: "My favorite song is Mambo No" },
{ type: "full-stop", src: "." },
{ type: "sentence", src: " 5" },
{ type: "full-stop", src: "." },
];
expect( mockTokenizer.getSentencesFromTokens( tokens ) ).toEqual( [ "My favorite song is Mambo No. 5." ] );
} );
it( "endsWithAbbreviation returns true if a string ends in an abbreviation", function() {
const testString = "This string ends with an abbreviation e.g.";
expect( mockTokenizer.endsWithAbbreviation( testString ) ).toBeTruthy();
} );
it( "endsWithAbbreviation returns false if a string does not end with an abbreviation,", function() {
const testString = "This string does not end with an abbreviation.";
expect( mockTokenizer.endsWithAbbreviation( testString ) ).toBeFalsy();
} );
it( "endsWithAbbreviation returns false if string does not end with an abbreviation but has an abbreviation in the middle.", function() {
const testString = "This is e.g. a sentence.";
expect( mockTokenizer.endsWithAbbreviation( testString ) ).toBeFalsy();
} );
it( "endsWithAbbreviation returns true if string contains multiple abbreviations and ends with one.", function() {
const testString = "Prof. Barabas was helped by Dr.";
expect( mockTokenizer.endsWithAbbreviation( testString ) ).toBeTruthy();
} );
it( "endsWithAbbreviation returns false if string contains multiple abbreviations and does not end with one.", function() {
const testString = "Prof. Barabas was helped by Dr. Wargaren.";
expect( mockTokenizer.endsWithAbbreviation( testString ) ).toBeFalsy();
} );
it( "splits sentences if 'block-end' is preceded by a sentence ending and followed by a valid sentence beginning.", function() {
let tokens = [
{ type: "block-start", src: "<form>" },
{ type: "sentence", src: "One cat is special" },
{ type: "full-stop", src: "." },
{ type: "block-end", src: "</form>" },
{ type: "sentence", src: "Five is perfect!" },
];
expect( mockTokenizer.getSentencesFromTokens( tokens ) ).toEqual( [ "<form>One cat is special.</form>", "Five is perfect!" ] );
tokens = [
{ type: "block-start", src: "<form>" },
{ type: "sentence", src: "One cat is special" },
{ type: "full-stop", src: "." },
{ type: "block-end", src: "</form>" },
{ type: "block-start", src: "<footer>" },
{ type: "block-end", src: "</footer>" },
];
expect( mockTokenizer.getSentencesFromTokens( tokens ) ).toEqual( [ "<form>One cat is special.</form>", "<footer></footer>" ] );
} );
it( "tokens that represent a '<', followed by content until it enters another '<' or '>' gets another pass by the tokenizer.", function() {
let token = { type: "smaller-than-sign-content", src: "<h2>Positive feedback</h2>" };
let tokenSentences = [ "First sentence:", "second sentence.", "<h2>Positive feedback</h2>" ];
let currentSentence = "5 is < than 7.";
expect( mockTokenizer.tokenizeSmallerThanContent( token, tokenSentences, currentSentence ) ).toEqual( {
currentSentence: "<h2>Positive feedback</h2>",
tokenSentences: [ "First sentence:", "second sentence.", "<h2>Positive feedback</h2>", "5 is < than 7." ],
} );
token = { type: "smaller-than-sign-content", src: "positive feedback" };
tokenSentences = [ "First sentence:", "second sentence." ];
currentSentence = "Third sentence.";
expect( mockTokenizer.tokenizeSmallerThanContent( token, tokenSentences, currentSentence ) ).toEqual( {
currentSentence: "Third sentence.<ositive feedback",
tokenSentences: [ "First sentence:", "second sentence." ],
} );
token = { type: "smaller-than-sign-content", src: "<p>To force<br> line breaks<br> in a text,<br> use the br<br> element.</p>" };
expect( mockTokenizer.tokenizeSmallerThanContent( token, tokenSentences, currentSentence ) ).toEqual( {
currentSentence: "</p>",
tokenSentences: [ "First sentence:", "second sentence.", "Third sentence.", "<p>To force", " line breaks",
" in a text,", " use the br", " element." ],
} );
token = { type: "smaller-than-sign-content", src: "<p>Use<br> this</p>?" };
tokenSentences = [ "First sentence:", "second sentence." ];
currentSentence = "Another sentence.";
expect( mockTokenizer.tokenizeSmallerThanContent( token, tokenSentences, currentSentence ) ).toEqual( {
currentSentence: "",
tokenSentences: [ "First sentence:", "second sentence.", "Another sentence.", "<p>Use", " this</p>?" ],
} );
} );
it( "throws an error.", function() {
const errorMessage = "Test end.";
const tokenizer = mockTokenizer.createTokenizer();
tokenizer.tokenizer.end = () => {
const error = new Error( errorMessage );
error.tokenizer2 = "test";
throw error;
};
const consoleSpy = jest.spyOn( console, "error" );
console.error.mockImplementation( () => {} );
mockTokenizer.tokenize( tokenizer.tokenizer, "This is a string." );
expect( consoleSpy ).toHaveBeenCalledWith( "Tokenizer end error:", new Error( errorMessage ), "test" );
console.error.mockRestore();
} );
it( "does not break a sentence in two if there are initials within.", function() {
// The reprint was favourably reviewed by "A. B." in The Musical Times in 1935, who commented "Praise is due to Mr Mercer.
const tokens = [
{ type: "sentence", src: 'The reprint was favourably reviewed by "A' },
{ type: "full-stop", src: "." },
{ type: "sentence", src: " B" },
{ type: "full-stop", src: "." },
{ type: "sentence", src: '" in The Musical Times in , who commented "Praise is due to Mr Mercer' },
{ type: "full-stop", src: "." },
];
// eslint-disable-next-line max-len
expect( mockTokenizer.getSentencesFromTokens( tokens ) ).toEqual( [ "The reprint was favourably reviewed by \"A. B.\" in The Musical Times in , who commented \"Praise is due to Mr Mercer." ] );
} );
it( "recognizes sentence boundary when a sentence starts with initials", function() {
// 'This is a sentence. E.F. is a good writer. G. H. is a very very great personality.'
const tokens = [
{ type: "sentence", src: " This is a sentence" },
{ type: "full-stop", src: "." },
{ type: "sentence", src: " E" },
{ type: "full-stop", src: "." },
{ type: "sentence", src: "F" },
{ type: "full-stop", src: "." },
{ type: "sentence", src: " is a good writer" },
{ type: "full-stop", src: "." },
{ type: "sentence", src: " G" },
{ type: "full-stop", src: "." },
{ type: "sentence", src: " H" },
{ type: "full-stop", src: "." },
{ type: "sentence", src: " is a very very great personality" },
{ type: "full-stop", src: "." },
];
// eslint-disable-next-line max-len
expect( mockTokenizer.getSentencesFromTokens( tokens ) ).toEqual( [ "This is a sentence.", "E.F. is a good writer.", "G. H. is a very very great personality." ] );
} );
it( "recognizes sentence boundary when a sentence ends with initials", function() {
// 'A cat is possessed by C. D. This is a sentence.'
const tokens = [
{ type: "sentence", src: " A cat is possessed by C" },
{ type: "full-stop", src: "." },
{ type: "sentence", src: " D" },
{ type: "full-stop", src: "." },
{ type: "sentence", src: " This is a sentence" },
{ type: "full-stop", src: "." },
];
expect( mockTokenizer.getSentencesFromTokens( tokens ) ).toEqual( [ "A cat is possessed by C. D.", "This is a sentence." ] );
} );
it( "recognizes when a full stop is part of a person's initials", function() {
const tokens = [
{ type: "sentence", src: " A cat is possessed by C" },
{ type: "full-stop", src: "." },
{ type: "sentence", src: " D" },
{ type: "full-stop", src: "." },
{ type: "sentence", src: " This is a sentence" },
{ type: "full-stop", src: "." },
];
const token = tokens[ 1 ];
const previousToken = tokens[ 0 ];
const nextToken = tokens[ 2 ];
const secondToNextToken = tokens[ 3 ];
expect( mockTokenizer.isPartOfPersonInitial( token, previousToken, nextToken, secondToNextToken ) ).toBeTruthy();
} );
it( "recognizes when a full stop is not part of a person's initials", function() {
const tokens = [
{ type: "sentence", src: "This is a sentence" },
{ type: "full-stop", src: "." },
{ type: "sentence", src: " This is a sentence" },
{ type: "full-stop", src: "." },
];
const token = tokens[ 1 ];
const previousToken = tokens[ 0 ];
const nextToken = tokens[ 2 ];
const secondToNextToken = tokens[ 3 ];
expect( mockTokenizer.isPartOfPersonInitial( token, previousToken, nextToken, secondToNextToken ) ).toBeFalsy();
} );
xit( "recognizes when a full stop is part of initials with multiple letters", function() {
const tokens = [
{ type: "sentence", src: "This is a sentence" },
{ type: "full-stop", src: "." },
{ type: "sentence", src: " A" },
{ type: "full-stop", src: "." },
{ type: "sentence", src: " F" },
{ type: "full-stop", src: "." },
{ type: "sentence", src: " Th" },
{ type: "full-stop", src: "." },
{ type: "sentence", src: " van der Heijden is a well known Dutch writer." },
{ type: "full-stop", src: "." },
{ type: "sentence", src: "This is a sentence" },
{ type: "full-stop", src: "." },
];
const token = tokens[ 5 ];
const previousToken = tokens[ 4 ];
const nextToken = tokens[ 6 ];
const secondToNextToken = tokens[ 7 ];
expect( mockTokenizer.isPartOfPersonInitial( token, previousToken, nextToken, secondToNextToken ) ).toBeTruthy();
} );
it( "endsWithOrdinalDot should return false when the German tokenizer is not used", () => {
expect( mockTokenizer.endsWithOrdinalDot( "Anything you want to put here, it shouldn't matter." ) ).toBe( false );
} );
it( "recognizes initials in the edge case where there are quotes around the initials", function() {
doubleQuotePairs.forEach( ( quotePair ) => {
const tokens = [
{ type: "sentence", src: "The reprint was favourably reviewed by" },
{ type: "sentence-delimiter", src: quotePair.start },
{ type: "sentence", src: "A" },
{ type: "full-stop", src: "." },
{ type: "sentence", src: " B" },
{ type: "full-stop", src: "." },
{ type: "sentence-delimiter", src: quotePair.end },
{ type: "sentence", src: " in The Musical Times in 1935, who commented" },
{ type: "sentence-delimiter", src: quotePair.start },
{ type: "sentence", src: "Praise is due to Mr Mercer" },
{ type: "full-stop", src: "." },
{ type: "sentence-delimiter", src: quotePair.end },
];
const token = tokens[ 3 ];
const previousToken = tokens[ 2 ];
const nextToken = tokens[ 4 ];
const secondToNextToken = tokens[ 5 ];
expect( mockTokenizer.isPartOfPersonInitial( token, previousToken, nextToken, secondToNextToken ) ).toBe( true );
} );
} );
it( "correctly constructs the sentence in the edge case where there are quotes around the initials", function() {
doubleQuotePairs.forEach( ( quotePair ) => {
const tokens = [
{ type: "sentence", src: "The reprint was favourably reviewed by" },
{ type: "sentence-delimiter", src: " " + quotePair.start },
{ type: "sentence", src: "A" },
{ type: "full-stop", src: "." },
{ type: "sentence", src: " B" },
{ type: "full-stop", src: "." },
{ type: "sentence-delimiter", src: quotePair.end },
{ type: "sentence", src: " in The Musical Times in 1935, who commented" },
{ type: "sentence-delimiter", src: quotePair.start },
{ type: "sentence", src: "Praise is due to Mr Mercer" },
{ type: "full-stop", src: "." },
{ type: "sentence-delimiter", src: quotePair.end },
];
expect( mockTokenizer.getSentencesFromTokens( tokens ) ).toEqual( [
"The reprint was favourably reviewed by " + quotePair.start + "A. B." + quotePair.end +
" in The Musical Times in 1935, who commented" + quotePair.start + "Praise is due to Mr Mercer." + quotePair.end ] );
} );
} );
} );
describe( "testing the isValidTagPair helper method", function() {
it( "returns true if the tags are of the same type and the correct type", function() {
[ "p", "div", "h1", "h2", "h3", "h4", "h5", "h6", "span" ].forEach( function( tagType ) {
const mockOpenTag = { src: `<${tagType}>` };
const mockCloseTag = { src: `</${tagType}>` };
expect( mockTokenizer.isValidTagPair( mockOpenTag, mockCloseTag ) ).toBe( true );
} );
} );
it( "returns true if the tags are of the same type and the correct type and the have attributes", function() {
[ "p", "div", "h1", "h2", "h3", "h4", "h5", "h6", "span" ].forEach( function( tagType ) {
const mockOpenTag = { src: `<${tagType} class="onzin" messy="1">` };
const mockCloseTag = { src: `</${tagType}>` };
expect( mockTokenizer.isValidTagPair( mockOpenTag, mockCloseTag ) ).toBe( true );
} );
} );
it( "returns false if the tags are of the same type but not of the correct type", function() {
const mockOpenTag = { src: "<i>" };
const mockCloseTag = { src: "</i>" };
expect( mockTokenizer.isValidTagPair( mockOpenTag, mockCloseTag ) ).toBe( false );
} );
it( "returns false if the tags are of te correct type but not of the same type", function() {
const mockOpenTag = { src: "<div>" };
const mockCloseTag = { src: "</span>" };
expect( mockTokenizer.isValidTagPair( mockOpenTag, mockCloseTag ) ).toBe( false );
} );
it( "returns false if the tags are of te wrong type and not of the same type", function() {
const mockOpenTag = { src: "<i>" };
const mockCloseTag = { src: "</b>" };
expect( mockTokenizer.isValidTagPair( mockOpenTag, mockCloseTag ) ).toBe( false );
} );
} );