-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrams_test.go
218 lines (207 loc) · 5.28 KB
/
grams_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
package tokenize
import (
"reflect"
"regexp"
"strings"
"testing"
)
func getPointer(s string) *string {
return &s
}
func TestCreateCharFilterMapper(t *testing.T) {
type args struct {
excludes []string
}
tests := []struct {
name string
args args
want map[string]string
}{
{
name: "it should exclude the string we put in the excludes array",
args: args{
excludes: []string{"ไม่", "⌘", " 👨👩👧"},
},
want: map[string]string{
"ไม่มี": "มี",
"this is a book 👨👩👧": "this is a book ",
"this is a book⌘": "this is a book",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mapper := CreateCharFilterMapper(tt.args.excludes)
for k, v := range tt.want {
if got := mapper(k); !reflect.DeepEqual(got, v) {
t.Errorf("CreateCharFilterMapper() = %v, want %v", got, v)
}
}
})
}
}
func TestCreateSplitterRegExp(t *testing.T) {
type args struct {
regex *regexp.Regexp
}
tests := []struct {
name string
args args
want map[string][]string
}{
{
name: "it should split the string by the sentence",
args: args{
regex: regexp.MustCompile(`\s+|\n|\t`),
},
want: map[string][]string{
"ไม่มี นะ": []string{"ไม่มี", "นะ"},
`this an
thaist ing`: []string{"this", "an", "thaist", "ing"},
"thai land": []string{"thai", "land"},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
splitter := CreateSplitterRegExp(tt.args.regex)
for k, v := range tt.want {
if got := splitter(k); !reflect.DeepEqual(got, v) {
t.Errorf("CreateSplitterRegExp() = %v, want %v", got, v)
}
}
})
}
}
func TestCreateSplitterByRunes(t *testing.T) {
type args struct {
splitters []rune
}
tests := []struct {
name string
args args
want map[string][]string
}{
{
name: "it should split the string by the sentence",
args: args{
splitters: []rune{' ', '\t', '\n'},
},
want: map[string][]string{
"ไม่มี นะ": []string{"ไม่มี", "นะ"},
`this an
thaist ing`: []string{"this", "an", "thaist", "ing"},
"thai land": []string{"thai", "land"},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
splitter := CreateSplitterByRunes(tt.args.splitters)
for k, v := range tt.want {
if got := splitter(k); !reflect.DeepEqual(got, v) {
t.Errorf("CreateSplitterByRunes() = %v, want %v", got, v)
}
}
})
}
}
func TestCreateTokenization(t *testing.T) {
type args struct {
tokenFn func(string) []string
stemmer map[string]string
filterFn func(string) bool
}
tests := []struct {
name string
args args
want map[string][]string
}{
{
name: "it make a tokenization process correctly",
args: args{
tokenFn: func(s string) []string {
return strings.Split(s, "และ")
},
stemmer: map[string]string{"ร๊าก": "รัก", "thailand": "ไทย"},
filterFn: func(s string) bool {
if s == "เหี้ย" {
return false
}
return true
},
},
want: map[string][]string{
"มีกันและกัน": []string{"มีกัน", "กัน"},
`ร๊ากและจะร๊าก กับรักและThailandและthaistและing`: []string{"รัก", "จะร๊าก กับรัก", `ไทย`, "thaist", "ing"},
"เหี้ยและเหี้ย": []string{},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tokenization := CreateTokenization(tt.args.tokenFn, tt.args.stemmer, tt.args.filterFn)
for k, v := range tt.want {
if got := tokenization(k); !reflect.DeepEqual(got, v) {
if len(got) != 0 || len(v) != 0 {
t.Errorf("CreateTokenization() = %v, want %v", got, v)
}
}
}
})
}
}
func TestCreateNGram(t *testing.T) {
str := []string{"A", "B", "C", "D", "E", "F"}
type args struct {
gramConfig NGramConfig
strIn chan []string
gramOut chan string
}
tests := []struct {
name string
args args
gramsResult []string
}{
{
name: "it should create the correct gram 1-4",
args: args{
gramConfig: NGramConfig{
MinGram: 1,
MaxGram: 4,
GramFilter: func(s string) bool { return true },
},
strIn: make(chan []string),
gramOut: make(chan string),
},
gramsResult: []string{"A", "AB", "ABC", "ABCD", "B", "BC", "BCD", "BCDE", "C", "CD", "CDE", "CDEF", "D", "DE", "DEF", "E", "EF", "F"},
},
{
name: "it should create the correct 2-2",
args: args{
gramConfig: NGramConfig{
MinGram: 2,
MaxGram: 2,
GramFilter: func(s string) bool { return true },
},
strIn: make(chan []string),
gramOut: make(chan string),
},
gramsResult: []string{"AB", "BC", "CD", "DE", "EF"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
go CreateNGram(tt.args.strIn, tt.args.gramOut, tt.args.gramConfig)
tt.args.strIn <- str
close(tt.args.strIn)
i := 0
for gram := range tt.args.gramOut {
if gram != tt.gramsResult[i] {
t.Errorf("TestCreateNGram() = %v, want %v", gram, tt.gramsResult[i])
}
i++
}
})
}
}