-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
177 lines (134 loc) · 4.91 KB
/
main.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
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
import nltk
from nltk.corpus import gutenberg
from nltk.corpus import brown
from nltk.corpus import stopwords
# from nltk.tokenize import word_tokenize
nltk.download('brown')
nltk.download('gutenberg')
class Preprocess:
stemmer = nltk.PorterStemmer()
stop_words = set(stopwords.words('english'))
def remove_stopwords(self, list):
return [word for word in list if word not in self.stop_words]
def clean(self, string):
string = string.replace(".", "")
string = string.replace("\s+", " ")
string = string.lower()
return string
def tokenise(self, string):
string = self.clean(string)
words = string.split(" ")
return [self.stemmer.stem(word) for word in words]
def prep(self, document):
clean_text = self.clean(document['text'])
all_terms = self.tokenise(clean_text)
return self.remove_stopwords(all_terms)
class Appearance:
def __init__(self, docId, frequency):
self.docId = docId
self.frequency = frequency
def __repr__(self):
return str(self.__dict__)
class Database:
def __init__(self):
self.db = dict()
def __repr__(self):
return str(self.__dict__)
def get(self, id):
return self.db.get(id, None)
def add(self, document):
return self.db.update({document['id']: document})
def remove(self, document):
return self.db.pop(document['id'], None)
class InvertedIndex:
"""
Inverted Index class.
"""
def __init__(self, db):
self.index = dict()
self.db = db
def __repr__(self):
return str(self.index)
def index_document(self, document):
preproc = Preprocess()
# clean_text = main.clean(document['text'])
# all_terms = main.tokenise(clean_text)
# terms = main.remove_stopwords(all_terms)
terms = preproc.prep(document)
appearances_dict = dict()
for term in terms:
term_frequency = appearances_dict[term].frequency if term in appearances_dict else 0
appearances_dict[term] = Appearance(document['id'], term_frequency + 1)
update_dict = {key: [appearance]
if key not in self.index
else self.index[key] + [appearance]
for (key, appearance) in appearances_dict.items()}
self.index.update(update_dict)
self.db.add(document)
return document
def lookup_query(self, query):
"""
Returns the dictionary of terms with their correspondent Appearances.
This is a very naive search since it will just split the terms and show
the documents where they appear.
"""
return {term: self.index[term] for term in query.split(' ') if term in self.index}
def lookup_vect(self,query):
list= {term: self.index[term] for term in query.split(' ') if term in self.index}
class Vectorize:
def getVectorKeywordIndex(self, wordList):
vectorIndex = {}
offset = 0
# Associate a position with the keywords which maps to the dimension on the vector used to represent this word
for word in wordList:
vectorIndex[word] = offset
offset += 1
return vectorIndex
def makeVector(self, vectorIndex, document):
""" @pre: unique(vectorIndex) """
# Initialise vector with 0's
vector = [0] * len(vectorIndex)
preproc = Preprocess()
wordList = preproc.prep(document)
for word in wordList:
vector[word] += 1; # Use simple Term Count Model
return vector
def highlight_term(id, term, text):
replaced_text = text.replace(term, "\033[1;32;40m {term} \033[0;0m".format(term=term))
return "--- document {id}: {replaced}".format(id=id, replaced=replaced_text)
def Main():
db = Database()
index = InvertedIndex(db)
brown_list = brown.fileids()
gutenberg_list = gutenberg.fileids()
# document1 = {
# 'id': '1',
# 'text': 'The big sharks of Belgium drink beer.'
# }
# document2 = {
# 'id': '2',
# 'text': 'Belgium has great beer. They drink beer all the time.'
# }
i = 0;
for item in brown_list:
documentTemp = {
'id': str(i),
'text': brown.raw(item)
}
index.index_document(documentTemp)
for item in gutenberg_list:
documentTemp = {
'id': str(i),
'text': gutenberg.raw(item)
}
index.index_document(documentTemp)
while True:
search_term = input("Enter term(s) to search: ")
result = index.lookup_query(search_term.lower())
for term in result.keys():
for appearance in result[term]:
# Belgium: { docId: 1, frequency: 1}
document = db.get(appearance.docId)
print(highlight_term(appearance.docId, term, document['text']))
print("-----------------------------")
Main()