diff options
Diffstat (limited to 'index')
| -rw-r--r-- | index/index.go | 26 |
1 files changed, 17 insertions, 9 deletions
diff --git a/index/index.go b/index/index.go index 7456213..53f9fca 100644 --- a/index/index.go +++ b/index/index.go @@ -2,31 +2,39 @@ import "sync" +type Posting struct { + DocId int + Count int +} type InvertedIndex struct { Mu sync.RWMutex - Data map[string][]int + Data map[string][]Posting DocNames map[int]string } func New() *InvertedIndex { return &InvertedIndex{ - Data: make(map[string][]int), + 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 - seen := make(map[string]struct{}) - - for _, token := range tokens { - if _, exists := seen[token]; !exists { - idx.Data[token] = append(idx.Data[token], docID) - seen[token] = struct{}{} - } + for token, count := range tokenCounts { + idx.Data[token] = append(idx.Data[token], Posting{ + DocId: docID, + Count: count, + }) } } |
