-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathquery.py
270 lines (213 loc) · 9.36 KB
/
query.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
from index import regexp_tokenize
import pickle
from test.test_iterlen import len
import re
import sys
import nltk
import operator
def getTitle(xmlTag):
'''
function to get the title
'''
outpath = 'C:\Users\Romi\Desktop\picklef.txt'
pkl_file = open(outpath, 'rb')
getTitle = pickle.load(pkl_file)
print getTitle[xmlTag]
pkl_file.close()
def getDoc(docno):
'''
function to get the doc number
'''
file1 = open('C:\Users\Romi\Desktop\CranField 1'+'\\'+docno, 'r')
print file1.read()
file1.close()
def similar(word):
outpath = 'C:\Users\Romi\Desktop\words.txt'
pkl_file = open(outpath, 'rb')
words = pickle.load(pkl_file)
text = nltk.Text(words)
text.similar(word)
#def calulateTF(docno,tf):
# try:
# print "term-frequency for ", tf , " in ", docno , " : " ,len(index[tf][docno])
# except:
# print "term-frequency for ", tf , " in ", docno , " : 0"
if __name__ == '__main__':
'''
the main for query.py
'''
path3 = 'C:\Users\Romi\Desktop\myfile'
pkl_file = open(path3, 'rb')
index = pickle.load(pkl_file)
pkl_file.close()
print 'Loading index... please wait...'
print 'Index loaded. You may now proceed to search'
while True:
queriess = raw_input("whats the query??")
requests = queriess.split(" ")
newqueries = regexp_tokenize(queriess, pattern = r'\"(.*?)\"')
queries=regexp_tokenize(re.sub(r'\".*?\"', "", queriess), pattern=r'\w+')
'''splitting of query into tokens using regex_tokenization'''
if requests[0] == 'similar':
similar(requests[1])
continue
if requests[0] == 'df':
req = ' '.join(requests[1:])
reqs = regexp_tokenize(re.sub(r'\".*?\"', "", req), pattern=r'\w+')
print reqs
if len(reqs) == 1:
''' handling single word queries'''
if reqs[0] not in index:
print 'Sorry, not found in any document!'
else:
print 'number of documents it appears in is' , len(index[reqs[0]])
requestset = set()
if len(reqs) > 1:
'''
handling only < single word requests
'''
print reqs
for r0 in reqs:
if r0 not in index:
continue
else:
requestset = requestset.union(set(index[r0].keys()))
print 'Found the request in' ,len(requestset), 'documents!'
req = ' '.join(requests[1:])
reqs = regexp_tokenize(req, pattern = r'\"(.*?)\"')
t = set()
for r in reqs:
newquer=regexp_tokenize(r, pattern=r'\w+')
#print newquer
#print newqueries
if newquer[0] not in index.keys():
print " no documents for given phrase"
else:
finaldoclist = index[newquer[0]]
tempdoclist={}
i =1
for new in newquer[1:]:
#print index
for d in finaldoclist:
if d not in index[new].keys():
pass
#del finaldoclist[d]
else:
s = set(finaldoclist[d])& set([p-i for p in index[new][d]])
if len(s) is not 0 :
tempdoclist[d] = s
finaldoclist = tempdoclist
i = i +1
print len(finaldoclist), finaldoclist
print 'The phrase',r,' appears in', len(finaldoclist), 'documents'
'''handling the phrase terms'''
continue
if requests[0] == 'title':
'''calling the function to get the title'''
req = ' '.join(requests[1:])
getTitle(req)
continue
if requests[0] == 'doc':
'''calling the function to get document number'''
req = ' '.join(requests[1:])
getDoc(req)
continue
if requests[0] == 'tf':
'''handling the term frequency'''
if requests[2] not in index.keys():
print 0
else:
this = index[requests[2]]
if requests[1] in this.keys():
print 'the term frequency is' , len(this[requests[1]])
else:
print 'term frequency is 0'
continue
if requests[0] == 'freq':
reqs = ' '.join(requests[1:])
reqs = regexp_tokenize(reqs, pattern = r'\"(.*?)\"')
for r in reqs:
newquer=regexp_tokenize(r, pattern=r'\w+')
if newquer[0] not in index.keys():
print " no documents for given phrase"
else:
finaldoclist = index[newquer[0]]
tempdoclist={}
i =1
for new in newquer[1:]:
#print index
for d in finaldoclist:
if d not in index[new].keys():
pass
else:
s = set(finaldoclist[d])& set([p-i for p in index[new][d]])
if len(s) is not 0 :
tempdoclist[d] = s
finaldoclist = tempdoclist
i = i +1
#print len(finaldoclist), finaldoclist
freq =0
for d in finaldoclist.keys():
freq = freq + len(finaldoclist[d])
print 'The phrase',r,' appears ', freq, 'times'
'''handling the phrase terms'''
continue
'''The above will give the phrase terms or frequency'''
#query processing
# newqueries = regexp_tokenize(queriess, pattern = r'\"(.*?)\"')
# queries=regexp_tokenize(re.sub(r'\".*?\"', "", queriess), pattern=r'\w+')
#doclist: {doc :[list of postion]}
doc_list = {}
if len(queries)==1:
if queries[0] in index:
doc_list = index[queries[0]]
'''print doc_list'''
elif len(queries) >1:
x = {}
for query in queries:
if query in index.keys():
x = index[query]
for x1 in x.keys():
if not x1 in doc_list:
doc_list[x1] = x[x1]
else:
doc_list[x1] = doc_list[x1].append(x[x1])
newdoc_list = {}
'''print doc_list'''
# sorted_newdoc_list = sorted(newdoc_list.iteritems(), key=operator.itemgetter(1))
# print sorted_newdoc_list.reverse()
for newqueries in newqueries:
newquer=regexp_tokenize(newqueries, pattern=r'\w+')
#print newquer
#print newqueries
if newquer[0] not in index.keys():
print " no documents for given phrase"
else:
finaldoclist = index[newquer[0]]
tempdoclist={}
i =1
for new in newquer[1:]:
#print index
if new not in index.keys():
finaldoclist = {}
break
for d in finaldoclist:
if d not in index[new].keys():
continue
#del finaldoclist[d]
else:
s = set(finaldoclist[d])& set([p-i for p in index[new][d]])
if len(s) is not 0 :
tempdoclist[d] = s
finaldoclist = tempdoclist
i = i +1
for x1 in finaldoclist.keys():
if not x1 in doc_list:
doc_list[x1] = list(finaldoclist[x1])
else:
doc_list[x1] = doc_list[x1].append(list(finaldoclist[x1]))
'''print len(finaldoclist), finaldoclist'''
newdoc_list = {}
for doc in doc_list.keys():
newdoc_list[doc] = len(doc_list[doc])
print sorted(newdoc_list, key=newdoc_list.get, reverse=True)