-
Notifications
You must be signed in to change notification settings - Fork 4
/
search_jus.py
263 lines (215 loc) · 7.7 KB
/
search_jus.py
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#!/usr/bin/python
import sys
import getopt
import nltk
import json
import math
# =========================================================================
#
# Posting Class
#
# =========================================================================
class Posting:
def __init__(self, docID, titleCount, contentCount, dateCount, courtCount):
self.currentDocID = docID
self.currentTitleCount = titleCount
self.currentContentCount = contentCount
self.currentDateCount = dateCount
self.currentCourtCount = courtCount
self.currentCombinedTF = titleCount + contentCount + dateCount + courtCount
def getDocID(self):
return self.currentDocID
def getTitleCount(self):
return self.currentTitleCount
def getContentCount(self):
return self.currentContentCount
def getDateCount(self):
return self.currentDateCount
def getCourtCount(self):
return self.currentCourtCount
def getTF(self):
return self.currentCombinedTF
# =========================================================================
#
# PostingHandler Class
#
# =========================================================================
class PostingHandler:
def __init__(self, dictionary_file, postings_file):
self.dictionaryFile = dictionary_file
self.postingsFile = open(postings_file, 'r')
self.dictionary = importDS(self.dictionaryFile)
self.currentPostingsList = ""
self.currentWord = ""
self.currentPointer = None
self.currentDocFreq = 0
self.postingIndex = 0
self.currentPosting = None
def getDocLength(self, docID):
return self.dictionary["DOC_ID"][docID][1]
def getDocName(self, docID):
return self.dictionary["DOC_ID"][docID][0]
def getDocFreq(self, word):
if word in self.dictionary:
return self.dictionary[word]["docFreq"]
else:
return 0
def getStartPointer(self, word):
if word in self.dictionary:
return self.dictionary[word]["index"]
else:
print("No such word: " + word + "in dictionary!")
def extractPostingList(self, word):
self.currentWord = word
if word not in self.dictionary:
return False
startPointer = self.getStartPointer(word)
self.postingsFile.seek(startPointer)
postingList = self.postingsFile.readline()
self.currentPostingsList = postingList
self.currentPointer = 0
self.currentDocFreq = self.getDocFreq(word)
self.postingIndex = 0
return True
def getNextPosting(self):
self.postingIndex += 1
if self.postingIndex > self.currentDocFreq:
return None
nextStartPointer = self.currentPointer
docIDLen = int(self.currentPostingsList[nextStartPointer: nextStartPointer + 2])
nextStartPointer += 2
docID = self.currentPostingsList[nextStartPointer: nextStartPointer + docIDLen]
nextStartPointer += docIDLen
titleCount = int(self.currentPostingsList[nextStartPointer: nextStartPointer + 1])
nextStartPointer += 1
contentCountLen = int(self.currentPostingsList[nextStartPointer: nextStartPointer + 1])
nextStartPointer += 1
contentCount = int(self.currentPostingsList[nextStartPointer: nextStartPointer + contentCountLen])
nextStartPointer += contentCountLen
dateCount = int(self.currentPostingsList[nextStartPointer: nextStartPointer + 1])
nextStartPointer += 1
courtCount = int(self.currentPostingsList[nextStartPointer: nextStartPointer + 1])
nextStartPointer += 1
self.currentPointer = nextStartPointer
self.currentPosting = Posting(docID, titleCount, contentCount, dateCount, courtCount)
return self.currentPosting
# =========================================================================
# Imports the dataStructure using pickle interface
# input: outputFile(String)
# output: DS(Object)
# =========================================================================
def importDS(outputFile):
data = open(outputFile, 'r')
DS = json.load(data)
return DS
# =========================================================================
#
# Justin's Part
#
# =========================================================================
def main():
f = open(file_of_queries)
queries = f.readlines()
f.close()
results = []
for line in queries:
results.append(' '.join(processquery(line[:-1])) + "\n")
f = open(file_of_output, 'w+')
f.writelines(results)
f.close()
def processquery(query):
# Since only boolean operator is AND, each element in this list will be AND-merged
query = query.split(" AND ")
stemmer(query)
convertToScores(query)
# Since skip lists aren't implemented, no need to prioritise shortest query
while len(query) > 1:
query[0] = doAnd(query[0], query[1])
del query[1]
query = query[0]
if len(query) == 0:
return ""
else:
query.sort(key=lambda x: x[1], reverse=True)
return [str(i) for i in zip(*query)[0]]
def stemmer(list):
# Uses the same stemmer as index phase
ps = nltk.stem.PorterStemmer()
for i in range(len(list)):
list[i] = ps.stem(list[i])
def convertToScores(list):
# Retrieve postings and calculate lnc.ltc
# Returns array of postings of format [str(docID), float(lnc)]
ph = PostingHandler(dictionary_file, postings_file)
ltc = {}
sum = 0
for i in range(len(list)):
word = list[i]
ph.extractPostingList(word)
df = int(ph.getDocFreq(word))
if df != 0:
ltc[word] = math.log10(1.0/df)
else:
ltc[word] = 0
sum += pow(ltc[word], 2)
if sum == 0:
sum = 1
for word in ltc:
ltc[word] /= pow(sum, 0.5)
lnc = []
for i in range(len(list)):
word = list[i]
list[i] = []
ph.extractPostingList(word)
sum = 0
for count in range(int(ph.getDocFreq(word))):
post = ph.getNextPosting()
docID = post.getDocID()
tf = post.getTF()
lnc.append([docID, 1 + math.log10(tf)])
sum += pow(1 + math.log10(tf), 2)
for j in range(len(lnc)):
docID = lnc[j][0]
lncScore = lnc[j][1] / pow(sum, 0.5)
list[i].append([docID, lncScore * ltc[word]])
def doAnd(t1, t2):
# AND-merge of two nested arrays of format [[docID, score], ...]
# Returns nested array of similar format
i = j = 0
result = []
while i < len(t1) and j < len(t2):
print("i: %d of %d\nj: %d of %d" % (i, len(t1), j, len(t2)))
d1, s1 = t1[i]
d2, s2 = t2[j]
if d1 == d2:
result.append([d1, s1 * s2])
i += 1
j += 1
elif d1 < d2:
i += 1
else:
j += 1
return result
def usage():
print "usage: " + sys.argv[0] + " -d dictionary-file -p postings-file -q file-of-queries -o output-file-of-results"
dictionary_file = postings_file = file_of_queries = output_file_of_results = None
try:
opts, args = getopt.getopt(sys.argv[1:], 'd:p:q:o:')
except getopt.GetoptError, err:
usage()
sys.exit(2)
for o, a in opts:
if o == '-d':
dictionary_file = a
elif o == '-p':
postings_file = a
elif o == '-q':
file_of_queries = a
elif o == '-o':
file_of_output = a
else:
assert False, "unhandled option"
if dictionary_file == None or postings_file == None or file_of_queries == None or file_of_output == None:
usage()
sys.exit(2)
main()