-
Notifications
You must be signed in to change notification settings - Fork 0
/
reverse_index.py
79 lines (59 loc) · 2.21 KB
/
reverse_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
from BTrees.OOBTree import OOBTree
from collections import defaultdict
class Reverse_index:
def __init__(self, index_type='dict'): # BTrees are slow
if index_type == 'BTree':
self.reverse_index = OOBTree()
self.index_type = 'BTree'
elif index_type == 'dict':
self.reverse_index = defaultdict(dict)
self.index_type = 'defaultdict'
else:
raise ValueError('Incorrect index type!')
self.id_set = None
self.idf = {}
self.other_infos = {}
return None
def _store_all_ids(self):
ids_list = []
for word in self.reverse_index:
ids_list.extend(self.reverse_index[word].keys())
return set(ids_list)
def get_index(self):
return self.reverse_index
def get_entry(self, term):
return self.reverse_index[term]
def get_all_ids_set(self):
if self.id_set:
return self.id_set
else:
self.set_id_set(self._store_all_ids())
return self.id_set
def set_id_set(self, id_set):
self.id_set = id_set
return None
def get_ids_for_term(self, term):
return self.get_entry(term).keys()
def get_ponderation(self, term, document_id):
dict_of_docs = self.get_entry(term)
if document_id in dict_of_docs.keys():
return dict_of_docs[document_id]
else:
return 0
def get_all_words(self):
return self.get_index().keys()
def add_entry(self, term, document_id, ponderation):
if self.index_type == 'BTree':
self._add_entry_btree(term, document_id, ponderation)
else:
self._add_entry_defaultdict(term, document_id, ponderation)
return None
def _add_entry_defaultdict(self, term, document_id, ponderation):
self.reverse_index[term][document_id] = ponderation
return None
def _add_entry_btree(self, term, document_id, ponderation):
if(term in self.reverse_index.keys()): # The term is already in the tree
self.reverse_index[term][document_id] = ponderation
else:
self.reverse_index[term] = {document_id: ponderation}
return None