-
Notifications
You must be signed in to change notification settings - Fork 0
/
BooleanQuery.py
289 lines (221 loc) · 8.39 KB
/
BooleanQuery.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
'''
Process:
- Boolean operators: AND, OR , NOT , NEAR query
- Note: postings lists are in increasing order by docId
- retrieve postings for X, for Y
- Intersect postings
- Repeat
'''
from nltk.tokenize import word_tokenize
from Postings import PositionalPostings
import math
'''
AndQuery
'''
class AndQuery:
def __init__(self, literals, index):
self.__literals = literals
self.__index = index
def getQueryToken(self, query:str):
# query = query.lower()
# self.__tokens = word_tokenize(query)
return self.__literals
def getPostings(self):
queries = self.__literals
result = queries[0].getPostings()
for i in range (1, len(queries)):
postingList = queries[i].getPostings()
result = self.AndMerge(result, postingList)
return result
# Do AndQuery Merge -- Intersecting the resulting postings
def AndMerge(self, PostingsList1, PostingsList2):
result = []
position1 = 0
position2 = 0
# print('doing andMerge....')
# print('posting1...', PostingsList1)
# print('posting2...', PostingsList2)
while ( position1 < len(PostingsList1) and position2 < len(PostingsList2)):
first = PostingsList1[position1]
second = PostingsList2[position2]
if first.getDocumentId() == second.getDocumentId() :
result.append(PostingsList1[position1])
position1 += 1
position2 += 1
elif ( first.getDocumentId() < second.getDocumentId()):
position1 += 1
else:
position2 += 1
return result
def print(self):
print(self.__literals)
'''
OrQuery
'''
class OrQuery:
def __init__(self, literals, index):
self.__literals = literals
self.__index = index
'''
Retrieves a list of postings for the query component, using an Index as the source.
'''
def getPostings(self):
queries = self.__literals
# print("vocab ", index.getVocabulary())
# result = queries[0].getPostings(self.__index)
result = queries[0].getPostings()
self.printDocId(result)
print('first query...',queries[0], queries[0].getQueryToken())
print('result ...', result)
for i in range(1, len(queries)):
postingList = queries[i].getPostings()
self.printDocId(postingList)
# postingList = index.getPostings(q)
result = self.OrMerge(result, postingList)
return result
# merge two posting lists -- Or Merge
def OrMerge(self, PostingsList1, PostingsList2):
print('inside ormerge...',PostingsList1)
result = []
position1 = 0
position2 = 0
while ( position1 < len(PostingsList1) and position2 < len(PostingsList2)):
first = PostingsList1[position1]
second = PostingsList2[position2]
if first.getDocumentId() == second.getDocumentId() :
result.append(first)
position1 += 1
position2 += 1
elif ( first.getDocumentId() < second.getDocumentId()):
result.append(first)
position1 += 1
else:
result.append(second)
position2 += 1
# take care the left over postingsList
if position1 < len(PostingsList1):
result.append(PostingsList1[position1])
position1 += 1
if position2 < len(PostingsList2):
result.append(PostingsList2[position2])
position2 += 1
return result
def print(self):
for lit in self.__literals:
print(lit)
lit.print()
def printDocId(self,postingList):
for p in postingList:
print(p.getDocumentId())
def getComponent(self):
return self.__literals
'''
PhraseLiteral: Liteal object to support Phrase query
'''
class PhraseLiteral:
def __init__(self, literals, index):
self.__literals = literals
self.__index = index
def getPostings(self):
queries = self.__literals
return self.__index.getPostings(queries)
def print(self):
print(self.__literal)
'''
NearLiteral: Literal object to support NEAR query
'''
class NearLiteral:
def __init__(self,literals, index, offset:int):
self.__literals = literals
self.__index = index
self.__offset = offset
def getPostings(self):
queries = self.__literals
postingList1 = self.__index.getPostings(self.__literals[0])
postingList2 = self.__index.getPostings(self.__literals[1])
result = self.PositionalMerge(postingList1, postingList2, self.__offset)
return result
'''
program this method. Retrieve the postings for the individual terms in the phrase,
// and positional merge them together.
Order of the term appearance matter Baseball Near/2 Angels --> Baseball must appear before Angels
'''
def PositionalMerge(self,PostingsList1, PostingsList2, offset):
mergeResult = []
mergeDict = {}
postlist = []
position1 = 0
position2 = 0
while ( position1 < len(PostingsList1) and position2 < len(PostingsList2)):
first = PostingsList1[position1]
second = PostingsList2[position2]
if first.getDocumentId() == second.getDocumentId() :
result=[]
pos1 = first.getPositions()
pos2 = first.getPositions()
l1= l2 = 0
while(l1 < len(pos1)):
while (l2 < len(pos2)):
# terms appear near each other within the k offset
if abs(pos1[l1] - pos2[l2]) <= offset:
# add this position
result.append(pos2[l2])
elif(pos2[l2] > pos1[l1]):
break
l2 += 1
# left over position in pos1
while((len(result) != 0) & (abs(result[0] - pos1[l1]) > offset)):
result.remove(result[0])
if first.getDocumentId() in mergeDict:
postings = mergeDict[first.getDocumentId()]
positionslist = postings.getPositions()
for pos in result:
if pos not in positionslist:
positionslist.append(pos)
postings.setPositions(positionslist)
mergeDict[first.getDocumentId()] = postings
else:
postings = PositionalPostings(first.getDocumentId())
postings.setPositions(result)
mergeDict[first.getDocumentId()] = postings
postings.setPositions(result)
l1 += 1
if (len(result) != 0):
if first.getDocumentId() not in postlist:
postlist.append(first.getDocumentId())
position1 += 1
position2 += 1
elif ( first.getDocumentId() < second.getDocumentId()):
position1 += 1
else:
position2 += 1
for docId in mergeDict.keys():
mergeResult.append(mergeDict[docId])
return mergeResult
'''
TermLiteral:
'''
class TermLiteral:
def __init__(self, literal, index):
print('initialize termLiteral....', literal)
literal = literal.replace(' ','')
self.__term = literal
self.__index = index
def getPostings(self):
print('Getting postings in termLiteral...', self.getQueryToken())
term = self.getQueryToken()
return self.__index.getPostings(term)
def print(self):
print(self.__term)
def getQueryToken(self):
print('getquerytoken: \"', self.__term,'\"')
return self.__term
'''
Wildcard queries
'''
class WildcardLiteral:
def __init__(self, literal, index):
self.__literal = literal
self.__index = index
def getPostings(self):
pass