-
Notifications
You must be signed in to change notification settings - Fork 1
/
trie.go
184 lines (152 loc) · 3.78 KB
/
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
package LevenshteinTrie
import (
"fmt"
"sort"
"strings"
"unicode/utf8"
)
func Min(a ...int) int {
min := int(^uint(0) >> 1) // largest int
for _, i := range a {
if i < min {
min = i
}
}
return min
}
func Max(a ...int) int {
max := int(0)
for _, i := range a {
if i > max {
max = i
}
}
return max
}
type TrieNode struct {
letter rune //Equivalent to int32
children map[rune]*TrieNode
final bool
text string
}
func (t *TrieNode) String() string {
s := fmt.Sprintf("%#U\n", t.letter)
for _, v := range t.children {
s += fmt.Sprintf("-%#U\n", v)
}
return s
}
func NewTrie() *TrieNode {
return &TrieNode{children: make(map[rune]*TrieNode)}
}
func (root *TrieNode) InsertText(text string) {
if root == nil {
return
}
text = strings.ToLower(text)
currNode := root //Starts at root
for i, w := 0, 0; i < len(text); i += w {
runeValue, width := utf8.DecodeRuneInString(text[i:])
final := false
if width+i == len(text) {
final = true
}
w = width
currNode = NewTrieNode(currNode, runeValue, final, text)
}
}
func NewTrieNode(t *TrieNode, runeValue rune, final bool, text string) *TrieNode {
node, exists := t.children[runeValue]
if !exists {
node = &TrieNode{letter: runeValue, children: make(map[rune]*TrieNode)}
t.children[runeValue] = node
}
if final {
node.final = true
node.text = text
}
return node
}
func (t *TrieNode) Suffix(query string) []string {
var curr *TrieNode
var ok bool
curr = t
//first, find the end of the prefix
for _, letter := range query {
if curr != nil {
curr, ok = curr.children[letter]
if ok {
//do nothing
}
} else {
return nil
}
}
candidates := getsuffixr(curr)
return candidates
}
func getsuffixr(n *TrieNode) []string {
if n == nil {
return nil
}
candidates := make([]string, 0)
if n.final == true {
candidates = append(candidates, n.text)
}
for _, childNode := range n.children {
candidates = append(candidates, getsuffixr(childNode)...)
}
return candidates
}
type QueryResult struct {
Val string
Distance int
}
func (q QueryResult) String() string {
return fmt.Sprintf("Val: %s, Dist: %d\n", q.Val, q.Distance)
}
type ByDistance []QueryResult
func (a ByDistance) Len() int { return len(a) }
func (a ByDistance) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByDistance) Less(i, j int) bool { return a[i].Distance < a[j].Distance }
func (n *TrieNode) Levenshtein(text string, distance int) []QueryResult {
//initialize the first row for the dynamic programming alg
l := utf8.RuneCount([]byte(text))
currentRow := make([]int, l+1)
for i := 0; i < len(currentRow); i++ {
currentRow[i] = i
}
candidates := make([]QueryResult, 0)
for letter, childNode := range n.children {
candidates = append(candidates, searchlevr(childNode, currentRow, letter, []rune(text), distance)...)
}
sort.Sort(ByDistance(candidates))
return candidates
}
func searchlevr(n *TrieNode, prevRow []int, letter rune, text []rune, maxDistance int) []QueryResult {
columns := len(prevRow)
currentRow := make([]int, columns)
currentRow[0] = prevRow[0] + 1
for col := 1; col < columns; col++ {
if text[col-1] == letter {
currentRow[col] = prevRow[col-1]
continue
}
insertCost := currentRow[col-1] + 1
deleteCost := prevRow[col] + 1
replaceCost := prevRow[col-1] + 1
currentRow[col] = Min(insertCost, deleteCost, replaceCost)
}
candidates := make([]QueryResult, 0)
distance := currentRow[len(currentRow)-1]
if distance <= maxDistance && n.final == true {
candidates = append(candidates, QueryResult{Val: n.text, Distance: distance})
}
mi := Min(currentRow[1:]...)
if mi <= maxDistance {
for l, childNode := range n.children {
candidates = append(candidates, searchlevr(childNode, currentRow, l, text, maxDistance)...)
}
}
return candidates
}