-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.py
240 lines (202 loc) · 7.91 KB
/
index.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
import re
import math
class BuildIndex:
def __init__(self, files):
self.tf = {}
self.df = {}
self.idf = {}
self.filenames = files
self.file_to_terms = self.process_files()
self.regdex = self.regIndex()
self.totalIndex = self.execute()
self.vectors = self.vectorize()
self.mags = self.magnitudes(self.filenames)
self.populateScores()
def process_files(self):
file_to_terms = {}
for file in self.filenames:
pattern = re.compile('[\W_]+')
file_to_terms[file] = open(file, 'r').read().lower();
file_to_terms[file] = pattern.sub(' ',file_to_terms[file])
re.sub(r'[\W_]+','', file_to_terms[file])
file_to_terms[file] = file_to_terms[file].split()
return file_to_terms
def index_one_file(self, termlist):
fileIndex = {}
for index, word in enumerate(termlist):
if word in fileIndex.keys():
fileIndex[word].append(index)
else:
fileIndex[word] = [index]
return fileIndex
def make_indices(self, termlists):
total = {}
for filename in termlists.keys():
total[filename] = self.index_one_file(termlists[filename])
return total
def fullIndex(self):
total_index = {}
indie_indices = self.regdex
for filename in indie_indices.keys():
self.tf[filename] = {}
for word in indie_indices[filename].keys():
self.tf[filename][word] = len(indie_indices[filename][word])
if word in self.df.keys():
self.df[word] += 1
else:
self.df[word] = 1
if word in total_index.keys():
if filename in total_index[word].keys():
total_index[word][filename].append(indie_indices[filename][word][:])
else:
total_index[word][filename] = indie_indices[filename][word]
else:
total_index[word] = {filename: indie_indices[filename][word]}
return total_index
def vectorize(self):
vectors = {}
for filename in self.filenames:
vectors[filename] = [len(self.regdex[filename][word]) for word in self.regdex[filename].keys()]
return vectors
def document_frequency(self, term):
if term in self.totalIndex.keys():
return len(self.totalIndex[term].keys())
else:
return 0
def collection_size(self):
return len(self.filenames)
def magnitudes(self, documents):
mags = {}
for document in documents:
mags[document] = pow(sum(map(lambda x: x**2, self.vectors[document])),.5)
return mags
def term_frequency(self, term, document):
return self.tf[document][term]/self.mags[document] if term in self.tf[document].keys() else 0
def populateScores(self):
for filename in self.filenames:
for term in self.getUniques():
self.tf[filename][term] = self.term_frequency(term, filename)
if term in self.df.keys():
self.idf[term] = self.idf_func(self.collection_size(), self.df[term])
else:
self.idf[term] = 0
return self.df, self.tf, self.idf
def idf_func(self, N, N_t):
if N_t != 0:
return math.log(N/N_t)
else:
return 0
def generateScore(self, term, document):
return self.tf[document][term] * self.idf[term]
def execute(self):
return self.fullIndex()
def regIndex(self):
return self.make_indices(self.file_to_terms)
def getUniques(self):
return self.totalIndex.keys()
self.filenames = files
self.file_to_terms = self.process_files()
self.regdex = self.regIndex()
self.totalIndex = self.execute()
self.vectors = self.vectorize()
self.mags = self.magnitudes(self.filenames)
self.populateScores()
def process_files(self):
file_to_terms = {}
for file in self.filenames:
pattern = re.compile('[\W_]+')
file_to_terms[file] = open(file, 'r').read().lower();
file_to_terms[file] = pattern.sub(' ',file_to_terms[file])
re.sub(r'[\W_]+','', file_to_terms[file])
file_to_terms[file] = file_to_terms[file].split()
return file_to_terms
def index_one_file(self, termlist):
fileIndex = {}
for index, word in enumerate(termlist):
if word in fileIndex.keys():
fileIndex[word].append(index)
else:
fileIndex[word] = [index]
return fileIndex
def make_indices(self, termlists):
total = {}
for filename in termlists.keys():
total[filename] = self.index_one_file(termlists[filename])
return total
def fullIndex(self):
total_index = {}
indie_indices = self.regdex
for filename in indie_indices.keys():
self.tf[filename] = {}
for word in indie_indices[filename].keys():
self.tf[filename][word] = len(indie_indices[filename][word])
if word in self.df.keys():
self.df[word] += 1
else:
self.df[word] = 1
if word in total_index.keys():
if filename in total_index[word].keys():
total_index[word][filename].append(indie_indices[filename][word][:])
else:
total_index[word][filename] = indie_indices[filename][word]
else:
total_index[word] = {filename: indie_indices[filename][word]}
return total_index
def vectorize(self):
vectors = {}
for filename in self.filenames:
vectors[filename] = [len(self.regdex[filename][word]) for word in self.regdex[filename].keys()]
return vectors
def document_frequency(self, term):
if term in self.totalIndex.keys():
return len(self.totalIndex[term].keys())
else:
return 0
def collection_size(self):
return len(self.filenames)
def magnitudes(self, documents):
mags = {}
for document in documents:
mags[document] = pow(sum(map(lambda x: x**2, self.vectors[document])),.5)
return mags
def term_frequency(self, term, document):
return self.tf[document][term]/self.mags[document] if term in self.tf[document].keys() else 0
def populateScores(self):
for filename in self.filenames:
for term in self.getUniques():
self.tf[filename][term] = self.term_frequency(term, filename)
if term in self.df.keys():
self.idf[term] = self.idf_func(self.collection_size(), self.df[term])
else:
self.idf[term] = 0
return self.df, self.tf, self.idf
def idf_func(self, N, N_t):
if N_t != 0:
return math.log(N/N_t)
else:
return 0
def generateScore(self, term, document):
return self.tf[document][term] * self.idf[term]
def execute(self):
return self.fullIndex()
def regIndex(self):
return self.make_indices(self.file_to_terms)
def getUniques(self):
return self.totalIndex.keys()
def docLens(self):
# return as a number
return len(self.filenames)
def getDocs(self):
return self.filenames
# bm25 score for a given term and document
def bm25(self, term, document):
k1 = 1.5
b = 0.75
N = self.collection_size()
n = self.document_frequency(term)
d = self.docLens()
f = self.tf[document][term]
R = d / N
K = k1 * ((1-b) + b * (d / N))
score = (f * (k1 + 1)) / (K + f)
return score