-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
37 lines (33 loc) · 1.05 KB
/
app.py
File metadata and controls
37 lines (33 loc) · 1.05 KB
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
from flask import Flask, render_template, request, Response,redirect, url_for
import re
from gensim.models import Word2Vec
app = Flask(__name__)
model = Word2Vec.load('./data/model')
def getResult(pos, neg):
try:
return model.most_similar(positive=pos, negative=neg)[:5]
except:
return []
@app.route('/')
def home():
pos = []
neg = []
res = []
non = []
query = request.args.get('query')
if query:
for p in query.split('+'):
d = p.split('-')
if len(d) > 1: neg.append(d[1].strip())
pos.append(d[0].strip())
pos = list(filter(lambda x: x in model.wv.vocab, pos))
neg = list(filter(lambda x: x in model.wv.vocab, neg))
print (pos, neg)
res = getResult(pos, neg)
return render_template('home.html', positive=pos, negative=neg, results=res)
@app.route('/query', methods=['POST'])
def query():
query = request.form['query']
return redirect(url_for('home', query=query))
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=False)