-
Notifications
You must be signed in to change notification settings - Fork 0
/
search_engine_3.py
51 lines (40 loc) · 1.88 KB
/
search_engine_3.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
from search_engine_interface import search_engine_interface
from searcher import Searcher
from nltk.corpus import wordnet
from configuration import ConfigClass
class SearchEngine(search_engine_interface):
##############################################
########### WordNet ###########
##############################################
def __init__(self, config=None):
super(SearchEngine, self).__init__(config)
self.local_cache = {}
def search(self, query):
query_as_list = self._parser.parse_sentence(query)
query_expansion = self.query_expansion(query_as_list)
self.add_similar_word_to_query(query_as_list, query_expansion)
searcher = Searcher(self._parser, self._indexer, model=self._model)
n_relevant, ranked_doc_ids = searcher.search(query_as_list,5)
return n_relevant, ranked_doc_ids
def add_similar_word_to_query(self, query_as_list, query_expansion):
query_as_list.extend([word for word in query_expansion])
def query_expansion(self, query_as_list):
result = []
for term in query_as_list:
if not term[0].isupper():
if term in self.local_cache:
result.extend(self.local_cache[term])
else:
synonyms = []
for syn in wordnet.synsets(term):
for l in filter(lambda el: el.name() not in [term, term.lower()] + result, syn.lemmas()[:3]):
synonyms.append(l.name())
self.local_cache[term] = synonyms[:3]
result.extend(synonyms[:3])
return result
if __name__ == '__main__':
s = SearchEngine()
s.build_index_from_parquet("/Users/samuel/Desktop/Corpus/test")
s.search("Coronavirus is less dangerous than the flu")
#'bioweapon'
s.query_expansion(['dangerous','Trump'])