@@ -31,12 +31,6 @@ type suggestItem struct {
31
31
score int
32
32
}
33
33
34
- type suggestItems []* suggestItem
35
-
36
- func (s suggestItems ) Len () int { return len (s ) }
37
- func (s suggestItems ) Less (i , j int ) bool { return s [i ].score < s [j ].score }
38
- func (s suggestItems ) Swap (i , j int ) { s [i ], s [j ] = s [j ], s [i ] }
39
-
40
34
// ////////////////////////////////////////////////////////////////////////////////// //
41
35
42
36
// Train trains words by given string slice
@@ -130,14 +124,9 @@ func (m *Model) Correct(word string) string {
130
124
return word
131
125
}
132
126
133
- var result * suggestItem
127
+ result := suggestItem { score : 99999999999 }
134
128
135
129
for _ , si := range getSuggestSlice (m .terms , word ) {
136
- if result == nil {
137
- result = si
138
- continue
139
- }
140
-
141
130
if si .score < result .score {
142
131
result = si
143
132
continue
@@ -163,7 +152,9 @@ func (m *Model) Suggest(word string, max int) []string {
163
152
164
153
sis := getSuggestSlice (m .terms , word )
165
154
166
- sort .Sort (sis )
155
+ sort .Slice (sis , func (i , j int ) bool {
156
+ return sis [i ].score < sis [j ].score
157
+ })
167
158
168
159
var result []string
169
160
@@ -176,11 +167,11 @@ func (m *Model) Suggest(word string, max int) []string {
176
167
177
168
// ////////////////////////////////////////////////////////////////////////////////// //
178
169
179
- func getSuggestSlice (terms []string , word string ) suggestItems {
180
- var result suggestItems
170
+ func getSuggestSlice (terms []string , word string ) [] suggestItem {
171
+ var result [] suggestItem
181
172
182
173
for _ , t := range terms {
183
- result = append (result , & suggestItem {t , Distance (strings .ToLower (t ), strings .ToLower (word ))})
174
+ result = append (result , suggestItem {t , Distance (strings .ToLower (t ), strings .ToLower (word ))})
184
175
}
185
176
186
177
return result
0 commit comments