-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch_frontend.py
264 lines (233 loc) · 12 KB
/
search_frontend.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ I M P O R T S ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
from flask import Flask, request, jsonify
import search_backend
from search_backend import *
from google.cloud import storage
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ T A B L E O F C O N T E N T S ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#~~~~ 1. Run Class ~~~~
#~~~~ 2. run ~~~~
#~~~~ 2.1 inverted_body ~~~~
#~~~~ 2.2 inverted_title ~~~~
#~~~~ 2.3 inverted_anchor ~~~~
#~~~~ 2.4 prDict ~~~~
#~~~~ 2.4 wid2pv ~~~~
#~~~~ 3. Functions ~~~~
#~~~~ 3.1 search ~~~~
#~~~~ 3.2 search_body ~~~~
#~~~~ 3.2 search_title ~~~~
#~~~~ 3.2 search_anchor ~~~~
#~~~~ 3.2 get_pagerank ~~~~
#~~~~ 3.2 get_pageview ~~~~
#~~~~ 4. Main ~~~~
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ R U N C L A S S ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
class MyFlaskApp(Flask):
os.environ["GCLOUD_PROJECT"] = "ir-ass3-84763"
bucket_name = 'project_ir_os' # project_ir_os
client = storage.Client()
bucket = client.bucket(bucket_name)
inverted_body = None # load inverted index for body
inverted_title = None # load inverted index from bucket
inverted_anchor = None # load inverted index for anchor_text
prDict = None # load pageRank from bucket
wid2pv = None # load pageView index from bucket
# load inverted index from bucket
def run(self, host=None, port=None, debug=None, **options):
# body index from bucket
index_src = "index_body.pkl"
blob_index = bucket.blob(f"postings_gcp_body/{index_src}")
pickel_in = blob_index.download_as_string()
self.inverted_body = pickle.loads(pickel_in)
# title index from bucket
title_src = "index_title.pkl"
blob_title = bucket.blob(f"postings_gcp_title/{title_src}")
pickel_in = blob_title.download_as_string()
self.inverted_title = pickle.loads(pickel_in)
# anchor_text index from bucket
anchor_text_src = "index_anchor.pkl"
blob_anchor_text = bucket.blob(f"postings_gcp_anchor/{anchor_text_src}")
pickel_in = blob_anchor_text.download_as_string()
self.inverted_anchor = pickle.loads(pickel_in)
# pagerank from bucket
pageRank_src = "pageRank.pkl"
blob_pageRank = bucket.blob(f"pr/{pageRank_src}")
pickel_in = blob_pageRank.download_as_string()
self.prDict = pickle.loads(pickel_in)
# PageView from bucket
pv_src = "pv_rank.pkl"
blob_PV = bucket.blob(f"pv/{pv_src}")
pickel_in = blob_PV.download_as_string()
self.wid2pv = pickle.loads(pickel_in)
super(MyFlaskApp, self).run(host=host, port=port, debug=debug, **options)
app = MyFlaskApp(__name__)
app.config['JSONIFY_PRETTYPRINT_REGULAR'] = False
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ F U N C T I O N S ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@app.route("/search")
def search():
''' Returns up to a 100 of your best search results for the query. This is
the place to put forward your best search engine, and you are free to
implement the retrieval whoever you'd like within the bound of the
project requirements (efficiency, quality, etc.). That means it is up to
you to decide on whether to use stemming, remove stopwords, use
PageRank, query expansion, etc.
To issue a query navigate to a URL like:
http://YOUR_SERVER_DOMAIN/search?query=hello+world
where YOUR_SERVER_DOMAIN is something like XXXX-XX-XX-XX-XX.ngrok.io
if you're using ngrok on Colab or your external IP on GCP.
Returns:
--------
list of up to 100 search results, ordered from best to worst where each
element is a tuple (wiki_id, title).
'''
res = []
query = request.args.get('query', '')
if len(query) == 0:
return jsonify(res)
# BEGIN SOLUTION
res = search_backend.BM25_and_binary_search(app.inverted_body, app.inverted_title, query)
# END SOLUTION
return jsonify(res)
@app.route("/search_body")
def search_body():
''' Returns up to a 100 search results for the query using TFIDF AND COSINE
SIMILARITY OF THE BODY OF ARTICLES ONLY. DO NOT use stemming. DO USE the
staff-provided tokenizer from Assignment 3 (GCP part) to do the
tokenization and remove stopwords.
To issue a query navigate to a URL like:
http://YOUR_SERVER_DOMAIN/search_body?query=hello+world
where YOUR_SERVER_DOMAIN is something like XXXX-XX-XX-XX-XX.ngrok.io
if you're using ngrok on Colab or your external IP on GCP.
Returns:
--------
list of up to 100 search results, ordered from best to worst where each
element is a tuple (wiki_id, title).
'''
res = []
query = request.args.get('query', '')
if len(query) == 0:
return jsonify(res)
# BEGIN SOLUTION
res = search_backend.search_body_implement(app.inverted_body, query)
# END SOLUTION
return jsonify(res)
@app.route("/search_title")
def search_title():
''' Returns ALL (not just top 100) search results that contain A QUERY WORD
IN THE TITLE of articles, ordered in descending order of the NUMBER OF
DISTINCT QUERY WORDS that appear in the title. DO NOT use stemming. DO
USE the staff-provided tokenizer from Assignment 3 (GCP part) to do the
tokenization and remove stopwords. For example, a document
with a title that matches two distinct query words will be ranked before a
document with a title that matches only one distinct query word,
regardless of the number of times the term appeared in the title (or
query).
Test this by navigating to the a URL like:
http://YOUR_SERVER_DOMAIN/search_title?query=hello+world
where YOUR_SERVER_DOMAIN is something like XXXX-XX-XX-XX-XX.ngrok.io
if you're using ngrok on Colab or your external IP on GCP.
Returns:
--------
list of ALL (not just top 100) search results, ordered from best to
worst where each element is a tuple (wiki_id, title).
'''
res = []
query = request.args.get('query', '')
if len(query) == 0:
return jsonify(res)
# BEGIN SOLUTION
TITLE_SCORE = booleanRanking(app.inverted_title, query)
res = returnTopNdocWithTitles(inverted_title=app.inverted_title, searchDictVal=TITLE_SCORE)
# END SOLUTION
return jsonify(res)
@app.route("/search_anchor")
def search_anchor():
''' Returns ALL (not just top 100) search results that contain A QUERY WORD
IN THE ANCHOR TEXT of articles, ordered in descending order of the
NUMBER OF QUERY WORDS that appear in anchor text linking to the page.
DO NOT use stemming. DO USE the staff-provided tokenizer from Assignment
3 (GCP part) to do the tokenization and remove stopwords. For example,
a document with a anchor text that matches two distinct query words will
be ranked before a document with anchor text that matches only one
distinct query word, regardless of the number of times the term appeared
in the anchor text (or query).
Test this by navigating to the a URL like:
http://YOUR_SERVER_DOMAIN/search_anchor?query=hello+world
where YOUR_SERVER_DOMAIN is something like XXXX-XX-XX-XX-XX.ngrok.io
if you're using ngrok on Colab or your external IP on GCP.
Returns:
--------
list of ALL (not just top 100) search results, ordered from best to
worst where each element is a tuple (wiki_id, title).
'''
res = []
query = request.args.get('query', '')
if len(query) == 0:
return jsonify(res)
# BEGIN SOLUTION
ANCHOR_TEXT_SCORE = booleanRanking(app.inverted_anchor, query)
res = returnTopNdocWithTitles(inverted_title=app.inverted_anchor, searchDictVal=ANCHOR_TEXT_SCORE)
# END SOLUTION
return jsonify(res)
@app.route("/get_pagerank", methods=['POST'])
def get_pagerank():
''' Returns PageRank values for a list of provided wiki article IDs.
Test this by issuing a POST request to a URL like:
http://YOUR_SERVER_DOMAIN/get_pagerank
with a json payload of the list of article ids. In python do:
import requests
requests.post('http://YOUR_SERVER_DOMAIN/get_pagerank', json=[1,5,8])
As before YOUR_SERVER_DOMAIN is something like XXXX-XX-XX-XX-XX.ngrok.io
if you're using ngrok on Colab or your external IP on GCP.
Returns:
--------
list of floats:
list of PageRank scores that correrspond to the provided article IDs.
'''
res = []
wiki_ids = request.get_json()
if len(wiki_ids) == 0:
return jsonify(res)
# BEGIN SOLUTION
res = [app.prDict.get(i, 0) for i in wiki_ids]
# END SOLUTION
return jsonify(res)
@app.route("/get_pageview", methods=['POST'])
def get_pageview():
''' Returns the number of page views that each of the provide wiki articles
had in August 2021.
Test this by issuing a POST request to a URL like:
http://YOUR_SERVER_DOMAIN/get_pageview
with a json payload of the list of article ids. In python do:
import requests
requests.post('http://YOUR_SERVER_DOMAIN/get_pageview', json=[1,5,8])
As before YOUR_SERVER_DOMAIN is something like XXXX-XX-XX-XX-XX.ngrok.io
if you're using ngrok on Colab or your external IP on GCP.
Returns:
--------
list of ints:
list of page view numbers from August 2021 that correrspond to the
provided list article IDs.
'''
res = []
wiki_ids = request.get_json()
if len(wiki_ids) == 0:
return jsonify(res)
# BEGIN SOLUTION
res = [app.wid2pv.get(view, 0) for view in wiki_ids]
# END SOLUTION
return jsonify(res)
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ M A I N ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if __name__ == '__main__':
# run the Flask RESTful API, make the server publicly available (host='0.0.0.0') on port 8080
app.run(host='0.0.0.0', port=8080, debug=True)