-
Notifications
You must be signed in to change notification settings - Fork 1
/
ngrams_test.go
353 lines (287 loc) · 7.81 KB
/
ngrams_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
package ngrams
import (
"errors"
"io/ioutil"
"os"
"strings"
"testing"
"github.com/stretchr/testify/require"
stores "github.com/mochi-co/ngrams/stores"
tk "github.com/mochi-co/ngrams/tokenizers"
)
type MockTokenizer struct {
tokens []string
formatted string
}
func (m *MockTokenizer) Tokenize(str string) []string {
return m.tokens
}
func (m *MockTokenizer) Scanner(data []byte, atEOF bool) (advance int, token []byte, err error) {
return
}
func (m *MockTokenizer) Format(tokens []string) string {
return m.formatted
}
type MockStore struct {
added []string
errAdd bool
errClose bool
errGet bool
errAny bool
}
func (m *MockStore) Add(key, future string) error {
if m.errAdd {
return errors.New("test")
}
m.added = append(m.added, key+" "+future)
return nil
}
func (m *MockStore) Get(key string) (bool, stores.Variations) {
if m.errGet {
return false, stores.Variations{}
}
return true, stores.Variations{"test": 100}
}
func (m *MockStore) Delete(key string) error {
return nil
}
func (m *MockStore) Any() (string, stores.Variations, error) {
if m.errAny {
return "", stores.Variations{}, errors.New("test")
}
return "", stores.Variations{}, nil
}
func (m *MockStore) Close() error {
if m.errClose {
return errors.New("test")
}
return nil
}
func TestNewIndex(t *testing.T) {
// Force all defaults
i := NewIndex(0, nil)
require.NotNil(t, i)
require.IsType(t, new(stores.MemoryStore), i.Store)
require.IsType(t, new(tk.DefaultWord), i.Tokenizer)
require.Equal(t, i.N, defaultN)
// Custom n value
i = NewIndex(2, nil)
require.Equal(t, i.N, 2)
// Custom Tokenizer and Store
i = NewIndex(0, &Options{
Tokenizer: &MockTokenizer{
formatted: "test",
},
Store: new(MockStore),
})
require.NotNil(t, i)
require.Equal(t, "test", i.Tokenizer.Format([]string{}))
ok, v := i.Store.Get("any")
require.Equal(t, true, ok)
require.NotNil(t, v["test"])
require.Equal(t, int64(100), v["test"])
}
func TestParse(t *testing.T) {
i := NewIndex(3, nil)
tokens, err := i.Parse("to be or not to be that is the question")
require.NoError(t, err)
require.Equal(t, 10, len(tokens))
require.Equal(t, "question", tokens[9])
i = NewIndex(3, &Options{
Store: &MockStore{
errAdd: true,
},
})
tokens, err = i.Parse("to be or not to be that is the question")
require.Error(t, err)
}
func TestRead(t *testing.T) {
i := NewIndex(3, &Options{
Store: new(MockStore),
})
text := "to be or not to be that is the question"
r := strings.NewReader(text)
err := i.Read(r)
require.NoError(t, err)
ex := []string{
"to be or",
"be or not",
"or not to",
"not to be",
"to be that",
"be that is",
"that is the",
"is the question",
}
require.Equal(t, ex, i.Store.(*MockStore).added)
i.Store = new(MockStore)
t2 := "to be, or not to be, is that the question?"
r = strings.NewReader(t2)
err = i.Read(r)
require.NoError(t, err)
ex2 := []string{
"to be ,",
"be , or",
", or not",
"or not to",
"not to be",
"to be ,",
"be , is",
", is that",
"is that the",
"that the question",
"the question ?",
}
require.Equal(t, ex2, i.Store.(*MockStore).added)
}
func TestClose(t *testing.T) {
i := NewIndex(3, nil)
err := i.Close()
require.NoError(t, err)
i = NewIndex(3, &Options{
Store: &MockStore{
errClose: true,
},
})
err = i.Close()
require.Error(t, err)
}
func TestExtractNgram(t *testing.T) {
tokens := []string{"to", "be", "or", "not", "to", "be", ",", "that", "is", "the", "question", "."}
i := NewIndex(3, nil)
key, future := i.extractNgram(0, tokens)
require.Equal(t, "to be", key)
require.Equal(t, "or", future)
key, future = i.extractNgram(9, tokens)
require.Equal(t, "the question", key)
require.Equal(t, ".", future)
i = NewIndex(3, nil)
key, future = i.extractNgram(0, tokens)
require.Equal(t, "to be", key)
require.Equal(t, "or", future)
key, future = i.extractNgram(9, tokens)
require.Equal(t, "the question", key)
require.Equal(t, ".", future)
key, future = i.extractNgram(11, tokens)
require.Equal(t, "", key) // blank key for out of range.
require.Equal(t, "", future) // blank future for out of range.
i = NewIndex(4, nil)
key, future = i.extractNgram(0, tokens)
require.Equal(t, "to be or", key)
require.Equal(t, "not", future)
key, future = i.extractNgram(8, tokens)
require.Equal(t, "is the question", key)
require.Equal(t, ".", future)
i = NewIndex(2, nil)
key, future = i.extractNgram(0, tokens)
require.Equal(t, "to", key)
require.Equal(t, "be", future)
i = NewIndex(1, nil)
key, future = i.extractNgram(0, tokens)
require.Equal(t, "to", key)
require.Equal(t, "to", future) // I don't know why you'd use this for monograms but here we are.
}
func TestExtractAndStore(t *testing.T) {
tokens := []string{"to", "be", ",", "or", "not", "to", "be", ",", "is", "that", "the", "question", "?"}
i := NewIndex(3, &Options{
Store: new(MockStore),
})
for j := 0; j < len(tokens); j++ {
err := i.extractAndStore(j, tokens)
require.NoError(t, err)
}
ex2 := []string{
"to be ,",
"be , or",
", or not",
"or not to",
"not to be",
"to be ,",
"be , is",
", is that",
"is that the",
"that the question",
"the question ?",
}
require.Equal(t, ex2, i.Store.(*MockStore).added)
}
func TestSeek(t *testing.T) {
i := NewIndex(2, nil)
i.Parse("to be or not to be that is the question")
ok, result := i.Seek("to")
require.Equal(t, true, ok)
require.Equal(t, "to", result.Prefix) // this is unusued with bigrams
require.Equal(t, stores.Variations{"be": 2}, result.Next)
i = NewIndex(3, nil)
i.Parse("to be or not to be that is the question")
ok, result = i.Seek("to be")
require.Equal(t, true, ok)
require.Equal(t, "be", result.Prefix)
require.Equal(t, stores.Variations{"that": 1, "or": 1}, result.Next)
ok, result = i.Seek("be that")
require.Equal(t, true, ok)
require.Equal(t, "that", result.Prefix)
require.Equal(t, stores.Variations{"is": 1}, result.Next)
i = NewIndex(4, nil)
i.Parse("to be or not to be that is the question")
ok, result = i.Seek("to be or")
require.Equal(t, true, ok)
require.Equal(t, "or", result.Prefix)
require.Equal(t, stores.Variations{"not": 1}, result.Next)
i = NewIndex(3, &Options{
Store: new(MockStore),
Tokenizer: new(MockTokenizer),
})
ok, result = i.Seek("to be or")
require.Equal(t, true, ok)
require.Nil(t, result)
}
func TestBabble(t *testing.T) {
i := NewIndex(3, nil)
file, err := os.Open("training/pride-prejudice.txt")
if err != nil {
require.NoError(t, err)
}
defer file.Close()
d, err := ioutil.ReadAll(file)
require.NoError(t, err)
i.Parse(string(d))
start, _, _ := i.Store.Any()
b, err := i.Babble(start, 200)
require.NoError(t, err)
require.NotEmpty(t, b)
}
func TestShortBabble(t *testing.T) {
i := NewIndex(3, nil)
i.Parse("to be or not to be, that is the question.")
i.Parse("be or not to be something, what is the question?")
i.Parse("what can we be, or not be, if we ask the question of ourselves.")
//i.Parse("Mr. Bingley was good-looking and gentlemanlike; he had a pleasant countenance, and easy, unaffected manners.")
// i.Parse("To think it more than commonly anxious to get round to the preference of one, and offended by the other as politely and more cheerfully.")
// i.Parse("Their visit afforded was produced by the lady with whom she almost looked up to the stables. They were to set out with such a woman.")
b, err := i.Babble("be something", 200)
require.NoError(t, err)
require.NotEmpty(t, b)
i = NewIndex(3, &Options{
Store: &MockStore{
errGet: true,
errAny: true,
},
})
_, err = i.Babble("", 10)
require.Error(t, err)
i = NewIndex(3, &Options{
Store: &MockStore{
errGet: true,
},
})
_, err = i.Babble("", 10)
require.Error(t, err)
require.Equal(t, ErrEmptyIndex, err)
i = NewIndex(3, &Options{
Store: &MockStore{},
})
_, err = i.Babble("", 10)
require.Error(t, err)
require.Equal(t, ErrNoResult, err)
}