-
Notifications
You must be signed in to change notification settings - Fork 0
/
prunningRadixTrie.go
298 lines (263 loc) · 8.14 KB
/
prunningRadixTrie.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
package prunningRadixTrie
import (
"fmt"
"math"
"sort"
"io"
"bufio"
"strconv"
"strings"
"os"
)
type Node struct {
Children []struct {
key string
node *Node
}
termFrequencyCount int64
termFrequencyCountChildMax int64
}
func NewNode(termFrequencyCount int64) *Node {
return &Node{
termFrequencyCount: termFrequencyCount,
}
}
type PruningRadixTrie struct {
termCount int64
termCountLoaded int64
trie *Node
}
func NewPruningRadixTrie() *PruningRadixTrie {
return &PruningRadixTrie{
trie: &Node{},
}
}
func (t *PruningRadixTrie) AddTerm(term string, termFrequencyCount int64) {
nodeList := make([]*Node, 0)
t.addTerm(t.trie, term, termFrequencyCount, 0, 0, &nodeList)
}
func (t *PruningRadixTrie) UpdateMaxCounts(nodeList []*Node, termFrequencyCount int64) {
for _, node := range nodeList {
if termFrequencyCount > node.termFrequencyCountChildMax {
node.termFrequencyCountChildMax = termFrequencyCount
}
}
}
func (t *PruningRadixTrie) addTerm(curr *Node, term string, termFrequencyCount int64, id int, level int, nodeList *[]*Node) {
*nodeList = append(*nodeList, curr)
common := 0
if curr.Children != nil {
for j := 0; j < len(curr.Children); j++ {
child := curr.Children[j].node
key := curr.Children[j].key
for i := 0; i < int(math.Min(float64(len(term)), float64(len(key)))); i++ {
if term[i] == key[i] {
common = i + 1
} else {
break
}
}
if common > 0 {
if common == len(term) && common == len(key) {
if child.termFrequencyCount == 0 {
t.termCount++
}
child.termFrequencyCount += termFrequencyCount
t.UpdateMaxCounts(*nodeList, child.termFrequencyCount)
} else if common == len(term) {
newChild := &Node{
termFrequencyCount: termFrequencyCount,
Children: []struct {
key string
node *Node
}{
{key[len(term):], child},
},
termFrequencyCountChildMax: int64(math.Max(float64(child.termFrequencyCountChildMax), float64(child.termFrequencyCount))),
}
t.UpdateMaxCounts(*nodeList, termFrequencyCount)
curr.Children[j] = struct {
key string
node *Node
}{term[:common], newChild}
sort.Slice(curr.Children, func(i, j int) bool {
return curr.Children[i].node.termFrequencyCountChildMax > curr.Children[j].node.termFrequencyCountChildMax
})
t.termCount++
} else if common == len(key) {
t.addTerm(child, term[common:], termFrequencyCount, id, level+1, nodeList)
} else {
newChild := &Node{
termFrequencyCount: termFrequencyCount,
Children: []struct {
key string
node *Node
}{
{key[common:], child},
{term[common:], &Node{termFrequencyCount: termFrequencyCount}},
},
termFrequencyCountChildMax: int64(math.Max(float64(child.termFrequencyCountChildMax), math.Max(float64(termFrequencyCount), float64(child.termFrequencyCount)))),
}
t.UpdateMaxCounts(*nodeList, termFrequencyCount)
curr.Children[j] = struct {
key string
node *Node
}{term[:common], newChild}
sort.Slice(curr.Children, func(i, j int) bool {
return curr.Children[i].node.termFrequencyCountChildMax > curr.Children[j].node.termFrequencyCountChildMax
})
t.termCount++
}
return
}
}
}
if curr.Children == nil {
curr.Children = []struct {
key string
node *Node
}{
{term, &Node{termFrequencyCount: termFrequencyCount}},
}
} else {
curr.Children = append(curr.Children, struct {
key string
node *Node
}{term, &Node{termFrequencyCount: termFrequencyCount}})
sort.Slice(curr.Children, func(i, j int) bool {
return curr.Children[i].node.termFrequencyCountChildMax > curr.Children[j].node.termFrequencyCountChildMax
})
}
t.termCount++
t.UpdateMaxCounts(*nodeList, termFrequencyCount)
}
func (t *PruningRadixTrie) FindAllChildTerms(prefix string, topK int, termFrequencyCountPrefix *int64, prefixString string, results *[]struct {
term string
termFrequencyCount int64
}, pruning bool) {
t.findAllChildTerms(prefix, t.trie, topK, termFrequencyCountPrefix, prefixString, results, nil, pruning)
}
func (t *PruningRadixTrie) findAllChildTerms(prefix string, curr *Node, topK int, termFrequencyCountPrefix *int64, prefixString string, results *[]struct {
term string
termFrequencyCount int64
}, file io.Writer, pruning bool) {
if pruning && topK > 0 && len(*results) == topK && curr.termFrequencyCountChildMax <= (*results)[topK-1].termFrequencyCount {
return
}
noPrefix := prefix == ""
if curr.Children != nil {
for _, child := range curr.Children {
key := child.key
node := child.node
if pruning && topK > 0 && len(*results) == topK && node.termFrequencyCount <= (*results)[topK-1].termFrequencyCount && node.termFrequencyCountChildMax <= (*results)[topK-1].termFrequencyCount {
if !noPrefix {
break
} else {
continue
}
}
if noPrefix || (len(key) >= len(prefix) && key[:len(prefix)] == prefix) {
if node.termFrequencyCount > 0 {
if prefix == key {
*termFrequencyCountPrefix = node.termFrequencyCount
}
if file != nil {
fmt.Fprintf(file, "%s%s\t%d\n", prefixString, key, node.termFrequencyCount)
} else if topK > 0 {
t.addTopKSuggestion(prefixString+key, node.termFrequencyCount, topK, results)
} else {
*results = append(*results, struct {
term string
termFrequencyCount int64
}{prefixString + key, node.termFrequencyCount})
}
}
if node.Children != nil && len(node.Children) > 0 {
t.findAllChildTerms("", node, topK, termFrequencyCountPrefix, prefixString+key, results, file, pruning)
}
if !noPrefix {
break
}
} else if len(prefix) >= len(key) && prefix[:len(key)] == key {
if node.Children != nil && len(node.Children) > 0 {
t.findAllChildTerms(prefix[len(key):], node, topK, termFrequencyCountPrefix, prefixString+key, results, file, pruning)
}
break
}
}
}
}
func (t *PruningRadixTrie) GetTopkTermsForPrefix(prefix string, topK int, pruning bool) ([]struct {
term string
termFrequencyCount int64
}, int64) {
results := make([]struct {
term string
termFrequencyCount int64
}, 0)
termFrequencyCountPrefix := int64(0)
t.FindAllChildTerms(prefix, topK, &termFrequencyCountPrefix, "", &results, pruning)
return results, termFrequencyCountPrefix
}
func (t *PruningRadixTrie) WriteTermsToFile(path string) {
if t.termCountLoaded == t.termCount {
return
}
file, err := os.Create(path)
if err != nil {
fmt.Println("Error creating file:", err)
return
}
defer file.Close()
prefixCount := int64(0)
t.findAllChildTerms("", t.trie, 0, &prefixCount, "", nil, file, true)
fmt.Printf("%d terms written.\n", t.termCount)
}
func (t *PruningRadixTrie) ReadTermsFromFile(path string) bool {
if _, err := os.Stat(path); os.IsNotExist(err) {
fmt.Println("Could not find file", path)
return false
}
file, err := os.Open(path)
if err != nil {
fmt.Println("Error opening file:", err)
return false
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
lineParts := strings.Split(line, "\t")
if len(lineParts) == 2 {
if count, err := strconv.ParseInt(lineParts[1], 10, 64); err == nil {
t.AddTerm(lineParts[0], count)
}
}
}
t.termCountLoaded = t.termCount
fmt.Printf("%d terms loaded.\n", t.termCount)
return true
}
func (t *PruningRadixTrie) addTopKSuggestion(term string, termFrequencyCount int64, topK int, results *[]struct {
term string
termFrequencyCount int64
}) {
if len(*results) < topK || termFrequencyCount >= (*results)[topK-1].termFrequencyCount {
index := sort.Search(len(*results), func(i int) bool {
return termFrequencyCount > (*results)[i].termFrequencyCount
})
newResult := struct {
term string
termFrequencyCount int64
}{term, termFrequencyCount}
*results = append(*results, struct {
term string
termFrequencyCount int64
}{})
copy((*results)[index+1:], (*results)[index:])
(*results)[index] = newResult
if len(*results) > topK {
*results = (*results)[:topK]
}
}
}