-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
489 lines (266 loc) · 12.2 KB
/
utils.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
import pandas as pd
import numpy as np
import os
import nltk
from nltk.tokenize import TweetTokenizer
from nltk.corpus import stopwords
from transformers import BertForSequenceClassification, BertTokenizer, BertForMaskedLM
from simpletransformers.language_modeling import LanguageModelingModel
from sklearn.metrics.pairwise import cosine_similarity, paired_euclidean_distances
from sklearn.metrics.pairwise import euclidean_distances
from sklearn.preprocessing import normalize, StandardScaler, MinMaxScaler
from tqdm import tqdm
import torch
import networkx as nx
from functools import partial
import itertools
import matplotlib.pyplot as plt
stop_words = set(stopwords.words('english'))
from collections import deque
import plotly.graph_objects as go
def evaluateText(text, model, tokenizer, compareEmb):
tokens = tokenizer.encode(text)
temp = tokenizer.convert_ids_to_tokens(tokens)
decoded = tokenizer.decode(tokens).split(" ")
logits, hidden_states = model(torch.Tensor(tokens).unsqueeze(0).long())
hidden_states = torch.stack(hidden_states).squeeze(1).permute(1,0,2)
emb = torch.sum(hidden_states[:,9:13,:],1).detach().cpu().numpy()
sim = cosine_similarity(emb, compareEmb.reshape(1,-1)).reshape(-1)
sim = cosine_similarity(emb, compareEmb.reshape(1,-1)).reshape(-1)
for i in range(len(temp)):
print(f"{temp[i]:10s} : {str(np.round(sim[i],3))}")
def getCosineDist(x,y):
if x.shape == (768,):
x = x.reshape(1,-1)
if y.shape == (768,):
y = y.reshape(1,-1)
dist = cosine_similarity(x,y)
return dist
def getSymptomEmbedding(model, tokenizer, df, symptom, symptomToken, embeddingType = 'last4sum', subset = None):
embeddingList = []
messageList = []
if subset is not None:
df = df.iloc[subset]
if type(df) == pd.Series:
df = pd.DataFrame(df).T
symptomToken = tokenizer.encode(symptom)[1]
for i in range(len(df)):
if symptomToken in tokenizer.encode(df.iloc[i]['message'].lower()):
tokens = tokenizer.encode(df.iloc[i]['message'].lower())
decoded = tokenizer.decode(tokens).split(" ")
logits, hidden_states = model(torch.Tensor(tokens).unsqueeze(0).long())
hidden_states = torch.stack(hidden_states).squeeze(1).permute(1,0,2)
try:
tokenIndex = tokens.index(symptomToken)
except:
a= 1
continue
# print(df.iloc[i]['message'])
# print(tokens)
if embeddingType == 'last4sum':
embedding = torch.sum(hidden_states[tokenIndex,9:13,:],0)
elif embeddingType =='last4concat':
embedding = hidden_states[tokenIndex,9:13,:].reshape(-1)
elif embeddingType == 'secondlast':
embedding = hidden_states[tokenIndex,-2,:]
else:
embedding = hidden_states[tokenIndex,-1,:]
embeddingList.append(embedding.detach().cpu().numpy())
messageList.append(df.iloc[i]['message'].lower())
if len(embeddingList)==30:
break
return embeddingList, messageList
def getSimilarWords(model, tokenizer, df, symptom, embList, similarityThreshold = 0.3, numThreshold = 10000):
output = []
symptomToken = tokenizer.encode(symptom)[1]
for i in range(len(df)):
if symptomToken in tokenizer.encode(df.iloc[i]['message'].lower()):
tokens = tokenizer.encode(df.iloc[i]['message'].lower())
decoded = tokenizer.decode(tokens).split(" ")
logits, hidden_states = model(torch.Tensor(tokens).unsqueeze(0).long())
hidden_states = torch.stack(hidden_states).squeeze(1).permute(1,0,2)
hidden_states = hidden_states[:,9:13,:]
hidden_states = torch.sum(hidden_states,1).detach().cpu().numpy()
similarity = cosine_similarity(hidden_states, embList.reshape(1,-1)).reshape(-1)
index = np.where([similarity> similarityThreshold])[1]
selectTokens = np.array(tokens)[index]
selectSim = similarity[index]
for j in range(len(index)):
token = tokenizer.ids_to_tokens[selectTokens[j]]
sim = selectSim[j]
output.append((token, sim,i))
if i==numThreshold:
break
return output
def getOutput(out):
output = out
outMap = {}
for i in range(len(output)):
if output[i][0] in outMap:
outMap[output[i][0]].append(output[i][1])
else:
outMap[output[i][0]] = [output[i][1]]
outMap_ = {}
for i in range(len(output)):
if output[i][0] in outMap_:
outMap_[output[i][0]].append(output[i][2])
else:
outMap_[output[i][0]] = [output[i][2]]
outputDf = []
for key in outMap.keys():
length = len(outMap[key])
mean = np.mean(outMap[key])
outputDf.append([key, length, mean])
outputDf = pd.DataFrame(outputDf)
outputDf.columns = ['word','counts','mean_sim']
outputDf = outputDf.sort_values('mean_sim', ascending=False)
return outputDf, outMap, outMap_
class Node(object):
def __init__(self, word, token, ID, vector = None, depth = None ):
self.word = word
self.token = token
self.ID = ID
self.depth = depth
self.edges_in = []
self.edges_out = []
self.textIDList = []
self.vector = vector
self.masterDist = None
def addInEdge(self, ID):
self.edges_in.append(ID)
def addOutEdge(self, ID):
self.edges_out.append(ID)
def getNodeInCount(self,graph):
nodeCount = 0
for i in range(len(self.edges_in)):
edgeID = self.edges_in[i]
nodeCount += graph.edgeList[edgeID].numCount
return nodeCount
def getWeightList(self,graph):
weightList = []
for i in range(len(self.edges_in)):
edgeID = self.edges_in[i]
weightList.append(graph.edgeList[edgeID].weight)
if weightList == []:
return [1.0] #Weight for starting node
else:
return weightList
def getOutEdges(self,graph):
for i in range(len(self.edges_out)):
edgeID = self.edges_out[i]
edge = graph.edgeList[edgeID]
nodeb = edge.nodeb
print(f"{nodeb.word} || {edge.numCount} || {edge.weight}")
def getInEdges(self,graph):
for i in range(len(self.edges_in)):
edgeID = self.edges_in[i]
edge = graph.edgeList[edgeID]
nodea = edge.nodea
print(f"{nodea.word} || {edge.numCount} || {edge.weight}")
class Edge(object):
def __init__(self, nodeA, nodeB, ID, numCount, weight, textID):
self.nodea = nodeA
self.nodeb = nodeB
self.ID = ID
self.textID = textID
self.numCount = numCount
self.weight = weight
class Graph(object):
def __init__(self):
self.nodeList = []
self.edgeList = []
self.wordMap = {}
self.depthMap = {}
def __getitem__(self,word):
ID = self.wordMap[word]
return self.nodeList[ID]
def addNode(self, word, token , depth):
if word in self.wordMap:
return
else:
node = Node(word, token, len(self.nodeList), depth = depth)
self.wordMap[word] = len(self.nodeList)
self.nodeList.append(node)
if depth in self.depthMap:
self.depthMap[depth].append(word)
else:
self.depthMap[depth] = [word]
def addEdge(self, wordA, wordB, numCount, weight, textID):
ID = len(self.edgeList)
nodeaID = self.wordMap[wordA]
nodebID = self.wordMap[wordB]
edge = Edge(self.nodeList[nodeaID], self.nodeList[nodebID], ID, numCount, weight, textID)
self.edgeList.append(edge)
self.nodeList[nodeaID].addOutEdge(ID)
self.nodeList[nodebID].addInEdge(ID)
def getTextIDs(self, word):
ID = self.wordMap[word]
node = self.nodeList[ID]
textIDs = node.textIDList
return textIDs
def describeNode(self, word):
node = self.__getitem__(word)
print(f"Exploring {word}")
for edgeID in node.edges_in:
edge = self.edgeList[edgeID]
worda = edge.nodea.word
edgeCount = edge.numCount
edgeWeight = edge.weight
textIDs = edge.textID
print(f"{worda:10} -> {word:10} | {edgeCount} | {np.round(edgeWeight,3):6} | {textIDs}")
print("-"*20)
for edgeID in node.edges_out:
edge = self.edgeList[edgeID]
wordb = edge.nodeb.word
edgeCount = edge.numCount
edgeWeight = edge.weight
textIDs = edge.textID
print(f"{word:10} -> {wordb:10} | {edgeCount} | {np.round(edgeWeight,3):6} | {textIDs}")
def exploreNode(word, depth, q, fullDf, graph, model, tokenizer, maxDepth = 3, topk = 5):
graph.addNode(word,0, depth)
print(f"Depth : {depth} Exploring {word}")
if depth == maxDepth:
print("Reached max depth")
return
keyWord = word
token = tokenizer.encode(keyWord)[1]
if graph[word].vector is None:
inEdgeList = graph[word].edges_in
if len(inEdgeList)==0:
textIDList = None
else:
textIDList = []
for edge in inEdgeList:
textIDList.append(graph.edgeList[edge].textID)
textIDList = list(set(list(itertools.chain.from_iterable(textIDList))))
embList,msgList = getSymptomEmbedding(model, tokenizer, fullDf, keyWord, token, embeddingType='last4sum', subset = textIDList)
meanEmb = np.array(embList)
meanEmb = np.mean(meanEmb,0)
graph[word].vector = meanEmb
else:
meanEmb = graph[word].vector
symptom_ =''
embList_ = meanEmb
out = getSimilarWords(model, tokenizer, fullDf.iloc[0:100], symptom_, meanEmb, similarityThreshold = 0.3, numThreshold = 100000)
outputDf, outMap, outMap_ = getOutput(out)
outputDf = outputDf[outputDf.word!=keyWord]
# outputDf = outputDf[~outputDf.word.isin(list(graph.wordMap.keys()))]
outputDf = outputDf.sort_values('mean_sim', ascending=False)
outputDf = outputDf.head(topk)
outputDf = outputDf[outputDf.mean_sim>0.4]
print(outputDf)
print("-----------------------")
for i in range(len(outputDf)):
word = outputDf.iloc[i]['word']
numCount = outputDf.iloc[i]['counts']
weight = outputDf.iloc[i]['mean_sim']
textIDs = outMap_[word]
wordList = set(graph.wordMap.keys())
graph.addNode(word,0, depth+1)
graph[word].textIDList.append(textIDs)
graph.addEdge(keyWord, word, numCount, weight, textIDs)
if word in wordList:
continue
if "#" in word:
continue
q.append((word, depth+1))