-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.py
39 lines (31 loc) · 1.38 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
# -*- coding: utf-8 -*-
"""
Answers queries about documents with tf-idf
"""
import json
from flask import Flask, request, render_template
from term_document import TermDocumentMatrixDD
from tokenizers import MY_TOKENIZER_INDEX, MY_TOKENIZER_SEARCH, MY_TOKENIZER_NO_STEMMING
from loader import LOADER_MONGODB
import settings
tfidf_vanilla = TermDocumentMatrixDD(LOADER_MONGODB, MY_TOKENIZER_INDEX, MY_TOKENIZER_INDEX)
tfidf_word2vec = TermDocumentMatrixDD(LOADER_MONGODB, MY_TOKENIZER_INDEX, MY_TOKENIZER_SEARCH) # query expansion with word2vec
app = Flask(__name__)
@app.route("/")
def search():
query = request.args.get('q', '')
if not query:
query = "white wooden baby bedding"
return render_template('search.html', results=tfidf_vanilla.search(query),
text='<p>A search page using word2vec (to provide synonyms+weights) is <a href="/word2vec">available here</a>.</p>')
@app.route("/word2vec")
def search_word2vec():
query = request.args.get('q', '')
return render_template('search.html', results=tfidf_word2vec.search(query))
@app.route("/similar")
def similar():
""" Show some word2vec similar words"""
query = request.args.get('q', '')
return render_template('similar.html', results=MY_TOKENIZER_NO_STEMMING.tokenize(query))
if __name__ == "__main__":
app.run(debug=settings.DEBUG, port=6001)