-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
104 lines (75 loc) · 2.69 KB
/
app.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
from flask import Flask, render_template, request, abort
from stopwords import stopwords
import sqlite3
import json
import time
app = Flask(__name__)
query_template2 = """SELECT
rowid,
title,
snippet(stackoverflow_fts, '<b>', '</b>', '...', 1, 40)
FROM stackoverflow_fts
WHERE title
MATCH ? LIMIT 10;"""
query_template = """SELECT
stackoverflow_fts.rowid,
stackoverflow_fts.title,
snippet(stackoverflow_fts.body)
FROM stackoverflow_fts JOIN stackoverflow_posts ON
stackoverflow_fts.rowid=stackoverflow_posts.rowid WHERE stackoverflow_fts.title
MATCH ? LIMIT 10;"""
question_query = """ SELECT id, score, title, body FROM stackoverflow_posts WHERE rowid = ? LIMIT 1; """
answer_query = """ SELECT id, score, ownerdisplayname, body, owneruserid FROM stackoverflow_posts WHERE parentid=? ORDER BY score DESC """
@app.route('/')
def index():
return render_template('index.html')
#Accepts parameters:
# query: string to query questions for
@app.route('/query', methods=["POST"])
def query():
conn = sqlite3.connect("stackoverflow.sqlite3")
db = conn.cursor()
db.arraysize = 10
q = request.form.get("query")
filtered = q.split()
filtered = ' '.join([x for x in filtered if x not in stopwords])
print(filtered)
if not q:
abort(404)
start = time.time()
db.execute(query_template2, (filtered,))
end = time.time()
print('query took {}s'.format(end-start))
start = time.time()
data = json.dumps(db.fetchall())
end = time.time()
print('retrieiving took {}s'.format(end-start))
conn.close()
return data
@app.route('/question/<qid>')
def question(qid):
conn = sqlite3.connect("stackoverflow.sqlite3")
db = conn.cursor()
db.execute(question_query, (qid,))
answer = db.fetchone()
if answer is None:
abort(404)
question = { "id" : answer[0],
"score" : answer[1],
"title" : answer[2],
"body" : answer[3] }
db.execute(answer_query, (question['id'],))
answers_raw = db.fetchall()
answers = []
for answer_i in answers_raw:
ans = { "id" : answer_i[0],
"score" : answer_i[1],
"author" : answer_i[2],
"body" : answer_i[3] }
if answer_i[2] == "NULL":
ans['author'] = "user{}".format(answer_i[4])
answers.append(ans)
return json.dumps({'question' : question, 'answers' : answers})
if __name__ == '__main__':
app.debug = True
app.run()