/
trigram.go
373 lines (298 loc) · 7.63 KB
/
trigram.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
// Package trigram is a simple trigram index
package trigram
import (
"sort"
)
// T is a trigram
type T uint32
func (t T) String() string {
b := [3]byte{byte(t >> 16), byte(t >> 8), byte(t)}
return string(b[:])
}
// DocID is a document ID
type DocID uint32
// Index is a trigram index
type Index map[T][]DocID
// a special (and invalid) trigram that holds all the document IDs
const TAllDocIDs T = 0xFFFFFFFF
// Extract returns a list of all the unique trigrams in s
func Extract(s string, trigrams []T) []T {
for i := 0; i <= len(s)-3; i++ {
t := T(uint32(s[i])<<16 | uint32(s[i+1])<<8 | uint32(s[i+2]))
trigrams = appendIfUnique(trigrams, t)
}
return trigrams
}
// ExtractAll returns a list of all the trigrams in s
func ExtractAll(s string, trigrams []T) []T {
for i := 0; i <= len(s)-3; i++ {
t := T(uint32(s[i])<<16 | uint32(s[i+1])<<8 | uint32(s[i+2]))
trigrams = append(trigrams, t)
}
return trigrams
}
func appendIfUnique(t []T, n T) []T {
for _, v := range t {
if v == n {
return t
}
}
return append(t, n)
}
// NewIndex returns an index for the strings in docs
func NewIndex(docs []string) Index {
idx := make(Index)
var allDocIDs []DocID
var trigrams []T
for id, d := range docs {
ts := ExtractAll(d, trigrams)
docid := DocID(id)
allDocIDs = append(allDocIDs, docid)
for _, t := range ts {
idxt := idx[t]
l := len(idxt)
if l == 0 || idxt[l-1] != docid {
idx[t] = append(idxt, docid)
}
}
trigrams = trigrams[:0]
}
idx[TAllDocIDs] = allDocIDs
return idx
}
// Add adds a new string to the search index
func (idx Index) Add(s string) DocID {
id := DocID(len(idx[TAllDocIDs]))
idx.Insert(s, id)
return id
}
// AddTrigrams adds a set of trigrams to the search index
func (idx Index) AddTrigrams(ts []T) DocID {
id := DocID(len(idx[TAllDocIDs]))
idx.InsertTrigrams(ts, id)
return id
}
// Insert adds a string with a given document ID
func (idx Index) Insert(s string, id DocID) {
ts := ExtractAll(s, nil)
idx.InsertTrigrams(ts, id)
}
// InsertTrigrams adds a set of trigrams with a given document ID
func (idx Index) InsertTrigrams(ts []T, id DocID) {
for _, t := range ts {
idxt, ok := idx[t]
// this trigram post list has been pruned. we must keep it empty
if ok && idxt == nil {
continue
}
l := len(idxt)
if l == 0 || idxt[l-1] != id {
idx[t] = append(idxt, id)
}
}
idx[TAllDocIDs] = append(idx[TAllDocIDs], id)
}
// Delete removes a document from the index
func (idx Index) Delete(s string, id DocID) {
ts := ExtractAll(s, nil)
for _, t := range ts {
ids := idx[t]
if ids == nil {
continue
}
if len(ids) == 1 && ids[0] == id {
delete(idx, t)
continue
}
i := sort.Search(len(ids), func(i int) bool { return ids[i] >= id })
if i != -1 && i < len(ids) && ids[i] == id {
copy(ids[i:], ids[i+1:])
idx[t] = ids[:len(ids)-1]
}
}
}
// for sorting
type docList []DocID
func (d docList) Len() int { return len(d) }
func (d docList) Swap(i, j int) { d[i], d[j] = d[j], d[i] }
func (d docList) Less(i, j int) bool { return d[i] < d[j] }
// Sort ensures all the document IDs are in order.
func (idx Index) Sort() {
for _, v := range idx {
dl := docList(v)
if !sort.IsSorted(dl) {
sort.Sort(dl)
}
}
}
// Prune removes all trigrams that are present in more than the specified percentage of the documents.
func (idx Index) Prune(pct float64) int {
maxDocs := int(pct * float64(len(idx[TAllDocIDs])))
var pruned int
for k, v := range idx {
if k != TAllDocIDs && len(v) > maxDocs {
pruned++
idx[k] = nil
}
}
return pruned
}
// Query returns a list of document IDs that match the trigrams in the query s
func (idx Index) Query(s string) []DocID {
ts := Extract(s, nil)
return idx.QueryTrigrams(ts)
}
type tfList struct {
tri []T
freq []int
}
func (tf tfList) Len() int { return len(tf.tri) }
func (tf tfList) Swap(i, j int) {
tf.tri[i], tf.tri[j] = tf.tri[j], tf.tri[i]
tf.freq[i], tf.freq[j] = tf.freq[j], tf.freq[i]
}
func (tf tfList) Less(i, j int) bool { return tf.freq[i] < tf.freq[j] }
// QueryTrigrams returns a list of document IDs that match the trigram set ts
func (idx Index) QueryTrigrams(ts []T) []DocID {
if len(ts) == 0 {
return idx[TAllDocIDs]
}
var freq []int
for _, t := range ts {
d, ok := idx[t]
if !ok {
return nil
}
freq = append(freq, len(d))
}
sort.Sort(tfList{ts, freq})
var nonzero int
for nonzero < len(freq) && freq[nonzero] == 0 {
nonzero++
}
// query consists only of pruned trigrams -- return all documents
if nonzero == len(freq) {
return idx[TAllDocIDs]
}
ids := idx.Filter(idx[ts[nonzero]], ts[nonzero+1:])
return ids
}
// FilterOr removes documents that don't contain any of the list of specified trigrams
// in other words, it's the union of the results of invidial filters
func (idx Index) FilterOr(docs []DocID, tss [][]T) []DocID {
// no provided filter trigrams
if len(tss) == 0 {
return docs
}
maxDocs := len(docs)
filtered := idx.Filter(docs, tss[0])
for i := 1; i < len(tss); i++ {
// docs can be a live postings list so we can't repurpose that array
result := make([]DocID, 0, maxDocs)
out := idx.Filter(docs, tss[i])
filtered = union(result, filtered, out)
}
return filtered
}
// Filter removes documents that don't contain the specified trigrams
func (idx Index) Filter(docs []DocID, ts []T) []DocID {
// no provided filter trigrams
if len(ts) == 0 {
return docs
}
// interesting implementation detail:
// we don't want to repurpose/alter docs since it's typically
// a live postings list, hence allocating a result slice
// however, upon subsequent loop runs we do repurpose the input
// as the output, because at that point its safe for reuse
result := make([]DocID, len(docs))
for _, t := range ts {
d, ok := idx[t]
// unknown trigram
if !ok {
return nil
}
if d == nil {
// the trigram was removed via Prune()
continue
}
result = intersect(result[:0], docs, d)
docs = result
}
return docs
}
// intersect intersects the input slices and puts the output in result slice
// note that result may be backed by the same array as a or b, since
// we only add docs that also exist in both inputs, it's guaranteed that we
// never overwrite/clobber the input, as long as result's start and len are proper
func intersect(result, a, b []DocID) []DocID {
var aidx, bidx int
scan:
for aidx < len(a) && bidx < len(b) {
if a[aidx] == b[bidx] {
result = append(result, a[aidx])
aidx++
bidx++
if aidx == len(a) || bidx == len(b) {
break scan
}
}
for a[aidx] < b[bidx] {
aidx++
if aidx == len(a) {
break scan
}
}
for a[aidx] > b[bidx] {
bidx++
if bidx == len(b) {
break scan
}
}
}
return result
}
// union takes the union of the input slices
// result slice will be used for output
// specifying a result slice backed by the same array as a or b
// is almost always a bad idea and will clobber your input,
// unless you know what you're doing.
func union(result, a, b []DocID) []DocID {
var aidx, bidx int
scan:
for aidx < len(a) && bidx < len(b) {
if a[aidx] == b[bidx] {
result = append(result, a[aidx])
aidx++
bidx++
if aidx == len(a) || bidx == len(b) {
break scan
}
}
for a[aidx] < b[bidx] {
result = append(result, a[aidx])
aidx++
if aidx == len(a) {
break scan
}
}
for a[aidx] > b[bidx] {
result = append(result, b[bidx])
bidx++
if bidx == len(b) {
break scan
}
}
}
// we may have broken out because we either finished b, or a, or both
// processes any remainders
for aidx < len(a) {
result = append(result, a[aidx])
aidx++
}
for bidx < len(b) {
result = append(result, b[bidx])
bidx++
}
return result
}