-
Notifications
You must be signed in to change notification settings - Fork 0
/
smac_engine_lino.go
396 lines (340 loc) · 10 KB
/
smac_engine_lino.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
// Copyright Piero de Salvia.
// All Rights Reserved
package smac
import (
"bufio"
"encoding/gob"
"errors"
"io"
"os"
"sort"
"strings"
)
type liNo struct {
accepts int
next string
}
// AutoCompleteLiNo is a "list node" implementation of AutoComplete
type AutoCompleteLiNo struct {
wordMap map[string]*liNo
head string
tail string
resultSize int
radius int
removedWords map[string]bool
newWords map[string]bool
prefixMap map[string]string
prefixMapDepth int
}
// NewAutoCompleteLinoE returns a new, empty autocompleter.
//
// prefixMapDepth is an index of performance (the higher the better). It should be comprised between 1 and 4.
//
// resultSize is the number of hits returned. If 0 is used, it defaults to DEF_RESULTS_SIZE
//
// radius is the depth of the scan the engine will perform 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 NewAutoCompleteLinoE(prefixMapDepth, resultSize, radius uint) (AutoCompleteLiNo, error) {
return NewAutoCompleteLinoS([]string{}, prefixMapDepth, resultSize, radius)
}
// NewAutoCompleteLinoF returns a new autocompleter.
//
// 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 NewAutoCompleteLinoF(dictionaryFileName string, prefixMapDepth, resultSize, radius uint) (AutoCompleteLiNo, error) {
var nAc AutoCompleteLiNo
f, err := os.Open(dictionaryFileName)
defer f.Close()
if err != nil {
return nAc, err
}
lineScanner := bufio.NewScanner(f)
var dictionary []string
for lineScanner.Scan() {
word := lineScanner.Text()
if len(word) == 0 {
return nAc, errors.New("Empty word in dictionary")
}
dictionary = append(dictionary, word)
}
return NewAutoCompleteLinoS(dictionary, prefixMapDepth, resultSize, radius)
}
func makePrefixMap(sortedDictionary []string, maxDepth int) map[string]string {
prefixes := make(map[string]string)
for _, word := range sortedDictionary {
runes := []rune(word)
for depth := 1; depth <= maxDepth && depth <= len(runes); depth++ {
prefix := string(runes[:depth])
if _, exists := prefixes[prefix]; !exists {
prefixes[prefix] = word
}
}
}
return prefixes
}
// NewAutoCompleteLinoS returns a new autocompleter.
//
// dictionary is a slice of words to be used for completion.
//
// prefixMapDepth is an index of performance (the higher the better). It should be comprised between 1 and 4.
//
// 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 NewAutoCompleteLinoS(dictionary []string, prefixMapDepth, resultSize, radius uint) (AutoCompleteLiNo, error) {
var nAc AutoCompleteLiNo
if resultSize > radius {
return nAc, errors.New("resultSize > radius")
}
if resultSize == 0 {
resultSize = DefaultResultSize
}
if radius == 0 {
radius = DefaultRadius
}
autoComplete := AutoCompleteLiNo{
wordMap: make(map[string]*liNo),
resultSize: int(resultSize),
radius: int(radius),
newWords: make(map[string]bool),
removedWords: make(map[string]bool),
}
sort.Strings(dictionary)
var linop *liNo
for _, word := range dictionary {
newLinop := &liNo{}
autoComplete.wordMap[word] = newLinop
if linop != nil {
linop.next = word
}
linop = newLinop
}
if len(dictionary) > 0 {
autoComplete.head = dictionary[0]
autoComplete.tail = dictionary[len(dictionary)-1]
}
autoComplete.prefixMap = makePrefixMap(dictionary, int(prefixMapDepth))
autoComplete.prefixMapDepth = int(prefixMapDepth)
return autoComplete, nil
}
// Complete : see description in AutoComplete interface
func (autoComplete *AutoCompleteLiNo) Complete(stem string) ([]string, error) {
result := sOLILI{}
hits := 0
//radius := 0
lino, hit := autoComplete.wordMap[stem]
if hit {
hits++
result.insert(stem, lino.accepts)
} else {
subStem := stem
prefixRoot, prefixExists := autoComplete.prefixMap[subStem]
for !prefixExists && len(subStem) > 0 {
subStem = subStem[:len(subStem)-1]
prefixRoot, prefixExists = autoComplete.prefixMap[subStem]
}
if prefixExists {
searchPtr := prefixRoot
for !strings.HasPrefix(searchPtr, stem) {
searchPtr = autoComplete.wordMap[searchPtr].next
if searchPtr == "" || !strings.HasPrefix(searchPtr, subStem) {
return []string{}, nil
}
}
hit = true
lino = autoComplete.wordMap[searchPtr]
if _, prefixRootIsWord := autoComplete.wordMap[searchPtr]; prefixRootIsWord {
hits++
result.insert(searchPtr, lino.accepts)
}
}
}
for hit && hits < autoComplete.radius {
word := lino.next
hit = strings.HasPrefix(word, stem)
if hit {
lino = autoComplete.wordMap[word]
hits++
result.insert(word, lino.accepts)
}
}
return result.flushL(autoComplete.resultSize), nil
}
// Accept : see description in AutoComplete interface
func (autoComplete *AutoCompleteLiNo) Accept(acceptedWord string) error {
var lino *liNo
var exist bool
if lino, exist = autoComplete.wordMap[acceptedWord]; !exist {
return errors.New("Word to be accepted not found")
}
lino.accepts++
return nil
}
// Learn : see description in AutoComplete interface
func (autoComplete *AutoCompleteLiNo) Learn(word string) error {
if _, exists := autoComplete.wordMap[word]; exists {
return errors.New("Word already in dictionary")
}
prevWord := autoComplete.findPreviousWord(word)
prevLino := autoComplete.wordMap[prevWord]
newLino := &liNo{}
autoComplete.wordMap[word] = newLino
if prevWord != autoComplete.head {
newLino.next = prevLino.next
prevLino.next = word
} else { // we're at head
if autoComplete.head != "" {
if word > autoComplete.head {
newLino.next = autoComplete.wordMap[autoComplete.head].next
autoComplete.wordMap[autoComplete.head].next = word
} else {
newLino.next = autoComplete.head
}
}
}
if autoComplete.head > word || autoComplete.head == "" {
autoComplete.head = word
}
if autoComplete.tail < word {
autoComplete.tail = word
}
for i := 1; i <= autoComplete.prefixMapDepth && i <= len(word); i++ {
prefix := word[:i]
if _, exists := autoComplete.prefixMap[prefix]; !exists {
autoComplete.prefixMap[prefix] = word
} else {
if word < autoComplete.prefixMap[prefix] {
autoComplete.prefixMap[prefix] = word
}
}
}
autoComplete.newWords[word] = true
return nil
}
// UnLearn : see description in AutoComplete interface
func (autoComplete *AutoCompleteLiNo) UnLearn(word string) error {
if _, exists := autoComplete.wordMap[word]; !exists {
return errors.New("Word not in dictionary")
}
prevWord := autoComplete.findPreviousWord(word)
var nextWord string
prevLino := autoComplete.wordMap[prevWord]
if autoComplete.wordMap[word].next == "" {
prevLino.next = ""
} else {
nextWord = autoComplete.wordMap[word].next
prevLino.next = nextWord
}
delete(autoComplete.wordMap, word)
for i := 1; i <= autoComplete.prefixMapDepth && i < len(word); i++ {
prefix := word[:i]
if _, exists := autoComplete.prefixMap[prefix]; exists {
if autoComplete.prefixMap[prefix] == word {
// does next word start with prefix? if yes, assign, otherwise prefix is gone
if strings.HasPrefix(nextWord, prefix) {
autoComplete.prefixMap[prefix] = nextWord
} else {
delete(autoComplete.prefixMap, prefix)
}
}
}
}
if autoComplete.head == word {
autoComplete.head = nextWord
}
if autoComplete.tail == word {
autoComplete.tail = prevWord
}
if _, contains := autoComplete.newWords[word]; !contains {
autoComplete.removedWords[word] = true
} else {
delete(autoComplete.newWords, word)
}
return nil
}
func (autoComplete *AutoCompleteLiNo) findPreviousWord(word string) string {
prefix := word[:len(word)-1]
searchPtr, prefixExists := autoComplete.prefixMap[prefix]
for len(prefix) > 0 && (!prefixExists || word <= searchPtr) {
prefix = prefix[:len(prefix)-1]
searchPtr, prefixExists = autoComplete.prefixMap[prefix]
}
// find the longest prefix present in prefixMap
if searchPtr == "" { // prefix not found
searchPtr = autoComplete.head
}
prevWord := searchPtr
// now scan from longest prefix ptr until next word in dictionary is found
for searchPtr != "" && searchPtr < word {
prevWord = searchPtr
searchPtr = autoComplete.wordMap[searchPtr].next
}
return prevWord
}
// Save : see description in AutoComplete interface
func (autoComplete *AutoCompleteLiNo) 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)
for w, liNo := range autoComplete.wordMap {
if liNo.accepts > 0 {
enc.Encode(wordAccepts{
w,
liNo.accepts,
})
} else if _, exists := autoComplete.newWords[w]; exists {
enc.Encode(wordAccepts{
w,
liNo.accepts,
})
}
}
for w := range autoComplete.removedWords {
enc.Encode(wordAccepts{
w,
-1,
})
}
return f.Close()
}
// Retrieve : see description in AutoComplete interface
func (autoComplete *AutoCompleteLiNo) 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
}
if _, exists := autoComplete.wordMap[wA.Word]; !exists {
err = autoComplete.Learn(wA.Word)
if err != nil {
return err
}
}
if wA.Accepts > 0 {
l := autoComplete.wordMap[wA.Word]
l.accepts = wA.Accepts
} else if wA.Accepts < 0 {
autoComplete.UnLearn(wA.Word)
}
}
return nil
}