-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.go
75 lines (56 loc) · 1.27 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
package cleo
import (
"strings"
)
type document struct {
id string
bloom int
}
func getPrefix(query string) string {
qLen := min(len(query), 4)
q := query[0:qLen]
return strings.ToLower(q)
}
//Inverted Index - Maps the query prefix to the matching documents
type invertedIndex map[string][]document
func NewInvertedIndex() *invertedIndex {
i := make(invertedIndex)
return &i
}
func (x *invertedIndex) Size() int {
return len(map[string][]document(*x))
}
func (x *invertedIndex) Add(id string, value string) {
bloom := computeBloomFilter(value)
for _, word := range strings.Fields(value) {
word = getPrefix(word)
ref, ok := (*x)[word]
if !ok {
ref = nil
}
(*x)[word] = append(ref, document{id: id, bloom: bloom})
}
}
func (x *invertedIndex) Search(query string) []document {
q := getPrefix(query)
if ref, ok := (*x)[q]; ok {
return ref
}
return nil
}
//Forward Index - Maps the document id to the document
type forwardIndex map[string]string
func NewForwardIndex() *forwardIndex {
i := make(forwardIndex)
return &i
}
func (x *forwardIndex) Add(id string, value string) {
for _, word := range strings.Fields(value) {
if _, ok := (*x)[id]; !ok {
(*x)[id] = word
}
}
}
func (x *forwardIndex) itemAt(i string) string {
return (*x)[i]
}