blob: 53f9fcac21eb92363460222fb374ecb7182b10ad (
plain)
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
|
package index
import "sync"
type Posting struct {
DocId int
Count int
}
type InvertedIndex struct {
Mu sync.RWMutex
Data map[string][]Posting
DocNames map[int]string
}
func New() *InvertedIndex {
return &InvertedIndex{
Data: make(map[string][]Posting),
DocNames: make(map[int]string),
}
}
func (idx *InvertedIndex) Add(docID int, title string, tokens []string) {
tokenCounts := make(map[string]int)
for _, token := range tokens {
tokenCounts[token]++
}
idx.Mu.Lock()
defer idx.Mu.Unlock()
idx.DocNames[docID] = title
for token, count := range tokenCounts {
idx.Data[token] = append(idx.Data[token], Posting{
DocId: docID,
Count: count,
})
}
}
|