forked from f1monkey/spellchecker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spellchecker.go
145 lines (115 loc) · 2.52 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
package spellchecker
import (
"bufio"
"fmt"
"io"
"sync"
)
// OptionFunc option setter
type OptionFunc func(m *Spellchecker) error
type Spellchecker struct {
mtx sync.RWMutex
dict *dictionary
splitter bufio.SplitFunc
}
func New(alphabet Alphabet, opts ...OptionFunc) (*Spellchecker, error) {
dict, err := newDictionary(alphabet)
if err != nil {
return nil, err
}
result := &Spellchecker{
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, fmt.Errorf("%w: %s", ErrUnknownWord, word)
}
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}, fmt.Errorf("%w: %s", ErrUnknownWord, word)
}
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
}
}