-
Notifications
You must be signed in to change notification settings - Fork 16
/
data.py
417 lines (351 loc) · 14.5 KB
/
data.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
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
from datasets import load_dataset
from sentence_transformers.readers import InputExample
from sentence_transformers import util
import pandas as pd
import os
import gzip
import csv
import logging
import subprocess
from zipfile import ZipFile
SCORE = "score"
SPLIT = "split"
SENTENCE = "sentence"
SENTENCE1 = "sentence1"
SENTENCE2 = "sentence2"
QUESTION = "question"
QUESTION1 = "question1"
QUESTION2 = "question2"
def load_snli():
"""
Load the SNLI dataset (https://nlp.stanford.edu/projects/snli/) from huggingface dataset portal.
Parameters
----------
None
Returns
----------
all_pairs: a list of sentence pairs from the SNLI dataset
"""
all_pairs = []
dataset = load_dataset("snli")
all_pairs += [(row["premise"], row["hypothesis"]) for row in dataset["train"]]
all_pairs += [(row["premise"], row["hypothesis"]) for row in dataset["validation"]]
all_pairs += [(row["premise"], row["hypothesis"]) for row in dataset["test"]]
return all_pairs, None, None
def load_sts():
"""
Load the STS datasets:
STS 2012: https://www.cs.york.ac.uk/semeval-2012/task6/
STS 2013: http://ixa2.si.ehu.eus/sts/
STS 2014: https://alt.qcri.org/semeval2014/task10/
STS 2015: https://alt.qcri.org/semeval2015/task2/
STS 2016: https://alt.qcri.org/semeval2016/task1/
STS-Benchmark: http://ixa2.si.ehu.eus/stswiki/index.php/STSbenchmark
Parameters
----------
None
Returns
----------
all_pairs: a list of sentence pairs from the STS datasets
all_test: a dict of all test sets of the STS datasets
dev_samples: a list of InputExample instances as the dev set
"""
# Check if STS datasets exsist. If not, download and extract it
sts_dataset_path = "data/"
if not os.path.exists(os.path.join(sts_dataset_path, "STS_data")):
logging.info("Dataset not found. Download")
zip_save_path = "data/STS_data.zip"
#os.system("wget https://fangyuliu.me/data/STS_data.zip -P data/")
subprocess.run(["wget", "--no-check-certificate", "https://fangyuliu.me/data/STS_data.zip", "-P", "data/"])
with ZipFile(zip_save_path, "r") as zipIn:
zipIn.extractall(sts_dataset_path)
all_pairs = []
all_test = {}
dedup = set()
# read sts 2012-2016
for year in ["2012","2013","2014","2015","2016"]:
all_test[year] = []
for year in ["2012","2013","2014","2015","2016"]:
df = pd.read_csv(f"data/STS_data/en/{year}.test.tsv", delimiter="\t",
quoting=csv.QUOTE_NONE, encoding="utf-8", names=[SCORE, SENTENCE1, SENTENCE2])
for row in df.iterrows():
if str(row[1][SCORE]) == "nan": continue
all_test[year].append(InputExample(texts=[row[1][SENTENCE1], row[1][SENTENCE2]], label=row[1][SCORE]))
df = pd.read_csv("data/STS_data/en/2012_to_2016.test.tsv", delimiter="\t",
quoting=csv.QUOTE_NONE, encoding="utf-8", names=[SCORE, SENTENCE1, SENTENCE2])
for row in df.iterrows():
concat = row[1][SENTENCE1]+row[1][SENTENCE2]
if concat in dedup:
continue
all_pairs.append([row[1][SENTENCE1], row[1][SENTENCE2]])
dedup.add(concat)
# sts-b
# Check if STS-B exsists. If not, download and extract it
sts_dataset_path = "data/stsbenchmark.tsv.gz"
if not os.path.exists(sts_dataset_path):
util.http_get("https://sbert.net/datasets/stsbenchmark.tsv.gz", sts_dataset_path)
# read sts-b
dev_samples_stsb = []
test_samples_stsb = []
with gzip.open(sts_dataset_path, "rt", encoding="utf8") as fIn:
reader = csv.DictReader(fIn, delimiter="\t", quoting=csv.QUOTE_NONE)
for row in reader:
score = float(row[SCORE]) / 5.0 # Normalize score to range 0 ... 1
if row[SPLIT] == "dev":
dev_samples_stsb.append(InputExample(texts=[row[SENTENCE1], row[SENTENCE2]], label=score))
elif row[SPLIT] == "test":
test_samples_stsb.append(InputExample(texts=[row[SENTENCE1], row[SENTENCE2]], label=score))
# add (non-duplicated) sentence pair to all_pairs
concat = row[SENTENCE1]+row[SENTENCE2]
if concat in dedup:
continue
all_pairs.append([row[SENTENCE1], row[SENTENCE2]])
dedup.add(concat)
all_test["stsb"] = test_samples_stsb
dev_samples = dev_samples_stsb
return all_pairs, all_test, dev_samples
def load_sickr():
"""
Load the SICK-R dataset: http://clic.cimec.unitn.it/composes/sick.html
Parameters
----------
None
Returns
----------
all_pairs: a list of sentence pairs from the SICK-R dataset
all_test: a dict of all test sets
dev_samples: a list of InputExample instances as the dev set
"""
sts_dataset_path = "data/"
if not os.path.exists(os.path.join(sts_dataset_path, "STS_data")):
logging.info("Dataset not found. Download")
zip_save_path = "data/STS_data.zip"
subprocess.run(["wget", "--no-check-certificate", "https://fangyuliu.me/data/STS_data.zip", "-P", "data/"])
with ZipFile(zip_save_path, "r") as zipIn:
zipIn.extractall(sts_dataset_path)
all_pairs = []
all_test = {}
dedup = set()
# read sickr
test_samples_sickr = []
dev_samples_sickr = []
df = pd.read_csv("data/STS_data/en/SICK_annotated.txt", delimiter="\t",
quoting=csv.QUOTE_NONE, encoding="utf-8")
for row in df.iterrows():
row = row[1]
score = row["relatedness_score"] / 5.0
if row["SemEval_set"] == "TEST":
test_samples_sickr.append(InputExample(texts=[row["sentence_A"], row["sentence_B"]], label=score))
elif row["SemEval_set"] == "TRIAL":
dev_samples_sickr.append(InputExample(texts=[row["sentence_A"], row["sentence_B"]], label=score))
concat = row["sentence_A"]+row["sentence_B"]
if concat in dedup:
continue
all_pairs.append([row["sentence_A"], row["sentence_B"]])
dedup.add(concat)
all_test["sickr"] = test_samples_sickr
dev_samples = dev_samples_sickr
return all_pairs, all_test, dev_samples
def load_qqp():
"""
Load the QQP dataset (https://www.kaggle.com/c/quora-question-pairs) from huggingface dataset portal.
Parameters
----------
None
Returns
----------
all_pairs: a list of sentence pairs from the QQP dataset
all_test: a dict of all test sets
dev_samples: a list of InputExample instances as the dev set
"""
all_pairs = []
all_test = {}
dev_samples_qqp = []
test_samples_qqp = []
# Check if the QQP dataset exists. If not, download and extract
qqp_dataset_path = "data/quora-IR-dataset"
if not os.path.exists(qqp_dataset_path):
logging.info("Dataset not found. Download")
zip_save_path = 'data/quora-IR-dataset.zip'
util.http_get(url='https://sbert.net/datasets/quora-IR-dataset.zip', path=zip_save_path)
with ZipFile(zip_save_path, 'r') as zipIn:
zipIn.extractall(qqp_dataset_path)
qqp_datapoints_cut_train = 10000
qqp_datapoints_cut_val = 1000
qqp_datapoints_cut_test = 10000
with open(os.path.join(qqp_dataset_path, "classification/train_pairs.tsv"), encoding="utf8") as fIn:
reader = csv.DictReader(fIn, delimiter="\t", quoting=csv.QUOTE_NONE)
for i, row in enumerate(reader):
if i == qqp_datapoints_cut_train: break
all_pairs.append([row[QUESTION1], row[QUESTION2]])
with open(os.path.join(qqp_dataset_path, "classification/dev_pairs.tsv"), encoding="utf8") as fIn:
reader = csv.DictReader(fIn, delimiter="\t", quoting=csv.QUOTE_NONE)
for i, row in enumerate(reader):
if i == qqp_datapoints_cut_val: break
dev_samples_qqp.append(InputExample(texts=[row[QUESTION1], row[QUESTION2]], label=int(row['is_duplicate'])))
all_pairs.append([row[QUESTION1], row[QUESTION2]])
with open(os.path.join(qqp_dataset_path, "classification/test_pairs.tsv"), encoding="utf8") as fIn:
reader = csv.DictReader(fIn, delimiter="\t", quoting=csv.QUOTE_NONE)
for i, row in enumerate(reader):
if i == qqp_datapoints_cut_test: break
test_samples_qqp.append(InputExample(texts=[row[QUESTION1], row[QUESTION2]], label=int(row['is_duplicate'])))
all_pairs.append([row[QUESTION1], row[QUESTION2]])
all_test["qqp"] = test_samples_qqp
dev_samples = dev_samples_qqp
return all_pairs, all_test, dev_samples
def load_qnli():
"""
Load the QNLI dataset (part of GLUE: https://gluebenchmark.com/) from huggingface dataset portal.
Parameters
----------
None
Returns
----------
all_pairs: a list of sentence pairs from the QNLI dataset
all_test: a dict of all test sets
dev_samples: a list of InputExample instances as the dev set
"""
all_pairs = []
all_test = {}
dev_samples_qnli = []
test_samples_qnli = []
dataset = load_dataset("glue", "qnli")
qnli_datapoints_cut_train = 10000
for i, row in enumerate(dataset["train"]):
if i == qnli_datapoints_cut_train: break
all_pairs.append([row[QUESTION], row[SENTENCE]])
for row in dataset["validation"]:
label = 0 if row["label"]==1 else 1
dev_samples_qnli.append(
InputExample(texts=[row[QUESTION], row[SENTENCE]], label=label))
all_pairs.append([row[QUESTION], row[SENTENCE]])
# test labels of qnli are not given, use the first 1k in dev set as test
all_test["qnli"] = dev_samples_qnli[1000:]
dev_samples = dev_samples_qnli[:1000]
return all_pairs, all_test, dev_samples
def load_mrpc():
"""
Load the MRPC dataset (https://www.microsoft.com/en-us/download/details.aspx?id=52398) from huggingface dataset portal.
Parameters
----------
None
Returns
----------
all_pairs: a list of sentence pairs from the MRPC dataset
all_test: a dict of all test sets
dev_samples: a list of InputExample instances as the dev set
"""
all_pairs = []
all_test = {}
dev_samples_mrpc = []
test_samples_mrpc = []
dataset = load_dataset("glue", "mrpc")
for row in dataset["train"]:
all_pairs.append([row[SENTENCE1], row[SENTENCE2]])
for row in dataset["validation"]:
dev_samples_mrpc.append(
InputExample(texts=[row[SENTENCE1], row[SENTENCE2]], label=int(row["label"])))
all_pairs.append([row[SENTENCE1], row[SENTENCE2]])
for row in dataset["test"]:
test_samples_mrpc.append(
InputExample(texts=[row[SENTENCE1], row[SENTENCE2]], label=int(row["label"])))
all_pairs.append([row[SENTENCE1], row[SENTENCE2]])
all_test["mrpc"] = test_samples_mrpc
dev_samples = dev_samples_mrpc
return all_pairs, all_test, dev_samples
def load_sts_and_sickr():
"""
Load both STS and SICK-R datasets. Use STS-B's dev set for dev.
Parameters
----------
None
Returns
----------
all_pairs: a list of sentence pairs from the STS+SICK-R dataset
all_test: a dict of all test sets
dev_samples: a list of InputExample instances as the dev set
"""
all_pairs_sts, all_test_sts, dev_samples_sts = load_sts()
all_pairs_sickr, all_test_sickr, dev_samples_sickr = load_sickr()
all_pairs = all_pairs_sts+all_pairs_sickr
all_test = {**all_test_sts, **all_test_sickr}
return all_pairs, all_test, dev_samples_sts # sts-b's dev is used
def load_custom(fpath):
"""
Load custom sentence-pair corpus. Use STS-B's dev set for dev.
Parameters
----------
fpath: path to the training file, where sentence pairs are formatted as 'sent1||sent2'
Returns
----------
all_pairs: a list of sentence pairs from the STS+SICK-R dataset
all_test: a dict of all test sets
dev_samples: a list of InputExample instances as the dev set
"""
all_pairs = []
all_test = {}
# load custom training corpus
with open(fpath, "r") as f:
lines = f.readlines()
for line in lines:
line = line.strip()
if len(line.split("||")) != 2: continue # skip
sent1, sent2 = line.split("||")
all_pairs.append([sent1, sent2])
# load STS-b dev/test set
# Check if STS-B exsists. If not, download and extract it
sts_dataset_path = "data/stsbenchmark.tsv.gz"
if not os.path.exists(sts_dataset_path):
util.http_get("https://sbert.net/datasets/stsbenchmark.tsv.gz", sts_dataset_path)
# read sts-b
dev_samples_stsb = []
test_samples_stsb = []
with gzip.open(sts_dataset_path, "rt", encoding="utf8") as fIn:
reader = csv.DictReader(fIn, delimiter="\t", quoting=csv.QUOTE_NONE)
for row in reader:
score = float(row[SCORE]) / 5.0 # Normalize score to range 0 ... 1
if row[SPLIT] == "dev":
dev_samples_stsb.append(InputExample(texts=[row[SENTENCE1], row[SENTENCE2]], label=score))
elif row[SPLIT] == "test":
test_samples_stsb.append(InputExample(texts=[row[SENTENCE1], row[SENTENCE2]], label=score))
# add entence pair to all_pairs
#all_pairs.append([row[SENTENCE1], row[SENTENCE2]])
all_test["stsb"] = test_samples_stsb
dev_samples = dev_samples_stsb
return all_pairs, all_test, dev_samples
task_loader_dict = {
"sts": load_sts,
"sickr": load_sickr,
"sts_sickr": load_sts_and_sickr,
"qqp": load_qqp,
"qnli": load_qnli,
"mrpc": load_mrpc,
"snli": load_snli,
"custom": load_custom
}
def load_data(task, fpath=None):
"""
A unified dataset loader for all tasks.
Parameters
----------
task: a string specifying dataset/task to be loaded (for possible options see 'task_loader_dict')
Returns
----------
all_pairs: a list of sentence pairs from the specified dataset
all_test: a dict of all test sets
dev_samples: a list of InputExample instances as the dev set
"""
if task not in task_loader_dict.keys():
raise NotImplementedError()
if task == "custom":
return task_loader_dict[task](fpath)
else:
return task_loader_dict[task]()
if __name__ == "__main__":
# test if all datasets can be properly loaded
for task in task_loader_dict:
print (f"loading {task}...")
load_data(task)
print ("done.")