-
Notifications
You must be signed in to change notification settings - Fork 0
/
trie_builder.go
148 lines (127 loc) · 3.14 KB
/
trie_builder.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
package trie
type TrieBuilder struct {
words []string
optimize bool
}
func NewTrie() *TrieBuilder {
return &TrieBuilder{
optimize: true,
}
}
// AddWord adds new word to list of words we will be searching for
func (tb *TrieBuilder) AddWord(word string) *TrieBuilder {
if len(word) != 0 {
tb.words = append(tb.words, word)
}
return tb
}
// AddWord adds new word to list of words we will be searching for
func (tb *TrieBuilder) WithWords(words ...string) *TrieBuilder {
for _, word := range words {
if len(word) != 0 {
tb.words = append(tb.words, word)
}
}
return tb
}
func (tb *TrieBuilder) Optimize(optimize bool) *TrieBuilder {
tb.optimize = optimize
return tb
}
// Build returns build search trie
func (tb *TrieBuilder) Build() *Trie {
var trie *Trie = &Trie{
root: newNode(),
words: tb.words,
}
//Build our trie
for indx, word := range trie.words {
insertWord(trie, word, indx)
}
if tb.optimize {
tb.optimizeSkiping(trie)
}
return trie
}
func insertWord(trie *Trie, word string, indx int) {
currentNode := trie.root
for _, char := range []byte(word) {
if node := currentNode.FindChild(char); node != nil {
currentNode = node
} else {
// Missing node, create one and add it to children.
currentNode = currentNode.AddChild(char)
}
}
//Last visited node is end of our word
currentNode.Word = indx
}
// Calculates skipping, might take long time
func (tb *TrieBuilder) optimizeSkiping(trie *Trie) {
// Calculate for regular words.
for _, word := range trie.words {
calculateForWord(trie, word)
}
}
func calculateForWord(trie *Trie, word string) {
charBytes := []byte(word)
//Small words are not interesting
if len(charBytes) <= 1 {
return
}
calculatedSkips := make([]int, len(charBytes))
for j := len(charBytes); j >= 2; j -= 1 {
charactersToSkip := 1
for i := 1; i < j-1; i += 1 {
sliceLength := j - i
matchedBytes, matched := lookup(trie, charBytes[i:j])
// When matched is true: we found submatch along the way.
// When matchedBytes == sliceLength, but matched is false: we can safely say that there is another word depeper in tree
if matched || matchedBytes == sliceLength {
// Skip by our current offset
charactersToSkip = i
break
} else {
// No equivalent of this path in our trie.
// We can definetly skip first byte.
charactersToSkip = i + 1
}
}
calculatedSkips[j-1] = charactersToSkip
// There is no point to look further down.
if charactersToSkip == 1 {
for indx := 0; indx < j; indx += 1 {
calculatedSkips[indx] = 1
}
break
}
}
applySkips(trie, charBytes, calculatedSkips)
}
func lookup(trie *Trie, part []byte) (int, bool) {
matched := false
currentNode := trie.root
i := 0
for ; i < len(part); i++ {
if node := currentNode.FindChild(part[i]); node != nil {
if node.Word != -1 {
matched = true
}
currentNode = node
} else {
break
}
}
return i, matched
}
func applySkips(trie *Trie, word []byte, skips []int) {
currentNode := trie.root
for i := 0; i < len(word); i++ {
if node := currentNode.FindChild(word[i]); node != nil {
node.Skip = skips[i]
currentNode = node
} else {
break
}
}
}