-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathspellchecker.go
185 lines (149 loc) · 3.59 KB
/
spellchecker.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
package spellchecker
import (
"bufio"
"fmt"
"io"
"math"
"sync"
)
const DefaultMaxErrors = 2
// OptionFunc option setter
type OptionFunc func(s *Spellchecker) error
type Spellchecker struct {
mtx sync.RWMutex
dict *dictionary
splitter bufio.SplitFunc
scoreFunc scoreFunc
maxErrors int
}
func New(alphabet string, opts ...OptionFunc) (*Spellchecker, error) {
result := &Spellchecker{
maxErrors: DefaultMaxErrors,
scoreFunc: defaultScorefunc,
}
dict, err := newDictionary(alphabet, result.scoreFunc, result.maxErrors)
if err != nil {
return nil, err
}
result.dict = dict
for _, o := range opts {
if err := o(result); err != nil {
return nil, err
}
}
return result, nil
}
// AddFrom reads input, splits it with spellchecker splitter func and adds words to dictionary
func (m *Spellchecker) AddFrom(input io.Reader) error {
words := make([]string, 1000)
i := 0
for item := range readInput(input, m.splitter) {
if item.err != nil {
return item.err
}
if i == len(words) {
m.Add(words...)
i = 0
}
words[i] = item.word
i++
}
if i > 0 {
m.Add(words[:i]...)
}
return nil
}
// Add adds provided words to dictionary
func (m *Spellchecker) Add(words ...string) {
m.mtx.Lock()
defer m.mtx.Unlock()
for _, word := range words {
if id := m.dict.id(word); id > 0 {
m.dict.inc(id)
continue
}
m.dict.add(word)
}
}
var ErrUnknownWord = fmt.Errorf("unknown word")
// IsCorrect check if provided word is in the dictionary
func (s *Spellchecker) IsCorrect(word string) bool {
s.mtx.RLock()
defer s.mtx.RUnlock()
return s.dict.has(word)
}
func (s *Spellchecker) Fix(word string) (string, error) {
s.mtx.RLock()
defer s.mtx.RUnlock()
if s.dict.has(word) {
return word, nil
}
hits := s.dict.find(word, 1)
if len(hits) == 0 {
return word, ErrUnknownWord
}
return hits[0].Value, nil
}
// Suggest find top n suggestions for the word
func (s *Spellchecker) Suggest(word string, n int) ([]string, error) {
s.mtx.RLock()
defer s.mtx.RUnlock()
if s.dict.has(word) {
return []string{word}, nil
}
hits := s.dict.find(word, n)
if len(hits) == 0 {
return []string{word}, ErrUnknownWord
}
result := make([]string, len(hits))
for i, h := range hits {
result[i] = h.Value
}
return result, nil
}
// WithOpt set spellchecker options
func (s *Spellchecker) WithOpts(opts ...OptionFunc) error {
s.mtx.Lock()
defer s.mtx.Unlock()
for _, o := range opts {
if err := o(s); err != nil {
return err
}
}
return nil
}
// WithSplitter set splitter func for AddFrom() reader
func WithSplitter(f bufio.SplitFunc) OptionFunc {
return func(s *Spellchecker) error {
s.splitter = f
return nil
}
}
// WithMaxErrors set maxErrors, which is a max diff in bits betweeen the "search word" and a "dictionary word".
// i.e. one simple symbol replacement (problam => problem ) is a two-bit difference
func WithMaxErrors(maxErrors int) OptionFunc {
return func(s *Spellchecker) error {
s.maxErrors = maxErrors
return nil
}
}
type ScoreFunc = scoreFunc
// WithScoreFunc specify a function that will be used for scoring
func WithScoreFunc(f ScoreFunc) OptionFunc {
return func(s *Spellchecker) error {
s.dict.scoreFunc = f
return nil
}
}
var defaultScorefunc scoreFunc = func(src, candidate []rune, distance, cnt int) float64 {
mult := math.Log1p(float64(cnt))
// if first letters are the same, increase score
if src[0] == candidate[0] {
mult *= 1.5
// if second letters are the same too, increase score even more
if len(src) > 1 && len(candidate) > 1 && src[1] == candidate[1] {
mult *= 1.5
}
}
return 1 / (1 + float64(distance*distance)) * mult
}