-
Notifications
You must be signed in to change notification settings - Fork 3
/
textbugger_utils.py
313 lines (227 loc) · 8.05 KB
/
textbugger_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
import numpy as np
import json
import pickle
import os
import time
import pprint
import pandas as pd
# import keras
import random
from scipy import spatial
import json
from spellchecker import SpellChecker
# from google.cloud import language
# from google.cloud.language import enums
# from google.cloud.language import types
#
# from ibm_watson import NaturalLanguageUnderstandingV1
# from ibm_watson.natural_language_understanding_v1 import Features, EntitiesOptions, KeywordsOptions, SentimentOptions, CategoriesOptions
# from ibm_cloud_sdk_core.authenticators import IAMAuthenticator
#
# from azure.ai.textanalytics import TextAnalyticsClient
# from azure.core.credentials import AzureKeyCredential
#
# import boto3
#
# import fasttext
def get_prediction_given_tokens(model_type, model, doc, glove_vectors = None, embed_map = None, dataset = None):
if (model_type == 'LSTM'):
X_embed = []
for word in doc:
if word in embed_map['w2i']:
X_embed.append(embed_map['w2i'][word])
else:
X_embed.append(random.randrange(1,170000))
y = model.predict(X_embed)[0][0]
# print(X_embed)
return y
elif (model_type == 'CNN'):
X_embed = []
for word in doc:
if word in embed_map['w2i']:
X_embed.append(embed_map['w2i'][word])
else:
X_embed.append(random.randrange(1,170000))
X_embed = chunk_input(X_embed, dataset)
y = model.predict([[X_embed]])[0][0]
return y
elif (model_type == 'LR'):
X_vector = transform_to_feature_vector(doc, glove_vectors)
y = model.predict_proba(X_vector)[0][1]
return y
def chunk_input(doc, dataset):
doc_size = len(doc)
if (dataset == 'RT'):
max_len = 20
elif (dataset == 'IMDB' or dataset == 'Kaggle'):
max_len = 200
elif (dataset == 'Kaggle'):
max_len = 40
if (doc_size > max_len):
res = doc[0:max_len]
return res
elif (doc_size < max_len):
diff = max_len - doc_size
for i in range(0,diff):
doc.append(random.randint(1,10))
return doc
else:
return doc
def transform_to_feature_vector(tokens, glove_vectors):
vectors = []
for token in tokens:
if token in glove_vectors:
vect = glove_vectors[token]
vectors.append(vect)
else:
# sampling from the uniform distribution in [-0.1, 0.1]
vect = [(random.random()/5)-0.1 for i in range(300)]
vectors.append(vect)
means = np.mean(vectors, axis=0)
return [means] # [ [x11,x12,x13,...] ]
def transform_to_word_feature_vector(tokens, glove_vectors, max_len):
#print("333333")
print(max_len)
# same length
doc_size = len(tokens)
if (doc_size > max_len):
tokens = tokens[0:max_len]
elif (doc_size < max_len):
diff = max_len - doc_size
for i in range(0, diff):
tokens.append('<pad>')
#print("3333-111111")
vectors = []
for token in tokens:
if token in glove_vectors:
vect = glove_vectors[token]
vectors.append(vect)
#print("3333-22222")
else:
# sampling from the uniform distribution in [-0.1, 0.1]
vect = [(random.random() / 5) - 0.1 for i in range(300)]
vectors.append(vect)
#print("3333-33333")
return vectors
## SEMANTIC SIMILARITY -------------------------------------------------------------
def getSemanticSimilarity(X, X_prime, epsilon):
# Fill after initial testing
return epsilon + 1
## JACOBIAN MATRIX -------------------------------------------------------------
def get_word_importances_for_whitebox(tokens, y, F, model_type, glove_vectors, embed_map, dataset):
pred_proba = get_prediction_given_tokens(model_type, F, tokens, glove_vectors = glove_vectors, embed_map = embed_map, dataset = dataset)
pred = y
## Compute importance for each word
excludes = get_excludes(tokens) # To see the relative importance of each word, remove that word and predict
JM = {}
for ex_word, ex_tokens in excludes.items():
ex_pred_proba = get_prediction_given_tokens(model_type, F, ex_tokens, glove_vectors = glove_vectors, embed_map = embed_map, dataset = dataset)
if (pred == 1):
C = pred_proba - ex_pred_proba
else:
C = ex_pred_proba - pred_proba
JM[ex_word] = C
ordered_list_by_importance = getImportances(JM)
return ordered_list_by_importance
def get_excludes(l1):
res = {}
for el in l1:
sub = [x for x in l1 if x != el]
res[el] = sub
return res
def getImportances(JM):
df = pd.DataFrame(JM.items(), columns=['Word', 'C'])
df = df.sort_values('C', ascending=False)
return df['Word'].tolist()
## BUG GENERATION -------------------------------------------------------------
def generateBugs(word, glove_vectors, sub_w_enabled=False, typo_enabled=False):
bugs = {"insert": word, "delete": word, "swap": word, "sub_C": word, "sub_W": word}
if (len(word) <= 2):
return bugs
bugs["insert"] = bug_insert(word)
bugs["delete"] = bug_delete(word)
bugs["swap"] = bug_swap(word)
bugs["sub_C"] = bug_sub_C(word)
if (typo_enabled):
bugs["typoW"] = bug_typo(bugs['swap'])
if (not sub_w_enabled):
return bugs
bugs["sub_W"] = bug_sub_W(word, glove_vectors)
return bugs
def bug_insert(word):
if (len(word) >= 6):
return word
res = word
point = random.randint(1, len(word)-1)
res = res[0:point] + " " + res[point:]
return res
def bug_delete(word):
res = word
point = random.randint(1, len(word)-2)
res = res[0:point] + res[point+1:]
# print("hi")
# print(res[7:])
return res
def bug_swap(word):
if (len(word) <= 4):
return word
res = word
points = random.sample(range(1, len(word)-1), 2)
# print(points)
a = points[0]
b = points[1]
res = list(res)
w = res[a]
res[a] = res[b]
res[b] = w
res = ''.join(res)
return res
def bug_sub_C(word):
res = word
key_neighbors = get_key_neighbors()
point = random.randint(0,len(word)-1)
if word[point] not in key_neighbors:
return word
choices = key_neighbors[word[point]]
subbed_choice = choices[random.randint(0,len(choices)-1)]
res = list(res)
res[point] = subbed_choice
res = ''.join(res)
return res
def bug_sub_W(word, glove_vectors):
if word not in glove_vectors:
return word
closest_neighbors = find_closest_words(glove_vectors[word], glove_vectors)[1:6]
return random.choice(closest_neighbors)
# return closest_neighbors # Change later
def bug_typo(word):
spell = SpellChecker(distance = 10)
candidates = spell.candidates(word)
chosen_candidate_typo = random.choice(list(candidates))
return chosen_candidate_typo
def get_key_neighbors():
# By keyboard proximity
neighbors = {
"q": "was","w": "qeasd","e": "wrsdf","r": "etdfg","t": "ryfgh","y": "tughj","u": "yihjk","i": "uojkl","o": "ipkl","p": "ol",
"a": "qwszx","s": "qweadzx","d": "wersfxc","f": "ertdgcv","g": "rtyfhvb","h": "tyugjbn","j": "yuihknm","k": "uiojlm","l": "opk",
"z": "asx","x": "sdzc","c": "dfxv","v": "fgcb","b": "ghvn","n": "hjbm","m": "jkn"
}
# By visual proximity
neighbors['i'] += '1'
neighbors['l'] += '1'
neighbors['z'] += '2'
neighbors['e'] += '3'
neighbors['a'] += '4'
neighbors['s'] += '5'
neighbors['g'] += '6'
neighbors['b'] += '8'
neighbors['g'] += '9'
neighbors['q'] += '9'
neighbors['o'] += '0'
return neighbors
def find_closest_words(point, glove_vectors):
return sorted(glove_vectors.keys(), key=lambda word: spatial.distance.euclidean(glove_vectors[word], point))
## BLACKBOX ----------------------------------------------------------------
def get_blackbox_classifier_score(classifier_type, input_text):
## ORIGINALLY CONTAINS ACCOUNT INFORMATION
return 0.5