Skip to content

Commit 43a72bf

Browse files
committed
[spellcheck] Code refactoring
1 parent 81596f5 commit 43a72bf

File tree

1 file changed

+7
-16
lines changed

1 file changed

+7
-16
lines changed

spellcheck/spellcheck.go

+7-16
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,6 @@ type suggestItem struct {
3131
score int
3232
}
3333

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-
4034
// ////////////////////////////////////////////////////////////////////////////////// //
4135

4236
// Train trains words by given string slice
@@ -130,14 +124,9 @@ func (m *Model) Correct(word string) string {
130124
return word
131125
}
132126

133-
var result *suggestItem
127+
result := suggestItem{score: 99999999999}
134128

135129
for _, si := range getSuggestSlice(m.terms, word) {
136-
if result == nil {
137-
result = si
138-
continue
139-
}
140-
141130
if si.score < result.score {
142131
result = si
143132
continue
@@ -163,7 +152,9 @@ func (m *Model) Suggest(word string, max int) []string {
163152

164153
sis := getSuggestSlice(m.terms, word)
165154

166-
sort.Sort(sis)
155+
sort.Slice(sis, func(i, j int) bool {
156+
return sis[i].score < sis[j].score
157+
})
167158

168159
var result []string
169160

@@ -176,11 +167,11 @@ func (m *Model) Suggest(word string, max int) []string {
176167

177168
// ////////////////////////////////////////////////////////////////////////////////// //
178169

179-
func getSuggestSlice(terms []string, word string) suggestItems {
180-
var result suggestItems
170+
func getSuggestSlice(terms []string, word string) []suggestItem {
171+
var result []suggestItem
181172

182173
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))})
184175
}
185176

186177
return result

0 commit comments

Comments
 (0)