-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathtest_tokenize_sentence.py
494 lines (380 loc) · 17.6 KB
/
test_tokenize_sentence.py
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
import pytest
import flair
from flair.data import Sentence, Token
from flair.splitter import (
NewlineSentenceSplitter,
NoSentenceSplitter,
SciSpacySentenceSplitter,
SegtokSentenceSplitter,
SpacySentenceSplitter,
TagSentenceSplitter,
)
from flair.tokenization import (
JapaneseTokenizer,
SciSpacyTokenizer,
SegtokTokenizer,
SpaceTokenizer,
SpacyTokenizer,
TokenizerWrapper,
)
def test_create_sentence_on_empty_string():
sentence: Sentence = Sentence("")
assert len(sentence.tokens) == 0
def test_create_sentence_with_newline():
sentence: Sentence = Sentence(["I", "\t", "ich", "\n", "you", "\t", "du", "\n"])
assert len(sentence.tokens) == 8
assert sentence.tokens[3].text == "\n"
sentence: Sentence = Sentence("I \t ich \n you \t du \n", use_tokenizer=False)
assert len(sentence.tokens) == 8
assert sentence.tokens[0].start_position == 0
assert sentence.tokens[3].text == "\n"
def test_create_sentence_with_extra_whitespace():
sentence: Sentence = Sentence("I love Berlin .")
assert len(sentence.tokens) == 4
assert sentence.get_token(1).text == "I"
assert sentence.get_token(2).text == "love"
assert sentence.get_token(3).text == "Berlin"
assert sentence.get_token(4).text == "."
def test_create_sentence_difficult_encoding():
text = "so out of the norm ❤ ️ enjoyed every moment️"
sentence = Sentence(text)
assert len(sentence) == 9
text = (
"equivalently , accumulating the logs as :( 6 ) sl = 1N ∑ t = 1Nlogp "
"( Ll | xt \u200b , θ ) where "
"p ( Ll | xt \u200b , θ ) represents the class probability output"
)
sentence = Sentence(text)
assert len(sentence) == 37
text = "This guy needs his own show on Discivery Channel ! "
sentence = Sentence(text)
assert len(sentence) == 10
text = "n't have new vintages."
sentence = Sentence(text, use_tokenizer=True)
assert len(sentence) == 5
def test_create_sentence_word_by_word():
token1: Token = Token("Munich")
token2: Token = Token("and")
token3: Token = Token("Berlin")
token4: Token = Token("are")
token5: Token = Token("nice")
sentence: Sentence = Sentence([token1, token2, token3, token4, token5, Token("cities"), Token(".")])
assert sentence.to_tokenized_string() == "Munich and Berlin are nice cities ."
def test_create_sentence_pretokenized():
pretoks = ["The", "grass", "is", "green", "."]
sent = Sentence(pretoks)
for i, token in enumerate(sent):
assert token.text == pretoks[i]
def test_create_sentence_without_tokenizer():
sentence: Sentence = Sentence("I love Berlin.", use_tokenizer=False)
assert len(sentence.tokens) == 3
assert sentence.tokens[0].start_position == 0
assert sentence.tokens[0].text == "I"
assert sentence.tokens[1].start_position == 2
assert sentence.tokens[1].text == "love"
assert sentence.tokens[2].start_position == 7
assert sentence.tokens[2].text == "Berlin."
def test_create_sentence_with_default_tokenizer():
sentence: Sentence = Sentence("I love Berlin.", use_tokenizer=True)
assert len(sentence.tokens) == 4
assert sentence.tokens[0].start_position == 0
assert sentence.tokens[0].text == "I"
assert sentence.tokens[1].start_position == 2
assert sentence.tokens[1].text == "love"
assert sentence.tokens[2].start_position == 7
assert sentence.tokens[2].text == "Berlin"
assert sentence.tokens[3].start_position == 13
assert sentence.tokens[3].text == "."
def test_create_sentence_with_segtok():
sentence: Sentence = Sentence("I love Berlin.", use_tokenizer=SegtokTokenizer())
assert len(sentence.tokens) == 4
assert sentence.tokens[0].text == "I"
assert sentence.tokens[1].text == "love"
assert sentence.tokens[2].text == "Berlin"
assert sentence.tokens[3].text == "."
def test_create_sentence_with_custom_tokenizer():
sentence: Sentence = Sentence("I love Berlin.", use_tokenizer=TokenizerWrapper(no_op_tokenizer))
assert len(sentence.tokens) == 1
assert sentence.tokens[0].start_position == 0
assert sentence.tokens[0].text == "I love Berlin."
@pytest.mark.skip(reason="SpacyTokenizer needs optional requirements, so we skip the test by default")
def test_create_sentence_with_spacy_tokenizer():
sentence: Sentence = Sentence("I love Berlin.", use_tokenizer=SpacyTokenizer("en_core_sci_sm"))
assert len(sentence.tokens) == 4
assert sentence.tokens[0].start_position == 0
assert sentence.tokens[0].text == "I"
assert sentence.tokens[1].start_position == 2
assert sentence.tokens[1].text == "love"
assert sentence.tokens[2].start_position == 7
assert sentence.tokens[2].text == "Berlin"
assert sentence.tokens[3].start_position == 13
assert sentence.tokens[3].text == "."
def test_create_sentence_using_japanese_tokenizer():
sentence: Sentence = Sentence("私はベルリンが好き", use_tokenizer=JapaneseTokenizer("janome"))
assert len(sentence.tokens) == 5
assert sentence.tokens[0].text == "私"
assert sentence.tokens[1].text == "は"
assert sentence.tokens[2].text == "ベルリン"
assert sentence.tokens[3].text == "が"
assert sentence.tokens[4].text == "好き"
@pytest.mark.skip(reason="SciSpacyTokenizer need optional requirements, so we skip the test by default")
def test_create_sentence_using_scispacy_tokenizer():
sentence: Sentence = Sentence(
"Spinal and bulbar muscular atrophy (SBMA) is an inherited motor neuron",
use_tokenizer=SciSpacyTokenizer(),
)
assert len(sentence.tokens) == 13
assert sentence.tokens[0].text == "Spinal"
assert sentence.tokens[1].text == "and"
assert sentence.tokens[2].text == "bulbar"
assert sentence.tokens[3].text == "muscular"
assert sentence.tokens[4].text == "atrophy"
assert sentence.tokens[5].text == "("
assert sentence.tokens[6].text == "SBMA"
assert sentence.tokens[7].text == ")"
assert sentence.tokens[8].text == "is"
assert sentence.tokens[9].text == "an"
assert sentence.tokens[10].text == "inherited"
assert sentence.tokens[11].text == "motor"
assert sentence.tokens[12].text == "neuron"
assert sentence.tokens[0].start_position == 0
assert sentence.tokens[1].start_position == 7
assert sentence.tokens[2].start_position == 11
assert sentence.tokens[3].start_position == 18
assert sentence.tokens[4].start_position == 27
assert sentence.tokens[5].start_position == 35
assert sentence.tokens[6].start_position == 36
assert sentence.tokens[7].start_position == 40
assert sentence.tokens[8].start_position == 42
assert sentence.tokens[9].start_position == 45
assert sentence.tokens[10].start_position == 48
assert sentence.tokens[11].start_position == 58
assert sentence.tokens[12].start_position == 64
assert sentence.tokens[4].whitespace_after == 1
assert sentence.tokens[5].whitespace_after != 1
assert sentence.tokens[6].whitespace_after != 1
assert sentence.tokens[7].whitespace_after == 1
def test_split_text_segtok():
segtok_splitter = SegtokSentenceSplitter()
sentences = segtok_splitter._perform_split("I love Berlin. Berlin is a great city.")
assert len(sentences) == 2
assert sentences[0].start_position == 0
assert len(sentences[0].tokens) == 4
assert sentences[1].start_position == 15
assert len(sentences[1].tokens) == 6
segtok_splitter = SegtokSentenceSplitter(tokenizer=TokenizerWrapper(no_op_tokenizer))
sentences = segtok_splitter._perform_split("I love Berlin. Berlin is a great city.")
assert len(sentences) == 2
assert sentences[0].start_position == 0
assert len(sentences[0].tokens) == 1
assert sentences[1].start_position == 15
assert len(sentences[1].tokens) == 1
def test_split_text_nosplit():
no_splitter = NoSentenceSplitter()
sentences = no_splitter._perform_split("I love Berlin")
assert len(sentences) == 1
assert sentences[0].start_position == 0
assert len(sentences[0].tokens) == 3
no_splitter = NoSentenceSplitter(TokenizerWrapper(no_op_tokenizer))
sentences = no_splitter._perform_split("I love Berlin")
assert len(sentences) == 1
assert sentences[0].start_position == 0
assert len(sentences[0].tokens) == 1
def test_split_text_on_tag():
tag_splitter = TagSentenceSplitter(tag="#!")
sentences = tag_splitter._perform_split("I love Berlin#!Me too")
assert len(sentences) == 2
assert sentences[0].start_position == 0
assert len(sentences[0].tokens) == 3
assert sentences[1].start_position == 15
assert len(sentences[1].tokens) == 2
tag_splitter = TagSentenceSplitter(tag="#!", tokenizer=TokenizerWrapper(no_op_tokenizer))
sentences = tag_splitter._perform_split("I love Berlin#!Me too")
assert len(sentences) == 2
assert sentences[0].start_position == 0
assert len(sentences[0].tokens) == 1
assert sentences[1].start_position == 15
assert len(sentences[1].tokens) == 1
sentences = tag_splitter._perform_split("I love Berlin Me too")
assert len(sentences) == 1
sentences = tag_splitter._perform_split("I love Berlin#!#!Me too")
assert len(sentences) == 2
sentences = tag_splitter._perform_split("I love Berl#! #!inMe too")
assert len(sentences) == 2
def test_split_text_on_newline():
newline_splitter = NewlineSentenceSplitter()
sentences = newline_splitter._perform_split("I love Berlin\nMe too")
assert len(sentences) == 2
assert sentences[0].start_position == 0
assert len(sentences[0].tokens) == 3
assert sentences[0].start_position == 0
assert len(sentences[1].tokens) == 2
newline_splitter = NewlineSentenceSplitter(tokenizer=TokenizerWrapper(no_op_tokenizer))
sentences = newline_splitter._perform_split("I love Berlin\nMe too")
assert len(sentences) == 2
assert len(sentences[0].tokens) == 1
assert sentences[1].start_position == 14
assert len(sentences[1].tokens) == 1
sentences = newline_splitter._perform_split("I love Berlin Me too")
assert len(sentences) == 1
sentences = newline_splitter._perform_split("I love Berlin\n\nMe too")
assert len(sentences) == 2
sentences = newline_splitter._perform_split("I love Berlin\n \nMe too")
assert len(sentences) == 2
def test_split_sentence_linkage():
splitter = SegtokSentenceSplitter()
text = "This is a single sentence."
sentences = splitter.split(text)
assert len(sentences) == 1
assert sentences[0].previous_sentence() is None
assert sentences[0].next_sentence() is None
text = "This is a sentence. This is another sentence. This is yet another sentence."
sentences = splitter.split(text)
assert len(sentences) == 3
assert sentences[0].previous_sentence() is None
assert sentences[0].next_sentence() == sentences[1]
assert sentences[1].previous_sentence() == sentences[0]
assert sentences[1].next_sentence() == sentences[2]
assert sentences[2].previous_sentence() == sentences[1]
assert sentences[2].next_sentence() is None
def test_split_sentence_linkage_false():
splitter = SegtokSentenceSplitter()
text = "This is a sentence. This is another sentence. This is yet another sentence."
sentences = splitter.split(text, link_sentences=False)
assert len(sentences) == 3
assert all(s.next_sentence() is None and s.previous_sentence() is None for s in sentences)
@pytest.mark.skip(reason="SpacySentenceSplitter need optional requirements, so we skip the test by default")
def test_split_text_spacy():
spacy_splitter = SpacySentenceSplitter("en_core_sci_sm")
sentences = spacy_splitter._perform_split("This a sentence. And here is another one.")
assert len(sentences) == 2
assert sentences[0].start_position == 0
assert len(sentences[0].tokens) == 4
assert sentences[1].start_position == 17
assert len(sentences[1].tokens) == 6
sentences = spacy_splitter._perform_split("VF inhibits something. ACE-dependent (GH+) issuses too.")
assert len(sentences) == 2
assert sentences[0].start_position == 0
assert len(sentences[0].tokens) == 4
assert sentences[1].start_position == 23
assert len(sentences[1].tokens) == 7
spacy_splitter = SpacySentenceSplitter("en_core_sci_sm", tokenizer=TokenizerWrapper(no_op_tokenizer))
sentences = spacy_splitter._perform_split("This a sentence. And here is another one.")
assert len(sentences) == 2
assert sentences[0].start_position == 0
assert len(sentences[0].tokens) == 1
assert sentences[1].start_position == 17
assert len(sentences[1].tokens) == 1
@pytest.mark.skip(reason="SciSpacySentenceSplitter need optional requirements, so we skip the test by default")
def test_split_text_scispacy():
scispacy_splitter = SciSpacySentenceSplitter()
sentences = scispacy_splitter._perform_split("VF inhibits something. ACE-dependent (GH+) issuses too.")
assert len(sentences) == 2
assert sentences[0].start_position == 0
assert len(sentences[0].tokens) == 4
assert sentences[1].start_position == 23
assert len(sentences[1].tokens) == 9
def test_print_sentence_tokenized():
sentence: Sentence = Sentence("I love Berlin.", use_tokenizer=SegtokTokenizer())
assert sentence.to_tokenized_string() == "I love Berlin ."
def test_print_original_text():
text = ": nation on"
sentence = Sentence(text)
assert text == sentence.to_original_text()
text = ": nation on"
sentence = Sentence(text, use_tokenizer=SegtokTokenizer())
assert text == sentence.to_original_text()
text = "I love Berlin."
sentence = Sentence(text)
assert text == sentence.to_original_text()
text = (
'Schartau sagte dem " Tagesspiegel " vom Freitag , Fischer sei " '
"in einer Weise aufgetreten , die alles andere als überzeugend "
'war " .'
)
sentence = Sentence(text)
assert text == sentence.to_original_text()
text = (
'Schartau sagte dem " Tagesspiegel " vom Freitag , Fischer sei " '
"in einer Weise aufgetreten , die alles andere als überzeugend "
'war " .'
)
sentence = Sentence(text, use_tokenizer=SegtokTokenizer())
assert text == sentence.to_original_text()
def test_print_sentence_plain(tasks_base_path):
sentence: Sentence = Sentence("I love Berlin.", use_tokenizer=SegtokTokenizer())
assert sentence.to_plain_string() == "I love Berlin."
corpus = flair.datasets.NER_GERMAN_GERMEVAL(base_path=tasks_base_path)
sentence = corpus.train[0]
sentence.infer_space_after()
assert (
sentence.to_tokenized_string() == 'Schartau sagte dem " Tagesspiegel " vom Freitag , Fischer sei " in '
"einer Weise aufgetreten , "
'die alles andere als überzeugend war " .'
)
assert (
sentence.to_plain_string() == 'Schartau sagte dem "Tagesspiegel" vom Freitag, Fischer sei "in einer '
"Weise aufgetreten, die "
'alles andere als überzeugend war".'
)
sentence = corpus.train[1]
sentence.infer_space_after()
assert (
sentence.to_tokenized_string() == "Firmengründer Wolf Peter Bree arbeitete Anfang der siebziger Jahre als "
"Möbelvertreter , als er einen fliegenden Händler aus dem Libanon traf ."
)
assert (
sentence.to_plain_string() == "Firmengründer Wolf Peter Bree arbeitete Anfang der siebziger Jahre als "
"Möbelvertreter, als er einen fliegenden Händler aus dem Libanon traf."
)
def test_infer_space_after():
sentence: Sentence = Sentence([Token("xyz"), Token('"'), Token("abc"), Token('"')])
sentence.infer_space_after()
assert sentence.to_tokenized_string() == 'xyz " abc "'
assert sentence.to_plain_string() == 'xyz "abc"'
sentence: Sentence = Sentence('xyz " abc "')
sentence.infer_space_after()
assert sentence.to_tokenized_string() == 'xyz " abc "'
assert sentence.to_plain_string() == 'xyz "abc"'
def test_sentence_get_item():
sentence: Sentence = Sentence("I love Berlin.", use_tokenizer=SegtokTokenizer())
assert sentence.get_token(1) == sentence[0]
assert sentence.get_token(3) == sentence[2]
with pytest.raises(IndexError):
_ = sentence[4]
def test_token_positions_when_creating_with_tokenizer():
sentence = Sentence("I love Berlin .", use_tokenizer=SpaceTokenizer())
assert sentence.tokens[0].start_position == 0
assert sentence.tokens[0].end_position == 1
assert sentence.tokens[1].start_position == 2
assert sentence.tokens[1].end_position == 6
assert sentence.tokens[2].start_position == 7
assert sentence.tokens[2].end_position == 13
sentence = Sentence(" I love Berlin.", use_tokenizer=SegtokTokenizer())
assert sentence.tokens[0].start_position == 1
assert sentence.tokens[0].end_position == 2
assert sentence.tokens[1].start_position == 3
assert sentence.tokens[1].end_position == 7
assert sentence.tokens[2].start_position == 9
assert sentence.tokens[2].end_position == 15
def test_token_positions_when_creating_word_by_word():
sentence: Sentence = Sentence(
[
Token("I"),
Token("love"),
Token("Berlin"),
Token("."),
]
)
assert sentence.tokens[0].start_position == 0
assert sentence.tokens[0].end_position == 1
assert sentence.tokens[1].start_position == 2
assert sentence.tokens[1].end_position == 6
assert sentence.tokens[2].start_position == 7
assert sentence.tokens[2].end_position == 13
def test_line_separator_is_ignored():
with_separator = "Untersuchungs-\u2028ausschüsse"
without_separator = "Untersuchungs-ausschüsse"
assert Sentence(with_separator).to_original_text() == Sentence(without_separator).to_original_text()
def no_op_tokenizer(text: str) -> list[str]:
return [text]