-
Notifications
You must be signed in to change notification settings - Fork 1
/
views.py
216 lines (170 loc) · 7.56 KB
/
views.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
210
211
212
213
214
215
216
from django.contrib.auth.mixins import LoginRequiredMixin
from django.shortcuts import render, redirect, get_object_or_404
from django.http import HttpResponseRedirect
from django.views import View
from django.views.generic.edit import CreateView, UpdateView, DeleteView
from django.views.generic import TemplateView
from django.urls import reverse_lazy
from django.http import HttpResponse
from django.template.loader import render_to_string
from web.models import Task, Project
# PDF Parsing
import pandas as pd
import numpy as np
import re
import json
from sklearn.feature_extraction.text import TfidfVectorizer
from scipy import sparse
import nltk
from nltk.corpus import stopwords
import gensim
from nltk.tokenize import word_tokenize
import scipy
#class MainView(LoginRequiredMixin, View) :
class MainView(TemplateView) :
def get(self, request):
return render(request, 'web/home.html')#, ctx)
class ProjectsView(TemplateView) :
template_name = "web/projects.html"
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['project_list'] = Project.objects.all()
if context['project_list'] and context['project_list'] == 'word2vec' :
template_name = "web/project_word2vec.html"
return context
class word2vecView(TemplateView) :
def get(self, request):
if request.method == 'GET':
# Debug
print(request.GET)
return render(request, "web/project_word2vec.html")#, context)
def post(self, request, **kwargs):
if request.method == 'POST':
# Debug
print(request.POST)
context = {
'search_query': "",
'search_results': [],
}
query = request.POST['search-query']
context['search_query'] = query
df = pd.read_csv('model_final.csv')
df.summary = df.summary.astype(str)
lowered_summary = [k.lower() for k in df.summary]
df['summary_cleaned'] = lowered_summary
replaced = []
for i in df.summary_cleaned:
i = re.sub(r"[^ a-zA-Z0-9]+", "", i)
replaced.append(i)
df['summary_cleaned'] = replaced
stop = stopwords.words('english')
splitted = [k.split() for k in df.summary_cleaned]
new_words = []
for k in splitted:
instance = []
for l in k:
if l not in stop:
instance.append(l)
new_words.append(instance)
joined = []
for i in new_words:
sentences = " ".join(i)
joined.append(sentences)
df['summary_cleaned'] = joined
path = 'GoogleNews-vectors-negative300-SLIM.bin'
w2vec = gensim.models.KeyedVectors.load_word2vec_format(path, binary=True)
def get_embeddings(word):
if word in w2vec.vocab:
return w2vec[word]
else:
return np.zeros(300)
out_dict = {}
docs = df['summary_cleaned'].values.tolist()
for sent in docs:
average_vector = np.mean(np.array([get_embeddings(word) for word in nltk.word_tokenize(sent)]),axis=0)
d = {sent:average_vector}
out_dict.update(d)
def get_similarity(query, doc):
cos_sim = np.dot(query, doc)/(np.linalg.norm(query)*np.linalg.norm(doc))
return cos_sim
def rank_text(query):
query_vector = np.mean(np.array([get_embeddings(word) for word in nltk.word_tokenize(query)]),axis=0)
rank = []
for k,v in out_dict.items():
rank.append((k,get_similarity(query_vector, v)))
rank = sorted(rank, key=lambda x:x[1], reverse=True)
return rank
top_10 = rank_text(query)[:10]
for i in top_10:
# Create a new PDF dictionary and add it to the list of search results
pdf = {
"title": str(df[(df.summary_cleaned == i[0])].Title.values[0]),
"link": str(df[(df.summary_cleaned == i[0])].URL.values[0]),
"summary_short": str(df[(df.summary_cleaned == i[0])].summary.values[0])[:600] + "...", # Truncate summary after 450 characters
"summary_full": str(df[(df.summary_cleaned == i[0])].summary.values[0]),
}
context['search_results'].append(pdf)
return render(request, "web/project_word2vec.html", context)
class bm25LView(TemplateView) :
template_name = "web/projects_bm25L.html"
def get(self, request):
if request.method == 'GET':
# Debug
print(request.GET)
return render(request, "web/project_bm25L.html")
def post(self, request, **kwargs):
if request.method == 'POST':
# Debug
print(request.POST)
context = {
'search_query': "",
'search_results': [],
}
query = request.POST['search-query']
context['search_query'] = query
# Prepare dataframe
#Uncomment in test, comment in prod
df = pd.read_csv('model_final.csv')
# Extract summaries from PDFs and queries from query list
df.summary = df.summary.astype(str)
lowered_summary = [k.lower() for k in df.summary]
df['summary_cleaned'] = lowered_summary
replaced = []
for i in df.summary_cleaned:
i = re.sub(r"[^ a-zA-Z0-9]+", "", i)
replaced.append(i)
df['summary_cleaned'] = replaced
stop = stopwords.words('english')
splitted = [k.split() for k in df.summary_cleaned]
new_words = []
for k in splitted:
instance = []
for l in k:
if l not in stop:
instance.append(l)
new_words.append(instance)
joined = []
for i in new_words:
sentences = " ".join(i)
joined.append(sentences)
df['summary_cleaned'] = joined
from rank_bm25 import BM25L
docs = df['summary_cleaned'].values.tolist()
tokenized_corpus = [doc.split(" ") for doc in docs]
bm25L = BM25L(tokenized_corpus)
# queries = [x for x in df_queries.Query]
tokenized_query = query.split(" ")
retrieve = bm25L.get_top_n(tokenized_query, docs, n=10)
# Debug
print('Query: ' + query + '\n')
for i in retrieve:
# Create a new PDF dictionary and add it to the list of search results
pdf = {
"title": str(df[(df.summary_cleaned == i)].Title.values[0]),
"link": str(df[(df.summary_cleaned == i)].URL.values[0]),
"summary_short": str(df[(df.summary_cleaned == i)].summary.values[0])[:600] + "...", # Truncate summary after 450 characters
"summary_full": str(df[(df.summary_cleaned == i)].summary.values[0]),
}
context['search_results'].append(pdf)
# print(context)
return render(request, "web/project_bm25L.html", context)