-
Notifications
You must be signed in to change notification settings - Fork 4
/
dataset.py
359 lines (281 loc) · 10.7 KB
/
dataset.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
"""Collect and expose datasets for experiments."""
from torch.utils.data import Dataset, DataLoader
import pytorch_lightning as pl
import torch
import pandas as pd
from operator import itemgetter
import logging
import os
logging.basicConfig(
format="%(levelname)s:%(asctime)s:%(module)s:%(message)s", level=logging.INFO
)
logger = logging.getLogger(__name__)
MADLIBS_DATASETS = ["madlibs77k", "madlibs89k"]
TOX_DATASETS = ["tox_nonfuzz", "tox_fuzz"]
MISO_DATASETS = ["miso", "miso-ita-raw", "miso-ita-synt"]
MISOSYNT_DATASETS = ["miso_synt_test"]
MLMA_DATASETS = ["mlma"]
MLMA_RAW_DATASETS = ["mlma_en", "mlma_fr", "mlma_ar"]
AVAIL_DATASETS = (
MADLIBS_DATASETS
+ TOX_DATASETS
+ MISO_DATASETS
+ MISOSYNT_DATASETS
+ MLMA_DATASETS
)
def get_dataset_by_name(name: str, base_dir=None):
path = os.path.join(base_dir, name) if base_dir else name
train, dev, test = None, None, None
if name in MADLIBS_DATASETS:
test = Madlibs.build_dataset(path)
elif name in TOX_DATASETS:
test = Toxicity.build_dataset(path)
elif name in MISO_DATASETS:
if name == "miso-ita-synt":
test = MisoDataset.build_dataset(name, "test")
else:
train = MisoDataset.build_dataset(name, "train")
dev = MisoDataset.build_dataset(name, "dev")
test = MisoDataset.build_dataset(name, "test")
elif name in MISOSYNT_DATASETS:
test = MisoSyntDataset.build_dataset(name)
elif name in MLMA_RAW_DATASETS:
test = MLMARawDataset.build_dataset(name)
elif name in MLMA_DATASETS:
train = MLMADataset.build_dataset(split="train")
dev = MLMADataset.build_dataset(split="dev")
test = MLMADataset.build_dataset(split="test")
else:
raise ValueError(f"Can't recognize dataset name {name}")
return train, dev, test
def get_tokenized_path(path: str):
base_dir, filename = os.path.dirname(path), os.path.basename(path)
return os.path.join(base_dir, f"{os.path.splitext(filename)[0]}.pt")
class MLMARawDataset(Dataset):
# DEPRECATED
"""Multilingual and Multi-Aspect Hate Speech Analysis"""
def __init__(self, path: str):
self.path = path
data = pd.read_csv(path)
# define the hate binary label
data["hate"] = 1
data.loc[data.sentiment == "normal", "hate"] = 0
data = data.loc[
data.sentiment.apply(lambda x: "normal" not in x or x == "normal")
]
self.data = data
self.texts = data["tweet"].tolist()
self.labels = data["hate"].astype(int).tolist()
self.tokenized_path = get_tokenized_path(path)
def __getitem__(self, idx):
return {"text": self.texts[idx], "label": self.labels[idx]}
def __len__(self):
return len(self.labels)
def get_texts(self):
return self.texts
def get_labels(self):
return self.labels
@classmethod
def build_dataset(cls, name: str):
if name == "mlma_en":
return cls(os.path.join("data", "hate_speech_mlma", f"en_dataset.csv"))
elif name == "mlma_fr":
return cls(os.path.join("data", "hate_speech_mlma", f"fr_dataset.csv"))
elif name == "mlma_ar":
return cls(os.path.join("data", "hate_speech_mlma", f"ar_dataset.csv"))
else:
raise ValueError("Name not recognized.")
class MLMADataset(Dataset):
def __init__(self, path: str):
self.path = path
data = pd.read_csv(path, sep="\t")
self.texts = data["tweet"].tolist()
self.labels = data["hate"].astype(int).tolist()
self.tokenized_path = get_tokenized_path(path)
def __getitem__(self, idx):
return {"text": self.texts[idx], "label": self.labels[idx]}
def __len__(self):
return len(self.labels)
def get_texts(self):
return self.texts
def get_labels(self):
return self.labels
@classmethod
def build_dataset(cls, split: str):
return cls(f"./data/mlma_{split}.tsv")
class MisoDataset(Dataset):
def __init__(self, path: str):
self.path = path
data = pd.read_csv(path, sep="\t")
self.texts = data["text"].tolist()
self.labels = data["misogynous"].astype(int).tolist()
self.tokenized_path = get_tokenized_path(path)
def __getitem__(self, idx):
return {"text": self.texts[idx], "label": self.labels[idx]}
def __len__(self):
return len(self.labels)
def get_texts(self):
return self.texts
def get_labels(self):
return self.labels
@classmethod
def build_dataset(cls, name: str, split: str):
if name == "miso":
return cls(f"./data/miso_{split}.tsv")
elif name == "miso-ita-raw":
return cls(f"./data/AMI2020_{split}_raw.tsv")
elif name == "miso-ita-synt":
return cls(f"./data/AMI2020_{split}_synt.tsv")
else:
raise ValueError("Type not recognized.")
class MisoSyntDataset(Dataset):
def __init__(self, path: str):
self.path = path
data = pd.read_csv(path, sep="\t", header=None, names=["Text", "Label"])
self.texts = data["Text"].tolist()
self.labels = data["Label"].astype(int).tolist()
self.tokenized_path = get_tokenized_path(path)
def __getitem__(self, idx):
return {"text": self.texts[idx], "label": self.labels[idx]}
def __len__(self):
return len(self.labels)
def get_texts(self):
return self.texts
def get_labels(self):
return self.labels
@classmethod
def build_dataset(cls, type: str):
if type not in MISOSYNT_DATASETS:
raise ValueError("Type not recognized.")
else:
return cls(f"./data/miso_synt_test.tsv")
class Madlibs(Dataset):
def __init__(self, path: str):
self.path = path
data = pd.read_csv(path)
# Use the same convention for binary labels: 0 (NOT_BAD/FALSE), 1 (BAD/TRUE)
self.texts = data["Text"].tolist()
self.labels = pd.get_dummies(data.Label)["BAD"].tolist()
self.tokenized_path = get_tokenized_path(path)
def __getitem__(self, idx):
return {"text": self.texts[idx], "label": self.labels[idx]}
def __len__(self):
return len(self.labels)
def get_texts(self):
return self.texts
def get_labels(self):
return self.labels
@classmethod
def build_dataset(cls, type: str):
if type not in MADLIBS_DATASETS:
raise ValueError("Type not recognized.")
if type == "madlibs77k":
return cls(f"./data/bias_madlibs_77k.csv")
else:
return cls(f"./data/bias_madlibs_89k.csv")
class TokenizerDataModule(pl.LightningDataModule):
def __init__(
self,
dataset_name,
tokenizer,
batch_size,
max_seq_length,
num_workers,
pin_memory,
load_pre_tokenized=False,
store_pre_tokenized=False,
):
super().__init__()
self.dataset_name = dataset_name
self.tokenizer = tokenizer
self.batch_size = batch_size
self.max_seq_length = max_seq_length
self.num_workers = num_workers
self.pin_memory = pin_memory
self.load_pre_tokenized = load_pre_tokenized
self.store_pre_tokenized = store_pre_tokenized
self.train, self.val, self.test = get_dataset_by_name(dataset_name)
self.train_steps = int(len(self.train) / batch_size)
def prepare_data(self):
train, val, test = self.train, self.val, self.test
for split in [train, val, test]:
if self.load_pre_tokenized and os.path.exists(split.tokenized_path):
logging.info(
"""
Loading pre-tokenized dataset.
Beware! Using pre-tokenized embeddings could not match you choice for max_length
"""
)
continue
if self.load_pre_tokenized:
logging.info(f"Load tokenized but {split.tokenized_path} is not found")
logger.info("Tokenizing...")
encodings = self.tokenizer(
split.get_texts(),
truncation=True,
padding="max_length",
max_length=self.max_seq_length,
return_tensors="pt",
)
if self.store_pre_tokenized:
logger.info(f"Saving to {split.tokenized_path}")
torch.save(encodings, split.tokenized_path)
def setup(self, stage=None):
if stage == "fit":
train, val = self.train, self.val
logging.info(f"TRAIN len: {len(train)}")
logging.info(f"VAL len: {len(val)}")
train_encodings = torch.load(train.tokenized_path)
train_labels = torch.LongTensor([r["label"] for r in train])
self.train_data = EncodedDataset(train_encodings, train_labels)
val_encodings = torch.load(val.tokenized_path)
val_labels = torch.LongTensor([r["label"] for r in val])
self.val_data = EncodedDataset(val_encodings, val_labels)
elif stage == "test":
test = self.test
logging.info(f"TEST len: {len(test)}")
test_encodings = torch.load(test.tokenized_path)
test_labels = torch.LongTensor([r["label"] for r in test])
self.test_data = EncodedDataset(test_encodings, test_labels)
else:
raise ValueError(f"Stage {stage} not known")
def train_dataloader(self):
return DataLoader(
self.train_data,
batch_size=self.batch_size,
shuffle=True,
num_workers=self.num_workers,
pin_memory=self.pin_memory,
)
def val_dataloader(self):
return DataLoader(
self.val_data,
batch_size=self.batch_size,
num_workers=self.num_workers,
pin_memory=self.pin_memory,
)
def test_dataloader(self):
return DataLoader(
self.test_data,
batch_size=self.batch_size,
num_workers=self.num_workers,
pin_memory=self.pin_memory,
)
class EncodedDataset(Dataset):
def __init__(self, encodings, labels):
self.encodings = encodings
self.labels = labels
def __getitem__(self, idx):
item = {k: v[idx] for k, v in self.encodings.items()}
item["labels"] = self.labels[idx]
return item
def __len__(self):
return self.labels.shape[0]
class PlainDataset(Dataset):
def __init__(self, texts, labels):
self.texts = texts
self.labels = labels
def __getitem__(self, index):
return {"text": self.texts[index], "label": self.labels[index]}
def __len__(self):
return len(self.labels)