-
Notifications
You must be signed in to change notification settings - Fork 6
/
trie.go
317 lines (289 loc) · 9.75 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
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
package trie
import (
"sort"
"strings"
"unicode"
"unicode/utf8"
"golang.org/x/text/runes"
"golang.org/x/text/transform"
"golang.org/x/text/unicode/norm"
)
const (
shortStringLevenshteinLimit uint8 = 0
mediumStringLevenshteinLimit uint8 = 1
longStringLevenshteinLimit uint8 = 2
shortStringThreshold uint8 = 0
mediumStringThreshold uint8 = 3
longStringThreshold uint8 = 5
)
// Trie is a data structure for storing common prefixes to strings for efficient comparison
// and retrieval.
type Trie struct {
root *node
fuzzy, normalised, caseSensitive bool
levenshteinScheme map[uint8]uint8
levenshteinIntervals []uint8
// originalDict is a mapping of normalised to original string.
originalDict map[string][]string
}
// node is a node in a Trie which contains a map of runes to more node pointers
// if word is non-empty, this indicates that the node defines the end of a word
type node struct {
children map[rune]*node
word string
}
type score struct {
levenshtein uint8
fuzzy bool
}
// New creates a new empty trie. By default fuzzy search is on and string normalisation is on.
// The default levenshtein scheme is on, where search strings of len 1-2 characters allow no
// distance, search strings of length 3-4 allow a levenshtein distance of 1, and search strings
// of length 5 or more runes allow a levenshtein distance of two.
func New() *Trie {
t := new(Trie)
t.root = new(node)
t.root.children = make(map[rune]*node)
t.originalDict = make(map[string][]string)
t.WithFuzzy()
t.WithNormalisation()
t.DefaultLevenshtein()
t.CaseInsensitive()
return t
}
// WithFuzzy sets the Trie to use fuzzy matching on search.
func (t *Trie) WithFuzzy() *Trie {
t.fuzzy = true
return t
}
// WithoutFuzzy sets the Trie not to use fuzzy matching on search.
func (t *Trie) WithoutFuzzy() *Trie {
t.fuzzy = false
return t
}
// WithNormalisation sets the Trie to use normalisation on search.
// For example, Jurg will find Jürgen, Jürg will find Jurgen.
func (t *Trie) WithNormalisation() *Trie {
t.normalised = true
return t
}
// WithoutNormalisation sets the Trie not to use normalisation on search.
// for example Jurg won't find Jürgen, Jürg won't find Jurgen.
func (t *Trie) WithoutNormalisation() *Trie {
t.normalised = false
return t
}
// CaseSensitive sets the Trie to use case sensitive search.
func (t *Trie) CaseSensitive() *Trie {
t.caseSensitive = true
return t
}
// CaseInsensitive sets the Trie to use case insensitive search.
func (t *Trie) CaseInsensitive() *Trie {
t.caseSensitive = false
return t
}
// WithoutLevenshtein sets the Trie not to allow any levenshtein distance between
// between the search string and any matches.
func (t *Trie) WithoutLevenshtein() *Trie {
t.levenshteinScheme = map[uint8]uint8{0: 0}
t.levenshteinIntervals = []uint8{0}
return t
}
// DefaultLevenshtein sets the trie to use the default levenshtein scheme.
func (t *Trie) DefaultLevenshtein() *Trie {
t.levenshteinScheme = map[uint8]uint8{
shortStringThreshold: shortStringLevenshteinLimit,
mediumStringThreshold: mediumStringLevenshteinLimit,
longStringThreshold: longStringLevenshteinLimit}
t.levenshteinIntervals = []uint8{longStringThreshold, mediumStringThreshold, longStringThreshold}
return t
}
// CustomLevenshtein sets up a custom levenshtein scheme.
// WARNING, this function will panic if the scheme is invalid.
// A valid scheme is a series of pairs of search string length -> levenshtein distance.
// There must be one entry with zero as search string length.
func (t *Trie) CustomLevenshtein(scheme map[uint8]uint8) *Trie {
_, ok := scheme[0]
if !ok {
panic("invalid levenshtein scheme for GAT")
}
t.levenshteinIntervals = make([]uint8, 0, len(scheme))
for key := range scheme {
t.levenshteinIntervals = append(t.levenshteinIntervals, key)
}
sort.Slice(t.levenshteinIntervals, func(i, j int) bool {
return t.levenshteinIntervals[i] > t.levenshteinIntervals[j]
})
t.levenshteinScheme = scheme
return t
}
// Insert inserts strings into the Trie
func (t *Trie) Insert(entries ...string) {
transformer := transform.Chain(norm.NFD, runes.Remove(runes.In(unicode.Mn)), norm.NFC)
for _, entry := range entries {
if len(entry) == 0 {
continue
}
switch {
case t.normalised && t.caseSensitive:
normal, _, err := transform.String(transformer, entry)
if err != nil {
continue
}
t.originalDict[normal] = append(t.originalDict[normal], entry)
entry = normal
case t.normalised && !t.caseSensitive:
normal, _, err := transform.String(transformer, entry)
if err != nil {
continue
}
normal = strings.ToLower(normal)
t.originalDict[normal] = append(t.originalDict[normal], entry)
entry = normal
case !t.normalised && !t.caseSensitive:
lower := strings.ToLower(entry)
t.originalDict[lower] = append(t.originalDict[lower], entry)
entry = lower
}
currentNode := t.root
for index, character := range entry {
child, ok := currentNode.children[character]
if !ok {
child = new(node)
child.children = make(map[rune]*node)
if index == len(entry)-len(string(character)) {
child.word = entry
}
currentNode.children[character] = child
}
currentNode = child
}
}
}
// SearchAll is just like Search, but without a limit.
func (t *Trie) SearchAll(search string) []string {
return t.Search(search, 0)
}
// Search will return all complete words in the trie that have the search string as a prefix,
// taking into account the Trie's settings for normalisation, fuzzy matching and levenshtein distance scheme.
func (t *Trie) Search(search string, limit int) []string {
if len(search) == 0 {
return []string{}
}
if t.normalised {
transformer := transform.Chain(norm.NFD, runes.Remove(runes.In(unicode.Mn)), norm.NFC)
var err error
search, _, err = transform.String(transformer, search)
if err != nil {
return []string{}
}
}
if !t.caseSensitive {
search = strings.ToLower(search)
}
maxDistance := t.maxDistance(search)
// start the recursive function
collection := make(map[string]score)
t.collect(collection, search, t.root, 0, maxDistance, limit, t.fuzzy, false)
hits := make([]string, 0, len(collection))
for key := range collection {
hits = append(hits, key)
}
sort.Slice(hits, func(i, j int) bool {
switch {
case collection[hits[i]].levenshtein != collection[hits[j]].levenshtein:
return collection[hits[i]].levenshtein < collection[hits[j]].levenshtein
case collection[hits[i]].fuzzy && !collection[hits[j]].fuzzy:
return false
case !collection[hits[i]].fuzzy && collection[hits[j]].fuzzy:
return true
default:
return hits[i] < hits[j]
}
})
if len(hits) >= limit && limit != 0 {
return hits[:limit]
}
if !t.normalised && t.caseSensitive {
return hits
}
originals := make([]string, 0, len(hits)*2)
for _, hit := range hits {
originals = append(originals, t.originalDict[hit]...)
}
return originals
}
// collect is a recursive function that traverses the Trie and inserts words from Word-final nodes which match the search
// text in the map collection. It handles substitution, insertion and deletion to the levenshtein distance limit and also
// allows fuzzy search.
func (t *Trie) collect(collection map[string]score, word string, node *node, distance, maxDistance uint8, limit int, fuzzyAllowed, fuzzyUsed bool) {
if len(word) == 0 {
if node.word != "" {
previousScore, ok := collection[node.word]
if !ok || distance < previousScore.levenshtein ||
(distance == previousScore.levenshtein && previousScore.fuzzy && !fuzzyUsed) {
collection[node.word] = score{levenshtein: distance, fuzzy: fuzzyUsed}
}
node.collectAllDescendentWords(collection, distance, fuzzyUsed)
return
}
node.collectAllDescendentWords(collection, distance, fuzzyUsed)
}
character, size := utf8.DecodeRuneInString(word)
subword := word[size:]
// special rune for string collisions
if character == '*' {
t.collect(collection, subword, node, distance, maxDistance, limit, false, fuzzyUsed)
}
if next := node.children[character]; next != nil {
t.collect(collection, subword, next, distance, maxDistance, limit, false, fuzzyUsed)
}
if distance < maxDistance {
distance++
for character, next := range node.children {
// Substition
t.collect(collection, string(character)+subword, node, distance, maxDistance, limit, false, fuzzyUsed)
// Insertion
t.collect(collection, string(character)+word, node, distance, maxDistance, limit, false, fuzzyUsed)
// Fuzzy
if fuzzyAllowed {
t.collect(collection, word, next, distance-1, maxDistance, limit, true, true)
}
}
// Deletion
t.collect(collection, subword, node, distance, maxDistance, limit, false, false)
} else if distance == 0 {
for _, next := range node.children {
// Fuzzy without levenshtein
if fuzzyAllowed {
t.collect(collection, word, next, distance, maxDistance, limit, true, true)
}
}
}
}
// collectAllDescendentWords returns the words from all nodes that are descedent of the current node.
func (n *node) collectAllDescendentWords(collection map[string]score, distance uint8, fuzzyUsed bool) {
for _, node := range n.children {
if node.word != "" {
previousScore, ok := collection[node.word]
if !ok || distance < previousScore.levenshtein ||
(distance == previousScore.levenshtein && previousScore.fuzzy && !fuzzyUsed) {
collection[node.word] = score{levenshtein: distance, fuzzy: fuzzyUsed}
}
}
node.collectAllDescendentWords(collection, distance, fuzzyUsed)
}
}
// maxDistance determines the maximum levenshein distance based on the levenshtein scheme
// and search string length.
func (t *Trie) maxDistance(search string) (maxDistance uint8) {
runes := []rune(search)
for _, limit := range t.levenshteinIntervals {
if len(runes) >= int(limit) {
maxDistance = t.levenshteinScheme[limit]
return
}
}
return
}