-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsummary_search.py
125 lines (103 loc) · 4.15 KB
/
summary_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
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
""" Aby uruchomić produkcyjną wersję należy skorzystać z gunicorna
Uruchamiamy poleceniem:
gunicorn -w THREADS -b HOST:PORT summary_search:app
- THREADS - liczba niezależnych workerów (wątków)
- HOST - host, dla localhosta ustawić 127.0.0.1
- PORT - port, można wybrać dowolny, polecam 5000
"""
from flask import Flask, jsonify, request, abort, render_template, url_for
from services.SeleniumServices import DuckDuckGoService, BingService, GoogleService, YahooService
from services.QueryGeneration import QueryGenerator, SingleQueryStrategy, StopwordsStrategy, ChunksStrategy
app = Flask(__name__)
app_name = "Zapytajka - Silnik wyszukiwania podsumowań"
engines = ["combined", "duckduckgo", "bing", "google", "yahoo"]
strategies = ["singlequery", "stopwords", "chunks"]
def create_snippet_service(engine):
if engine == "duckduckgo":
snippet_service = DuckDuckGoService()
elif engine == "bing":
snippet_service = BingService()
elif engine == "google":
snippet_service = GoogleService()
elif engine == "yahoo":
snippet_service = YahooService()
else:
abort(404, description="Search engine not found")
return snippet_service
def create_query_strategy(strategy):
if strategy == "singlequery":
strategy_algorithm = SingleQueryStrategy()
elif strategy == "stopwords":
strategy_algorithm = StopwordsStrategy()
elif strategy == "chunks":
strategy_algorithm = ChunksStrategy()
else:
abort(404, description="Query generation strategy not found")
return strategy_algorithm
def get_summaries_by_engine(queries, engine):
snippet_service = create_snippet_service(engine)
results = list()
for query in queries:
results += snippet_service.process_query(query)
return results
def get_summaries_combined(queries):
processors = list()
processors.append(DuckDuckGoService())
processors.append(BingService())
processors.append(GoogleService())
processors.append(YahooService())
results = list()
for query in queries:
for processor in processors:
results += processor.process_query(query)
return results
@app.route("/")
def index():
data = {
"engines": engines,
"strategies": strategies
}
return render_template('summary/index.html', app_name=app_name, data=data)
@app.route("/fe/search", methods=["GET"])
def show_results():
engine = request.args.get("engine")
strategy = request.args.get("strategy")
query = request.args.get("query")
strategy_algorithm = create_query_strategy(strategy)
query_generator = QueryGenerator(strategy_algorithm)
queries = query_generator.generate_queries(query)
if engine != "combined":
results = get_summaries_by_engine(queries, engine)
else:
results = get_summaries_combined(queries)
return render_template('summary/result.html',
app_name=app_name,
query=request.args.get("query"),
records=len(results),
strategy=strategy,
data=results)
@app.route("/search/<engine>/<strategy>/<query>", methods=["GET"])
def search(engine, strategy, query):
strategy_algorithm = create_query_strategy(strategy)
query_generator = QueryGenerator(strategy_algorithm)
queries = query_generator.generate_queries(query)
if engine == "combined":
results = get_summaries_combined(queries)
else:
results = get_summaries_by_engine(queries, engine)
return jsonify(results)
@app.route("/combined/<strategy>/<query>", methods=["GET"])
def combined_results(strategy, query):
strategy_algorithm = create_query_strategy(strategy)
query_generator = QueryGenerator(strategy_algorithm)
queries = query_generator.generate_queries(query)
results = get_summaries_combined(queries)
return jsonify(results)
@app.route("/engines", methods=["GET"])
def engines_list():
return jsonify(engines)
@app.route("/strategies", methods=["GET"])
def strategies_list():
return jsonify(strategies)
if __name__ == "__main__":
app.run()