-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3runswithoutstopping.py
291 lines (237 loc) · 7.6 KB
/
3runswithoutstopping.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
289
290
291
from helper import *
import nltk
from math import log
import os
import sys
from relevence_working import *
from query_to_dic import *
from generatingstopwords import *
import collections
from collections import OrderedDict
from operator import itemgetter
dl = {} # dict of document name and doc length by doc name ie id
avdl = 0 #avg document length
colvoc=0 #total words in volcal
docnames = {} #document names dic with no can be used for mapping
m = 0 #counter while printing
termdocidtf = {} # term with document id as well as term frequency in each file
termdocfreq = {} # term with its document frequency ie term - df
docidbm = {} # For BM25 dict with doc id and bm25 value
docdf={} # for doc and document freq
docidtfidf={} # for TF IDF dict with doc id and tf idf value
docidsmq={} #for SMoothed Query Doc id and smq value
termcolfreq={} # term and collection frequency
# To make the document with Doc Id and Document Length
# with storing doc names in one list
def dlandavdl():
global m, avdl, dl, docnames,colvoc
v = os.path.join(sys.path[0], "SP")
os.chdir(v)
y = os.listdir(v)
for i in y:
m = m + 1
with open(i, encoding='utf-8') as f:
for line in f:
dm = i.split(".txt")
dm = dm[0]
line = line.split(" ")
dl[dm] = len(line)
colvoc= colvoc+ len(line)
for i in line:
if i in termcolfreq:
termcolfreq[i] = termcolfreq[i] + 1
else:
termcolfreq[i] = 1
docnames[m] = dm
avdl = avdl + len(line)
avdl = avdl / 3204
# String in a dictionary of dictionary
# that is a 2D array kind of a thing
# docid d1 d2 d3 d4 d5
# terms
# term1 3 4 9 2 4
# term2 5 7 1 5 8
# .
# .
# .term n
def docwithtermfreq():
x = os.path.join(sys.path[0], "projonegramterm.txt")
file = open(x, 'r', encoding='utf-8')
for line in file:
line = line.split(">>")
term = line[0]
eachd = line[1].split(";")
termdocfreq[term] = len(eachd) - 1
termid = {}
for doc in eachd:
if doc not in ("\n"):
doc = doc.split(" ")
docid = doc[0]
tf = doc[1]
termid[docid] = tf
termdocidtf[term] = termid
file.close()
def colfreq(q):
return termcolfreq[q]/colvoc
def calcR(q_id):
q_id=str(q_id)
try:
return qid_R[q_id]
except:
return 0
def calri(q,q_id):
c=0;
q_id=str(q_id)
try:
for i in qid_reldocs[q_id]:
try:
termdocidtf[q][i]
c = c + 1
except:
c = c
return c
except:
return 0
def tf(term,docname,nam):
global termdocidtf,dl
return int(termdocidtf[term][docname])/int(dl[nam])
def idf(term):
global termdocfreq
return log(3204/termdocfreq[term])
def bmranking(query, q_id):
for i in range(1, len(docnames)):
nam = docnames[i]
docidbm[nam] = 0
ii = nam.split(" ")
ii = ''.join(ii)
bm = 0
for q in query:
try:
t = termdocidtf[q][ii]
except:
t = 0
bm = bm + score_BM25(termdocfreq[q], int(t), query[q], calri(q,q_id), 3204, dl[nam], avdl,calcR(q_id))
docidbm[nam] = docidbm[nam] + bm
keys = sorted(docidbm, key=docidbm.get, reverse=True)
m = 0
# os.chdir(os.path.join(sys.path[0],"bm25_result"))
#os.chdir("C:\\Users\\Rahul\\PycharmProjects\\IRpr\\bm25_result")
#file = open(str(q_id)+".txt", 'w', encoding='utf-8')
for i in keys:
if m > 99:
break
else:
m = m + 1
rank = str(m)
print(str(q_id) + " Q0" " " + i + " " + rank + " " + str(docidbm[i]) + " BM25" + "\n")
#file.write(str(q_id) + " Q0" " " + i + " " + rank + " " + str(docidbm[i]) + " BM25" + "\n")
#file.close()
def tfidfranking(query, q_id):
for i in range(1, len(docnames)):
final=0
nam = docnames[i]
docidtfidf[nam] = 0
ii = nam.split(" ")
ii = ''.join(ii)
ii=ii.rstrip()
bm = 0
for q in query:
try:
final = final + query[q]*tf(q,ii,nam)*idf(q)
except:
final = final + 0
docidtfidf[nam] = docidtfidf[nam] + final
keys = sorted(docidtfidf, key=docidtfidf.get, reverse=True)
m = 0
#os.chdir(os.path.join(sys.path[0], "tfidf_result"))
#os.chdir("C:\\Users\\Rahul\\PycharmProjects\\IRpr\\tfidf_result")
#file = open(str(q_id) + ".txt", 'w', encoding='utf-8')
for i in keys:
if m > 99:
break
else:
m = m + 1
rank = str(m)
print(str(q_id) + " Q0" " " + i + " " + rank + " " + str(docidtfidf[i]) + " TF-IDF " + "\n")
#file.write(str(q_id) +" Q0" " "+ i + " " + rank + " " + str(docidtfidf[i]) + " TF-IDF " + "\n")
#file.close()
def smoothedquery(query, q_id):
for i in range(1, len(docnames)):
final=0
nam = docnames[i]
docidsmq[nam] = 0
ii = nam.split(" ")
ii = ''.join(ii)
ii=ii.rstrip()
bm = 0
for q in query:
try:
final = final + query[q]*log(((0.65*tf(q,ii,nam))/(0.35*colfreq(q)))+1)
except:
final = final + 0
docidsmq[nam] = docidsmq[nam] + final
keys = sorted(docidsmq, key=docidsmq.get, reverse=True)
m = 0
#os.chdir(os.path.join(sys.path[0], "smq_result"))
#os.chdir("C:\\Users\\Rahul\\PycharmProjects\\IRpr\\smq_result")
#file = open(str(q_id) + ".txt", 'w', encoding='utf-8')
for i in keys:
if m > 99:
break
else:
m = m + 1
rank = str(m)
print(str(q_id) + " Q0 " + i + " " + rank + " " + str(docidsmq[i]) + " SMQ " + "\n")
#file.write(str(q_id) +" Q0" " "+ i + " " + rank + " " + str(docidsmq[i]) + " SMQ " + "\n")
#file.close()
def query_ref(q):
query = {}
filename = q
q = q.lower()
q = nltk.word_tokenize(q)
ql = len(q)
c = []
for i in q:
if i in termdocidtf:
c.append(i)
#print(c)
for i in c:
if i in query:
query[i] += 1
else:
query[i] = 1
return query
def main():
dlandavdl()
docwithtermfreq()
print("Select")
print("1 for BM25")
print("2 for TFIDF")
print("3 for Smoothed_query")
print("q to quit")
sel = input()
if (sel == "1"):
for qid in qid_query:
query = qid_query[qid]
q_id = qid
query = query.rstrip()
bmranking(query_ref(query), q_id)
main()
if (sel == "2"):
for qid in qid_query:
query = qid_query[qid]
q_id = qid
query = query.rstrip()
tfidfranking(query_ref(query), q_id)
main()
if (sel == "3"):
for qid in qid_query:
query = qid_query[qid]
q_id = qid
query = query.rstrip()
smoothedquery(query_ref(query), q_id)
main()
if(sel=="q"):
print("Thank You")
main()
#bmranking(query_ref("time sharing system"),"1")