-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathtest.py
More file actions
407 lines (274 loc) · 15.5 KB
/
test.py
File metadata and controls
407 lines (274 loc) · 15.5 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
# noqa: E501
from wtpsplit import WtP, SaT
import numpy as np
def test_weighting():
sat = SaT("sat-3l-sm", ort_providers=["CPUExecutionProvider"])
text = "This is a test sentence This is another test sentence."
splits_default = sat.split(text, threshold=0.25)
splits_uniform = sat.split(text, threshold=0.25, weighting="uniform")
splits_hat = sat.split(text, threshold=0.25, weighting="hat")
expected_splits = ["This is a test sentence ", "This is another test sentence."]
assert splits_default == splits_uniform == splits_hat == expected_splits
assert "".join(splits_default) == text
def test_split_ort():
sat = SaT("sat-3l-sm", ort_providers=["CPUExecutionProvider"])
splits = sat.split("This is a test sentence This is another test sentence.", threshold=0.25)
assert splits == ["This is a test sentence ", "This is another test sentence."]
def test_split_torch():
sat = SaT("segment-any-text/sat-3l", hub_prefix=None)
splits = sat.split("This is a test sentence This is another test sentence.", threshold=0.025)
assert splits == ["This is a test sentence ", "This is another test sentence."]
def test_split_torch_sm():
sat = SaT("segment-any-text/sat-12l-sm", hub_prefix=None)
splits = sat.split("This is a test sentence. This is another test sentence.", threshold=0.25)
assert splits == ["This is a test sentence. ", "This is another test sentence."]
def test_move_device():
sat = SaT("segment-any-text/sat-3l", hub_prefix=None)
sat.half().to("cpu")
def test_strip_whitespace():
sat = SaT("segment-any-text/sat-3l", hub_prefix=None)
splits = sat.split(
"This is a test sentence This is another test sentence. ", strip_whitespace=True, threshold=0.025
)
assert splits == ["This is a test sentence", "This is another test sentence."]
def test_strip_newline_behaviour():
sat = SaT("segment-any-text/sat-3l", hub_prefix=None)
splits = sat.split(
"Yes\nthis is a test sentence. This is another test sentence.",
)
assert splits == ["Yes", "this is a test sentence. ", "This is another test sentence."]
def test_strip_newline_behaviour_as_spaces():
sat = SaT("segment-any-text/sat-3l", hub_prefix=None)
splits = sat.split("Yes\nthis is a test sentence. This is another test sentence.", treat_newline_as_space=True)
assert splits == ["Yes\nthis is a test sentence. ", "This is another test sentence."]
def test_split_noisy():
sat = SaT("segment-any-text/sat-12l-sm", hub_prefix=None)
splits = sat.split("this is a sentence :) this is another sentence lol")
assert splits == ["this is a sentence :) ", "this is another sentence lol"]
def test_split_batched():
sat = SaT("segment-any-text/sat-3l", hub_prefix=None)
splits = list(sat.split(["Paragraph-A Paragraph-B", "Paragraph-C100 Paragraph-D"]))
assert splits == [
["Paragraph-A ", "Paragraph-B"],
["Paragraph-C100 ", "Paragraph-D"],
]
def test_split_lora():
ud = SaT("segment-any-text/sat-3l", hub_prefix=None, style_or_domain="ud", language="en")
opus = SaT("segment-any-text/sat-3l", hub_prefix=None, style_or_domain="opus100", language="en")
ersatz = SaT("segment-any-text/sat-3l", hub_prefix=None, style_or_domain="ersatz", language="en")
text = "’I couldn’t help it,’ said Five, in a sulky tone; ’Seven jogged my elbow.’ | On which Seven looked up and said, ’That’s right, Five! Always lay the blame (...)!’"
splits_ud = ud.split(text)
splits_opus100 = opus.split(text)
splits_ersatz = ersatz.split(text)
assert splits_ud != splits_opus100 != splits_ersatz
def test_split_paragraphs():
sat = SaT("segment-any-text/sat-3l", hub_prefix=None)
text = " ".join(
"""
Text segmentation is the process of dividing written text into meaningful units, such as words, sentences, or topics. The term applies both to mental processes used by humans when reading text, and to artificial processes implemented in computers, which are the subject of natural language processing. The problem is non-trivial, because while some written languages have explicit word boundary markers, such as the word spaces of written English and the distinctive initial, medial and final letter shapes of Arabic, such signals are sometimes ambiguous and not present in all written languages.
Daniel Wroughton Craig CMG (born 2 March 1968) is an English actor who gained international fame by playing the fictional secret agent James Bond for five installments in the film series, from Casino Royale (2006) up to No Time to Die (2021).
""".strip().split()
)
splits = sat.split(text, do_paragraph_segmentation=True)
paragraph1 = "".join(splits[0])
paragraph2 = "".join(splits[1])
assert paragraph1.startswith("Text segmentation is")
assert paragraph2.startswith("Daniel Wroughton Craig CMG (born 2 March 1968) is")
def test_split_empty_strings():
sat = SaT("segment-any-text/sat-3l", hub_prefix=None)
text = " "
splits = sat.split(text)
assert splits == [" "]
text = " \n"
splits = sat.split(text)
assert splits == [" ", ""]
text = ""
splits = sat.split(text)
assert splits == []
def test_split_ort_wtp():
wtp = WtP("wtp-bert-mini", ort_providers=["CPUExecutionProvider"])
splits = wtp.split("This is a test sentence This is another test sentence.", threshold=0.005)
assert splits == ["This is a test sentence ", "This is another test sentence."]
def test_split_torch_wtp():
wtp = WtP("benjamin/wtp-bert-mini", hub_prefix=None)
splits = wtp.split("This is a test sentence This is another test sentence.", threshold=0.005)
assert splits == ["This is a test sentence ", "This is another test sentence."]
def test_split_torch_canine_wtp():
wtp = WtP("benjamin/wtp-canine-s-1l", hub_prefix=None)
splits = wtp.split("This is a test sentence. This is another test sentence.", lang_code="en")
assert splits == ["This is a test sentence. ", "This is another test sentence."]
def test_move_device_wtp():
wtp = WtP("benjamin/wtp-bert-mini", hub_prefix=None)
wtp.half().to("cpu")
def test_strip_whitespace_wtp():
wtp = WtP("benjamin/wtp-bert-mini", hub_prefix=None)
splits = wtp.split(
"This is a test sentence This is another test sentence. ", strip_whitespace=True, threshold=0.005
)
assert splits == ["This is a test sentence", "This is another test sentence."]
def test_split_long_wtp():
prefix = "x" * 2000
wtp = WtP("benjamin/wtp-bert-mini", hub_prefix=None)
splits = wtp.split(prefix + " This is a test sentence. This is another test sentence.")
assert splits == [prefix + " " + "This is a test sentence. ", "This is another test sentence."]
def test_split_batched_wtp():
wtp = WtP("benjamin/wtp-bert-mini", hub_prefix=None)
splits = list(wtp.split(["Paragraph-A Paragraph-B", "Paragraph-C100 Paragraph-D"]))
assert splits == [
["Paragraph-A ", "Paragraph-B"],
["Paragraph-C100 ", "Paragraph-D"],
]
def test_split_style_wtp():
wtp = WtP("benjamin/wtp-bert-mini", hub_prefix=None)
text = "’I couldn’t help it,’ said Five, in a sulky tone; ’Seven jogged my elbow.’ | On which Seven looked up and said, ’That’s right, Five! Always lay the blame (...)!’"
splits_ud = wtp.split(text, lang_code="en", style="ud")
splits_opus100 = wtp.split(text, lang_code="en", style="opus100")
splits_ersatz = wtp.split(text, lang_code="en", style="ersatz")
assert splits_ud != splits_opus100 != splits_ersatz
def test_split_paragraphs_wtp():
wtp = WtP("benjamin/wtp-bert-mini", hub_prefix=None)
text = " ".join(
"""
Text segmentation is the process of dividing written text into meaningful units, such as words, sentences, or topics. The term applies both to mental processes used by humans when reading text, and to artificial processes implemented in computers, which are the subject of natural language processing. The problem is non-trivial, because while some written languages have explicit word boundary markers, such as the word spaces of written English and the distinctive initial, medial and final letter shapes of Arabic, such signals are sometimes ambiguous and not present in all written languages.
Daniel Wroughton Craig CMG (born 2 March 1968) is an English actor who gained international fame by playing the fictional secret agent James Bond for five installments in the film series, from Casino Royale (2006) up to No Time to Die (2021).
""".strip().split()
)
splits = wtp.split(text, do_paragraph_segmentation=True)
paragraph1 = "".join(splits[0])
paragraph2 = "".join(splits[1])
assert paragraph1.startswith("Text segmentation is")
assert paragraph2.startswith("Daniel Wroughton Craig CMG (born 2 March 1968) is")
def test_split_paragraphs_with_language_adapters_wtp():
wtp = WtP("benjamin/wtp-canine-s-3l", hub_prefix=None)
text = " ".join(
"""
Text segmentation is the process of dividing written text into meaningful units, such as words, sentences, or topics. The term applies both to mental processes used by humans when reading text, and to artificial processes implemented in computers, which are the subject of natural language processing. The problem is non-trivial, because while some written languages have explicit word boundary markers, such as the word spaces of written English and the distinctive initial, medial and final letter shapes of Arabic, such signals are sometimes ambiguous and not present in all written languages.
Daniel Wroughton Craig CMG (born 2 March 1968) is an English actor who gained international fame by playing the fictional secret agent James Bond for five installments in the film series, from Casino Royale (2006) up to No Time to Die (2021).
""".strip().split()
)
splits = wtp.split(text, do_paragraph_segmentation=True, lang_code="en")
paragraph1 = "".join(splits[0])
paragraph2 = "".join(splits[1])
assert paragraph1.startswith("Text segmentation is")
assert paragraph2.startswith("Daniel Wroughton Craig CMG (born 2 March 1968) is")
def test_split_threshold_wtp():
wtp = WtP("benjamin/wtp-bert-mini", hub_prefix=None)
punct_threshold = wtp.get_threshold("en", "ud", return_punctuation_threshold=True)
threshold = wtp.get_threshold("en", "ud")
assert punct_threshold != threshold
# test threshold is being used
splits = wtp.split("This is a test sentence. This is another test sentence.", threshold=1.0)
assert splits == ["This is a test sentence. This is another test sentence."]
splits = wtp.split(
"This is a test sentence. This is another test sentence.", style="ud", lang_code="en", threshold=1.0
)
assert splits == ["This is a test sentence. This is another test sentence."]
splits = wtp.split("This is a test sentence. This is another test sentence.", threshold=-1e-3)
# space might still be included in a character split
assert splits[:3] == list("Thi")
# ============================================================================
# Length-Constrained Segmentation Tests
# ============================================================================
def test_min_length_constraint_wtp():
"""Test minimum length constraint with WtP"""
wtp = WtP("wtp-bert-mini", ort_providers=["CPUExecutionProvider"])
text = "Short. Test. Hello. World. This is longer."
splits = wtp.split(text, min_length=15, threshold=0.005)
# All segments should be >= 15 characters
for segment in splits:
assert len(segment) >= 15, f"Segment '{segment}' is shorter than min_length"
# Text should be preserved
assert "".join(splits) == text
def test_max_length_constraint_sat():
"""Test maximum length constraint with SaT"""
sat = SaT("sat-3l-sm", ort_providers=["CPUExecutionProvider"])
text = "This is a test sentence. " * 10
splits = sat.split(text, max_length=60, threshold=0.025)
# All segments should be <= 60 characters
for segment in splits:
assert len(segment) <= 60, f"Segment '{segment}' is longer than max_length"
# Text should be preserved
assert "".join(splits) == text
def test_min_max_constraints_together():
"""Test both constraints simultaneously"""
wtp = WtP("wtp-bert-mini", ort_providers=["CPUExecutionProvider"])
text = "Hello world. " * 15
splits = wtp.split(text, min_length=25, max_length=65, threshold=0.005)
# All segments should satisfy both constraints
for segment in splits:
assert 25 <= len(segment) <= 65, f"Segment '{segment}' violates constraints"
# Text should be preserved
assert "".join(splits) == text
def test_gaussian_prior():
"""Test Gaussian prior preference"""
sat = SaT("sat-3l-sm", ort_providers=["CPUExecutionProvider"])
text = "Sentence. " * 30
splits = sat.split(
text,
min_length=20,
max_length=60,
prior_type="gaussian",
prior_kwargs={"target_length": 40.0, "spread": 5.0},
threshold=0.025,
)
# Should produce valid splits
for segment in splits:
assert 20 <= len(segment) <= 60
# Text should be preserved
assert "".join(splits) == text
def test_greedy_algorithm():
"""Test greedy algorithm"""
wtp = WtP("wtp-bert-mini", ort_providers=["CPUExecutionProvider"])
text = "Test sentence. " * 10
splits = wtp.split(text, min_length=20, max_length=50, algorithm="greedy", threshold=0.005)
# Should produce valid splits
for segment in splits:
assert 20 <= len(segment) <= 50
# Text should be preserved
assert "".join(splits) == text
def test_constraints_with_paragraph_segmentation():
"""Test constraints with nested paragraph segmentation"""
wtp = WtP("wtp-bert-mini", ort_providers=["CPUExecutionProvider"])
text = " ".join(
[
"First paragraph first sentence. First paragraph second sentence.",
"Second paragraph first sentence. Second paragraph second sentence.",
]
)
paragraphs = wtp.split(text, do_paragraph_segmentation=True, min_length=20, max_length=70)
# Check structure
assert isinstance(paragraphs, list)
for paragraph in paragraphs:
assert isinstance(paragraph, list)
for sentence in paragraph:
assert 20 <= len(sentence) <= 70
def test_constraints_preserved_in_batched():
"""Test constraints work with batched processing"""
sat = SaT("sat-3l-sm", ort_providers=["CPUExecutionProvider"])
texts = [
"First batch text. " * 5,
"Second batch text. " * 5,
]
results = list(sat.split(texts, min_length=25, max_length=55, threshold=0.025))
assert len(results) == 2
for text, splits in zip(texts, results):
for segment in splits:
assert 25 <= len(segment) <= 55
assert "".join(splits) == text
def test_constraint_low_level():
"""Test constrained_segmentation directly"""
from wtpsplit.utils.constraints import constrained_segmentation
from wtpsplit.utils.priors import create_prior_function
probs = np.array([0.1, 0.3, 0.5, 0.7, 0.9, 0.2, 0.4, 0.6, 0.8, 1.0])
prior_fn = create_prior_function("uniform", {"max_length": 5})
indices = constrained_segmentation(probs, prior_fn, min_length=3, max_length=5, algorithm="viterbi")
# Verify constraints on chunk lengths
prev = 0
for idx in indices:
chunk_len = idx - prev
assert 3 <= chunk_len <= 5, f"Chunk length {chunk_len} violates constraints"
prev = idx
# Check last chunk
if prev < len(probs):
last_len = len(probs) - prev
assert 3 <= last_len <= 5, f"Last chunk length {last_len} violates constraints"