-
Notifications
You must be signed in to change notification settings - Fork 0
/
aggregate_search.py
31 lines (21 loc) · 1.11 KB
/
aggregate_search.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 logging.config
from search_engine.engine.engines.d2v_engine import D2VEngine
from search_engine.engine.engines.lda_engine import LdaEngine
from search_engine.engine.engines.tfidf_engine import TfidfEngine
class AggregateSearch(object):
def __init__(self, *engines):
self.engines = engines
def search(self, query, limit=50):
dict_results = [engine.dict_search(query, limit=limit) for engine in self.engines]
results = dict_results[0].keys()
weighted_results = [(url, self.weighted_res(url, dict_results)) for url in results]
return sorted(weighted_results, key=lambda x: -x[1])
def weighted_res(self, url, dict_results):
return sum([res[url] for res in dict_results if url in res]) / len(self.engines)
if __name__ == "__main__":
logging.config.fileConfig("search_engine/configuration/logger.cfg", disable_existing_loggers=False)
search = AggregateSearch(LdaEngine.from_configfile(), TfidfEngine.from_configfile(), D2VEngine.from_configfile())
inp = ""
while inp != "q":
inp = input("Enter query: ")
print(search.search(inp)[:3])