-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindexer.go
56 lines (46 loc) · 1.1 KB
/
indexer.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
// indexer.go
package main
import (
"fmt"
"strings"
)
type Document struct {
ID int
Text string
}
type Index struct {
InvIndex map[string][]int
Docs map[int]string
}
func NewIndex() *Index {
return &Index{
InvIndex: make(map[string][]int),
Docs: make(map[int]string),
}
}
func (idx *Index) Add(doc Document) {
idx.Docs[doc.ID] = doc.Text
words := strings.Fields(strings.ToLower(doc.Text))
for _, word := range words {
idx.InvIndex[word] = append(idx.InvIndex[word], doc.ID)
}
}
func (idx *Index) Search(query string) []int {
words := strings.Fields(strings.ToLower(query))
var results []int
for _, word := range words {
results = append(results, idx.InvIndex[word]...)
}
return results
}
func main() {
index := NewIndex()
// Add documents
index.Add(Document{ID: 1, Text: "Python crawler for NLP tasks"})
index.Add(Document{ID: 2, Text: "Go backend for search engine indexing"})
index.Add(Document{ID: 3, Text: "C++ high-performance algorithms"})
// Search query
query := "NLP"
results := index.Search(query)
fmt.Printf("Results for query '%s': %v\n", query, results)
}