-
Notifications
You must be signed in to change notification settings - Fork 0
/
indexer.py
40 lines (33 loc) · 1.14 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
32
33
34
35
36
37
38
39
40
import PyPDF4, re
class Indexer:
def __init__(self, file):
self.file = file
self.__index = {}
self.__stop_words = open("stopwords/stopwords.txt", "r").read()
def extractWords(self, text):
words = []
for word in re.split('\n| ', text):
if word.isalnum():
if(word not in self.__stop_words):
words.append(word.lower())
return words
def addToIndex(self, words, page):
for word in words:
if word in self.__index:
self.__index[word].append(page)
else:
self.__index[word] = [page]
def getPdfWords(self, file):
pdfFileObj = open(file, 'rb')
pdfReader = PyPDF4.PdfFileReader(pdfFileObj)
i = 0
while i < pdfReader.numPages:
pageObj = pdfReader.getPage(i)
words = self.extractWords(pageObj.extractText())
self.addToIndex(words, i)
i += 1
def createReverseIndex(self):
self.getPdfWords(self.file)
return self.__index
Economics = Indexer("Economics-2018.pdf")
index = Economics.createReverseIndex()