-
Notifications
You must be signed in to change notification settings - Fork 0
/
invertedindex.py
280 lines (252 loc) · 9.27 KB
/
invertedindex.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
#Code written by : 19k-1534 Muhammad Anas
#Approach: Created a positional index which also works as an inverted index for simple and complex
# queries. The queries are then classified accordingly in the process query fn and are processed
# according to their needs.
from preprocessing import terms
from operator import itemgetter
from nltk.stem.porter import PorterStemmer
import tkinter as tk
from tkinter import *
sorted_terms=sorted(terms,key=itemgetter(0))
stemmer=PorterStemmer()
#used Mylist class as it helped me in debugging and writing code efficiently.
class Mylist:
def __init__(self):
self.value=''
self.list=[]
postinglist=[]
#As name suggest, this fn creates the positional index.(inverted index also same without the positions)
def createIndex():
lastlist=[]
dictionary=Mylist()
dictionary.value=sorted_terms[0][0]
doclist=Mylist()
doclist.value=sorted_terms[0][1]
doclist.list.append(sorted_terms[0][2])
prevterm=sorted_terms[0][0]
idx=0
# print(len(sorted_terms))
for term in sorted_terms:
if idx > 0:
if idx == len(sorted_terms)-1:
dictionary.list.append({doclist.value:doclist.list})
lastlist=dictionary.list
if term[0]!=prevterm:
dictionary.list.append({doclist.value:doclist.list})
postinglist.append(dictionary.list)
dictionary=Mylist()
dictionary.value=term[0]
a=False
doclist=Mylist()
doclist.value=term[1]
doclist.list.append(term[2])
else:
if doclist.value != term[1]:
dictionary.list.append({doclist.value:doclist.list})
doclist=Mylist()
doclist.value=term[1]
doclist.list.append(term[2])
else:
doclist.list.append(term[2])
prevterm=term[0]
idx=idx+1
postinglist.append(lastlist)
def getAllTerms():
for t in sorted_terms:
if t[0] not in all_terms:
all_terms.append(t[0])
def simplequery(query_tokens):
result_docs=[]
#processing 1 word query
if len(query_tokens)==1:
query_tokens[0]=stemmer.stem(query_tokens[0])
idx=0
for term in all_terms:
if term==query_tokens[0]:
for doc in postinglist[idx]:
for key,items in doc.items():
if key not in result_docs:
result_docs.append(key)
idx=idx+1
#processing not only query
elif len(query_tokens)==2 and query_tokens[0]=="not":
query_tokens[1]=stemmer.stem(query_tokens[1])
idx=0
for term in all_terms:
if term!=query_tokens[1]:
for doc in postinglist[idx]:
for key,items in doc.items():
if key not in result_docs:
result_docs.append(key)
idx=idx+1
#processing and/or queries
elif len(query_tokens)==3:
conj_list1=[]
conj_list2=[]
query_tokens[0]=stemmer.stem(query_tokens[0])
print(query_tokens[0])
query_tokens[2]=stemmer.stem(query_tokens[2])
print(query_tokens[2])
idx=0
for term in all_terms:
if term == query_tokens[0]:
for doc in postinglist[idx]:
for key,items in doc.items():
conj_list1.append(key)
elif term == query_tokens[2]:
for doc in postinglist[idx]:
for key,items in doc.items():
conj_list2.append(key)
idx=idx+1
if query_tokens[1]=="and":
result_docs=[value for value in conj_list1 if value in conj_list2]
elif query_tokens[1]=="or":
result_docs=list(set(conj_list1)|set(conj_list2))
print(result_docs)
return result_docs
#for 3 terms queries
def complexquery(query_tokens):
result_docs=[]
term1=stemmer.stem(query_tokens[0])
term2=stemmer.stem(query_tokens[2])
term3=stemmer.stem(query_tokens[4])
operator1=query_tokens[1]
operator2=query_tokens[3]
term_list1=[]
term_list2=[]
term_list3=[]
result_doc1=[]
idx=0
for term in all_terms:
if term == term1:
for doc in postinglist[idx]:
for key,items in doc.items():
term_list1.append(key)
elif term == term2:
for doc in postinglist[idx]:
for key,items in doc.items():
term_list2.append(key)
elif term == term3:
for doc in postinglist[idx]:
for key,items in doc.items():
term_list3.append(key)
idx=idx+1
if operator1=="and":
result_doc1=[value for value in term_list1 if value in term_list2]
elif operator1=="or":
result_doc1=list(set(term_list1)|set(term_list2))
if operator2=="and":
print(result_doc1)
result_docs=[value for value in result_doc1 if value in term_list3]
elif operator2=="or":
result_docs=list(set(result_doc1)|set(term_list3))
return result_docs
#for proximity queries
def proximityquery(query_tokens,gap):
term1=stemmer.stem(query_tokens[0])
term2=stemmer.stem(query_tokens[1])
term_list1=[]
term_list2=[]
idx=0
for term in all_terms:
if term == term1:
for doc in postinglist[idx]:
term_list1.append(doc)
if term == term2:
for doc in postinglist[idx]:
term_list2.append(doc)
idx=idx+1
term1_doc=[]
term2_doc=[]
same_docs=[]
for doc in term_list1:
for key,item in doc.items():
term1_doc.append(key)
for doc in term_list2:
for key,item in doc.items():
term2_doc.append(key)
same_docs=[value for value in term1_doc if value in term2_doc]
term1_pi=[]
term2_pi=[]
for doc in term_list1:
for key,item in doc.items():
if key in same_docs:
term1_pi.append(item)
for doc in term_list2:
for key,item in doc.items():
if key in same_docs:
term2_pi.append(item)
result_doc=[]
idx=0
for term1 in term1_pi:
for t1 in term1:
for t2 in term2_pi[idx]:
if(abs(int(t1)-int(t2))<=gap+1):
if same_docs[idx] not in result_doc:
result_doc.append(same_docs[idx])
idx=idx+1
return result_doc
#main process query fn from where queries are redirected to their relevant fn.
def processquery(query):
query_tokens=query.split(" ")
if "/" in query:
gap=query_tokens[-1]
gap=gap.replace("/","")
gap=int(gap)
doc_list=proximityquery(query_tokens,gap)
elif len(query_tokens)<=3:
doc_list=simplequery(query_tokens)
elif len(query_tokens)==5:
doc_list=complexquery(query_tokens)
return doc_list
def inputqueryGUI():
#a method to get input queries from user and passing to the main process query fn.
def execute():
label2["text"]=""
query=str(entry.get())
query=query.lower()
doc_list=processquery(query)
if doc_list:
label2["fg"]="purple"
i=0
#for loop for mantaining the no of docs in one output line.
for doc in doc_list:
if i==20:
label2["text"]+="\n"+str(doc)+" "
i=0
else:
label2["text"]+=str(doc)+" "
i=i+1
else:
label2["fg"]="red"
label2["text"]="Sorry didn't find the document. Please enter correct query"
doc_list.clear()
#GUI design line[242-266]
Font_tuple = ("Comic Sans MS", 20, "bold")
window=tk.Tk()
window.title("Boolean Retreival Model( By 19k-1534)")
window.geometry("1000x800")
window.rowconfigure(0, minsize=500, weight=1)
window.columnconfigure([0, 1, 2], minsize=500, weight=1)
frame1=tk.Frame(master=window,width=200,height=70,bg="#4a0f61")
frame1.pack(fill=tk.BOTH, side=tk.LEFT, expand=True)
label1=tk.Label(master=frame1,font=Font_tuple,text="Welcome!",bg="#4a0f61",fg="white")
label1.pack()
frame=tk.Frame(master=window,width=100,height=70,bg="white")
frame.place(relx=.5, rely=.5,anchor= CENTER)
frame.pack(fill=tk.BOTH, side=tk.LEFT, expand=True)
label1=tk.Label(master=frame,text="Boolean Retreival Model",bg="white",fg="purple",font=Font_tuple)
label = tk.Label(master=frame,text="Enter your query",bg="white",fg="purple")
entry=tk.Entry(master=frame,width=40)
btn=tk.Button(master=frame,bg="purple",fg="white",text="Enter",command=execute)
label1.pack()
label.pack()
entry.pack()
btn.pack()
label2 = Label(frame,font=('"Comic Sans MS" 8 bold'),width=200,bg="white",fg="purple",text="waiting for your query :)")
label2.pack(pady=20)
window.mainloop()
createIndex() #creates the positional index.
all_terms=[] #list to store terms.
getAllTerms() #a method to populate all terms.
inputqueryGUI() # a tkinter GUI method for taking input queries & processing them.