-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
80 lines (64 loc) · 1.64 KB
/
utils.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
76
77
78
79
80
package main
import (
"sort"
"log"
)
type errorString struct {
s string
}
func (e *errorString) Error() string {
return e.s
}
func checkError(err error) {
if err != nil {
log.Fatal(err)
}
}
func createStemPairsList(stemmingMap map[string]map[string]int) map[string]StemPairList {
result := make(map[string]StemPairList)
for k, wordPositions := range stemmingMap {
stemList := make(StemPairList, len(wordPositions))
i := 0
for key, value := range wordPositions {
stemList[i] = StemPair{key, value}
i++
}
result[k] = stemList
}
return result
}
type StemPair struct {
Key string
Value int
}
type StemPairList []StemPair
func sortPostingsByTermFrequency(wordFrequencies map[int]int) PostingsList {
pl := make(PostingsList, len(wordFrequencies))
i := 0
for k, v := range wordFrequencies {
pl[i] = Posting{k, v}
i++
}
sort.Sort(sort.Reverse(pl))
return pl
}
type Posting struct {
Key int
Value int
}
type PostingsList []Posting
func (p PostingsList) Len() int { return len(p) }
func (p PostingsList) Less(i, j int) bool { return p[i].Value < p[j].Value }
func (p PostingsList) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
type DeserializedPosting struct {
Key int
Value int
}
func sortDeserializedPostingsByDocId(postings DeserializedPostingsList) DeserializedPostingsList {
sort.Sort(postings)
return postings
}
type DeserializedPostingsList []DeserializedPosting
func (p DeserializedPostingsList) Len() int { return len(p) }
func (p DeserializedPostingsList) Less(i, j int) bool { return p[i].Key < p[j].Key }
func (p DeserializedPostingsList) Swap(i, j int) { p[i], p[j] = p[j], p[i] }