This repository has been archived by the owner on Jul 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
/
nlp_test.go
322 lines (307 loc) · 5.29 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
package nlp
import (
"bytes"
"reflect"
"testing"
"time"
"github.com/cdipaolo/goml/text"
)
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)
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)
}
})
}
}