This repository was archived by the owner on May 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 169
Expand file tree
/
Copy pathtokenize_test.go
More file actions
308 lines (274 loc) · 12.8 KB
/
tokenize_test.go
File metadata and controls
308 lines (274 loc) · 12.8 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
package prose
import (
"encoding/json"
"path/filepath"
"reflect"
"testing"
)
var testdata = "testdata"
func checkTokens(t *testing.T, tokens []*Token, expected []string, name string) {
observed := []string{}
for i := range tokens {
observed = append(observed, tokens[i].Text)
}
if !reflect.DeepEqual(observed, expected) {
t.Errorf("%v: unexpected tokens: %#v", name, observed)
}
}
func checkCase(t *testing.T, doc *Document, expected []string, name string) {
tokens := getTokenText(doc)
if !reflect.DeepEqual(tokens, expected) {
t.Errorf("%v: unexpected tokens", name)
}
}
func checkStartEnd(t *testing.T, token *Token, expectedText string, expectedStart, expectedEnd int) {
if token.Text != expectedText {
t.Errorf("got %v, want %v", token.Text, expectedText)
}
if token.Start != expectedStart {
t.Errorf("got %v, want %v", token.Start, expectedStart)
}
if token.End != expectedEnd {
t.Errorf("got %v, want %v", token.End, expectedEnd)
}
}
func makeDoc(text string) (*Document, error) {
return NewDocument(
text,
WithSegmentation(false),
WithTagging(false),
WithExtraction(false))
}
func getTokenText(doc *Document) []string {
observed := []string{}
tokens := doc.Tokens()
for i := range tokens {
observed = append(observed, tokens[i].Text)
}
return observed
}
func getWordData(file string) ([]string, [][]string) {
in := readDataFile(filepath.Join(testdata, "treebank_sents.json"))
out := readDataFile(filepath.Join(testdata, file))
input := []string{}
output := [][]string{}
checkError(json.Unmarshal(in, &input))
checkError(json.Unmarshal(out, &output))
return input, output
}
func getWordBenchData() []string {
in := readDataFile(filepath.Join(testdata, "treebank_sents.json"))
input := []string{}
checkError(json.Unmarshal(in, &input))
return input
}
func TestTokenizationEmpty(t *testing.T) {
doc, _ := makeDoc("")
l := len(getTokenText(doc))
if l != 0 {
t.Errorf("TokenizationEmpty() expected = 0, got = %v", l)
}
}
func TestTokenizationSimple(t *testing.T) {
doc, _ := makeDoc("Vale is a natural language linter that supports plain text, markup (Markdown, reStructuredText, AsciiDoc, and HTML), and source code comments. Vale doesn't attempt to offer a one-size-fits-all collection of rules—instead, it strives to make customization as easy as possible.")
expected := []string{
"Vale", "is", "a", "natural", "language", "linter", "that", "supports",
"plain", "text", ",", "markup", "(", "Markdown", ",", "reStructuredText",
",", "AsciiDoc", ",", "and", "HTML", ")", ",", "and", "source",
"code", "comments", ".", "Vale", "does", "n't", "attempt", "to",
"offer", "a", "one-size-fits-all", "collection", "of", "rules—instead",
",", "it", "strives", "to", "make", "customization", "as", "easy", "as",
"possible", "."}
checkCase(t, doc, expected, "TokenizationSimple()")
}
func TestTokenizationTreebank(t *testing.T) {
input, output := getWordData("treebank_words.json")
for i, s := range input {
doc, _ := makeDoc(s)
tokens := getTokenText(doc)
if !reflect.DeepEqual(tokens, output[i]) {
t.Errorf("TokenizationTreebank(): unexpected tokens")
}
}
}
func TestTokenizationWeb(t *testing.T) {
web := `Independent of current body composition, IGF-I levels at 5 yr were significantly
associated with rate of weight gain between 0-2 yr (beta=0.19; P<0.0005);
and children who showed postnatal catch-up growth (i.e. those who showed gains in
weight or length between 0-2 yr by >0.67 SD score) had higher IGF-I levels than other
children (P=0.02; http://univ.edu.es/study.html) [20-22].`
expected := []string{"Independent", "of", "current", "body", "composition", ",", "IGF-I",
"levels", "at", "5", "yr", "were", "significantly", "associated", "with", "rate", "of",
"weight", "gain", "between", "0-2", "yr", "(", "beta=0.19", ";", "P<0.0005", ")", ";",
"and", "children", "who", "showed", "postnatal", "catch-up", "growth", "(", "i.e.", "those",
"who", "showed", "gains", "in", "weight", "or", "length", "between", "0-2", "yr", "by",
">0.67", "SD", "score", ")", "had", "higher", "IGF-I", "levels", "than", "other", "children",
"(", "P=0.02", ";", "http://univ.edu.es/study.html", ")", "[", "20-22", "]", "."}
doc, _ := makeDoc(web)
checkCase(t, doc, expected, "TokenizationWeb()")
}
func TestTokenizationWebParagraph(t *testing.T) {
web := `Independent of current body composition, IGF-I levels at 5 yr were significantly
associated with rate of weight gain between 0-2 yr (beta=0.19; P<0.0005);
and children who showed postnatal catch-up growth (i.e. those who showed gains in
weight or length between 0-2 yr by >0.67 SD score) had higher IGF-I levels than other
children (P=0.02; http://univ.edu.es/study.html) [20-22].
Independent of current body composition, IGF-I levels at 5 yr were significantly
associated with rate of weight gain between 0-2 yr (beta=0.19; P<0.0005);
and children who showed postnatal catch-up growth (i.e. those who showed gains in
weight or length between 0-2 yr by >0.67 SD score) had higher IGF-I levels than other
children (P=0.02; http://univ.edu.es/study.html) [20-22].
Independent of current body composition, IGF-I levels at 5 yr were significantly
associated with rate of weight gain between 0-2 yr (beta=0.19; P<0.0005);
and children who showed postnatal catch-up growth (i.e. those who showed gains in
weight or length between 0-2 yr by >0.67 SD score) had higher IGF-I levels than other
children (P=0.02; http://univ.edu.es/study.html) [20-22].`
expected := []string{"Independent", "of", "current", "body", "composition", ",", "IGF-I",
"levels", "at", "5", "yr", "were", "significantly", "associated", "with", "rate", "of",
"weight", "gain", "between", "0-2", "yr", "(", "beta=0.19", ";", "P<0.0005", ")", ";",
"and", "children", "who", "showed", "postnatal", "catch-up", "growth", "(", "i.e.", "those",
"who", "showed", "gains", "in", "weight", "or", "length", "between", "0-2", "yr", "by",
">0.67", "SD", "score", ")", "had", "higher", "IGF-I", "levels", "than", "other", "children",
"(", "P=0.02", ";", "http://univ.edu.es/study.html", ")", "[", "20-22", "]", ".", "Independent", "of", "current", "body", "composition", ",", "IGF-I",
"levels", "at", "5", "yr", "were", "significantly", "associated", "with", "rate", "of",
"weight", "gain", "between", "0-2", "yr", "(", "beta=0.19", ";", "P<0.0005", ")", ";",
"and", "children", "who", "showed", "postnatal", "catch-up", "growth", "(", "i.e.", "those",
"who", "showed", "gains", "in", "weight", "or", "length", "between", "0-2", "yr", "by",
">0.67", "SD", "score", ")", "had", "higher", "IGF-I", "levels", "than", "other", "children",
"(", "P=0.02", ";", "http://univ.edu.es/study.html", ")", "[", "20-22", "]", ".", "Independent", "of", "current", "body", "composition", ",", "IGF-I",
"levels", "at", "5", "yr", "were", "significantly", "associated", "with", "rate", "of",
"weight", "gain", "between", "0-2", "yr", "(", "beta=0.19", ";", "P<0.0005", ")", ";",
"and", "children", "who", "showed", "postnatal", "catch-up", "growth", "(", "i.e.", "those",
"who", "showed", "gains", "in", "weight", "or", "length", "between", "0-2", "yr", "by",
">0.67", "SD", "score", ")", "had", "higher", "IGF-I", "levels", "than", "other", "children",
"(", "P=0.02", ";", "http://univ.edu.es/study.html", ")", "[", "20-22", "]", "."}
doc, _ := makeDoc(web)
checkCase(t, doc, expected, "TokenizationWebParagraph()")
}
func TestTokenizationTwitter(t *testing.T) {
text := "@twitter, what time does it start :-)"
doc, _ := makeDoc(text)
expected := []string{"@twitter", ",", "what", "time", "does", "it", "start", ":-)"}
checkCase(t, doc, expected, "TokenizationWebParagraph(1)")
checkStartEnd(t, doc.tokens[0], "@twitter", 0, 8)
checkStartEnd(t, doc.tokens[1], ",", 8, 9)
checkStartEnd(t, doc.tokens[2], "what", 10, 14)
checkStartEnd(t, doc.tokens[3], "time", 15, 19)
checkStartEnd(t, doc.tokens[4], "does", 20, 24)
checkStartEnd(t, doc.tokens[5], "it", 25, 27)
checkStartEnd(t, doc.tokens[6], "start", 28, 33)
checkStartEnd(t, doc.tokens[7], ":-)", 34, len(text))
text = "Mr. James plays basketball in the N.B.A., do you?"
doc, _ = makeDoc(text)
expected = []string{
"Mr.", "James", "plays", "basketball", "in", "the", "N.B.A.", ",",
"do", "you", "?"}
checkCase(t, doc, expected, "TokenizationWebParagraph(2)")
checkStartEnd(t, doc.tokens[0], "Mr.", 0, 3)
checkStartEnd(t, doc.tokens[1], "James", 4, 9)
checkStartEnd(t, doc.tokens[2], "plays", 10, 15)
checkStartEnd(t, doc.tokens[3], "basketball", 16, 26)
checkStartEnd(t, doc.tokens[4], "in", 27, 29)
checkStartEnd(t, doc.tokens[5], "the", 30, 33)
checkStartEnd(t, doc.tokens[6], "N.B.A.", 34, 40)
checkStartEnd(t, doc.tokens[7], ",", 40, 41)
checkStartEnd(t, doc.tokens[8], "do", 42, 44)
checkStartEnd(t, doc.tokens[9], "you", 45, 48)
checkStartEnd(t, doc.tokens[10], "?", 48, len(text))
text = "ˌˌ kill the last letter"
doc, _ = makeDoc(text)
expected = []string{"ˌˌ", "kill", "the", "last", "letter"}
checkCase(t, doc, expected, "TokenizationWebParagraph(3)")
checkStartEnd(t, doc.tokens[0], "ˌˌ", 0, 4)
checkStartEnd(t, doc.tokens[1], "kill", 5, 9)
checkStartEnd(t, doc.tokens[2], "the", 10, 13)
checkStartEnd(t, doc.tokens[3], "last", 14, 18)
checkStartEnd(t, doc.tokens[4], "letter", 19, len(text))
text = "ˌˌˌ kill the last letter"
doc, _ = makeDoc(text)
expected = []string{"ˌˌˌ", "kill", "the", "last", "letter"}
checkCase(t, doc, expected, "TokenizationWebParagraph(4)")
checkStartEnd(t, doc.tokens[0], "ˌˌˌ", 0, 6)
checkStartEnd(t, doc.tokens[1], "kill", 7, 11)
checkStartEnd(t, doc.tokens[2], "the", 12, 15)
checkStartEnd(t, doc.tokens[3], "last", 16, 20)
checkStartEnd(t, doc.tokens[4], "letter", 21, len(text))
text = "March. July. March. June. January."
doc, _ = makeDoc(text)
expected = []string{
"March", ".", "July", ".", "March", ".", "June", ".", "January", "."}
checkCase(t, doc, expected, "TokenizationWebParagraph(5)")
checkStartEnd(t, doc.tokens[0], "March", 0, 5)
checkStartEnd(t, doc.tokens[1], ".", 5, 6)
checkStartEnd(t, doc.tokens[2], "July", 7, 11)
checkStartEnd(t, doc.tokens[3], ".", 11, 12)
checkStartEnd(t, doc.tokens[4], "March", 13, 18)
checkStartEnd(t, doc.tokens[5], ".", 18, 19)
checkStartEnd(t, doc.tokens[6], "June", 20, 24)
checkStartEnd(t, doc.tokens[7], ".", 24, 25)
checkStartEnd(t, doc.tokens[8], "January", 27, 34)
checkStartEnd(t, doc.tokens[9], ".", 34, len(text))
}
func TestTokenizationContractions(t *testing.T) {
tokenizer := NewIterTokenizer()
tokens := tokenizer.Tokenize("He's happy")
expected := []string{"He", "'s", "happy"}
checkTokens(t, tokens, expected, "TokenizationContraction(default-found)")
checkStartEnd(t, tokens[0], "He", 0, 2)
checkStartEnd(t, tokens[1], "'s", 2, 4)
checkStartEnd(t, tokens[2], "happy", 5, 10)
tokens = tokenizer.Tokenize("I've been better")
expected = []string{"I've", "been", "better"}
checkTokens(t, tokens, expected, "TokenizationContraction(default-missing)")
checkStartEnd(t, tokens[0], "I've", 0, 4)
checkStartEnd(t, tokens[1], "been", 5, 9)
checkStartEnd(t, tokens[2], "better", 10, 16)
tokenizer = NewIterTokenizer(UsingContractions([]string{"'ve"}))
tokens = tokenizer.Tokenize("I've been better")
expected = []string{"I", "'ve", "been", "better"}
checkTokens(t, tokens, expected, "TokenizationContraction(custom-found)")
checkStartEnd(t, tokens[0], "I", 0, 1)
checkStartEnd(t, tokens[1], "'ve", 1, 4)
checkStartEnd(t, tokens[2], "been", 5, 9)
checkStartEnd(t, tokens[3], "better", 10, 16)
tokens = tokenizer.Tokenize("He's happy")
expected = []string{"He's", "happy"}
checkTokens(t, tokens, expected, "TokenizationContraction(custom-missing)")
checkStartEnd(t, tokens[0], "He's", 0, 4)
checkStartEnd(t, tokens[1], "happy", 5, 10)
}
func TestTokenizationSuffixes(t *testing.T) {
tokenizer := NewIterTokenizer()
tokens := tokenizer.Tokenize("(Well,\nthat\twasn't good).")
expected := []string{"(", "Well", ",", "that", "was", "n't", "good", ")", "."}
checkTokens(t, tokens, expected, "TestTokenizationSuffixes")
checkStartEnd(t, tokens[0], "(", 0, 1)
checkStartEnd(t, tokens[1], "Well", 1, 5)
checkStartEnd(t, tokens[2], ",", 5, 6)
checkStartEnd(t, tokens[3], "that", 7, 11)
checkStartEnd(t, tokens[4], "was", 12, 15)
checkStartEnd(t, tokens[5], "n't", 15, 18)
checkStartEnd(t, tokens[6], "good", 19, 23)
checkStartEnd(t, tokens[7], ")", 23, 24)
checkStartEnd(t, tokens[8], ".", 24, 25)
}
func BenchmarkTokenization(b *testing.B) {
in := readDataFile(filepath.Join(testdata, "sherlock.txt"))
text := string(in)
for n := 0; n < b.N; n++ {
_, err := makeDoc(text)
if err != nil {
panic(err)
}
}
}
func BenchmarkTokenizationSimple(b *testing.B) {
for n := 0; n < b.N; n++ {
for _, s := range getWordBenchData() {
_, err := makeDoc(s)
if err != nil {
panic(err)
}
}
}
}