-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemory_index.go
55 lines (46 loc) · 1.21 KB
/
memory_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
package main
import (
"log"
)
// IndexMap can be used to map word to their frequencies
type IndexMap map[string]Frequency
type MemoryIndex struct {
WordCount UrlMap // map[url]{total words, title}
Index IndexMap // map[word][url]count
}
func (memoryIndex *MemoryIndex) containsUrl(s string) bool {
return memoryIndex.Index[s] != nil
}
func newMemoryIndex() *MemoryIndex {
return &MemoryIndex{make(UrlMap), make(IndexMap)}
}
func (memoryIndex *MemoryIndex) search(word string) *SearchResult {
word, err := getStemmedWord(word)
if err != nil {
log.Printf("[WARNING] Could not stem word %q: %v\n", word, err)
}
freq, found := memoryIndex.Index[word]
return &SearchResult{
memoryIndex.WordCount,
freq,
len(memoryIndex.WordCount),
found,
}
}
func (memoryIndex *MemoryIndex) getTotalWords(url string) int {
return memoryIndex.WordCount[url].TotalWords
}
func (memoryIndex *MemoryIndex) insertCrawlResults(c *CrawlResult) {
memoryIndex.WordCount[c.Url] = UrlEntry{
c.TotalWords,
c.Title,
c.Description,
}
for term, frequency := range c.TermFrequency {
_, found := memoryIndex.Index[term]
if !found {
memoryIndex.Index[term] = make(Frequency)
}
memoryIndex.Index[term][c.Url] = frequency
}
}