forked from sajari/fuzzy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fuzzy.go
440 lines (398 loc) · 10.6 KB
/
fuzzy.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
package fuzzy
import (
"bufio"
"encoding/json"
"fmt"
"log"
"os"
"regexp"
"strings"
"sync"
)
type Pair struct {
str1 string
str2 string
}
type Potential struct {
term string
score int
leven int
method int // 0 - is word, 1 - suggest maps to input, 2 - input delete maps to dictionary, 3 - input delete maps to suggest
}
type Suggestion struct {
Term string
Domain
}
type Domain struct {
Name string
Frequency int
}
type Model struct {
Data map[string]int `json:"data"`
Maxcount int `json:"maxcount"`
Suggest map[string][]string `json:"suggest"`
Domains map[string]Domain `json:"type"`
Depth int `json:"depth"`
Threshold int `json:"threshold"`
sync.RWMutex
}
// Create and initialise a new model
func NewModel() *Model {
model := new(Model)
return model.Init()
}
func (model *Model) Init() *Model {
model.Data = make(map[string]int)
model.Suggest = make(map[string][]string)
model.Domains = make(map[string]Domain)
model.Depth = 2
model.Threshold = 3 // Setting this to 1 is most accurate, but "1" is 5x more memory and 30x slower processing than "4". This is a big performance tuning knob
return model
}
// Save a spelling model to disk
func (model *Model) Save(filename string) error {
model.RLock()
defer model.RUnlock()
f, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
if err != nil {
log.Println("Fuzzy model:", err)
return err
}
b := bufio.NewWriter(f)
e := json.NewEncoder(b)
defer f.Close()
defer b.Flush()
err = e.Encode(model)
if err != nil {
log.Println("Fuzzy model:", err)
}
return err
}
// Load a saved model from disk
func Load(filename string) (*Model, error) {
model := new(Model)
f, err := os.Open(filename)
if err != nil {
return model, err
}
defer f.Close()
//b := bufio.NewReader(f)
d := json.NewDecoder(f)
err = d.Decode(model)
if err != nil {
return model, err
}
return model, nil
}
// Change the default depth value of the model. This sets how many
// character differences are indexed. The default is 2.
func (model *Model) SetDepth(val int) {
model.Lock()
model.Depth = val
model.Unlock()
}
// Change the default threshold of the model. This is how many times
// a term must be seen before suggestions are created for it
func (model *Model) SetThreshold(val int) {
model.Lock()
model.Threshold = val
model.Unlock()
}
// Calculate the Levenshtein distance between two strings
func Levenshtein(a, b *string) int {
la := len(*a)
lb := len(*b)
d := make([]int, la+1)
var lastdiag, olddiag, temp int
for i := 1; i <= la; i++ {
d[i] = i
}
for i := 1; i <= lb; i++ {
d[0] = i
lastdiag = i - 1
for j := 1; j <= la; j++ {
olddiag = d[j]
min := d[j] + 1
if (d[j-1] + 1) < min {
min = d[j-1] + 1
}
if (*a)[j-1] == (*b)[i-1] {
temp = 0
} else {
temp = 1
}
if (lastdiag + temp) < min {
min = lastdiag + temp
}
d[j] = min
lastdiag = olddiag
}
}
return d[la]
}
// Add an array of words to train the model in bulk
func (model *Model) Train(terms []string) {
for _, term := range terms {
model.TrainWord(term)
}
}
// Manually set the count of a word. Optionally trigger the
// creation of suggestion keys for the term. This function lets
// you build a model from an existing dictionary with word popularity
// counts without needing to run "TrainWord" repeatedly
func (model *Model) SetCount(term string, count int, suggest bool) {
model.Lock()
model.Data[term] += count
if suggest {
model.createSuggestKeys(term)
}
model.Unlock()
}
// Manually set the domain/type/class of a word. This is for arbitrary classification of terms.
func (model *Model) SetDomain(term string, count int, domain string) {
model.Lock()
model.Domains[term] = Domain{Name: domain, Frequency: count}
model.Unlock()
}
// Train the model word by word
func (model *Model) TrainWord(term string) {
model.Lock()
model.Data[term]++
// Set the max
if model.Data[term] > model.Maxcount {
model.Maxcount = model.Data[term]
}
// If threshold is triggered, store delete suggestion keys
if model.Data[term] == model.Threshold {
model.createSuggestKeys(term)
}
model.Unlock()
}
// For a given term, create the partially deleted lookup keys
func (model *Model) createSuggestKeys(term string) {
edits := model.EditsMulti(term, model.Depth)
for _, edit := range edits {
skip := false
for _, hit := range model.Suggest[edit] {
if hit == term {
// Already know about this one
skip = true
continue
}
}
if !skip && len(edit) > 1 {
model.Suggest[edit] = append(model.Suggest[edit], term)
}
}
}
// Edits at any depth for a given term. The depth of the model is used
func (model *Model) EditsMulti(term string, depth int) []string {
edits := Edits1(term)
for {
depth--
if depth <= 0 {
break
}
for _, edit := range edits {
edits2 := Edits1(edit)
for _, edit2 := range edits2 {
edits = append(edits, edit2)
}
}
}
return edits
}
// Edits1 creates a set of terms that are 1 char delete from the input term
func Edits1(word string) []string {
splits := []Pair{}
for i := 0; i <= len(word); i++ {
splits = append(splits, Pair{word[:i], word[i:]})
}
total_set := []string{}
for _, elem := range splits {
//deletion
if len(elem.str2) > 0 {
total_set = append(total_set, elem.str1+elem.str2[1:])
} else {
total_set = append(total_set, elem.str1)
}
}
return total_set
}
func (model *Model) score(input string) int {
if score, ok := model.Data[input]; ok {
return score
}
return 0
}
// From a group of potentials, work out the most likely result
func best(input string, potential map[string]*Potential) string {
best := ""
bestcalc := 0
for i := 0; i < 4; i++ {
for _, pot := range potential {
if pot.leven == 0 {
return pot.term
} else if pot.leven == i {
if pot.score > bestcalc {
bestcalc = pot.score
// If the first letter is the same, that's a good sign. Bias these potentials
if pot.term[0] == input[0] {
bestcalc += bestcalc * 100
}
best = pot.term
}
}
}
if bestcalc > 0 {
return best
}
}
return best
}
// Test an input, if we get it wrong, look at why it is wrong. This
// function returns a bool indicating if the guess was correct as well
// as the term it is suggesting. Typically this function would be used
// for testing, not for production
func (model *Model) CheckKnown(input string, correct string) bool {
model.RLock()
defer model.RUnlock()
suggestions := model.suggestPotential(input, true)
best := best(input, suggestions)
if best == correct {
// This guess is correct
fmt.Printf("Input correctly maps to correct term")
return true
}
if pot, ok := suggestions[correct]; !ok {
if model.score(correct) > 0 {
fmt.Printf("\"%v\" - %v (%v) not in the suggestions. (%v) best option.\n", input, correct, model.score(correct), best)
for _, sugg := range suggestions {
fmt.Printf(" %v\n", sugg)
}
} else {
fmt.Printf("\"%v\" - Not in dictionary\n", correct)
}
} else {
fmt.Printf("\"%v\" - (%v) suggested, should however be (%v).\n", input, suggestions[best], pot)
}
return false
}
// For a given input term, suggest some alternatives. If exhaustive, each of the 4
// cascading checks will be performed and all potentials will be sorted accordingly
func (model *Model) suggestPotential(input string, exhaustive bool) map[string]*Potential {
input = strings.ToLower(input)
suggestions := make(map[string]*Potential, 20)
// 0 - If this is a dictionary term we're all good, no need to go further
if model.score(input) > model.Threshold {
suggestions[input] = &Potential{term: input, score: model.score(input), leven: 0, method: 0}
if !exhaustive {
return suggestions
}
}
// 1 - See if the input matches a "suggest" key
if sugg, ok := model.Suggest[input]; ok {
for _, pot := range sugg {
if _, ok := suggestions[pot]; !ok {
suggestions[pot] = &Potential{term: pot, score: model.score(pot), leven: Levenshtein(&input, &pot), method: 1}
}
}
if !exhaustive {
return suggestions
}
}
// 2 - See if edit1 matches input
max := 0
edits := model.EditsMulti(input, model.Depth)
for _, edit := range edits {
score := model.score(edit)
if score > 0 && len(edit) > 2 {
if _, ok := suggestions[edit]; !ok {
suggestions[edit] = &Potential{term: edit, score: score, leven: Levenshtein(&input, &edit), method: 2}
}
if score > max {
max = score
}
}
}
if max > 0 {
if !exhaustive {
return suggestions
}
}
// 3 - No hits on edit1 distance, look for transposes and replaces
// Note: these are more complex, we need to check the guesses
// more thoroughly, e.g. levals=[valves] in a raw sense, which
// is incorrect
for _, edit := range edits {
if sugg, ok := model.Suggest[edit]; ok {
// Is this a real transpose or replace?
for _, pot := range sugg {
lev := Levenshtein(&input, &pot)
if lev <= model.Depth+1 { // The +1 doesn't seem to impact speed, but has greater coverage when the depth is not sufficient to make suggestions
if _, ok := suggestions[pot]; !ok {
suggestions[pot] = &Potential{term: pot, score: model.score(pot), leven: lev, method: 3}
}
}
}
}
}
return suggestions
}
func (model *Model) Suggestions(input string, exhaustive bool) []string {
model.RLock()
suggestions := model.suggestPotential(input, exhaustive)
model.RUnlock()
output := make([]string, 10)
for _, suggestion := range suggestions {
output = append(output, suggestion.term)
}
return output
}
func (model *Model) DomainSuggestions(input string, exhaustive bool) []Suggestion {
model.RLock()
suggestions := model.suggestPotential(input, exhaustive)
model.RUnlock()
output := make([]Suggestion, 0)
for _, suggestion := range suggestions {
var s Suggestion
s.Term = suggestion.term
s.Domain = model.Domains[suggestion.term]
output = append(output, s)
}
return output
}
// Return the most likely correction for the input term
func (model *Model) SpellCheck(input string) string {
model.RLock()
suggestions := model.suggestPotential(input, false)
model.RUnlock()
return best(input, suggestions)
}
func SampleEnglish() []string {
var out []string
file, err := os.Open("data/big.txt")
if err != nil {
fmt.Println(err)
return out
}
reader := bufio.NewReader(file)
scanner := bufio.NewScanner(reader)
scanner.Split(bufio.ScanLines)
// Count the words.
count := 0
for scanner.Scan() {
exp, _ := regexp.Compile("[a-zA-Z]+")
words := exp.FindAll([]byte(scanner.Text()), -1)
for _, word := range words {
if len(word) > 1 {
out = append(out, strings.ToLower(string(word)))
count++
}
}
}
if err := scanner.Err(); err != nil {
fmt.Fprintln(os.Stderr, "reading input:", err)
}
return out
}