This repository has been archived by the owner on May 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 164
/
segment_test.go
505 lines (430 loc) · 15.2 KB
/
segment_test.go
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
package prose
import (
"encoding/json"
"io/ioutil"
"path/filepath"
"testing"
)
type goldenRule struct {
Name string
Input string
Output []string
}
func readDataFile(path string) []byte {
p, err := filepath.Abs(path)
checkError(err)
data, ferr := ioutil.ReadFile(p)
checkError(ferr)
return data
}
func makeSegmenter(text string) (*Document, error) {
return NewDocument(
text,
UsingTokenizer(nil),
WithTagging(false),
WithExtraction(false))
}
func BenchmarkPunkt(b *testing.B) {
tests := make([]goldenRule, 0)
cases := readDataFile(filepath.Join(testdata, "golden_rules_en.json"))
checkError(json.Unmarshal(cases, &tests))
for n := 0; n < b.N; n++ {
for i := range tests {
_, err := makeSegmenter(tests[i].Input)
if err != nil {
panic(err)
}
}
}
}
func TestEnglishSmartQuotes(t *testing.T) {
actualText := "Here is a quote, ”a smart one.” Will this break properly?"
doc, _ := makeSegmenter(actualText)
actual := doc.Sentences()
expected := []string{
"Here is a quote, ”a smart one.”",
"Will this break properly?",
}
if len(actual) != len(expected) {
t.Fatalf("Actual: %d, Expected: %d", len(actual), len(expected))
}
for index, sent := range actual {
if sent.Text != expected[index] {
t.Fatalf("Actual: %s\nExpected: %s", sent, expected[index])
}
}
}
func TestEnglishCustomAbbrev(t *testing.T) {
actualText := "One custom abbreviation is F.B.I. The abbreviation, F.B.I. should properly break."
doc, _ := makeSegmenter(actualText)
actual := doc.Sentences()
expected := []string{
"One custom abbreviation is F.B.I.",
"The abbreviation, F.B.I. should properly break.",
}
if len(actual) != len(expected) {
t.Fatalf("Actual: %d, Expected: %d", len(actual), len(expected))
}
for index, sent := range actual {
if sent.Text != expected[index] {
t.Fatalf("Actual: %s\nExpected: %s", sent, expected[index])
}
}
actualText = "An abbreviation near the end of a G.D. sentence. J.G. Wentworth was cool."
doc, _ = makeSegmenter(actualText)
actual = doc.Sentences()
expected = []string{
"An abbreviation near the end of a G.D. sentence.",
"J.G. Wentworth was cool.",
}
if len(actual) != len(expected) {
t.Fatalf("Actual: %d, Expected: %d", len(actual), len(expected))
}
for index, sent := range actual {
if sent.Text != expected[index] {
t.Fatalf("Actual: %s\nExpected: %s", sent, expected[index])
}
}
}
func TestEnglishSupervisedAbbrev(t *testing.T) {
actualText := "I am a Sgt. in the army. I am a No. 1 student. The Gov. of Michigan is a dick."
doc, _ := makeSegmenter(actualText)
actual := doc.Sentences()
expected := []string{
"I am a Sgt. in the army.",
"I am a No. 1 student.",
"The Gov. of Michigan is a dick.",
}
if len(actual) != len(expected) {
t.Fatalf("Actual: %d, Expected: %d", len(actual), len(expected))
}
for index, sent := range actual {
if sent.Text != expected[index] {
t.Fatalf("Actual: %s\nExpected: %s", sent, expected[index])
}
}
}
func TestEnglishSemicolon(t *testing.T) {
actualText := "I am here; you are over there. Will the tokenizer output two complete sentences?"
doc, _ := makeSegmenter(actualText)
actual := doc.Sentences()
expected := []string{
"I am here; you are over there.",
"Will the tokenizer output two complete sentences?",
}
if len(actual) != len(expected) {
t.Fatalf("Actual: %d, Expected: %d", len(actual), len(expected))
}
for index, sent := range actual {
if sent.Text != expected[index] {
t.Fatalf("Actual: %s\nExpected: %s", sent, expected[index])
}
}
}
func compareSentences(t *testing.T, actualText string, expected []string, test string) bool {
doc, _ := makeSegmenter(actualText)
actual := doc.Sentences()
if len(actual) != len(expected) {
t.Log(test)
t.Logf("Actual: %v\n", actual)
t.Errorf("Actual: %d, Expected: %d\n", len(actual), len(expected))
t.Log("===")
return false
}
for index, sent := range actual {
if sent.Text != expected[index] {
t.Log(test)
t.Errorf("Actual: [%s] Expected: [%s]\n", sent, expected[index])
t.Log("===")
return false
}
}
return true
}
// Based on https://github.com/diasks2/pragmatic_segmenter#the-golden-rules.
func TestGoldenRules(t *testing.T) {
var actualText string
var expected []string
test := "1. Simple period to end sentence"
actualText = "Hello World. My name is Jonas."
expected = []string{
"Hello World.",
"My name is Jonas.",
}
compareSentences(t, actualText, expected, test)
test = "2. Question mark to end sentence"
actualText = "What is your name? My name is Jonas."
expected = []string{
"What is your name?",
"My name is Jonas.",
}
compareSentences(t, actualText, expected, test)
test = "3. Exclamation point to end sentence"
actualText = "There it is! I found it."
expected = []string{
"There it is!",
"I found it.",
}
compareSentences(t, actualText, expected, test)
test = "4. One letter upper case abbreviations"
actualText = "My name is Jonas E. Smith."
expected = []string{
"My name is Jonas E. Smith.",
}
compareSentences(t, actualText, expected, test)
test = "5. One letter lower case abbreviations"
actualText = "Please turn to p. 55."
expected = []string{
"Please turn to p. 55.",
}
compareSentences(t, actualText, expected, test)
test = "6. Two letter lower case abbreviations in the middle of a sentence"
actualText = "Were Jane and co. at the party?"
expected = []string{
"Were Jane and co. at the party?",
}
compareSentences(t, actualText, expected, test)
test = "7. Two letter upper case abbreviations in the middle of a sentence"
actualText = "They closed the deal with Pitt, Briggs & Co. at noon."
expected = []string{
"They closed the deal with Pitt, Briggs & Co. at noon.",
}
compareSentences(t, actualText, expected, test)
test = "8. Two letter lower case abbreviations at the end of a sentence"
actualText = "Let's ask Jane and co. They should know."
expected = []string{
"Let's ask Jane and co.",
"They should know.",
}
compareSentences(t, actualText, expected, test)
test = "9. Two letter upper case abbreviations at the end of a sentence"
actualText = "They closed the deal with Pitt, Briggs & Co. It closed yesterday."
expected = []string{
"They closed the deal with Pitt, Briggs & Co.",
"It closed yesterday.",
}
compareSentences(t, actualText, expected, test)
test = "10. Two letter (prepositive) abbreviations"
actualText = "I can see Mt. Fuji from here."
expected = []string{
"I can see Mt. Fuji from here.",
}
compareSentences(t, actualText, expected, test)
test = "11. Two letter (prepositive & postpositive) abbreviations"
actualText = "St. Michael's Church is on 5th st. near the light."
expected = []string{
"St. Michael's Church is on 5th st. near the light.",
}
compareSentences(t, actualText, expected, test)
test = "12. Possesive two letter abbreviations"
actualText = "That is JFK Jr.'s book."
expected = []string{
"That is JFK Jr.'s book.",
}
compareSentences(t, actualText, expected, test)
test = "13. Multi-period abbreviations in the middle of a sentence"
actualText = "I visited the U.S.A. last year."
expected = []string{
"I visited the U.S.A. last year.",
}
compareSentences(t, actualText, expected, test)
/* FIXME
test = "14. Multi-period abbreviations at the end of a sentence"
actualText = "I live in the E.U. How about you?"
expected = []string{
"I live in the E.U.",
"How about you?",
}
compareSentences(t, actualText, expected, test)*/
/* FIXME
test = "15. U.S. as sentence boundary"
actualText = "I live in the U.S. How about you?"
expected = []string{
"I live in the U.S.",
"How about you?",
}
compareSentences(t, actualText, expected, test) */
test = "16. U.S. as non sentence boundary with next word capitalized"
actualText = "I work for the U.S. Government in Virginia."
expected = []string{
"I work for the U.S. Government in Virginia.",
}
compareSentences(t, actualText, expected, test)
test = "17. U.S. as non sentence boundary"
actualText = "I have lived in the U.S. for 20 years."
expected = []string{
"I have lived in the U.S. for 20 years.",
}
compareSentences(t, actualText, expected, test)
/* FIXME
test = "18. A.M. / P.M. as non sentence boundary and sentence boundary"
actualText = "At 5 a.m. Mr. Smith went to the bank. He left the bank at 6 P.M. Mr. Smith then went to the store."
expected = []string{
"At 5 a.m. Mr. Smith went to the bank.",
"He left the bank at 6 P.M.",
"Mr. Smith then went to the store.",
}
compareSentences(t, actualText, expected, test) */
test = "19. Number as non sentence boundary"
actualText = "She has $100.00 in her bag."
expected = []string{
"She has $100.00 in her bag.",
}
compareSentences(t, actualText, expected, test)
test = "20. Number as sentence boundary"
actualText = "She has $100.00. It is in her bag."
expected = []string{
"She has $100.00.",
"It is in her bag.",
}
compareSentences(t, actualText, expected, test)
test = "21. Parenthetical inside sentence"
actualText = "He teaches science (He previously worked for 5 years as an engineer.) at the local University."
expected = []string{
"He teaches science (He previously worked for 5 years as an engineer.) at the local University.",
}
compareSentences(t, actualText, expected, test)
test = "22. Email addresses"
actualText = "Her email is [email protected]. I sent her an email."
expected = []string{
"Her email is [email protected].",
"I sent her an email.",
}
compareSentences(t, actualText, expected, test)
test = "23. Web addresses"
actualText = "The site is: https://www.example.50.com/new-site/awesome_content.html. Please check it out."
expected = []string{
"The site is: https://www.example.50.com/new-site/awesome_content.html.",
"Please check it out.",
}
compareSentences(t, actualText, expected, test)
test = "24. Single quotations inside sentence"
actualText = "She turned to him, 'This is great.' she said."
expected = []string{
"She turned to him, 'This is great.' she said.",
}
compareSentences(t, actualText, expected, test)
test = "25. Double quotations inside sentence"
actualText = "She turned to him, \"This is great.\" she said."
expected = []string{
"She turned to him, \"This is great.\" she said.",
}
compareSentences(t, actualText, expected, test)
test = "26. Double quotations at the end of a sentence"
actualText = "She turned to him, \"This is great.\" She held the book out to show him."
expected = []string{
"She turned to him, \"This is great.\"",
"She held the book out to show him.",
}
compareSentences(t, actualText, expected, test)
test = "27. Double punctuation (exclamation point)"
actualText = "Hello!! Long time no see."
expected = []string{
"Hello!!",
"Long time no see.",
}
compareSentences(t, actualText, expected, test)
test = "28. Double punctuation (question mark)"
actualText = "Hello?? Who is there?"
expected = []string{
"Hello??",
"Who is there?",
}
compareSentences(t, actualText, expected, test)
test = "29. Double punctuation (exclamation point / question mark)"
actualText = "Hello!? Is that you?"
expected = []string{
"Hello!?",
"Is that you?",
}
compareSentences(t, actualText, expected, test)
test = "30. Double punctuation (question mark / exclamation point)"
actualText = "Hello?! Is that you?"
expected = []string{
"Hello?!",
"Is that you?",
}
compareSentences(t, actualText, expected, test)
test = "40. Errant newlines in the middle of sentences (PDF)"
actualText = "This is a sentence\ncut off in the middle because pdf."
expected = []string{
"This is a sentence\ncut off in the middle because pdf.",
}
compareSentences(t, actualText, expected, test)
test = "41. Errant newlines in the middle of sentences"
actualText = "It was a cold \nnight in the city."
expected = []string{
"It was a cold \nnight in the city.",
}
compareSentences(t, actualText, expected, test)
/* FIXME
test = "43. Geo Coordinates"
actualText = "You can find it at N°. 1026.253.553. That is where the treasure is."
expected = []string{
"You can find it at N°. 1026.253.553.",
"That is where the treasure is.",
}
compareSentences(t, actualText, expected, test) */
test = "44. Named entities with an exclamation point"
actualText = "She works at Yahoo! in the accounting department."
expected = []string{
"She works at Yahoo! in the accounting department.",
}
compareSentences(t, actualText, expected, test)
test = "45. I as a sentence boundary and I as an abbreviation"
actualText = "We make a good team, you and I. Did you see Albert I. Jones yesterday?"
expected = []string{
"We make a good team, you and I.",
"Did you see Albert I. Jones yesterday?",
}
compareSentences(t, actualText, expected, test)
test = "46. Ellipsis at end of quotation"
actualText = "Thoreau argues that by simplifying one’s life, “the laws of the universe will appear less complex. . . .”"
expected = []string{
"Thoreau argues that by simplifying one’s life, “the laws of the universe will appear less complex. . . .”",
}
compareSentences(t, actualText, expected, test)
test = "47. Ellipsis with square brackets"
actualText = "\"Bohr [...] used the analogy of parallel stairways [...]\" (Smith 55)."
expected = []string{
"\"Bohr [...] used the analogy of parallel stairways [...]\" (Smith 55).",
}
compareSentences(t, actualText, expected, test)
test = "48. Ellipsis as sentence boundary (standard ellipsis rules)"
actualText = "If words are left off at the end of a sentence, and that is all that is omitted, indicate the omission with ellipsis marks (preceded and followed by a space) and then indicate the end of the sentence with a period . . . . Next sentence."
expected = []string{
"If words are left off at the end of a sentence, and that is all that is omitted, indicate the omission with ellipsis marks (preceded and followed by a space) and then indicate the end of the sentence with a period . . . .",
"Next sentence.",
}
compareSentences(t, actualText, expected, test)
test = "49. Ellipsis as sentence boundary (non-standard ellipsis rules)"
actualText = "I never meant that.... She left the store."
expected = []string{
"I never meant that....",
"She left the store.",
}
compareSentences(t, actualText, expected, test)
test = "50. Ellipsis as non sentence boundary"
actualText = "I wasn’t really ... well, what I mean...see . . . what I'm saying, the thing is . . . I didn’t mean it."
expected = []string{
"I wasn’t really ... well, what I mean...see . . . what I'm saying, the thing is . . . I didn’t mean it.",
}
compareSentences(t, actualText, expected, test)
test = "51. 4-dot ellipsis"
actualText = "One further habit which was somewhat weakened . . . was that of combining words into self-interpreting compounds. . . . The practice was not abandoned. . . ."
expected = []string{
"One further habit which was somewhat weakened . . . was that of combining words into self-interpreting compounds. . . .",
"The practice was not abandoned. . . .",
}
compareSentences(t, actualText, expected, test)
/* FIXME:
test = "52. No whitespace in between sentences"
actualText = "Hello world.Today is Tuesday.Mr. Smith went to the store and bought 1,000.That is a lot."
expected = []string{
"Hello world.",
"Today is Tuesday.",
"Mr. Smith went to the store and bought 1,000.",
"That is a lot.",
}
compareSentences(t, actualText, expected, test)*/
}