-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
77 lines (53 loc) · 1.87 KB
/
server.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
from flask import Flask, request, redirect
app = Flask(__name__)
entities = {"ORDINAL", "LOC", "DATE", "LANGUAGE", "NORP","PRODUCT", "GPE"}
from gensim.models import KeyedVectors
import spacy
import random
import requests
nlp = spacy.load('en_core_web_sm')
gmodel=KeyedVectors.load_word2vec_format("GoogleNews-vectors-negative300.bin", binary=True)
def changeQuerry(query):
doc = nlp(query)
queryOptions = []
entitiesToReplace = []
for ent in doc.ents:
entitiesToReplace.append(ent.text)
for ent in doc:
# print(ent.pos_)
if ent.text in entitiesToReplace:
temp = [ent.text]
sim = gmodel.most_similar(ent.text)
for i in sim:
temp.append(i[0])
queryOptions.append(temp)
else:
queryOptions.append([ent.text])
# print (queryOptions)
# print (entitiesToReplace)
queries = []
for i in range(5):
temp = ""
for j in queryOptions:
if len(j) == 1:
temp += str(j[0]) + " "
else:
index = random.randint(1, len(j)-1)
temp += str(j[index]) + " "
temp = temp.strip()
queries.append(temp)
return queries
@app.route('/search' , methods=['POST', 'GET'])
def search():
query = request.values.get('id').strip()
backgroundQueries = [ "https://www.google.com/search?q=" + "+".join(i.strip().split(" ")) for i in changeQuerry(query)]
print ("Making call to related random queries")
for i in backgroundQueries:
print(i)
r = requests.get(i)
return "https://www.google.com/search?q=" + "+".join(query.split(" "))
@app.route('/' , methods=['POST', 'GET'])
def test():
return "Server is alive"
if __name__ == '__main__':
app.run(debug=True)