-
Notifications
You must be signed in to change notification settings - Fork 0
/
inverted_index.py
137 lines (106 loc) · 3.41 KB
/
inverted_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
from operator import itemgetter
from typing import Dict
from helpers import read_csv_and_cleanup
def ii_execute():
docs, product_names = read_csv_and_cleanup('gathered-products-detail')
# Construct an inverted index
inverted_index: Dict[str, Dict[str, int | set]] = {}
for i, doc in enumerate(docs):
for term in doc.split():
if term in inverted_index:
inverted_index[term]['posting-list'].add(i)
inverted_index[term]['freq'] = len(inverted_index[term]['posting-list'])
else:
inverted_index[term] = {
'freq': 1,
'posting-list': {i}
}
# Sorting our inverted index alphabetically
inverted_index = dict(sorted(inverted_index.items(), key=lambda x: x[0].lower()))
# Perform or boolean retrieval
def or_postings(posting1, posting2):
p1 = 0
p2 = 0
result = list()
while p1 < len(posting1) and p2 < len(posting2):
if posting1[p1] == posting2[p2]:
result.append(posting1[p1])
p1 += 1
p2 += 2
elif posting1[p1] > posting2[p2]:
result.append(posting2[p2])
p2 += 1
else:
result.append(posting1[p1])
p1 += 1
while p1 < len(posting1):
result.append(posting1[p1])
p1 += 1
while p2 < len(posting2):
result.append(posting2[p2])
p2 += 1
return result
def and_postings(posting1, posting2):
p1 = 0
p2 = 0
result = list()
while p1 < len(posting1) and p2 < len(posting2):
if posting1[p1] == posting2[p2]:
result.append(posting1[p1])
p1 += 1
p2 += 1
elif posting1[p1] > posting2[p2]:
p2 += 1
else:
p1 += 1
return result
print('Pick: AND or OR Boolean Retrieval')
retriveal_type = input("Enter 'AND' or 'OR': ")
if retriveal_type == 'AND':
print("AND Boolean Retrieval\n")
posting_lists = []
while True:
term = input("Enter term: ")
if term != '.':
if term not in inverted_index:
continue
posting_lists.append(list(inverted_index[term]['posting-list']))
else:
break
if len(posting_lists) == 0:
return 0
# Sort postings lists asc by length (AND query performance improvement)
sorted_posting_lists = sorted(posting_lists, key=lambda x: len(x))
result = list()
for i in range(0, len(sorted_posting_lists), 2):
pl_1 = sorted_posting_lists[i]
pl_2 = sorted_posting_lists[i]
if i + 1 < len(sorted_posting_lists):
pl_2 = sorted_posting_lists[i + 1]
result = and_postings(pl_1, pl_2)
if len(result) == 0:
return 0
print(list(itemgetter(*result)(product_names))) if len(result) > 1 else print([product_names[result[0]]])
else:
print("OR Boolean Retrieval\n")
posting_lists = []
while True:
term = input("Enter term: ")
if term != '.':
if term not in inverted_index:
continue
posting_lists.append(list(inverted_index[term]['posting-list']))
else:
break
if len(posting_lists) == 0:
return 0
result = list()
for i in range(0, len(posting_lists), 2):
pl_1 = posting_lists[i]
pl_2 = list()
if i + 1 < len(posting_lists):
pl_2 = posting_lists[i + 1]
result = or_postings(pl_1, pl_2)
if len(result) == 0:
return 0
print(list(itemgetter(*result)(product_names))) if len(result) > 1 else print([product_names[result[0]]])