-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsearch_index.go
281 lines (245 loc) · 6.63 KB
/
search_index.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
package searchindex
import (
"github.com/iancoleman/orderedmap"
"golang.org/x/text/runes"
"golang.org/x/text/transform"
"golang.org/x/text/unicode/norm"
"reflect"
"regexp"
s "sort"
"strings"
"unicode"
)
type SearchIndexInterface interface {
AppendData(data SearchList)
Search(params SearchParams) []SearchData
}
type SearchIndex struct {
SearchIndexInterface
index Index
limit int
preprocessFunc func(key string, stopWords map[string]bool) []string
sortFunc func(i, j int, data interface{}) bool
indexParts bool
stopWords map[string]bool
}
type Index struct {
children *orderedmap.OrderedMap
key string
data SearchList
}
const (
Strict = iota
Beginning = iota
)
type SearchParams struct {
Text string
OutputSize int
Matching int
StartValues []SearchData
}
type SearchData interface{}
type SearchItem struct {
Key string
Data SearchData
}
type SearchList []*SearchItem
func defaultSortFunc(i, j int, data interface{}) bool {
return data.(SearchList)[i].Key < data.(SearchList)[j].Key
}
func defaultPreprocessFunc(key string, stopWords map[string]bool) []string {
// Replace punctuation to spaces
rePunctuation := regexp.MustCompile("[`'\".,:;\\?!+\\-–*=<>_~@#№$%^&()|/\\\\]")
// By default we remove special symbols, because we need searches BTCUSD and BTC-USD get BTC/USD key as result
processed := rePunctuation.ReplaceAllString(key, "")
// Replace double spaces to single space
reSpaces := regexp.MustCompile("\\s+")
processed = reSpaces.ReplaceAllString(processed, " ")
processed = strings.Trim(processed, " ")
processed = strings.ToLower(processed)
// Replace "São, Österreich" to "Sao, Osterreich"
t := transform.Chain(norm.NFD, runes.Remove(runes.In(unicode.Mn)), norm.NFC)
processed, _, _ = transform.String(t, processed)
parts := strings.Split(processed, " ")
// Exclude stop words
var result []string
for _, part := range parts {
if _, ok := stopWords[part]; !ok {
result = append(result, part)
}
}
return result
}
func NewSearchIndex(
data SearchList,
limit int,
sort func(i, j int, data interface{}) bool,
preprocess func(key string, stopWords map[string]bool) []string,
indexParts bool,
stopWords []string,
) SearchIndexInterface {
preprocessFunc := preprocess
if preprocessFunc == nil {
preprocessFunc = defaultPreprocessFunc
}
sortFunc := defaultSortFunc
if sort != nil {
sortFunc = sort
}
// Prepare stop words
sw := make(map[string]bool)
for _, word := range stopWords {
parts := preprocessFunc(word, make(map[string]bool))
for _, part := range parts {
sw[part] = true
}
}
// Create and fill index with initial data
searchIndex := &SearchIndex{
index: Index{
children: orderedmap.New(),
},
limit: limit,
preprocessFunc: preprocessFunc,
sortFunc: sortFunc,
indexParts: indexParts,
stopWords: sw,
}
searchIndex.AppendData(data)
return searchIndex
}
func (c SearchIndex) AppendData(data SearchList) {
// Copy original data
copied := copyOriginalData(data)
// Preprocess keys
var preprocessed SearchList
for _, item := range copied {
sortedParts := c.preprocessFunc(item.Key, c.stopWords)
for j, _ := range sortedParts {
d := *item
copiedItem := &d
copiedItem.Key = strings.Join(sortedParts[j:], " ")
preprocessed = append(preprocessed, copiedItem)
if !c.indexParts {
break
}
}
}
// Sort
s.SliceStable(preprocessed, func(i, j int) bool {
return c.sortFunc(i, j, preprocessed)
})
// Group by key
itemsByKey := orderedmap.New()
for _, item := range preprocessed {
current, ok := itemsByKey.Get(item.Key)
if !ok {
itemsByKey.Set(item.Key, SearchList{item})
} else {
current = append(current.(SearchList), item)
itemsByKey.Set(item.Key, current)
}
}
for _, key := range itemsByKey.Keys() {
item, _ := itemsByKey.Get(key)
addToIndex(&c.index, key, key, item.(SearchList))
}
}
func copyOriginalData(data SearchList) SearchList {
copied := make(SearchList, len(data))
for i, _ := range data {
d := *data[i]
copied[i] = &d
}
return copied
}
func addToIndex(index *Index, keyTail string, key string, data SearchList) {
if len(keyTail) == 0 {
index.key = key
index.data = data
return
}
first := keyTail[:1]
tail := keyTail[1:]
idx, ok := index.children.Get(first)
if !ok {
idx = &Index{
children: orderedmap.New(),
}
index.children.Set(first, idx)
}
addToIndex(idx.(*Index), tail, key, data)
}
func (c SearchIndex) Search(params SearchParams) []SearchData {
outputSize := params.OutputSize
if outputSize == 0 || outputSize > c.limit || outputSize <= 0 {
outputSize = c.limit
}
start := make(map[uintptr]bool)
for _, item := range params.StartValues {
ptr := reflect.ValueOf(item).Pointer()
start[ptr] = true
}
// Start search
data := c.searchInIndex(
&c.index,
strings.Join(c.preprocessFunc(params.Text, c.stopWords), " "),
params.Matching,
outputSize-len(params.StartValues),
start,
)
// And append result after start
result := make([]SearchData, len(params.StartValues))
copy(result, params.StartValues)
result = append(result, data...)
return result
}
func (c SearchIndex) searchInIndex(index *Index, key string, matching int, outputSize int, start map[uintptr]bool) []SearchData {
if key == "" {
found := make(map[uintptr]bool)
searched := c.searchList(index, make(SearchList, 0), matching, outputSize, found, start)
return c.getData(searched)
}
idx, ok := index.children.Get(key[:1])
if !ok {
return make([]SearchData, 0)
}
return c.searchInIndex(idx.(*Index), key[1:], matching, outputSize, start)
}
func (c SearchIndex) searchList(index *Index, items SearchList, matching int, outputSize int, found map[uintptr]bool, start map[uintptr]bool) SearchList {
if (outputSize > 0 && len(items) >= outputSize) || outputSize == 0 {
return items
}
if index.data != nil {
for _, item := range index.data {
// Check data in found, because we do not need to add duplicates in result
ptr := reflect.ValueOf(item.Data).Pointer()
if _, exists := found[ptr]; !exists {
if _, exists := start[ptr]; !exists {
items = append(items, item)
found[ptr] = true
if outputSize > 0 && len(items) >= outputSize {
return items
}
}
}
}
}
if len(index.children.Keys()) == 0 {
return items
}
if matching == Beginning {
for _, key := range index.children.Keys() {
idx, _ := index.children.Get(key)
items = c.searchList(idx.(*Index), items, matching, outputSize, found, start)
}
}
return items
}
func (c SearchIndex) getData(data SearchList) []SearchData {
result := make([]SearchData, len(data))
for i, item := range data {
result[i] = item.Data
}
return result
}