-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProximity_WithStopping.py
221 lines (170 loc) · 5.35 KB
/
Proximity_WithStopping.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
from math import log
import os
import sys
import collections
from collections import OrderedDict
from operator import itemgetter
import nltk
from query_to_dic import *
from generatingstopwords import *
k1 = 1.2
k2 = 100
b = 0.75
R = 0.0
r = 0.0
dl = {}
avdl = 0
docnames = {}
m = 0
termdocidtf = {}
doctermdocidtf = {}
docidbm = {}
def Positions(doc):
global noOfTermsAppearingConseq, sumOfIntermediateTerms, query
os.chdir(os.path.join(sys.path[0], "SP"))
file = open(doc+'.txt', 'r')
global query
queryNew = query.lower()
queryNew = nltk.word_tokenize(queryNew)
docTermsWithoutStop = file.read().split()
docTerms = []
for i in range(0, len(docTermsWithoutStop)):
if docTermsWithoutStop[i] not in stopwordlist:
docTerms.append(docTermsWithoutStop[i])
myDict = {}
counterList = []
commonTerms = []
for qTerm in queryNew:
if qTerm in docTerms:
commonTerms.append(qTerm)
for word in docTerms:
index = [i for i, x in enumerate(docTerms) if x == word]
if word not in myDict:
myDict[word] = index
timesProcessed = {}
sumOfIntermediateTerms = 0
noOfTermsAppearingConseq = 0
commonTermsReversed = list(reversed(commonTerms))
for i in range(0, len(commonTerms)-1):
for r in range(0, len(myDict[commonTerms[i]])):
for v in range(0, len(myDict[commonTerms[i+1]])):
subtraction = myDict[commonTerms[i]][r] - myDict[commonTerms[i+1]][v]
if subtraction<0 and abs(subtraction)<=4:
noOfTermsAppearingConseq = noOfTermsAppearingConseq + 1
sumOfIntermediateTerms = sumOfIntermediateTerms + abs(subtraction)
file.close()
def score_BM25(n, f, qf, r, N, dl, avdl):
K = compute_K(dl, avdl)
first = log(((r + 0.5) / (R - r + 0.5)) / ((n - r + 0.5) / (N - n - R + r + 0.5)))
second = ((k1 + 1) * f) / (K + f)
third = ((k2 + 1) * qf) / (k2 + qf)
return first * second * third
def compute_K(dl, avdl):
return k1 * ((1 - b) + b * (float(dl) / float(avdl)))
# To make the document with Doc Id and Document Length
# with storing doc names in one list
def dlandavdl():
global m, avdl, dl, docnames
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)
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(";")
doctermdocidtf[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 ranking(q):
global noOfTermsAppearingConseq, sumOfIntermediateTerms
query = {}
filename = q
q = q.lower()
q = nltk.word_tokenize(q)
ql = len(q)
#print(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
for i in range(1, len(docnames)):
nam = docnames[i]
Positions(nam)
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(doctermdocidtf[q], int(t), query[q], r, 3204, dl[nam], avdl)
if sumOfIntermediateTerms > 0 and noOfTermsAppearingConseq > 0:
bm = bm * noOfTermsAppearingConseq * (1 / sumOfIntermediateTerms)
elif noOfTermsAppearingConseq > 0 and sumOfIntermediateTerms <= 0:
bm = bm * noOfTermsAppearingConseq
elif noOfTermsAppearingConseq <= 0 and sumOfIntermediateTerms > 0:
bm = bm * (1 / sumOfIntermediateTerms)
else:
bm = bm
docidbm[nam] = docidbm[nam] + bm
keys = sorted(docidbm, key=docidbm.get, reverse=True)
m = 0
os.chdir(os.path.join(sys.path[0], "Results_WithTermPositions_WithStopping"))
file = open(str(q_id) + ".txt", "w")
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]) + " WithTermPositions_WithStopping" + "\n")
file.write(str(q_id) +" Q0" " " + i + " " + rank + " " + str(docidbm[i])+ " WithTermPositions_WithStopping" + "\n")
def bm25main():
global query, q_id
dlandavdl()
docwithtermfreq()
for qid in qid_query:
query = qid_query[qid]
q_id = qid
query = query.rstrip()
ranking(query)
print("Thank You")
bm25main()