-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfrontend.py
More file actions
422 lines (335 loc) · 11.2 KB
/
frontend.py
File metadata and controls
422 lines (335 loc) · 11.2 KB
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
# -*- coding: utf-8 -*-
"""
Created on Fri Sep 22 06:16:46 2017
@author: Rajat Biswas
"""
import nltk
import math
import string
from nltk.corpus import movie_reviews
from nltk.tokenize import word_tokenize,sent_tokenize
from nltk import PorterStemmer
from nltk.corpus import stopwords
import time
import pickle
from collections import OrderedDict as OD
import queue as Q
from heapq_max import *
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.scrollview import ScrollView
from kivy.properties import StringProperty,ObjectProperty
from kivy.base import runTouchApp
from kivy.lang import Builder
from kivy.uix.treeview import TreeView, TreeViewNode
from kivy.uix.treeview import TreeViewLabel
from kivy.uix.widget import Widget
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput
from functools import partial
from nltk.corpus import words
import re
word_list=[]#contains all the unstemmed tokens in the corpus
word_set=set()#Contains all the unique unstemmed words
stop_words=set(stopwords.words("english"))#Preparing a set of stopwords in english
ps=PorterStemmer()#Creating the stemmer
idss=nltk.corpus.movie_reviews.fileids()#File names of all the documents in the corpus
docs=[]#Reading the raw documents in the corpus
translator=str.maketrans(' ',' ',string.punctuation)#MAkeing a translator table for passing to translate
for x in idss:
str(movie_reviews.raw(x)).replace('-',' ')
docs.append(movie_reviews.raw(x).translate(translator))#stripping off the punctuations from the raw data
q_idf=OD()#dictionary used for chekcing with the query
magn_list=[]#List storing the magnitude of each document vector
k_best=[]#IT will store the final result
def preprocessing(raw_docs):
tokenized_docs=[]
dummy=[]#First storing the tokenised docs in this
for x in raw_docs:
dummy=word_tokenize(x)
temp=[]
for y in dummy:
if(y not in stop_words):#stripping off the stopwords from raw data
temp.append(ps.stem(y))#adding stem words to dictionary
word_set.add(y)
tokenized_docs.append(temp)
return tokenized_docs
#Creating a list for auto complete
preprocessing(docs)
for x in word_set:
if(len(x)>=3):
word_list.append(x)
word_list=sorted(word_list)
print(len(word_list))
def term_frequency(term,document):
count=document.count(term)
if(count==0):
return 0
return 1+math.log(count)
def inverse_document_frequency(tokenized_documents):
idf=OD()
all_tokens=set()
for x in tokenized_documents:
for word in x:
all_tokens.add(word)
#print("Size of docs"+str(len(tokenized_documents)))
for token in all_tokens:
word_list.append(token)
counter=0
for x in tokenized_documents:
if (token in x):
counter+=1
idf[token]=1+math.log((len(tokenized_documents))/(counter))
return idf
"""Special BLock"""
#q_idf=inverse_document_frequency(preprocessing(docs))
#sorted(q_idf)
"""ENds here"""
def tf_idf(documents):
tokenized_docs=preprocessing(documents)
#q_idf=inverse_document_frequency(tokenized_docs)
tfidf_documents=[]
for document in tokenized_docs:
doc_tfidf=[]
for term in q_idf.keys():
tf=term_frequency(term,document)
doc_tfidf.append(tf*q_idf[term])
tfidf_documents.append(doc_tfidf)
return tfidf_documents
t1=time.time()
#q_idf=inverse_document_frequency(preprocessing(docs))
#print("Created the inverse document frequency")
#pickle.dump(q_idf,open("idf.pickle","wb"))
print("Reading idf from a pickle file ")
q_idf=pickle.load(open("idf.pickle","rb"))
#sorted(q_idf)
t2=time.time()
#tfidf_final=tf_idf(docs)
tfidf_final=pickle.load(open("tfidf.pickle","rb"))#Loading the tfidf
"""
Now Calculating the magnitudeVector for query optimisation
"""
for x in tfidf_final:
sum=0
for y in x:
sum+=y*y
sum=sum**(0.5)
magn_list.append(sum)#Creating the list of magnitudes of vectors
print(t2-t1)
#pickle.dump(tfidf_final,open("tfidf.pickle","wb"))
#print("wrote the vectorizer into pickle file")
print("read tfidf from the pickle file")
t3=time.time()
print(t3-t2)
def cosine_similarity(v1,v2,ind_list,mag,ind):
dot=0
for x in ind_list:
dot+=v1[x]*v2[x]
magnitude = mag*(magn_list[ind])
if(magnitude==0):
return 0;
return dot/magnitude
def experience(l1):
word_list=l1+word_list
Builder.load_file("scroll.kv")
#For suggestions and taking input
class MyTextInput(TextInput):
def keyboard_on_key_down(self, window, keycode, text, modifiers):
if keycode[1] == 'spacebar':
#print("hsyvguhs")
self.suggestion_text = ' '
return True
if self.suggestion_text and keycode[1] == 'tab':
self.insert_text(self.suggestion_text +' ')
return True
return super(MyTextInput, self).keyboard_on_key_down(window, keycode, text, modifiers)
def on_text(self, instance, value):
self.suggestion_text = ' '
val = value[value.rfind(' ') + 1:]
if not val:
return
try:
word = [word for word in word_list
if word.startswith(val)][0][len(val):]
if not word:
#return
word1 = [word1 for word1 in word_list
if word1.startswith(val)][1][len(val):]
if not word1:
self.suggestion_text=' '
return
self.suggestion_text=word1
return
self.suggestion_text = word
except IndexError:
self.suggestion_text = ' '
print ('Index Error.')
print(len(word_list))
class ScrollableLabel(ScrollView):
ll1=StringProperty('')
ll2=StringProperty('')
ll3=StringProperty('')
ll4=StringProperty('')
ll5=StringProperty('')
ll6=StringProperty('')
ll7=StringProperty('')
ll8=StringProperty('')
ll9=StringProperty('')
ll10=StringProperty('')
name=[]
score=[]
l=[]#TO store the documents to be shown
sts=[]#Store the states of the button
def bPress1(self):
if (self.sts[0] == 1):
self.ll1=''
self.sts[0]=0
else:
self.ll1=self.l[0]
self.sts[0]=1
def bPress2(self):
if (self.sts[1] == 1):
self.ll2=''
self.sts[1]=0
else:
self.ll2=self.l[1]
self.sts[1]=1
def bPress3(self):
if (self.sts[2] == 1):
self.ll3=''
self.sts[2]=0
else:
self.ll3=self.l[2]
self.sts[2]=1
def bPress4(self):
if (self.sts[3] == 1):
self.ll4=''
self.sts[3]=0
else:
self.ll4=self.l[3]
self.sts[3]=1
def bPress5(self):
if (self.sts[4] == 1):
self.ll5=''
self.sts[4]=0
else:
self.ll5=self.l[4]
self.sts[4]=1
def bPress6(self):
if (self.sts[5] == 1):
self.ll6=''
self.sts[5]=0
else:
self.ll6=self.l[5]
self.sts[5]=1
def bPress7(self):
if (self.sts[6] == 1):
self.ll7=''
self.sts[6]=0
else:
self.ll7=self.l[6]
self.sts[6]=1
def bPress8(self):
if (self.sts[7] == 1):
self.ll8=''
self.sts[7]=0
else:
self.ll8=self.l[7]
self.sts[7]=1
def bPress9(self):
if (self.sts[8] == 1):
self.ll9=''
self.sts[8]=0
else:
self.ll9=self.l[8]
self.sts[8]=1
def bPress10(self):
if (self.sts[9] == 1):
self.ll10=''
self.sts[9]=0
else:
self.ll10=self.l[9]
self.sts[9]=1
def queryResult(self):
t4=time.time()
self.ll1=''
self.ll2=''
self.ll3=''
self.ll4=''
self.ll5=''
self.ll6=''
self.ll7=''
self.ll8=''
self.ll9=''
self.ll10=''
self.score.clear()
self.name.clear()
self.sts.clear()
self.l.clear()
self.ids['status'].text=""
query=""
query=self.ids['search'].text
query_tfidf=[]
dummy_query=word_tokenize(query)
global word_list
query_tokens=[]
for x in dummy_query:
word_list.insert(0,x)
query_tokens.append(ps.stem(x))
i=0
ind_list=[]#Contains the list of indices with whom we have to do the dot product
m_query=0#Storing the magnitude of query
for x in q_idf.keys():
if(x in query_tokens):
query_tfidf.append(q_idf[x])
ind_list.append(i)
m_query+=(q_idf[x])**2
else:
query_tfidf.append(0)
i+=1
m_query=m_query**(0.5)
mx=-1 #cosine similarity with other docs
idx=0#Storing the index of the doc for accessing the precomputed value in magn_list
for ind,x in enumerate(tfidf_final):
if(cosine_similarity(x,query_tfidf,ind_list,m_query,ind)>mx):
mx=cosine_similarity(x,query_tfidf,ind_list,m_query,ind)
idx=ind
var=1.0
topt=str(movie_reviews.raw(idss[ind]))
for y in dummy_query:
tl=len(re.findall(" "+y+" ",topt))
if(tl>=1):
var=var*(2**tl)
k_best.append([var*(cosine_similarity(x,query_tfidf,ind_list,m_query,ind)),ind])
heapify_max(k_best)
val=[]#TO store the tuples after being popped
no_results=-1
rev=""
for x in range(10):
temp=heappop_max(k_best)
val.append(temp)
if(temp[0]==0):#Checking for base condition
no_results=x
print("NO results found")
self.ids[str('b'+str(x+1))].text=str('')
rev="[b]"+str(movie_reviews.raw(idss[temp[1]]))+"[/b]"
for s in dummy_query:
rev=rev.replace(""+s+"","[color=ff3333]"+s+"[/color]")
self.score.append(temp[0])
self.name.append(idss[temp[1]])
print(temp[0])
print(idss[temp[1]])
#self.labels[x].text=rev
self.sts.append(0)
if(no_results==-1):
self.ids[str('b'+str(x+1))].height=30
self.ids[str('b'+str(x+1))].text=str('[b]Document '+str(x+1)+' File = '+str(idss[temp[1]])+' Score = '+str(temp[0])+'[/b]')
self.ids[str('b'+str(x+1))].markup=True
self.l.append(rev)
if(no_results==0):
self.ids['status'].text="[color=ff3333]NO RESULTS FOUND[/color]"
t5=time.time()
print(t5-t4)
k_best.clear()
val.clear()
runTouchApp(ScrollableLabel())