-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.py
More file actions
291 lines (221 loc) · 8.66 KB
/
index.py
File metadata and controls
291 lines (221 loc) · 8.66 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
from abc import ABC, abstractmethod
from collections import UserDict
import json
import time
import os
import search
import score
class Index(UserDict, ABC):
def __init__(self):
super().__init__()
self.terms = set()
self.documents = {}
self._doc_counter = 0
@property
def docs(self): return list(self.documents.values())
@property
def doc_ids(self): return list(self.documents.keys())
@property
@abstractmethod
def is_positional(self): return
@abstractmethod
def count_term(self, term): return # returns the frequency of the 'term'
@abstractmethod
def _add_document(self, terms, doc_id): pass # add a doc considering the dictinoary structure
@abstractmethod
def _remove_document(self, doc_id): pass
@abstractmethod
def _get_term(self, term): return
@classmethod
def _dump(cls, filepath, dictionary, overwrite=True, backup=False, indent=4):
if not overwrite and os.path.exists(filepath):
counter = 2
filedir, basename, ext = splitter(filepath)
while os.path.exists(filepath := os.path.join(filedir, basename+f' ({counter})'+ext)):
counter += 1
with open(filepath, 'w') as handler:
json.dump(dictionary, handler, indent=indent)
if backup:
filedir, basename, ext = splitter(filepath)
cls._dump(
os.path.join(
filedir,
basename + '.bac' + str(int(time.time())) + ext
),
dictionary,
overwrite=True,
backup=False,
indent=4
)
@classmethod
def view_positional(cls, result, term=None):
print(cls.__name__ + f" ({term})" if term else '')
print("Doc-ID | [Term-IDs]")
for doc_id, term_ids in result.items():
if isinstance(term_ids, list):
print(format(doc_id, '<7'), term_ids, sep=' | ')
print()
@classmethod
def view_nonpositional(cls, result, term=None):
print(cls.__name__ + f" ({term})" if term else '')
print('Doc-IDs:', result)
print()
def view_term(self, term):
if self.is_positional:
self.view_positional(self.get_term(term), term=term)
else:
self.view_nonpositional(self.get_term(term), term=term)
def get_term(self, term):
if not self.validate_term(term):
raise IndexError(f'Term <{term}> does not exists!')
return self._get_term(term)
def add_document(self, document, doc_id=None):
if doc_id is None:
self._doc_counter += 1
doc_id = self._doc_counter
if self.validate_document(doc_id):
raise IndexError(f'Doc-ID <{doc_id}> already exists!')
for term in document:
self.terms.add(term)
self.documents[doc_id] = document
self._add_document(document, doc_id)
return doc_id
def add_documents(self, docs):
for doc in docs:
self.add_document(doc)
def remove_document(self, doc_id):
if not self.validate_document(doc_id):
raise ValueError(f'Doc-ID <{doc_id}> does not exists!')
for term in self.documents.pop(doc_id):
if self.count_term(term) == 0:
self.terms.remove(term)
self._remove_document(doc_id)
def remove_documents(self, doc_ids):
for doc_id in doc_ids:
self.remove_document(doc_id)
def fetch_document(self, doc_id):
if not self.validate_document(doc_id):
raise IndexError(f'Doc-ID <{doc_id}> does not exists!')
return self.documents[doc_id]
def validate_document(self, doc_id):
return doc_id in self.documents
def validate_term(self, term):
return term in self.terms
def validate_terms(self, terms):
for term in terms:
if isinstance(term, list):
yield list(self.validate_terms(term))
elif self.validate_term(term):
yield term
def dump(self, filepath=None, **kwargs):
if filepath is None:
filepath = type(self).__name__ + '.json'
Index._dump(filepath, self.data, **kwargs)
def get_related_terms(self, term, itself=False):
return list(filter(lambda t: (term in t) and (True if itself else term != t), self.terms))
def format_query(self, query, wildcard=True, quote=True):
return search.format_query(query, index=self, wildcard=wildcard, quote=quote)
def search(self, query):
return search.search(self, self.documents, query)
def score(self, document, query):
return score.score(self.docs, document, query)
def average_precision(self, documents, query, minimum=0, maximum=1):
return score.average_precision(self.docs, documents, query, minimum=minimum, maximum=maximum)
def mean_average_precision(self, documents, queries, minimum=0, maximum=1):
return score.mean_average_precision(self.docs, documents, queries, minimum=minimum, maximum=maximum)
def steps_matrix(self, document, query):
return score.steps_matrix(self.docs, document, query)
def __setitem__(self, key, value):
self.data[key] = value
class Posting(Index, ABC):
def _get_term(self, term):
return self[term]
class Graph(Index, ABC):
def _get_term(self, term):
node = self.data
for char in term:
if char not in node:
return
node = node[char]
return node
def remove_key(self, key, node=None):
if node is None:
node = self.data
if isinstance(node, dict):
for item in Graph._get_doc_ids(node):
if item == key:
del node[key]
for value in node.values():
self.remove_key(key, node=value)
@classmethod
def _get_doc_ids(cls, dic):
return list(filter(lambda x: isinstance(x, int), dic.keys()))
class NonPositionalPosting(Posting):
is_positional = False
def _add_document(self, terms, doc_id):
for term in terms:
self.setdefault(term, list())
self[term].append(doc_id)
def _remove_document(self, doc_id):
for term, doc_ids in self.items():
if doc_id in doc_ids:
self[term].remove(doc_id)
def count_term(self, term):
return len(self.get_term(term))
class PositionalPosting(Posting):
is_positional = True
def _add_document(self, terms, doc_id):
for term_id, term in enumerate(terms):
self.setdefault(term, dict())
self[term].setdefault(doc_id, list())
self[term][doc_id].append(term_id)
def _remove_document(self, doc_id):
for term, result in self.items():
if doc_id in result:
self[term].pop(doc_id)
def count_term(self, term):
count = 0
for term_ids in self.get_term(term).values():
count += len(term_ids)
return count
class PositionalGraph(Graph):
is_positional = True
def _add_document(self, document, doc_id):
for term_id, term in enumerate(document):
node = self.data
for char in term:
node.setdefault(char, dict())
node = node[char]
node.setdefault(doc_id, list())
node[doc_id].append(term_id)
def _remove_document(self, doc_id):
self.remove_key(doc_id)
def count_term(self, term):
node = self.data
for char in term:
if char not in node:
break
node = node[char]
else:
return sum(map(len, map(lambda x: node[x], self._get_doc_ids(node))))
return -1
def splitter(path):
filedir, filename = os.path.split(path)
basename, extension = os.path.splitext(filename)
return filedir, basename, extension
if __name__ == '__main__':
documents = [
['hello', 'world'],
['hello', 'my', 'dear'],
['what', 'on', 'world', 'is', 'going'],
['how', 'the', 'world', 'seems', 'for', 'you']
]
print('Documents:', documents, '\n')
positional = PositionalIndex()
non_positional = NonPositionalIndex()
positional.add_documents(documents)
non_positional.add_documents(documents)
term = 'world'
positional.view_term(term)
print()
non_positional.view_term(term)