-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearcher.go
180 lines (164 loc) · 4.83 KB
/
searcher.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package main
import (
"github.com/caneroj1/stemmer"
"os"
"strings"
"bufio"
"io"
"strconv"
"sort"
"fmt"
"regexp"
)
var re = regexp.MustCompile("[0-9]+")
func search(query string) string {
var postingsLists []DeserializedPostingsList
queryTerms := strings.Split(query, " ")
for _, term := range queryTerms {
postingList, err := findTermPostings(term)
if err == nil {
postingsLists = append(postingsLists, postingList)
}
}
var resultPostings DeserializedPostingsList
if len(postingsLists) > 1 {
i := 0
for i < len(postingsLists) - 1 {
resultPostings = mergePostingsLists(postingsLists[0], postingsLists[1])
postingsLists[1] = resultPostings
postingsLists = postingsLists[1:]
}
} else if len(postingsLists) == 1 {
resultPostings = postingsLists[0]
}
if len(resultPostings) != 0 {
var result string
if len(resultPostings) > 20 {
result = returnPostings(resultPostings[:20])
result += strconv.Itoa(len(resultPostings)) + " results were found, but top 20 are displayed\n"
} else {
result = returnPostings(resultPostings)
result += strconv.Itoa(len(resultPostings)) + " results were found\n"
}
return result
} else {
return "Nothing was found! Try to change your query."
}
}
func mergePostingsLists(first DeserializedPostingsList, second DeserializedPostingsList) (result DeserializedPostingsList) {
first = sortDeserializedPostingsByDocId(first)
second = sortDeserializedPostingsByDocId(second)
small, big := first, second
if len(small) > len(big) {
small, big = second, first
}
for _, val1 := range small {
for j, val2 := range big {
if val1.Key == val2.Key {
result = append(result, val2)
big = big[:j+copy(big[j:], big[j+1:])]
break
}
}
}
return
}
func returnPostings(deserializedPostingsLists DeserializedPostingsList) (result string) {
var postingsList PostingsList
for _, deserializedPosting := range deserializedPostingsLists {
postingsList = append(postingsList, Posting{Key:deserializedPosting.Key, Value:deserializedPosting.Value})
}
sort.Sort(sort.Reverse(postingsList))
for _, posting := range postingsList {
documentsFile, err := os.Open("index/documentsIndexes")
checkError(err)
documentsReader := bufio.NewReader(documentsFile)
var docTitle string
for {
requiredLine, err := documentsReader.ReadString('|')
if err != nil {
break
}
documentContent := strings.Split(requiredLine, "->")
docNumber, err := strconv.ParseInt(re.FindString(documentContent[0]), 10, 64)
if int(docNumber) == posting.Key {
docBody := strings.TrimSpace(documentContent[1])
docTitle = strings.Split(docBody, ".\r")[0]
break
}
}
result += "Document #" + fmt.Sprintf("%d", posting.Key) + ": " + docTitle + "\n"
documentsFile.Close()
}
return
}
func findTermPostings(term string) (DeserializedPostingsList, error) {
var result DeserializedPostingsList
stemList, err := searchForStem(term)
if err != nil {
return nil, err
}
termIndex, err := findTermIndex(stemList, term)
if err != nil {
return nil, err
}
result = findPostings(int(termIndex))
return result, nil
}
func searchForStem(term string) (string, error) {
stem := stemmer.Stem(term)
stemFile, err := os.Open("index/stemmingData")
checkError(err)
defer stemFile.Close()
stemFileDataReader := bufio.NewReader(stemFile)
for {
fileLine, err := stemFileDataReader.ReadString('\n')
if err == io.EOF {
break
}
line := fileLine
lineArray := strings.Split(line, "->")
if lineArray[0] == stem {
return lineArray[1], nil
}
}
return "", &errorString{"Nothing is found"}
}
func findTermIndex(rawStemList string, term string) (int64, error) {
rawStemList = rawStemList[1:len(rawStemList) - 2] // Substring on pairs by "><"
for _, stemPair := range strings.Split(rawStemList, "><") {
stemPairArray := strings.Split(stemPair, ":")
pairTerm := stemPairArray[0]
termIndex := stemPairArray[1]
if pairTerm == term {
return strconv.ParseInt(termIndex, 10, 64)
}
}
return 0, &errorString{"Nothing is found"}
}
func findPostings(position int) DeserializedPostingsList {
var result DeserializedPostingsList
invertedIndexFile, err := os.Open("index/invertedIndex")
defer invertedIndexFile.Close()
checkError(err)
indexReader := bufio.NewReader(invertedIndexFile)
i := 0
var requiredLine string
for i <= position {
requiredLine, err = indexReader.ReadString('\n')
checkError(err)
i++
}
rawPostings := strings.Split(requiredLine, "->")[1]
rawPostings = rawPostings[1:len(rawPostings) - 2]
for _, rawPosting := range strings.Split(rawPostings, "><") {
postingValues := strings.Split(rawPosting, ":")
docId, err := strconv.ParseInt(postingValues[0], 10, 64)
checkError(err)
termFrequency, err := strconv.ParseInt(postingValues[1], 10, 64)
checkError(err)
posting := DeserializedPosting{int(docId), int(termFrequency)}
result = append(result, posting)
}
return result
}