-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindexer.py
31 lines (27 loc) · 1.09 KB
/
indexer.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
import re
class Indexer:
def __init__(self, n: int) -> None:
self.currentTermIndex = 0
self.n = n
def removeSpecialChars(self, term: str):
return re.sub(r'[^a-zA-Z0-9]','', term)
def processPage(self, index: dict[str, list[int]], termIndexMap: dict[str, int], text: str, id: int):
terms = text.split()
for term in terms:
editedTerm = self.removeSpecialChars(term)
if (editedTerm == ""):
continue
if (editedTerm not in termIndexMap):
termIndexMap[editedTerm] = self.currentTermIndex
self.currentTermIndex += 1
if (editedTerm not in index):
index[editedTerm] = []
if (id not in index[editedTerm]):
index[editedTerm].append(id)
def build(self):
termIndexMap: dict[str, int] = {}
index: dict[str, list[int]] = {}
for i in range(0, self.n):
text = open('pages/' + str(i) + '.txt', 'r').read()
self.processPage(index, termIndexMap, text, i)
return (index, termIndexMap)