-
Notifications
You must be signed in to change notification settings - Fork 0
/
smac_engine_trie.go
563 lines (481 loc) · 12.1 KB
/
smac_engine_trie.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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
// Copyright Piero de Salvia.
// All Rights Reserved
package smac
import (
"bufio"
"encoding/gob"
"errors"
"io"
"os"
)
type trieNode struct {
isWord bool
intRune int
accepts int
links []*trieNode
}
// AutoCompleteTrie represents the autocomplete engine.
type AutoCompleteTrie struct {
root *trieNode
alphabetMin int
alphabetMax int
alphabetSize int
resultSize int
radius int
newWords map[string]byte
removedWords map[string]byte
}
// NewAutoCompleteTrieE returns a new, empty autocompleter for a given alphabet (set of runes).
//
// resultSize is the number of hits returned. If 0 is used, it defaults to DEF_RESULTS_SIZE
//
// radius is the max length of words the engine will search while autocompleting. If 0 is used, it defaults to DEF_RADIUS
//
// The returned completer does not contain any words to be completed. New words can be added to it by using the Learn()
// function
func NewAutoCompleteTrieE(alphabet string, resultSize, radius uint) (AutoCompleteTrie, error) {
var nAc AutoCompleteTrie
if len(alphabet) == 0 {
return nAc, errors.New("Empty alphabet")
}
min, max := minMax([]rune(alphabet))
if resultSize == 0 {
resultSize = DefaultResultSize
}
if radius == 0 {
radius = DefaultRadius
}
autoComplete := AutoCompleteTrie{
alphabetMin: int(min),
alphabetMax: int(max),
alphabetSize: int(max) - int(min) + 1,
resultSize: int(resultSize),
radius: int(radius),
newWords: make(map[string]byte),
removedWords: make(map[string]byte),
}
autoComplete.root = &trieNode{
links: make([]*trieNode, autoComplete.alphabetSize),
}
return autoComplete, nil
}
// NewAutoCompleteTrieS returns a new autocompleter for a given alphabet (set of runes).
//
// dictionary is a slice of words to be used for completion.
//
// resultSize is the number of hits returned. If 0 is used, it defaults to DEF_RESULTS_SIZE
//
// radius is the max length of words the engine will search while autocompleting. If 0 is used, it defaults to DEF_RADIUS
//
// New words can be added to it by using the Learn() function
func NewAutoCompleteTrieS(alphabet string, dictionary []string, resultSize, radius uint) (AutoCompleteTrie, error) {
var nAc AutoCompleteTrie
if len(alphabet) == 0 {
return nAc, errors.New("Empty alphabet")
}
min, max := minMax([]rune(alphabet))
if resultSize == 0 {
resultSize = DefaultResultSize
}
if radius == 0 {
radius = DefaultRadius
}
autoComplete := AutoCompleteTrie{
alphabetMin: int(min),
alphabetMax: int(max),
alphabetSize: int(max) - int(min) + 1,
resultSize: int(resultSize),
radius: int(radius),
newWords: make(map[string]byte),
removedWords: make(map[string]byte),
}
autoComplete.root = &trieNode{
links: make([]*trieNode, autoComplete.alphabetSize),
}
for _, w := range dictionary {
err := autoComplete.put(w)
if err != nil {
return nAc, err
}
}
return autoComplete, nil
}
// NewAutoCompleteTrieF returns a new autocompleter for a given alphabet (set of runes).
//
// dictionaryFileName is the name of a dictionary file (a file containing words) to be used for completion.
//
// resultSize is the number of hits returned. If 0 is used, it defaults to DEF_RESULTS_SIZE
//
// radius is the max length of words the engine will search while autocompleting. If 0 is used, it defaults to DEF_RADIUS
//
// New words can be added to it by using the Learn() function
func NewAutoCompleteTrieF(alphabet, dictionaryFileName string, resultSize, radius uint) (AutoCompleteTrie, error) {
var nAc AutoCompleteTrie
if len(alphabet) == 0 {
return nAc, errors.New("Empty alphabet")
}
min, max := minMax([]rune(alphabet))
if resultSize == 0 {
resultSize = DefaultResultSize
}
if radius == 0 {
radius = DefaultRadius
}
autoComplete := AutoCompleteTrie{
alphabetMin: int(min),
alphabetMax: int(max),
alphabetSize: int(max) - int(min) + 1,
resultSize: int(resultSize),
radius: int(radius),
newWords: make(map[string]byte),
removedWords: make(map[string]byte),
}
f, err := os.Open(dictionaryFileName)
defer f.Close()
if err != nil {
return nAc, err
}
lineScanner := bufio.NewScanner(f)
autoComplete.root = &trieNode{
links: make([]*trieNode, autoComplete.alphabetSize),
}
for lineScanner.Scan() {
line := lineScanner.Text()
err := autoComplete.put(line)
if err != nil {
return nAc, err
}
}
return autoComplete, nil
}
// Accept : See description in AutoComplete interface
func (autoComplete *AutoCompleteTrie) Accept(acceptedWord string) error {
acceptedWordInts, err := autoComplete.runesToInts(acceptedWord)
if err != nil {
return err
}
node := autoComplete.root
for _, c := range acceptedWordInts {
if node.links[c-autoComplete.alphabetMin] == nil {
return errors.New("Word " + acceptedWord + " not in dictionary")
}
node = node.links[c-autoComplete.alphabetMin]
}
node.accepts++
return nil
}
func (autoComplete *AutoCompleteTrie) runesToInts(word string) ([]int, error) {
runes := []rune(word)
var conv []int
for _, r := range runes {
i := int(r)
if i < autoComplete.alphabetMin || i > autoComplete.alphabetMax {
return nil, errors.New("illegal char in word - " + string(r))
}
conv = append(conv, i)
}
return conv, nil
}
// Learn : see interface
func (autoComplete *AutoCompleteTrie) Learn(word string) error {
conv, err := autoComplete.runesToInts(word)
if err != nil {
return err
}
autoComplete.putIter(conv)
autoComplete.newWords[word] = 0
return nil
}
func (autoComplete *AutoCompleteTrie) put(word string) error {
conv, err := autoComplete.runesToInts(word)
if err != nil {
return err
}
autoComplete.putIter(conv)
return nil
}
func (autoComplete *AutoCompleteTrie) putIter(intVals []int) {
node := autoComplete.root
for i, c := range intVals {
if node.links[c-autoComplete.alphabetMin] == nil {
newNode := trieNode{
intRune: c,
links: make([]*trieNode, autoComplete.alphabetSize),
}
if i == len(intVals)-1 {
newNode.isWord = true
}
node.links[c-autoComplete.alphabetMin] = &newNode
node = &newNode
continue
}
node = node.links[c-autoComplete.alphabetMin]
if i == len(intVals)-1 {
node.isWord = true
}
}
}
// UnLearn : : See description in AutoComplete interface
func (autoComplete *AutoCompleteTrie) UnLearn(word string) error {
conv, err := autoComplete.runesToInts(word)
if err != nil {
return err
}
autoComplete.remove(conv)
if _, contains := autoComplete.newWords[word]; !contains {
autoComplete.removedWords[word] = 0
} else {
delete(autoComplete.newWords, word)
}
return nil
}
func (autoComplete *AutoCompleteTrie) remove(intVals []int) {
node := autoComplete.root
lifo := lIFO{}
for _, c := range intVals {
if node.links[c-autoComplete.alphabetMin] == nil {
return
}
lifo.push(node)
node = node.links[c-autoComplete.alphabetMin]
}
if !node.isWord {
return
}
isLeaf := true
for _, link := range node.links {
if link != nil {
isLeaf = false
break
}
}
if !isLeaf {
node.isWord = false
return
}
node.isWord = false
for lifo.size() > 0 {
parentNode := lifo.pop()
parentNode.links[node.intRune-autoComplete.alphabetMin] = nil
if parentNode.isWord {
return
}
for _, link := range parentNode.links {
if link != nil {
return
}
}
node = parentNode
}
}
// Complete : see description in Autocomplete interface
func (autoComplete *AutoCompleteTrie) Complete(word string) ([]string, error) {
ints, err := autoComplete.runesToInts(word)
if err != nil {
return nil, err
}
return autoComplete.complete(word, ints), nil
}
func (autoComplete *AutoCompleteTrie) complete(word string, intRunes []int) []string {
wordEnd := autoComplete.root
for _, c := range intRunes {
wordEnd = wordEnd.links[c-autoComplete.alphabetMin]
if wordEnd == nil {
return []string{}
}
}
words := sOLILI{}
fifo := fIFO{}
stem := []rune(word)
stem = stem[:len(stem)-1]
fifo.add(branch{
node: wordEnd,
parent: &stem,
})
results := 0
for fifo.size() > 0 {
if results == autoComplete.resultSize {
break
}
nodeBranch := fifo.remove()
if nodeBranch.node.isWord {
words.insert(string(append(*nodeBranch.parent, rune(nodeBranch.node.intRune))), nodeBranch.node.accepts)
results++
}
links := nodeBranch.node.links
if len(*nodeBranch.parent) < autoComplete.radius-1 {
parentString := make([]rune, len(*nodeBranch.parent)+1)
copy(parentString, *nodeBranch.parent)
parentString[len(parentString)-1] = rune(nodeBranch.node.intRune)
for _, link := range links {
if link != nil {
rightBranch := branch{
node: link,
parent: &parentString,
}
fifo.add(rightBranch)
}
}
}
}
return words.flush()
}
type branch struct {
node *trieNode
parent *[]rune
}
type fIFO struct {
slice []branch
}
func (fifo *fIFO) add(b branch) {
fifo.slice = append(fifo.slice, b)
}
func (fifo *fIFO) remove() branch {
b := fifo.slice[0]
fifo.slice = fifo.slice[1:]
return b
}
func (fifo *fIFO) size() int {
return len(fifo.slice)
}
type lIFO struct {
slice []*trieNode
}
func (lifo *lIFO) push(n *trieNode) {
lifo.slice = append(lifo.slice, n)
}
func (lifo *lIFO) pop() *trieNode {
node := lifo.slice[len(lifo.slice)-1]
lifo.slice = lifo.slice[:len(lifo.slice)-1]
return node
}
func (lifo *lIFO) size() int {
return len(lifo.slice)
}
// Save : see description in AutoComplete interface
func (autoComplete *AutoCompleteTrie) Save(fileName string) error {
f, err := os.OpenFile(fileName, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0755)
if err != nil {
return err
}
enc := gob.NewEncoder(f)
fifo := fIFO{}
var nSlice []rune
fifo.add(branch{
node: autoComplete.root,
parent: &nSlice,
})
for fifo.size() > 0 {
nodeBranch := fifo.remove()
if nodeBranch.node.isWord {
currWord := string(append(*nodeBranch.parent, rune(nodeBranch.node.intRune)))
if nodeBranch.node.accepts > 0 {
enc.Encode(wordAccepts{
currWord,
nodeBranch.node.accepts,
})
} else if _, exists := autoComplete.newWords[currWord]; exists {
enc.Encode(wordAccepts{
currWord,
nodeBranch.node.accepts,
})
}
}
links := nodeBranch.node.links
var parentString []rune
if *nodeBranch.parent != nil {
parentString = make([]rune, len(*nodeBranch.parent)+1)
copy(parentString, *nodeBranch.parent)
parentString[len(parentString)-1] = rune(nodeBranch.node.intRune)
} else {
parentString = []rune{}
}
for _, link := range links {
if link != nil {
rightBranch := branch{
node: link,
parent: &parentString,
}
fifo.add(rightBranch)
}
}
}
for w := range autoComplete.removedWords {
enc.Encode(wordAccepts{
w,
-1,
})
}
return f.Close()
}
// Retrieve : see description in AutoComplete interface
func (autoComplete *AutoCompleteTrie) Retrieve(fileName string) error {
f, err := os.Open(fileName)
defer f.Close()
if err != nil {
return err
}
dec := gob.NewDecoder(f)
for {
var wA wordAccepts
if err = dec.Decode(&wA); err == io.EOF {
break
} else if err != nil {
return err
}
runesAsInts, err := autoComplete.runesToInts(wA.Word)
if err != nil {
return err
}
node := autoComplete.root
for _, c := range runesAsInts {
if node.links[c-autoComplete.alphabetMin] == nil {
err = autoComplete.Learn(wA.Word)
if err != nil {
return err
}
}
node = node.links[c-autoComplete.alphabetMin]
}
if wA.Accepts > 0 {
if err = autoComplete.updateAccepts(runesAsInts, wA.Accepts); err != nil {
return err
}
} else if wA.Accepts < 0 {
autoComplete.UnLearn(wA.Word)
}
}
return nil
}
func (autoComplete *AutoCompleteTrie) updateAccepts(word []int, accepts int) error {
node := autoComplete.root
for _, c := range word {
if node.links[c-autoComplete.alphabetMin] == nil {
return errors.New("Word not found")
}
node = node.links[c-autoComplete.alphabetMin]
}
node.accepts = accepts
return nil
}
func minMax(runes []rune) (rune, rune) {
if len(runes) == 1 {
return runes[0], runes[0]
}
var min, max rune
if runes[0] > runes[1] {
max = runes[0]
min = runes[1]
} else {
max = runes[1]
min = runes[0]
}
for i := 2; i < len(runes); i++ {
if runes[i] > max {
max = runes[i]
} else if runes[i] < min {
min = runes[i]
}
}
return min, max
}