-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess_data.py
More file actions
349 lines (248 loc) · 11.2 KB
/
process_data.py
File metadata and controls
349 lines (248 loc) · 11.2 KB
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
import pandas as pd
import torch
from gensim.models import KeyedVectors
from gensim.utils import tokenize
from torch.utils.data import TensorDataset
from tqdm import tqdm
import re
import numpy as np
import random
idents = list(pd.read_csv('./data/random_split_data/train_identities.txt', header=None).iloc[:,0].astype('string'))
def get_CivilComments_Datasets(device='cpu', embed_lookup=None):
'''
gets the test split of civil comments dataset
'''
if not embed_lookup:
embed_lookup = init_embed_lookup()
CC_df = pd.read_csv('./data/civil_comments/civil_comments.csv', index_col=0)
CC_df['toxicity'] = (CC_df['toxicity'] >= 0.5).astype(int)
sub_df = CC_df[CC_df['split'] == 'test']
padded_id = []
for comment in tqdm(sub_df['comment_text'].values):
seq = tokenize(comment)
id = get_id(seq, embed_lookup)
padded_id.append(pad_seq(id))
features = torch.tensor(padded_id, device=device)
labels = torch.from_numpy(sub_df['toxicity'].values).to(device)
labels = labels.to(device).long()
return TensorDataset(features, labels)
def get_CivilComments_idents_Datasets(device='cpu', embed_lookup=None):
'''
returns subset of CivilComments dataset only with identity
'''
if not embed_lookup:
embed_lookup = init_embed_lookup()
df_nontoxic = pd.read_csv(f'./data/civil_comments/civil_train_data.csv', index_col=0)
df_toxic = pd.read_csv(f'./data/civil_comments/civil_toxic_train_data.csv', index_col=0)
df_nontoxic['toxicity'] = 0
df_toxic['toxicity'] = 1
df_idents = pd.concat([df_nontoxic[['comment_text', 'toxicity']], df_toxic[['comment_text', 'toxicity']]])
padded_id = []
for comment in tqdm(df_idents['comment_text'].values):
seq = tokenize(comment)
id = get_id(seq, embed_lookup)
padded_id.append(pad_seq(id))
features = torch.tensor(padded_id, device=device)
labels = torch.from_numpy(df_idents['toxicity'].values).to(device)
labels = labels.to(device).long()
return TensorDataset(features, labels)
def get_jigsaw_dev_data(file_path='./data', device='cpu', embed_lookup=None):
'''
returns the dev split of jigsaw dataset
'''
if not embed_lookup:
embed_lookup = init_embed_lookup()
# Create df with dev data
df_dev = pd.read_csv(f'{file_path}/jigsaw/test.csv')
df_dev_labels = pd.read_csv(f'{file_path}/jigsaw/test_labels.csv')
df_dev['toxic'] = df_dev_labels['toxic']
df_dev = df_dev[df_dev['toxic'] != -1]
df_dev.reset_index(inplace=True)
padded_id = []
for comment in tqdm(df_dev['comment_text']):
seq = tokenize(comment)
id = get_id(seq, embed_lookup)
padded_id.append(pad_seq(id))
X = torch.tensor(padded_id, device=device)
y = torch.tensor(df_dev['toxic'], device=device)
return TensorDataset(X, y)
def get_jigsaw_datasets(file_path='./data', device='cpu', data_type='baseline', embed_lookup=None):
'''
return datasets of the form X,y,M where M is metadata
M is only meaningful when data_type is baseline
baseline: returns baseline data
blind: returns data with blind preprocessing
augment: retuns data with augment preprocessing
CLP: returns A, a tensor of adversarially perturbed examples
'''
if not embed_lookup:
embed_lookup = init_embed_lookup()
# Create df with train data
df_train = pd.read_csv(f'{file_path}/jigsaw/train.csv')
df_train = df_train.drop(df_train.columns[3:8], axis=1)
# add identity to column indicating precence of identity in sentence
if data_type != 'baseline':
for row_index, row in enumerate(df_train.itertuples()):
for identity in idents:
regex = re.compile(r'\b' + re.escape(identity) + r'\b')
if regex.search(row[2]):
df_train.at[row_index, identity] = 1
else:
df_train.at[row_index, identity] = 0
if data_type == 'blind':
df_train = process_blind(df_train)
elif data_type == 'augment':
df_train = process_augment(df_train)
elif data_type == 'augment1':
df_train = process_augment(df_train, single=True)
elif data_type == 'CLP': # CLP
df_train, df_adversarial = process_clp(df_train)
#only need metadata for CLP, otherwise we just use some dummy data
if data_type == 'CLP':
M = torch.tensor(df_train['index'], device=device)
else:
M = torch.zeros(len(df_train), device=device)
padded_id = []
for comment in tqdm(df_train['comment_text']):
seq = tokenize(comment)
id = get_id(seq, embed_lookup)
padded_id.append(pad_seq(id))
X = torch.tensor(padded_id, device=device)
y = torch.tensor(df_train['toxic'], device=device)
dataset = TensorDataset(X, y, M)
# need to get the adversarial matrix
if data_type == 'CLP':
tokenized_adversarials = []
# tokenize every sentence in A
for row in tqdm(df_adversarial.itertuples(), total=len(df_adversarial)):
row_adv = []
for comment in row[3:]:
seq = tokenize(comment)
id = get_id(seq, embed_lookup)
row_adv.append(pad_seq(id))
tokenized_adversarials.append(row_adv)
A = torch.tensor(tokenized_adversarials, device=device)
return dataset, A
return dataset
def get_ctf_datasets(file_path='./data', dataset='civil_eval', device='cpu', embed_lookup=None):
'''
returns datasets to be used for CTF metric
civil_test: civil comment non-toxic held out identities
civil_train: civil comment non-toxic training identities
TODO implement getting different datasets
'''
if not embed_lookup:
embed_lookup = init_embed_lookup()
if dataset == 'civil_eval':
df = pd.read_csv(f'{file_path}/civil_comments/civil_test_data.csv', index_col=0)
elif dataset == 'civil_train':
df = pd.read_csv(f'{file_path}/civil_comments/civil_train_data.csv', index_col=0)
# TODO: implement this synthetic -- do the processing on the fly using synthetic
elif dataset == 'synth_nontoxic':
df = pd.read_csv(f'{file_path}/synthetic/synthetic_nontoxic_df.csv', index_col=0)
else:
df = pd.read_csv(f'{file_path}/synthetic/synthetic_toxic_df.csv', index_col=0)
X_comments = []
A_comments = []
# assume every column except comment_text is for an identity
num_idents = len(df.columns) - 1
num_sents = len(df)
for row in tqdm(df.itertuples(), total=len(df)):
# first tokenize/ get ID of comment
comment = row[1]
seq = tokenize(comment)
id = get_id(seq, embed_lookup)
X_comments.append(pad_seq(id))
# next do the same with adversarially perturbed sentences
perturbed_sentences = row[2:]
for perturbed in perturbed_sentences:
seq = tokenize(perturbed)
id = get_id(seq, embed_lookup)
A_comments.append(pad_seq(id))
X = torch.tensor(X_comments, device=device)
A = torch.tensor(A_comments, device=device).reshape(num_sents, num_idents, -1)
dataset = TensorDataset(X, A)
return dataset
def process_blind(df):
'''
Preprocess dataframe to have identity tokens masked as identity
'''
# Adding identity column to train_df_short (either works I think)
df['identity'] = (df[idents].sum(axis=1) > 0).astype(int)
# Replacing identities in comment text with an identity token
token = "identity"
for row_index in tqdm(range(len(df))):
if df.at[row_index, "identity"] == 1:
for identity in idents:
if df.at[row_index, identity] == 1 :
regex = r'\b' + re.escape(identity) + r'\b'
df.at[row_index, "comment_text"] = re.sub(regex, token, df.at[row_index, "comment_text"], flags=re.IGNORECASE)
return df
def process_augment(df, single = False):
# Adding identity column to train_df_short (either works I think)
df['identity'] = (df[idents].sum(axis=1) > 0).astype(int)
df_identities = df[df.identity==1].reset_index()
comment_list = []
toxic_list = []
for row_index in tqdm(range(len(df_identities))):
for identity in idents:
regex = r'\b' + re.escape(identity) + r'\b'
if df_identities.at[row_index, identity] == 1:
comment_list.append(df_identities.at[row_index, "comment_text"])
toxic_list.append(df_identities.at[row_index, "toxic"])
if single:
diff_identity = random.choice([ident for ident in idents if ident != identity])
comment_list.append(re.sub(regex, diff_identity, df_identities.at[row_index, "comment_text"], flags=re.IGNORECASE))
toxic_list.append(df_identities.at[row_index, "toxic"])
else:
for diff_identity in idents:
if diff_identity == identity:
continue
comment_list.append(re.sub(regex, diff_identity, df_identities.at[row_index, "comment_text"], flags=re.IGNORECASE))
toxic_list.append(df_identities.at[row_index, "toxic"])
data_tuples = list(zip(comment_list, toxic_list))
train_df_augment = pd.DataFrame(data_tuples, columns=['comment_text','toxic'])
df_nonidents = df[df.identity==0][['comment_text', 'toxic']].reset_index()
# train_df_augment['augmented'] = augmented
return pd.concat((train_df_augment, df_nonidents), ignore_index=True)
def process_clp(df):
df['identity'] = (df[idents].sum(axis=1) > 0).astype(int)
df_idents = df[df.identity==1].reset_index(drop=True)
df_idents['index'] = np.arange(len(df_idents))
df_nonidents = df[df.identity==0].reset_index(drop=True)
df_nonidents['index'] = -1
identity_regex = re.compile('|'.join(idents), re.IGNORECASE)
a = []
for comment_text in tqdm(df_idents['comment_text']):
identity = identity_regex.search(comment_text)[0].lower()
# generate adversarial
cur_a = []
for diff_identity in idents:
if diff_identity == identity:
continue
cur_a.append(comment_text.replace(identity, diff_identity))
a.append(cur_a)
df_adversarial = pd.DataFrame(list(zip(*a))).T
return pd.concat((df_idents, df_nonidents), ignore_index=True), df_adversarial
def init_embed_lookup(word2vec=True, file_path='./data'):
'''
intializes the embeddings
either word2vec or glove
'''
if word2vec:
return KeyedVectors.load_word2vec_format(f'{file_path}/GoogleNews-vectors-negative300.bin', binary=True)
return KeyedVectors.load_word2vec_format(f'{file_path}/glove_840B_300d.txt', binary=False, no_header=True)
def get_id(seq, embed_lookup):
seq_id = []
for word in seq:
try:
idx = embed_lookup.key_to_index[word]
except:
idx = 0
seq_id.append(idx)
return seq_id
def pad_seq(seq):
if len(seq) > 300:
return seq[:300]
else:
return seq + [0] * (300 - len(seq))