-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathaugment.py
401 lines (303 loc) · 15.9 KB
/
augment.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
"""
This module is used for cleaning dataset files and transforming the
input to extract a particular attribute (e.g., the hypothesis-premise
overlap in SNLI).
Each dataset has a parent class in which the cleaning is done and several
subclasses, one for each transformation.
"""
import string
import spacy
import os
import re
import pandas as pd
import random
import logging
from datasets import load_dataset
from transformers import AutoTokenizer
import argparse
parser = argparse.ArgumentParser()
import spacy
from spacytextblob.spacytextblob import SpacyTextBlob
nlp = spacy.load('en_core_web_sm')
nlp.add_pipe('spacytextblob')
class SNLITransformation(object):
"""
Parent class for transforming the SNLI input to extract some particular input
attribute (e.g., just the hypothesis by leaving out the premise). Also needed
to reformat the input into a single string. The transformed data is saved as a CSV.
"""
def __init__(self, name, output_dir, train_size=1.0):
"""
Args:
name: Transformation name
output_dir: where to save the CSV with the transformed attribute
train_size: fraction of the training data to use
"""
self.train_data = load_dataset('snli', split='train').filter(lambda x: x['label'] != -1)
self.test_data = load_dataset('snli', split='test').filter(lambda x: x['label'] != -1)
self.name = name
self.output_dir = output_dir
self.train_size = train_size
def transformation(self, example):
raise NotImplementedError
def transform(self):
logging.info(f'Applying {self.name} to SNLI')
if self.train_size < 1:
train_data = self.train_data.train_test_split(train_size=self.train_size)['train']
else:
train_data = self.train_data
train_data.map(self.transformation).to_csv(os.path.join(self.output_dir, f'snli_train_{self.name}' + (f'_{self.train_size}' if self.train_size < 1.0 else '') + '.csv'))
self.test_data.map(self.transformation).to_csv(os.path.join(self.output_dir, f'snli_test_{self.name}.csv'))
class MultiNLITransformation(object):
"""
Parent class for transforming the MNLI input to extract some particular input
attribute (e.g., just the hypothesis by leaving out the premise). Also needed
to reformat the input into a single string. The transformed data is saved as a CSV.
"""
def __init__(self, name, output_dir):
self.train_data = load_dataset('multi_nli', split='train').filter(lambda x: x['label'] != -1)
self.validation_data = load_dataset('multi_nli', split='validation_matched').filter(lambda x: x['label'] != -1)
self.name = name
self.output_dir = output_dir
def transformation(self, example):
raise NotImplementedError
def transform(self):
logging.info(f'Applying {self.name} to MutliNLI')
self.train_data.map(self.transformation).to_pandas()[['sentence1', 'label']].to_csv(
os.path.join(self.output_dir, f'multinli_train_{self.name}.csv'))
self.validation_data.map(self.transformation).to_pandas()[['sentence1', 'label']].to_csv(
os.path.join(self.output_dir, f'multinli_validation_{self.name}.csv'))
class DWMWTransformation(object):
def __init__(self, name, output_dir):
self.data = pd.read_csv('data/dwmw/labeled_data.csv').rename({"tweet" : "sentence1", "class" : "label"}, axis=1)
self.name = name
self.output_dir = output_dir
def transformation(self, example):
raise NotImplementedError
def transform(self):
logging.info(f'Applying {self.name} to DWMW')
self.data.apply(self.transformation, axis=1).to_csv(
os.path.join(self.output_dir, f'dwmw_{self.name}.csv'))
class COLATransformation(object):
def __init__(self, name, output_dir, train_size=1):
self.train_data = pd.read_csv('data/cola_public/raw/in_domain_train.tsv', sep='\t', names=['annotator', 'label', 'stars', 'sentence1'])
self.id_dev_data = pd.read_csv('data/cola_public/raw/in_domain_dev.tsv', sep='\t', names=['annotator', 'label', 'stars', 'sentence1'])
self.ood_dev_data = pd.read_csv('data/cola_public/raw/out_of_domain_dev.tsv', sep='\t', names=['annotator', 'label', 'stars', 'sentence1'])
self.name = name
self.output_dir = output_dir
self.train_size = train_size
def transformation(self, example):
raise NotImplementedError
def transform(self):
logging.info(f'Applying {self.name} to COLA')
self.train_data.sample(frac=self.train_size, random_state=1).apply(self.transformation, axis=1).to_csv(
os.path.join(self.output_dir, f'cola_train_{self.name}' + (f'_{self.train_size}' if self.train_size < 1.0 else '') + '.csv'))
self.id_dev_data.apply(self.transformation, axis=1).to_csv(
os.path.join(self.output_dir, f'cola_id_dev_{self.name}.csv'))
self.ood_dev_data.apply(self.transformation, axis=1).to_csv(
os.path.join(self.output_dir, f'cola_ood_dev_{self.name}.csv'))
class SNLIStandardTransformation(SNLITransformation):
def __init__(self, output_dir, train_size=1, suffix=''):
super().__init__(f'std{suffix}', output_dir, train_size=train_size)
def transformation(self, example):
example['sentence1'] = f"PREMISE: {example['premise']} HYPOTHESIS: {example['hypothesis']}"
return example
class SNLINullTransformation(SNLITransformation):
def __init__(self, output_dir, train_size=1, suffix=''):
super().__init__(f'null{suffix}', output_dir, train_size=train_size)
def transformation(self, example):
example['sentence1'] = " " # using only empty string can yield problems
return example
class SNLIHypothesisOnlyTransformation(SNLITransformation):
def __init__(self, output_dir):
super().__init__('hypothesis', output_dir)
def transformation(self, example):
example['sentence1'] = f"HYPOTHESIS: {example['hypothesis']}"
return example
class SNLIPremiseOnlyTransformation(SNLITransformation):
def __init__(self, output_dir):
super().__init__('premise', output_dir)
def transformation(self, example):
example['sentence1'] = f"PREMISE: {example['premise']}"
return example
class SNLIOverlapTransformation(SNLITransformation):
def __init__(self, output_dir):
super().__init__('overlap', output_dir)
self.tokenizer = AutoTokenizer.from_pretrained('bert-base-cased')
def transformation(self, example):
hypothesis_tokens = self.tokenizer.tokenize(example['hypothesis'])
overlap_tokens = [ t for t in hypothesis_tokens if t in self.tokenizer.tokenize(example['premise']) ]
overlap = len(overlap_tokens) / len(hypothesis_tokens)
if overlap >= 0.75:
msg = "HIGH OVERLAP"
elif overlap >= 0.5:
msg = "MEDIUM OVERLAP"
elif overlap >= 0.25:
msg = "LOW OVERLAP"
else:
msg = "NO OVERLAP"
example['sentence1'] = f"{msg}."
return example
class SNLIRawOverlapTransformation(SNLITransformation):
def __init__(self, output_dir):
super().__init__('raw_overlap', output_dir)
self.tokenizer = AutoTokenizer.from_pretrained('bert-base-cased')
def transformation(self, example):
hypothesis_tokens = self.tokenizer.tokenize(example['hypothesis'])
premise_tokens = self.tokenizer.tokenize(example['premise'])
overlap = set(hypothesis_tokens) & set(premise_tokens)
hypothesis = " ".join([ (t if t in overlap else self.tokenizer.mask_token) for t in hypothesis_tokens ])
premise = " ".join([ (t if t in overlap else self.tokenizer.mask_token) for t in premise_tokens ])
example['sentence1'] = f"PREMISE: {premise} HYPOTHESIS: {hypothesis}"
return example
class SNLIShuffleTransformation(SNLITransformation):
def __init__(self, output_dir):
super().__init__('shuffled', output_dir)
self.tokenizer = AutoTokenizer.from_pretrained('bert-base-cased')
def transformation(self, example):
"""
Randomly reorder the words in the hypothesis and premise.
"""
hyp = self.tokenizer.tokenize(example['hypothesis'])
random.shuffle(hyp)
hyp = self.tokenizer.convert_tokens_to_string(hyp)
prem = self.tokenizer.tokenize(example['premise'])
random.shuffle(prem)
prem = self.tokenizer.convert_tokens_to_string(prem)
example['sentence1'] = f"PREMISE: {prem} HYPOTHESIS: {hyp}"
return example
class SNLILengthTransformation(SNLITransformation):
def __init__(self, output_dir):
super().__init__('length', output_dir)
self.tokenizer = AutoTokenizer.from_pretrained('bert-base-cased')
def transformation(self, example):
hyp = ' '.join(['#'] * len(self.tokenizer.tokenize(example['hypothesis'])))
prem = ' '.join(['#'] * len(self.tokenizer.tokenize(example['premise'])))
example['sentence1'] = f"PREMISE: {prem} HYPOTHESIS: {hyp}"
return example
class MultiNLIStandardTransformation(MultiNLITransformation):
def __init__(self, output_dir):
super().__init__('std', output_dir)
def transformation(self, example):
example['sentence1'] = f"PREMISE: {example['premise']} HYPOTHESIS: {example['hypothesis']}"
return example
class MultiNLINullTransformation(MultiNLITransformation):
def __init__(self, output_dir):
super().__init__('null', output_dir)
def transformation(self, example):
example['sentence1'] = " " # using only empty string can yield problems
return example
class DWMWStandardTransformation(DWMWTransformation):
def __init__(self, output_dir):
super().__init__('std', output_dir)
def transformation(self, example):
return example
class DWMWNullTransformation(DWMWTransformation):
def __init__(self, output_dir):
super().__init__('null', output_dir)
def transformation(self, example):
example['sentence1'] = " " # using only empty string can yield problems
return example
class DWMWVocabTransformation(DWMWTransformation):
def __init__(self, output_dir):
super().__init__('bad_vocab', output_dir)
# potentially offensive words were manually selected
self.bad_words = [ 'nigga', 'niggas', 'niggah', 'niggahs', 'hoe', 'hoes', 'bitch', 'bitches', 'whitey', 'white trash', 'cracker', 'crackers', 'beaner', 'beaners',
'pussy', 'pussies', 'fag', 'fags', 'faggot', 'faggots', 'ho', 'hos', 'redneck', 'rednecks', 'porn', 'fuck', 'fucks', 'fucker', 'fuckers', 'motherfucker',
'motherfuckers', 'nigger', 'niggers', 'coon', 'coons', 'niggaz', 'nig', 'nigs', 'slut', 'sluts', 'wigger', 'wiggers', 'fucked', 'fucking', 'wigga', 'wiggas',
'retard', 'retards', 'retarded' ]
def transformation(self, example):
pattern = re.compile(rf"(?!\b({'|'.join(self.bad_words)})\b)\b[^ ]+\b", re.IGNORECASE)
example['sentence1'] = re.sub(pattern, "", example['sentence1'])
example['sentence1'] = example['sentence1'].translate(str.maketrans('', '', string.punctuation))
example['sentence1'] = example['sentence1'].strip()
if example['sentence1'] == "":
example['sentence1'] = ' ' #using only empty string can yield problems
return example
class DWMWSentimentVocabTransformation(DWMWTransformation):
def __init__(self, output_dir):
super().__init__('sentiment_vocab', output_dir)
self.bad_vocab = DWMWVocabTransformation(output_dir)
def transformation(self, example):
polarity = nlp(example['sentence1'])._.polarity
if -0.10 <= polarity <= 0.10:
sentiment = 'neutral'
elif polarity > 0.10:
sentiment = 'positive'
else:
sentiment = 'negative'
example['sentence1'] = ' '.join([sentiment, self.bad_vocab.transformation(example)['sentence1']])
if example['sentence1'] == "":
example['sentence1'] = ' ' #using only empty string can yield problems
return example
class DWMWSentimentTransformation(DWMWTransformation):
def __init__(self, output_dir):
super().__init__('sentiment', output_dir)
self.bad_vocab = DWMWVocabTransformation(output_dir)
def transformation(self, example):
polarity = nlp(example['sentence1'])._.polarity
if -0.10 <= polarity <= 0.10:
sentiment = 'neutral'
elif polarity > 0.10:
sentiment = 'positive'
else:
sentiment = 'negative'
example['sentence1'] = sentiment
return example
class COLAStandardTransformation(COLATransformation):
def __init__(self, output_dir, train_size=1):
super().__init__('std', output_dir, train_size)
def transformation(self, example):
return example
class COLANullTransformation(COLATransformation):
def __init__(self, output_dir, train_size=1):
super().__init__('null', output_dir, train_size)
def transformation(self, example):
example['sentence1'] = " " # using only empty string can yield problems
return example
class COLAShuffleTransformation(COLATransformation):
def __init__(self, output_dir, train_size=1):
super().__init__('shuffled', output_dir, train_size)
self.tokenizer = AutoTokenizer.from_pretrained('bert-base-cased')
def transformation(self, example):
"""
Randomly reorder the words in the hypothesis and premise.
"""
sentence = self.tokenizer.tokenize(example['sentence1'])
random.shuffle(sentence)
example['sentence1'] = self.tokenizer.convert_tokens_to_string(sentence)
return example
if __name__ == "__main__":
os.makedirs('data', exist_ok=True)
parser.add_argument('--raw_data_dir', help='raw_data directory', required=True, type=str)
args = parser.parse_args()
data_dir = args.raw_data_dir
SNLIStandardTransformation(data_dir).transform()
SNLINullTransformation(data_dir).transform()
SNLIHypothesisOnlyTransformation(data_dir).transform()
SNLIPremiseOnlyTransformation(data_dir).transform()
SNLIRawOverlapTransformation(data_dir).transform()
SNLIShuffleTransformation(data_dir).transform()
DWMWStandardTransformation(data_dir).transform()
DWMWNullTransformation(data_dir).transform()
DWMWVocabTransformation(data_dir).transform()
DWMWSentimentVocabTransformation(data_dir).transform()
DWMWSentimentTransformation(data_dir).transform()
COLAStandardTransformation(data_dir).transform()
COLANullTransformation(data_dir).transform()
COLAShuffleTransformation(data_dir).transform()
MultiNLIStandardTransformation(data_dir).transform()
MultiNLINullTransformation(data_dir).transform()
for suffix in ['_b', '_c', '_d', '_e']:
SNLIStandardTransformation(f'{data_dir}/frac', train_size=0.99, suffix=suffix).transform()
SNLIStandardTransformation(f'{data_dir}/frac', train_size=0.8, suffix=suffix).transform()
SNLIStandardTransformation(f'{data_dir}/frac', train_size=0.6, suffix=suffix).transform()
SNLIStandardTransformation(f'{data_dir}/frac', train_size=0.4, suffix=suffix).transform()
SNLIStandardTransformation(f'{data_dir}/frac', train_size=0.2, suffix=suffix).transform()
SNLIStandardTransformation(f'{data_dir}/frac', train_size=0.05, suffix=suffix).transform()
SNLINullTransformation(f'{data_dir}/frac', train_size=0.99, suffix=suffix).transform()
SNLINullTransformation(f'{data_dir}/frac', train_size=0.8, suffix=suffix).transform()
SNLINullTransformation(f'{data_dir}/frac', train_size=0.6, suffix=suffix).transform()
SNLINullTransformation(f'{data_dir}/frac', train_size=0.4, suffix=suffix).transform()
SNLINullTransformation(f'{data_dir}/frac', train_size=0.2, suffix=suffix).transform()
SNLINullTransformation(f'{data_dir}/frac', train_size=0.05, suffix=suffix).transform()