-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.go
333 lines (289 loc) · 6.21 KB
/
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
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
package main
import (
"fmt"
"log"
"sort"
"strings"
"sync"
"github.com/nfisher/mdindexer/edit"
)
var (
ErrWordNotIndexed = fmt.Errorf("index does not contain word")
)
type Document struct {
Name string
WordCount map[string]int
}
// New creates an index that can accommodate the number of documents specified by size.
func New(size int) *Index {
return &Index{
Words: make(map[string]*WordColumn),
Names: make([]string, 0, size),
}
}
// Index is a matrix that counts word occurrences in documents.
type Index struct {
Words map[string]*WordColumn
Names []string
sync.RWMutex `msg:"-"`
}
// Capacity returns the number of documents in the index.
func (z *Index) Capacity() int {
z.RLock()
defer z.RUnlock()
return cap(z.Names)
}
// WordCount provides the number of Words in the index.
func (z *Index) WordCount() int {
z.RLock()
defer z.RUnlock()
return len(z.Words)
}
// Update incorporates the documents word count frequency into the index.
func (z *Index) Update(doc *Document) {
z.Lock()
defer z.Unlock()
var isNew bool
pos := z.byName(doc.Name)
if pos == nameNotFound {
pos = len(z.Names)
z.Names = append(z.Names, doc.Name)
isNew = true
}
cur := make(map[string]bool)
for word, count := range doc.WordCount {
cur[word] = true
col, ok := z.Words[word]
if !ok {
col = NewColumn(word)
}
col.Upsert(pos, count)
z.Words[word] = col
}
if !isNew {
z.clean(pos, cur)
}
}
func (z *Index) clean(pos int, cur map[string]bool) {
for word, col := range z.Words {
if cur[word] {
continue
}
col.Remove(pos)
if col.Empty() {
delete(z.Words, word)
}
}
}
// Search returns the list of documents that contain needle.
func (z *Index) Search(needle string) (DocList, error) {
z.RLock()
defer z.RUnlock()
if 1 > len(z.Words) {
return nil, ErrWordNotIndexed
}
var words = Words{{needle, 0}}
_, ok := z.Words[needle]
if !ok {
words = Words{}
for k := range z.Words {
d := edit.Distance2(needle, k)
words = append(words, WordDist{k, d})
}
sort.Sort(words)
end := len(words)
min := words[0].Distance
for i := 1; i < len(words); i++ {
if words[i].Distance > min {
break
}
end = i
}
words = words[0:end]
}
pos := make(map[string]int)
var docs = make(DocList, 0, len(words))
for _, word := range words {
col := z.Words[word.Word]
col.Apply(func(id int, count int) {
if count > 0 {
doc := z.byId(id)
relevance := DocRelevance{Document: doc}
relevance.Count = count
relevance.Distance = word.Distance
p, ok := pos[doc]
if !ok {
p = len(docs)
docs = append(docs, relevance)
pos[doc] = p
return
}
if docs[p].Distance < relevance.Distance {
return
}
docs[p].Distance = relevance.Distance
docs[p].Count = relevance.Count
}
})
}
sort.Slice(docs, func(i, j int) bool {
return docs[i].Count < docs[j].Count
})
return docs, nil
}
const nameNotFound = -1
func (z *Index) byName(name string) int {
var id int
for ; id < len(z.Names); id++ {
if name == z.Names[id] {
return id
}
}
return nameNotFound
}
func (z *Index) byId(id int) string {
return z.Names[id]
}
// NewColumn returns a newly initialised WordColumn.
func NewColumn(name string) *WordColumn {
return &WordColumn{
Name: name,
Docs: make([][2]int, 0, 64),
}
}
// WordColumn maintains the frequency a word occurs in the named document.
type WordColumn struct {
Name string
Docs [][2]int
// msgpack/json can't serialise map with int keys
idx map[int]int
}
func (z *WordColumn) Upsert(pos int, count int) {
// lazily build the index so a read from file does not break
if z.idx == nil {
z.lazyIndex()
}
i, ok := z.idx[pos]
// make a sparse matrices, don't store count < 1
if count < 1 {
return
}
tup := [2]int{pos, count}
if !ok {
i := len(z.Docs)
z.idx[pos] = i
z.Docs = append(z.Docs, tup)
return
}
z.Docs[i] = tup
}
func (z *WordColumn) lazyIndex() {
z.idx = make(map[int]int)
for i, tup := range z.Docs {
pos := tup[0]
z.idx[pos] = i
}
}
func (z *WordColumn) Apply(each func(int, int)) {
for _, tup := range z.Docs {
if tup[1] > 0 {
each(tup[0], tup[1])
}
}
}
func (z *WordColumn) Empty() bool {
var count int
z.Apply(func(i int, i2 int) {
count++
})
return count == 0
}
func (z *WordColumn) Remove(pos int) {
i, ok := z.idx[pos]
if !ok {
return
}
delete(z.idx, pos)
// TODO: should consider compacting the array instead of using 0
z.Docs[i] = [2]int{pos, 0}
}
// Search executes the query against the index returning a document list.
func Search(query string, index *Index) ScoreList {
var result = make(Scores)
if query == "" {
return ScoreList{}
}
query = strings.ToLower(query)
terms := strings.Split(query, " ")
union := make(StrSet)
for i, term := range terms {
docs, err := index.Search(term)
if err != nil {
log.Printf("search=failed query=%s error='%v'\n", term, err)
continue
}
s := make(StrSet)
for _, d := range docs {
n := d.Document
if i == 0 {
result[n] = d.Distance + d.Rank
s[n] = true
continue
}
if !union[n] {
continue
}
result[n] += d.Distance + d.Rank
s[n] = true
}
union = s
}
list := make(ScoreList, 0, len(union))
for n := range result {
if !union[n] {
continue
}
length := len(n) - len(query)
dist := edit.Distance2(query, n) - length
list = append(list, Score{Document: n, Rank: result[n], NameDistance: dist})
}
sort.Slice(list, func(i, j int) bool {
return list[i].NameDistance < list[j].NameDistance
})
sort.SliceStable(list, func(i, j int) bool {
return list[i].Rank < list[j].Rank
})
return list
}
type Score struct {
Document string
Rank int
NameDistance int
}
type ScoreList []Score
type Scores map[string]int
type StrSet map[string]bool
func (s StrSet) Union(o StrSet) StrSet {
r := make(StrSet)
for k := range s {
if o[k] {
r[k] = true
}
}
return r
}
type DocList []DocRelevance
type DocRelevance struct {
Document string
Count int
Distance int
Rank int
}
type WordDist struct {
Word string
Distance int
}
type Words []WordDist
func (z Words) Len() int { return len(z) }
func (z Words) Swap(i, j int) { z[i], z[j] = z[j], z[i] }
func (z Words) Less(i, j int) bool { return z[i].Distance < z[j].Distance }