-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
147 lines (115 loc) · 3.95 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
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
import os
import configs
import re
from flask import Flask , render_template, send_from_directory,request,redirect,url_for,session
from flask_session import Session
from elasticsearch7 import Elasticsearch
from Processor import Processor
app = Flask(__name__)
app.config["SESSION_PERMANENT"] = False
app.config["SESSION_TYPE"] = "filesystem"
es_client = Elasticsearch(HOST=configs.HOST, PORT=configs.PORT)
Session(app)
@app.route('/')
def home():
if not session.get("results"):
session["results"]=[]
if not session.get("msg"):
session["msg"]=["WELCOME"]
results=session["results"]
msg=session["msg"]
return render_template('home.html',results=results,msg=msg)
@app.route('/submit',methods=['POST'])
def submit():
search_term = request.form['form-search']
processor = Processor()
search_term = processor.strip(search_term)
search_term = processor.special_char_remove(search_term)
tokens = processor.tokenize(search_term)
tokens = processor.stop_word_remove(tokens)
get_boosters=processor.get_boosters(tokens)
get_sorting_type = processor.get_sorter(tokens)
get_range = processor.get_range(tokens)
query_text = processor.query_text(tokens)
query={
"multi_match":{
"query":query_text,
"fields":get_boosters,
"operator":"or",
"type":"best_fields",
"fuzziness":"AUTO"
}
}
if get_sorting_type!=None:
sort=[
get_sorting_type
]
response = es_client.search(index=configs.INDEX,query=query,sort=sort,size=get_range)
else:
response = es_client.search(index=configs.INDEX,query=query,size=get_range)
songs_num = len(response["hits"]["hits"])
songs=[]
for i in range(songs_num):
songs.append(response["hits"]["hits"][i]["_source"])
msg =[]
msg.append("search term : "+str(search_term))
msg.append("tokens : "+str(query_text))
msg.append("sorting type : "+str(get_sorting_type))
msg.append("max range : "+str(get_range))
msg.append("boosters : "+str(get_boosters))
msg.append("no of results : "+str(songs_num))
session["results"]=songs
session["msg"]=msg
return redirect(url_for('home'))
@app.route('/favicon.ico')
def favicon():
return send_from_directory(os.path.join(app.root_path, 'templates'),
'favicon.ico',mimetype='image/vnd.microsoft.icon')
@app.route('/styles.css')
def styles():
return send_from_directory(os.path.join(app.root_path, 'templates'),
'styles.css',mimetype='text/css')
@app.get('/suggestions/<typed>')
def suggestions(typed):
all=str(typed).split()
last=all.pop()
all_str=""
for i in all:
all_str+=i+" "
query= {
"interpretation": {
"prefix": last,
"completion": {
"field": "metaphors.interpretation"
}
},
"lyrics": {
"prefix": last,
"completion": {
"field": "lyrics"
}
}
}
response = es_client.search(index=configs.INDEX,suggest=query)
suggestions=set()
for val in response['suggest']["interpretation"][0]['options']:
text = ""
for chr in str(val['text']):
if chr not in "[!@#$%^&*()[];:,./<>?\|`~-=_+]":
text+=chr
words = text.strip().split()
for word in words:
if last in word:
suggestions.add(all_str+word)
for val in response['suggest']["lyrics"][0]['options']:
text = ""
for chr in str(val['text']):
if chr not in "[!@#$%^&*()[];:,./<>?\|`~-=_+]":
text+=chr
words = text.strip().split()
for word in words:
if last in word:
suggestions.add(all_str+word)
return list(suggestions)
if __name__ == '__main__':
app.run(debug=True)