-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
209 lines (177 loc) · 6.23 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
from gensim.models.doc2vec import Doc2Vec, TaggedDocument
from gensim.models.word2vec import Word2Vec
from nltk.tokenize import word_tokenize
from flask import Flask, request, jsonify, render_template, send_from_directory
import pandas as pd
import os
from flask_cors import CORS
import itertools
import gensim.downloader
import pickle
import joblib
import random, re
from nltk.corpus import stopwords
stp = stopwords.words("english")
from gensim.models.callbacks import CallbackAny2Vec
# init callback class
class callback(CallbackAny2Vec):
"""
Callback to print loss after each epoch
"""
def __init__(self):
self.epoch = 0
self.model_dir = "gensim-models"
def on_epoch_end(self, model):
loss = model.get_latest_training_loss()
if self.epoch == 0:
print('Loss after epoch {}: {}'.format(self.epoch, loss))
elif self.epoch % 10 == 0:
l = loss- self.loss_previous_step
print('Loss after epoch {}: {}'.format(self.epoch, l))
model.save(f'{self.model_dir}/{self.epoch}-{l}.model')
print(f"Saved model successfully {self.epoch}-{l}")
self.epoch += 1
self.loss_previous_step = loss
word_model = Word2Vec.load(os.path.join("Word2vec", 'w2vec_smart.model'))
vectorizerF = joblib.load("vectorizerF.pkl")
km = joblib.load("km.pkl")
with open("k-means-doc2vec_DBOW.sav", "rb") as fp:
k = pickle.load(fp)
# with open("gensim.pkl", "rb") as fp:
# glove_vectors = pickle.load(fp)
#glove_vectors = api.load("glove-wiki-gigaword-50")
with open("magic_dmp.pkl", "rb") as fp:
magic_dmp = pickle.load(fp)
app = Flask(__name__, static_folder="static")
CORS(app)
model = Doc2Vec.load(r'DocToVector_200_DBOW.model')
#model = Doc2Vec.load('DocToVector.model')
def prep_smart(inp):
inp = inp.lower()
inp = re.sub("[^a-zA-Z\n]+"," ", inp)
inp = re.sub('\n+',"\n", inp)
# finding these at beginning of sentence
re_abs = re.search('\n\s*abstract', inp)
re_intro = re.search('\n\s*introduction', inp)
re_ref = re.search('\n\s*references?', inp)
st_end = [[-1,-1], [-1,-1], [-1,-1]]
for idx, rt in enumerate([re_abs, re_intro, re_ref]):
while rt:
st_end[idx] = [rt.start(), rt.end()]
break
# st = st_end[0][1] if st_end[0][1]>0 else st_end[1][1]
# en = st_end[-1][0] if st_end[-1][0]>0 else None
#modifying start and end to keep till introduction only
if st_end[1][1]>0:
print("Keeping till introduction")
st = 0
en = st_end[1][0]
elif st_end[0][1]>0:
print("Keeping till abstract")
st = 0
en = st_end[0][0]
elif st_end[-1][0]>0:
print("Fetching till references")
st = 0
en = st_end[-1][0]
else:
print("using full paper")
st = 0
en = len(inp)
doc = inp[st:en]
# st = st if st>0 else 0
# if en:
# doc= inp[st:en]
# else:
# doc = inp[st:]
doc = re.sub('\n+'," ", doc)
doc = " ".join(w for w in doc.split() if w.strip() and w not in stp and len(w)>4)
return doc
def prep_simp(text):
return re.sub("[^a-zA-Z\n]+", " ", text)
with open("trans_cls.pkl", "rb") as fp:
vectorizerC = pickle.load(fp)
with open("nb_cls.pkl", "rb") as fp:
nb_cls = pickle.load(fp)
def try_magic(fp):
with open(fp, encoding="utf") as f:
cont = f.read()
cont = prep_inp(cont)
vec = vectorizerF.transform([cont])
cl = km.predict(vec)[0]
return random.sample(magic_dmp[cl][:30], k=10)
def prep_inp(inp):
inp = inp.lower()
inp = re.sub("[^a-zA-Z\n]+", " ", inp)
inp = re.sub('\n+', "\n", inp)
doc = inp
doc = re.sub('\n', ' ', doc)
doc = re.sub(' ', ' ', doc)
return doc
app = Flask(__name__, static_folder="static")
CORS(app)
# glove_vector = gensim.downloader.load("fasttext-wiki-news-subwords-300")
# model = Doc2Vec.load(r'D:\Mayank\IR\Test\DocToVector_200_DBOW.model')
# model = Doc2Vec.load(r'DocToVector.model')
# with open("gensim.pkl", "rb") as fp:
# glove_vector = pickle.load(fp)
def query_expansion(inp_query):
words = [w for w in inp_query.split() if w.strip()]
if len(words)<4:
w = "_".join(words)
print(w)
try:
t = word_model.wv.most_similar(w, topn=4)
print("here", t)
return [" ".join(w.split("_")) for w,s in t]
except:
expp = []
for w in words:
try:
t = word_model.wv.most_similar(w, topn=10)
except:
t = []
if t:
expp.append([wr for wr,s in random.sample(t, k=2)])
if len(expp)==len(words):
return [" ".join(i) for i in list(itertools.product(*expp))]
return []
def get_similar_doc(in_fpath):
# base_path = r"D:\Mayank\IR\phase2"
with open( in_fpath, encoding='utf8') as input_file:
content = input_file.read()
prep_cls = prep_smart(content)
cls = nb_cls.predict(vectorizerC.transform([prep_cls]))[0]
cls = "Software" if cls=="icse" else "Database"
clstr = word_tokenize(prep_cls)
veclt = model.infer_vector(clstr)
clst = str(k.predict([veclt])[0])
## do classification and clusterring here
test = word_tokenize(content.lower())
vec = model.infer_vector(test)
sim_docs = model.docvecs.most_similar(positive=[vec], topn=10)[1:]
rel_terms = try_magic(in_fpath)
t_ = [{"p_": "/".join(doc.split("/")[-3:]), "r_t": rel} for (doc,sc),rel in zip(sim_docs,rel_terms)]
t_.append(clst)
t_.append(cls)
return t_
@app.route('/expQ', methods=['POST'])
def expQ():
inp_query = request.form.get('queryTerm')
print(inp_query)
exp_query = []
if len(inp_query.split())>1:
exp_query = query_expansion(inp_query)
return jsonify(exp_query)
@app.route('/simDoc', methods=['POST'])
def sim_doc():
in_fpath = request.form.get('in_fpath')[1:]
print(in_fpath)
# in_fpath = "\\".join(in_fpath.split("\\")[-3:])
sim_docs = get_similar_doc(in_fpath)
return jsonify(sim_docs)
@app.route('/', methods=['GET','POST'])
def index_pg():
return render_template(r"VueSample.htm")
if __name__ == "__main__":
app.run(host="0.0.0.0")