-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbasics.py
276 lines (210 loc) · 9.2 KB
/
basics.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import spacy
import torch
from flask import Flask, render_template, request
nlp = spacy.load('en_core_web_sm')
selected_keyphrases_list=[]
from spacy.matcher import Matcher
matcher = Matcher(nlp.vocab)
pattern1 = [{'POS':'ADJ', 'OP':'*'}, {'IS_PUNCT': True, 'OP':'*'}, {'POS':'NOUN', 'OP':'+'},{'IS_PUNCT': True, 'OP':'*'},{'POS':'ADP'},{'OP':'?'},{'POS':'ADJ'},{'IS_PUNCT': True, 'OP':'*'},{'POS':'NOUN'}]
#pat = [({'POS':'ADJ', 'OP':'*'}, {'IS_PUNCT': True, 'OP':'*'}, {'POS':'NOUN', 'OP':'+'},{'IS_PUNCT': True, 'OP':'*'},{'POS':'ADP'})?]
pattern2 = [{'POS':'ADJ'},{'IS_PUNCT': True, 'OP':'*'},{'POS':'NOUN'}]
pattern3 = [{'ENT_TYPE':'ORG', 'OP':'+'}]
pattern4 = [{'ENT_TYPE':'GPE', 'OP':'+'}]
matcher.add('Noun', None, pattern1, pattern2, pattern3, pattern4)
app = Flask(__name__)
#####################
@app.route('/')
def man():
return render_template('home.html')
@app.route('/home', methods=['POST'])
def home():
doc = request.files['text']
doc.save('static/{}.txt')
f = open("static/{}.txt", "r")
s = f.read()
doc = nlp(s)
found_matches = matcher(doc)
phrases = set()
for matcher_id, start, end in found_matches:
if((start!=end) and (doc[start:end] not in nlp.Defaults.stop_words)):
phrases.add(str(doc[start:end]))
phrases=list(phrases)
clean = list()
punc = '''!()-[]{};:'"<>./?@#$%^&*_~\,'''
for word in phrases:
x = list(word)
s=''
for i in range(len(x)):
if(x[i] in punc):
continue
else:
s=s+x[i]
clean.append(s)
#clean = set(clean)
clean = list(clean)
clean.sort()
print(len(clean))
c=0
new= list()
for i in range(len(clean)):
for j in range(len(clean)):
if(i==j):
continue
if(clean[i] in clean[j]):
c=1
break
c=0
if(c==0):
new.append(clean[i])
c=0
new = set(new)
phrases = list(new)
print(len(phrases))
phrases
from transformers import BertTokenizer, BertModel
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
model = BertModel.from_pretrained('bert-base-uncased',
output_hidden_states = True,)
model.eval()
# In[12]:
sentences= s.split('. ')
#here we tokenize each sentence and convert to ids
#finding maximum length of a sentence (for padding purposes, because bert requires this)
max_len = 0
# For every sentence...
for sentence in sentences:
# Tokenize the text and add `[CLS]` and `[SEP]` tokens.
sentence_id = tokenizer.encode(sentence, add_special_tokens=True)
# Update the maximum sentence length.
max_len = max(max_len, len(sentence_id))
#adding 10 extra to max_len just in case
max_len=max_len+10
# In[13]:
# Tokenize all of the sentences and map the tokens to their word IDs.
# input ids is a 2d list, each element is a list(that represents a sentence which is tokenised and converted to the ids)
#attention masks is also a 2d list each element is a list(that represents whether each element of the tokenised list is a padded element or not)
input_ids = []
attention_masks = []
# For every phrase...
for sentence in sentences:
# `encode_plus` will:
# (1) Tokenize the phrase.
# (2) Prepend the `[CLS]` token to the start.
# (3) Append the `[SEP]` token to the end.
# (4) Map tokens to their IDs.
# (5) Pad or truncate the phrase to `max_length`
# (6) Create attention masks for [PAD] tokens.
encoded_dict = tokenizer.encode_plus(
sentence, # phrase to encode.
add_special_tokens = True, # Add '[CLS]' and '[SEP]'
truncation = True,
max_length = 23 , # Pad & truncate all sentences.
pad_to_max_length = True,
return_attention_mask = True, # Construct attn. masks.
return_tensors = 'pt', # Return pytorch tensors.
)
# Add the encoded phrase to the list.
input_ids.append(encoded_dict['input_ids'])
# And its attention mask (simply differentiates padding from non-padding).
attention_masks.append(encoded_dict['attention_mask'])
# In[14]:
#this is where we call the model and pass the encoded tokens of each sentence
# Run the text through BERT, and collect all of the hidden states produced
# from all 12 layers.
hidden_states=[]
with torch.no_grad():
for i in range(len(input_ids)):
outputs = model(input_ids[i],attention_mask=attention_masks[i])
hidden_states.append(outputs[2])
print(len(hidden_states[0]))
# In[15]:
#here we calculate sentence and document embeddings
# `hidden_states` has shape [no of sentences x 13 x 1 x max_len x 768]
sentence_embeddings=[]
# `token_vecs` is a tensor with shape [22 x 768]
for i in range(len(hidden_states)):
token_vecs = hidden_states[i][-2][0]#we take the embeddings from the second last layer as it has the best balance in terms of context
# Calculate the average of all token vectors.
sentence_embedding = torch.mean(token_vecs, dim=0)
sentence_embeddings.append(sentence_embedding)
sentence_embeddings = torch.stack(sentence_embeddings,dim=0)
document_embedding=torch.mean(sentence_embeddings, dim=0)
# In[16]:
#finding maximum length of a phrase (for padding purposes, because bert requires this)
max_len = 0
for phrase in phrases:
# Tokenize the text and add `[CLS]` and `[SEP]` tokens.
ids = tokenizer.encode(phrase, add_special_tokens=True)
# Update the maximum phrase length.
max_len = max(max_len, len(ids))
#adding 10 extra to max_len just in case
max_len=max_len+10
print(max_len)
# In[17]:
# Tokenize all of the phrases and map the tokens to their word IDs.
input_ids = []
attention_masks = []
# For every phrase...
for phrase in phrases:
# `encode_plus` will:
# (1) Tokenize the phrase.
# (2) Prepend the `[CLS]` token to the start.
# (3) Append the `[SEP]` token to the end.
# (4) Map tokens to their IDs.
# (5) Pad or truncate the phrase to `max_length`
# (6) Create attention masks for [PAD] tokens.
encoded_dict = tokenizer.encode_plus(
phrase, # phrase to encode.
add_special_tokens = True, # Add '[CLS]' and '[SEP]'
truncation = True,
max_length = 23 , # Pad & truncate all sentences.
pad_to_max_length = True,
return_attention_mask = True, # Construct attn. masks.
return_tensors = 'pt', # Return pytorch tensors.
)
# Add the encoded phrase to the list.
input_ids.append(encoded_dict['input_ids'])
# And its attention mask (simply differentiates padding from non-padding).
attention_masks.append(encoded_dict['attention_mask'])
# In[18]:
# Run the text through BERT, and collect all of the hidden states produced
# from all 12 layers.
hidden_states=[]
with torch.no_grad():
for i in range(len(input_ids)):
outputs = model(input_ids[i],attention_mask=attention_masks[i])
hidden_states.append(outputs[2])
# In[19]:
# `hidden_states` has shape [no of sentences x 13 x 1 x max_len x 768]
phrase_embeddings=[]
for i in range(len(hidden_states)):
token_vecs = hidden_states[i][-2][0]
# `token_vecs` is a tensor with shape [22 x 768]
# Calculate the average of all token vectors.
phrase_embedding = torch.mean(token_vecs, dim=0)
phrase_embeddings.append(phrase_embedding)
# In[20]:
from scipy.spatial.distance import cosine
selected_keyphrases=[]
similarity=[]
keyword=[]
embed=[]
for i in range(len(phrase_embeddings)):
sim = 1 - cosine(document_embedding, phrase_embeddings[i])
similarity.append(sim)
keyword.append(phrases[i])
embed.append(phrase_embeddings[i])
for i in range(len(similarity)):
for j in range(0, len(similarity)-i-1):
if similarity[j] < similarity[j+1] :
similarity[j], similarity[j+1] = similarity[j+1], similarity[j]
keyword[j], keyword[j+1] = keyword[j+1], keyword[j]
embed[j], embed[j+1] = embed[j+1], embed[j]
selected_keyphrases=keyword[0:10]
selected_keyphrases_similarity=similarity[0:10]
selected_keyphrases_embeddings=embed[0:10]
print(selected_keyphrases)
return render_template('prediction.html', data=selected_keyphrases)
if __name__ == '__main__':
app.run(debug=True)
# In[ ]: