-
Notifications
You must be signed in to change notification settings - Fork 6
/
search.go
387 lines (361 loc) · 12.2 KB
/
search.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
package trie
import (
"container/heap"
"errors"
"math"
)
type EditOpType int
const (
EditOpTypeNoEdit EditOpType = iota
EditOpTypeInsert
EditOpTypeDelete
EditOpTypeReplace
)
// EditOp represents an Edit Operation.
type EditOp struct {
Type EditOpType
// KeyPart:
// - In case of NoEdit, KeyPart is to be retained.
// - In case of Insert, KeyPart is to be inserted in the key.
// - In case of Delete/Replace, KeyPart is the part of the key on which delete/replace is performed.
KeyPart string
// ReplaceWith is set for Type=EditOpTypeReplace
ReplaceWith string
}
type SearchResults struct {
Results []*SearchResult
heap *searchResultMaxHeap
tiebreakerCount int
}
type SearchResult struct {
// Key is the key that was Put() into the Trie.
Key []string
// Value is the value that was Put() into the Trie.
Value interface{}
// EditDistance is the number of edits (insert/delete/replace) needed to convert Key into the Search()-ed key.
EditDistance int
// EditOps is the list of edit operations (see EditOpType) needed to convert Key into the Search()-ed key.
EditOps []*EditOp
tiebreaker int
}
type SearchOptions struct {
// - WithExactKey
// - WithMaxResults
// - WithMaxEditDistance
// - WithEditOps
// - WithTopKLeastEdited
exactKey bool
maxResults bool
maxResultsCount int
editDistance bool
maxEditDistance int
editOps bool
topKLeastEdited bool
}
// WithExactKey can be passed to Search(). When passed, Search() returns just the result with
// Key=Search()-ed key. If the key does not exist, result list will be empty.
func WithExactKey() func(*SearchOptions) {
return func(so *SearchOptions) {
so.exactKey = true
}
}
// WithMaxResults can be passed to Search(). When passed, Search() will return at most maxResults
// number of results.
func WithMaxResults(maxResults int) func(*SearchOptions) {
if maxResults <= 0 {
panic(errors.New("invalid usage: maxResults must be greater than zero"))
}
return func(so *SearchOptions) {
so.maxResults = true
so.maxResultsCount = maxResults
}
}
// WithMaxEditDistance can be passed to Search(). When passed, Search() changes its default behaviour from
// Prefix search to Edit distance search. It can be used to return "Approximate" results instead of strict
// Prefix search results.
//
// maxDistance is the maximum number of edits allowed on Trie keys to consider them as a SearchResult.
// Higher the maxDistance, more lenient and slower the search becomes.
//
// e.g. If a Trie stores English words, then searching for "wheat" with maxDistance=1 might return similar
// looking words like "wheat", "cheat", "heat", "what", etc. With maxDistance=2 it might also return words like
// "beat", "ahead", etc.
//
// Read about Edit distance: https://en.wikipedia.org/wiki/Edit_distance
func WithMaxEditDistance(maxDistance int) func(*SearchOptions) {
if maxDistance <= 0 {
panic(errors.New("invalid usage: maxDistance must be greater than zero"))
}
return func(so *SearchOptions) {
so.editDistance = true
so.maxEditDistance = maxDistance
}
}
// WithEditOps can be passed to Search() alongside WithMaxEditDistance(). When passed, Search() also returns EditOps
// for each SearchResult. EditOps can be used to determine the minimum number of edit operations needed to convert
// a result Key into the Search()-ed key.
//
// e.g. Searching for "wheat" in a Trie that stores English words might return "beat". EditOps for this result might be:
// 1. insert "w" 2. replace "b" with "h".
//
// There might be multiple ways to edit a key into another. EditOps represents only one.
//
// Computing EditOps makes Search() slower.
func WithEditOps() func(*SearchOptions) {
return func(so *SearchOptions) {
so.editOps = true
}
}
// WithTopKLeastEdited can be passed to Search() alongside WithMaxEditDistance() and WithMaxResults(). When passed,
// Search() returns maxResults number of results that have the lowest EditDistances. Results are sorted on EditDistance
// (lowest to highest).
//
// e.g. In a Trie that stores English words searching for "wheat" might return "wheat" (EditDistance=0), "cheat" (EditDistance=1),
// "beat" (EditDistance=2) - in that order.
func WithTopKLeastEdited() func(*SearchOptions) {
return func(so *SearchOptions) {
so.topKLeastEdited = true
}
}
// Search() takes a key and some options to return results (see SearchResult) from the Trie.
// Without any options, it does a Prefix search i.e. result Keys have the same prefix as key.
// Order of the results is deterministic and will follow the order in which Put() was called for the keys.
// See "With*" functions for options accepted by Search().
func (t *Trie) Search(key []string, options ...func(*SearchOptions)) *SearchResults {
opts := &SearchOptions{}
for _, f := range options {
f(opts)
}
if opts.editOps && !opts.editDistance {
panic(errors.New("invalid usage: WithEditOps() must not be passed without WithMaxEditDistance()"))
}
if opts.topKLeastEdited && !opts.editDistance {
panic(errors.New("invalid usage: WithTopKLeastEdited() must not be passed without WithMaxEditDistance()"))
}
if opts.exactKey && opts.editDistance {
panic(errors.New("invalid usage: WithExactKey() must not be passed with WithMaxEditDistance()"))
}
if opts.exactKey && opts.maxResults {
panic(errors.New("invalid usage: WithExactKey() must not be passed with WithMaxResults()"))
}
if opts.topKLeastEdited && !opts.maxResults {
panic(errors.New("invalid usage: WithTopKLeastEdited() must not be passed without WithMaxResults()"))
}
if opts.editDistance {
return t.searchWithEditDistance(key, opts)
}
return t.search(key, opts)
}
func (t *Trie) searchWithEditDistance(key []string, opts *SearchOptions) *SearchResults {
// https://en.wikipedia.org/wiki/Levenshtein_distance#Iterative_with_full_matrix
// http://stevehanov.ca/blog/?id=114
columns := len(key) + 1
newRow := make([]int, columns)
for i := 0; i < columns; i++ {
newRow[i] = i
}
m := len(key)
if m == 0 {
m = 1
}
rows := make([][]int, 1, m)
rows[0] = newRow
results := &SearchResults{}
if opts.topKLeastEdited {
results.heap = &searchResultMaxHeap{}
}
keyColumn := make([]string, 1, m)
stop := false
// prioritize Node that has the same keyPart as key. this results in better results
// e.g. if key=national, build with Node(keyPart=n) first so that keys like notional, nation, nationally, etc. are prioritized
// same logic is used inside the recursive buildWithEditDistance() method
var prioritizedNode *Node
if len(key) > 0 {
if prioritizedNode = t.root.children[key[0]]; prioritizedNode != nil {
keyColumn[0] = prioritizedNode.keyPart
t.buildWithEditDistance(&stop, results, prioritizedNode, &keyColumn, &rows, key, opts)
}
}
for dllNode := t.root.childrenDLL.head; dllNode != nil; dllNode = dllNode.next {
node := dllNode.trieNode
if node == prioritizedNode {
continue
}
keyColumn[0] = node.keyPart
t.buildWithEditDistance(&stop, results, node, &keyColumn, &rows, key, opts)
}
if opts.topKLeastEdited {
n := results.heap.Len()
results.Results = make([]*SearchResult, n)
for n != 0 {
result := heap.Pop(results.heap).(*SearchResult)
result.tiebreaker = 0
results.Results[n-1] = result
n--
}
results.heap = nil
results.tiebreakerCount = 0
}
return results
}
func (t *Trie) buildWithEditDistance(stop *bool, results *SearchResults, node *Node, keyColumn *[]string, rows *[][]int, key []string, opts *SearchOptions) {
if *stop {
return
}
prevRow := (*rows)[len(*rows)-1]
columns := len(key) + 1
newRow := make([]int, columns)
newRow[0] = prevRow[0] + 1
for i := 1; i < columns; i++ {
replaceCost := 1
if key[i-1] == (*keyColumn)[len(*keyColumn)-1] {
replaceCost = 0
}
newRow[i] = min(
newRow[i-1]+1, // insertion
prevRow[i]+1, // deletion
prevRow[i-1]+replaceCost, // substitution
)
}
*rows = append(*rows, newRow)
if newRow[columns-1] <= opts.maxEditDistance && node.isTerminal {
editDistance := newRow[columns-1]
lazyCreate := func() *SearchResult { // optimization for the case where topKLeastEdited=true and the result should not be pushed to heap
resultKey := make([]string, len(*keyColumn))
copy(resultKey, *keyColumn)
result := &SearchResult{Key: resultKey, Value: node.value, EditDistance: editDistance}
if opts.editOps {
result.EditOps = t.getEditOps(rows, keyColumn, key)
}
return result
}
if opts.topKLeastEdited {
results.tiebreakerCount++
if results.heap.Len() < opts.maxResultsCount {
result := lazyCreate()
result.tiebreaker = results.tiebreakerCount
heap.Push(results.heap, result)
} else if (*results.heap)[0].EditDistance > editDistance {
result := lazyCreate()
result.tiebreaker = results.tiebreakerCount
heap.Pop(results.heap)
heap.Push(results.heap, result)
}
} else {
result := lazyCreate()
results.Results = append(results.Results, result)
if opts.maxResults && len(results.Results) == opts.maxResultsCount {
*stop = true
return
}
}
}
if min(newRow...) <= opts.maxEditDistance {
var prioritizedNode *Node
m := len(*keyColumn)
if m < len(key) {
if prioritizedNode = node.children[key[m]]; prioritizedNode != nil {
*keyColumn = append(*keyColumn, prioritizedNode.keyPart)
t.buildWithEditDistance(stop, results, prioritizedNode, keyColumn, rows, key, opts)
*keyColumn = (*keyColumn)[:len(*keyColumn)-1]
}
}
for dllNode := node.childrenDLL.head; dllNode != nil; dllNode = dllNode.next {
child := dllNode.trieNode
if child == prioritizedNode {
continue
}
*keyColumn = append(*keyColumn, child.keyPart)
t.buildWithEditDistance(stop, results, child, keyColumn, rows, key, opts)
*keyColumn = (*keyColumn)[:len(*keyColumn)-1]
}
}
*rows = (*rows)[:len(*rows)-1]
}
func (t *Trie) getEditOps(rows *[][]int, keyColumn *[]string, key []string) []*EditOp {
// https://gist.github.com/jlherren/d97839b1276b9bd7faa930f74711a4b6
ops := make([]*EditOp, 0, len(key))
r, c := len(*rows)-1, len((*rows)[0])-1
for r > 0 || c > 0 {
insertionCost, deletionCost, substitutionCost := math.MaxInt, math.MaxInt, math.MaxInt
if c > 0 {
insertionCost = (*rows)[r][c-1]
}
if r > 0 {
deletionCost = (*rows)[r-1][c]
}
if r > 0 && c > 0 {
substitutionCost = (*rows)[r-1][c-1]
}
minCost := min(insertionCost, deletionCost, substitutionCost)
if minCost == substitutionCost {
if (*rows)[r][c] > (*rows)[r-1][c-1] {
ops = append(ops, &EditOp{Type: EditOpTypeReplace, KeyPart: (*keyColumn)[r-1], ReplaceWith: key[c-1]})
} else {
ops = append(ops, &EditOp{Type: EditOpTypeNoEdit, KeyPart: (*keyColumn)[r-1]})
}
r -= 1
c -= 1
} else if minCost == deletionCost {
ops = append(ops, &EditOp{Type: EditOpTypeDelete, KeyPart: (*keyColumn)[r-1]})
r -= 1
} else if minCost == insertionCost {
ops = append(ops, &EditOp{Type: EditOpTypeInsert, KeyPart: key[c-1]})
c -= 1
}
}
for i, j := 0, len(ops)-1; i < j; i, j = i+1, j-1 {
ops[i], ops[j] = ops[j], ops[i]
}
return ops
}
func (t *Trie) search(prefixKey []string, opts *SearchOptions) *SearchResults {
results := &SearchResults{}
node := t.root
for _, keyPart := range prefixKey {
child, ok := node.children[keyPart]
if !ok {
return results
}
node = child
}
if opts.exactKey {
if node.isTerminal {
result := &SearchResult{Key: prefixKey, Value: node.value}
results.Results = append(results.Results, result)
}
return results
}
t.build(results, node, &prefixKey, opts)
return results
}
func (t *Trie) build(results *SearchResults, node *Node, prefixKey *[]string, opts *SearchOptions) (stop bool) {
if node.isTerminal {
key := make([]string, len(*prefixKey))
copy(key, *prefixKey)
result := &SearchResult{Key: key, Value: node.value}
results.Results = append(results.Results, result)
if opts.maxResults && len(results.Results) == opts.maxResultsCount {
return true
}
}
for dllNode := node.childrenDLL.head; dllNode != nil; dllNode = dllNode.next {
child := dllNode.trieNode
*prefixKey = append(*prefixKey, child.keyPart)
stop := t.build(results, child, prefixKey, opts)
*prefixKey = (*prefixKey)[:len(*prefixKey)-1]
if stop {
return true
}
}
return false
}
func min(s ...int) int {
m := s[0]
for _, a := range s[1:] {
if a < m {
m = a
}
}
return m
}