forked from shixzie/nlp
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnlp_test.go
425 lines (401 loc) · 8.23 KB
/
nlp_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
package nlp
import (
"bytes"
"reflect"
"testing"
"time"
"fmt"
"github.com/cdipaolo/goml/text"
)
const savedNL = `{"m":[{"t":"T","e":[[{"l":true,"v":"c3RyaW5n"},{"v":"U3RyaW5n"}],[{"l":true,"v":"aW50"},{"v":"SW50","f":1}],[{"l":true,"v":"dWludA=="},{"v":"VWludA==","f":2}],[{"l":true,"v":"ZmxvYXQ="},{"v":"RmxvYXQ=","f":3}],[{"l":true,"v":"dGltZQ=="},{"v":"VGltZQ==","f":4}],[{"l":true,"v":"ZHVy"},{"v":"RHVy","f":5}],[{"l":true,"v":"c3RyaW5n"},{"v":"U3RyaW5n"},{"l":true,"v":"aW50"},{"v":"SW50","f":1}],[{"l":true,"v":"c3RyaW5n"},{"v":"U3RyaW5n"},{"l":true,"v":"dGltZQ=="},{"v":"VGltZQ==","f":4}],[{"l":true,"v":"bmVlZA=="},{"v":"U3RyaW5n"},{"l":true,"v":"c2luY2U="},{"v":"VGltZQ==","f":4}]],"s":["c3RyaW5nIHtTdHJpbmd9","aW50IHtJbnR9","dWludCB7VWludH0=","ZmxvYXQge0Zsb2F0fQ==","dGltZSB7VGltZX0=","ZHVyIHtEdXJ9","c3RyaW5nIHtTdHJpbmd9IGludCB7SW50fQ==","c3RyaW5nIHtTdHJpbmd9IHRpbWUge1RpbWV9","bmVlZCB7U3RyaW5nfSBzaW5jZSB7VGltZX0="]}],"n":{"w":{"dur":{"c":[2],"s":2,"ds":1},"float":{"c":[2],"s":2,"ds":1},"int":{"c":[4],"s":4,"ds":2},"need":{"c":[1],"s":1,"ds":1},"since":{"c":[1],"s":1,"ds":1},"string":{"c":[7],"s":7,"ds":4},"time":{"c":[5],"s":5,"ds":3},"uint":{"c":[2],"s":2,"ds":1}},"c":[9],"p":[1],"d":9,"v":8},"o":"VHJhaW5pbmc6CglNb2RlbDogTXVsdGlub21pYWwgTmHDr3ZlIEJheWVzCglDbGFzc2VzOiAxClRyYWluaW5nIENvbXBsZXRlZC4KaCjOuCkgPSBhcmdtYXhfY3tsb2coUCh5ID0gYykpICsgzqNsb2coUCh4fHkgPSBjKSl9CglDbGFzc2VzOiAxCglEb2N1bWVudHMgZXZhbHVhdGVkIGluIG1vZGVsOiA5CglXb3JkcyBldmFsdWF0ZWQgaW4gbW9kZWw6IDgKCgo="}`
func failTest(t *testing.T, err error) {
if err != nil {
t.Error(err)
}
}
func TestNL_P(t *testing.T) {
type T struct {
String string
Int int
Uint uint
Float float32
Time time.Time
Dur time.Duration
}
tSamples := []string{
"string {String}",
"int {Int}",
"uint {Uint}",
"float {Float}",
"time {Time}",
"dur {Dur}",
"string {String} int {Int}",
"string {String} time {Time}",
"need {String} since {Time}",
}
nl := New()
err := nl.RegisterModel(T{}, tSamples)
failTest(t, err)
err = nl.Learn()
failTest(t, err)
// b, _ := nl.Export()
// fmt.Println(string(b))
tim, err := time.ParseInLocation("01-02-2006_3:04pm", "05-18-1999_6:42pm", time.Local)
failTest(t, err)
dur, err := time.ParseDuration("4h2m")
failTest(t, err)
cases := []struct {
name string
expression string
want *T
}{
0: {
"string",
"string Hello World",
&T{String: "Hello World"},
},
1: {
"int",
"int 42",
&T{Int: 42},
},
2: {
"uint",
"uint 43",
&T{Uint: 43},
},
3: {
"float",
"float 44",
&T{Float: 44},
},
4: {
"time",
"time 05-18-1999_6:42pm",
&T{Time: tim},
},
5: {
"duration",
"dur 4h2m",
&T{Dur: dur},
},
6: {
"string int",
"string Lmao int 42",
&T{
String: "Lmao",
Int: 42,
},
},
7: {
"string time",
"string What's Up Boy time 05-18-1999_6:42pm",
&T{
String: "What's Up Boy",
Time: tim,
},
},
8: {
"word string time",
"Hi, I am Patrice, I need Issue#4 since 05-18-1999_6:42pm",
&T{
String: "Issue#4",
Time: tim,
},
},
}
for i, tt := range cases {
t.Run(tt.name, func(t *testing.T) {
if res := nl.P(tt.expression); !reflect.DeepEqual(res, tt.want) {
t.Errorf("test#%d: got %v want %v", i, res, tt.want)
}
})
}
}
func TestNL_P_Import(t *testing.T) {
type T struct {
String string
Int int
Uint uint
Float float32
Time time.Time
Dur time.Duration
}
nl := New()
err := nl.RegisterModel(T{}, []string{})
failTest(t, err)
err = nl.Import([]byte(savedNL))
if err != nil {
fmt.Println(err.Error())
t.Fail()
return
}
tim, err := time.ParseInLocation("01-02-2006_3:04pm", "05-18-1999_6:42pm", time.Local)
failTest(t, err)
dur, err := time.ParseDuration("4h2m")
failTest(t, err)
cases := []struct {
name string
expression string
want *T
}{
0: {
"string",
"string Hello World",
&T{String: "Hello World"},
},
1: {
"int",
"int 42",
&T{Int: 42},
},
2: {
"uint",
"uint 43",
&T{Uint: 43},
},
3: {
"float",
"float 44",
&T{Float: 44},
},
4: {
"time",
"time 05-18-1999_6:42pm",
&T{Time: tim},
},
5: {
"duration",
"dur 4h2m",
&T{Dur: dur},
},
6: {
"string int",
"string Lmao int 42",
&T{
String: "Lmao",
Int: 42,
},
},
7: {
"string time",
"string What's Up Boy time 05-18-1999_6:42pm",
&T{
String: "What's Up Boy",
Time: tim,
},
},
8: {
"word string time",
"Hi, I am Patrice, I need Issue#4 since 05-18-1999_6:42pm",
&T{
String: "Issue#4",
Time: tim,
},
},
}
for i, tt := range cases {
t.Run(tt.name, func(t *testing.T) {
if res := nl.P(tt.expression); !reflect.DeepEqual(res, tt.want) {
t.Errorf("test#%d: got %v want %v", i, res, tt.want)
}
})
}
}
func TestNL_RegisterModel(t *testing.T) {
type fields struct {
models []*model
naive *text.NaiveBayes
Output *bytes.Buffer
}
type args struct {
i interface{}
samples []string
ops []ModelOption
}
type T struct {
unexported int
Time time.Time
}
tests := []struct {
name string
fields fields
args args
wantErr bool
}{
{
"nil struct",
fields{},
args{nil, nil, nil},
true,
},
{
"nil samples",
fields{},
args{args{}, nil, nil},
true,
},
{
"non-struct",
fields{},
args{[]int{}, []string{""}, nil},
true,
},
{
"unexported & time.Time",
fields{},
args{T{}, []string{""}, nil},
false,
},
{
"options",
fields{},
args{T{}, []string{""}, []ModelOption{
WithTimeFormat("02-01-2006"),
WithTimeLocation(time.Local),
}},
false,
},
}
for i, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
nl := &NL{
models: tt.fields.models,
naive: tt.fields.naive,
Output: tt.fields.Output,
}
if err := nl.RegisterModel(tt.args.i, tt.args.samples, tt.args.ops...); (err != nil) != tt.wantErr {
t.Errorf("[%d] NL.RegisterModel() error = %v, wantErr %v", i, err, tt.wantErr)
}
})
}
}
func TestNL_Learn(t *testing.T) {
type fields struct {
models []*model
naive *text.NaiveBayes
Output *bytes.Buffer
}
type T struct {
Name string
}
tests := []struct {
name string
fields fields
wantErr bool
}{
{
"no models",
fields{},
true,
},
{
"empty model sample",
fields{
models: []*model{
{
samples: [][]byte{{}},
},
},
Output: bytes.NewBufferString(""),
},
true,
},
{
"mistyped field",
fields{
models: []*model{
{
samples: [][]byte{[]byte("Hello {Namee}")},
},
},
Output: bytes.NewBufferString(""),
},
true,
},
{
"sample with no keys",
fields{
models: []*model{
{
samples: [][]byte{[]byte("Hello")},
},
},
Output: bytes.NewBufferString(""),
},
true,
},
}
for i, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
nl := &NL{
models: tt.fields.models,
naive: tt.fields.naive,
Output: tt.fields.Output,
}
if err := nl.Learn(); (err != nil) != tt.wantErr {
t.Errorf("[%d] NL.Learn() error = %v, wantErr %v", i, err, tt.wantErr)
}
})
}
}
func TestWithTimeFormat(t *testing.T) {
type args struct {
format string
m *model
}
tests := []struct {
name string
args args
wantErr bool
}{
{
"invalid format",
args{"2006 01 02", &model{}},
true,
},
{
"valid format",
args{"2006", &model{}},
false,
},
}
for i, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
op := WithTimeFormat(tt.args.format)
if err := op(tt.args.m); (err != nil) != tt.wantErr {
t.Errorf("[%d] WithTimeFormat() error = %v, wantErr %v", i, err, tt.wantErr)
}
})
}
}
func TestWithTimeLocation(t *testing.T) {
type args struct {
loc *time.Location
m *model
}
tests := []struct {
name string
args args
wantErr bool
}{
{
"invalid location",
args{nil, &model{}},
true,
},
{
"valid format",
args{time.Local, &model{}},
false,
},
}
for i, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
op := WithTimeLocation(tt.args.loc)
if err := op(tt.args.m); (err != nil) != tt.wantErr {
t.Errorf("[%d] WithTimeFormat() error = %v, wantErr %v", i, err, tt.wantErr)
}
})
}
}