-
Notifications
You must be signed in to change notification settings - Fork 13
/
tasks.py
759 lines (564 loc) · 28.5 KB
/
tasks.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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
import json
import random
import os
from itertools import chain
from abc import ABC, abstractmethod
from collections import defaultdict
from typing import Union, List, Iterable, Callable, Dict, Optional
from sklearn.metrics import precision_recall_fscore_support, accuracy_score, mean_absolute_error, matthews_corrcoef
from scipy.stats import pearsonr, spearmanr
from utils.normalizer import TextNormalizer
class DataExample(object):
def __init__(self, inputs: Union[str, List], label: Optional[str]):
self.inputs: List[str] = [inputs] if isinstance(inputs, str) else inputs
self.label: str = label
class TaskSpecification(object):
def __init__(self, task_dir: str, task_type: str, num_labels: int, num_inputs: int, group_dir: str=""):
self.task_dir: str = task_dir
self.group_dir: str = group_dir
self.output_dir: str = task_dir
self.task_type: str = task_type
self.num_labels: int = num_labels
self.num_inputs: int = num_inputs
self.evaluation_metric: Callable = self.accuracy if task_type == "classification" else self.corr
self.no_dev_set = False
def to_json(self):
return {
"dir": self.task_dir,
"group": self.group_dir,
"type": self.task_type,
"num_labels": self.num_labels,
"num_inputs": self.num_inputs,
"metric": self.evaluation_metric.__name__
}
def task_path(self) -> str:
return self.task_dir if not self.group_dir else f"{self.group_dir}/{self.task_dir}"
def output_path(self) -> str:
return self.output_dir if not self.group_dir else f"{self.group_dir}/{self.output_dir}"
def accuracy(self, y_true, y_pred):
return {"accuracy": accuracy_score(y_true, y_pred)}
def corr(self, y_true, y_pred):
return {"pearson": pearsonr(y_true, y_pred)[0], "spearman": spearmanr(y_true, y_pred)[0]}
def mcc(self, y_true, y_pred):
return {"mcc": matthews_corrcoef(y_true, y_pred)}
def f1(self, y_true, y_pred):
res = precision_recall_fscore_support(y_true, y_pred, average="micro")
acc = accuracy_score(y_true, y_pred)
return {"precision": res[0], "recall": res[1], "micro-f1": res[2], "accuracy": acc}
def binary_f1(self, y_true, y_pred):
y_true = [int(val) for val in y_true]
y_pred = [int(val) for val in y_pred]
res = precision_recall_fscore_support(y_true, y_pred, average="binary")
acc = accuracy_score(y_true, y_pred)
return {"precision": res[0], "recall": res[1], "binary-f1": res[2], "accuracy": acc}
def wmae(self, y_true, y_pred):
if(isinstance(y_true[0], str)):
y_true = [float(val) for val in y_true]
y_pred = [float(val) for val in y_pred]
y_true_per_class = defaultdict(list)
y_pred_per_class = defaultdict(list)
for yt, yp in zip(y_true, y_pred):
y_true_per_class[yt].append(yt)
y_pred_per_class[yt].append(yp)
mae = []
for clazz in y_true_per_class.keys():
yt = y_true_per_class[clazz]
yp = y_pred_per_class[clazz]
mae.append(mean_absolute_error(yt, yp))
mae_avg = sum(mae) / len(mae)
return {"wmae": mae_avg, "1-wmae": 1 - mae_avg}
class BaseTask(ABC):
@abstractmethod
def read(self, data_path: str, split: str) -> Iterable[DataExample]:
raise NotImplementedError
def spec(self) -> TaskSpecification:
return self.__getattribute__("_spec")
def get_split_path(self, data_path: str, split: str, extension: str="txt") -> str:
input_path = os.path.join(data_path, self.spec().task_path(), split + "." + extension)
if not os.path.exists(input_path):
raise FileNotFoundError(input_path)
return input_path
def read_simple(self, data_path: str, split: str, separator: str=" ", label_first: bool=True, normalize: bool=True):
label_idx = 0 if label_first else 1
text_idx = 1 if label_first else 0
input_path = self.get_split_path(data_path, split)
normalize_func = lambda val: val
if normalize:
normalizer = TextNormalizer()
normalize_func = lambda val: normalizer.process(val)
with open(input_path, "r", encoding="utf-8") as input_file:
for line in input_file:
values = line.split(sep=separator, maxsplit=1)
label = values[label_idx]
text = values[text_idx].strip()
text = normalize_func(text)
yield DataExample(text, label)
class CrossValidatedTask(BaseTask):
def __init__(self, wrapped_task: BaseTask, num_folds: int=4, seed: int=None):
self.wrapped_task: BaseTask = wrapped_task
self.num_folds = num_folds
self.folds = None
self._spec = wrapped_task.spec()
self.set_fold(0)
self.seed = seed
def set_fold(self, fold: int):
self.fold = fold
self._spec.output_dir = f"{self._spec.task_dir}-fold{self.fold}"
def _read_folds(self, data_path: str):
if self.seed is not None: random.seed(self.seed)
data: List[DataExample] = []
for record in self.wrapped_task.read(data_path, "train"):
data.append(record)
random.shuffle(data)
folds = [[] for _ in range(self.num_folds)]
for idx, record in enumerate(data):
fold_idx = idx % self.num_folds
folds[fold_idx].append(record)
return folds
def read(self, data_path: str, split: str) -> Iterable[DataExample]:
if self.folds is None:
self.folds = self._read_folds(data_path)
if split == "dev":
return self.wrapped_task.read(data_path, split)
elif split == "train":
folds = [self.folds[idx] for idx in range(self.num_folds) if idx != self.fold]
return chain(*folds)
elif split == "test":
return [rec for rec in self.folds[self.fold]]
@staticmethod
def cv_folds(wrapped_task: BaseTask, num_folds: int=4, seed: int=None) -> Iterable[BaseTask]:
task = CrossValidatedTask(wrapped_task, num_folds, seed)
for fold in range(num_folds):
task.set_fold(fold)
yield task
class WCCRSHotelsTask(BaseTask):
def __init__(self):
self._spec = TaskSpecification("WCCRS_HOTELS", "classification", 4, 1)
def read(self, data_path: str, split: str) -> Iterable[DataExample]:
return self.read_simple(data_path, split)
class WCCRSMedicineTask(BaseTask):
def __init__(self):
self._spec = TaskSpecification("WCCRS_MEDICINE", "classification", 4, 1)
def read(self, data_path: str, split: str) -> Iterable[DataExample]:
return self.read_simple(data_path, split)
class EightTagsTask(BaseTask):
def __init__(self):
self._spec = TaskSpecification("8TAGS", "classification", 8, 1)
def read(self, data_path: str, split: str) -> Iterable[DataExample]:
return self.read_simple(data_path, split)
class SICKTask(BaseTask):
def read(self, data_path: str, split: str) -> Iterable[DataExample]:
input_path = self.get_split_path(data_path, split)
normalizer = TextNormalizer()
with open(input_path, "r", encoding="utf-8") as input_file:
for idx, line in enumerate(input_file):
if idx == 0: continue
values = line.split("\t")
input1: str = normalizer.process(values[1].strip())
input2: str = normalizer.process(values[2].strip())
relatedness: float = float(values[3].strip())
entailment: str = values[4].strip()
yield self.create_example(input1, input2, relatedness, entailment)
@abstractmethod
def create_example(self, input1: str, input2: str, relatedness: float, entailment: str):
raise NotImplementedError
class SICKEntailmentTask(SICKTask):
def __init__(self):
self._spec = TaskSpecification("SICK", "classification", 3, 2)
self._spec.output_dir = "SICK-E"
def create_example(self, input1: str, input2: str, relatedness: float, entailment: str):
return DataExample([input1, input2], entailment)
class SICKRelatednessTask(SICKTask):
def __init__(self):
self._spec = TaskSpecification("SICK", "regression", 1, 2)
self._spec.output_dir = "SICK-R"
def create_example(self, input1: str, input2: str, relatedness: float, entailment: str):
label = "%.5f" % (relatedness / 5.0,)
return DataExample([input1, input2], label)
def format_output(self, value: float):
return "%.2f" % (value * 5,)
def postprocess_prediction(self, value: float):
return min((max(value, 0.0)), 1.0)
class CDSEntailmentTask(SICKTask):
def __init__(self):
self._spec = TaskSpecification("CDS", "classification", 3, 2)
self._spec.output_dir = "CDS-E"
def create_example(self, input1: str, input2: str, relatedness: float, entailment: str):
return DataExample([input1, input2], entailment)
class CDSRelatednessTask(SICKTask):
def __init__(self):
self._spec = TaskSpecification("CDS", "regression", 1, 2)
self._spec.output_dir = "CDS-R"
def create_example(self, input1: str, input2: str, relatedness: float, entailment: str):
label = "%.5f" % (relatedness / 5.0,)
return DataExample([input1, input2], label)
def format_output(self, value: float):
return "%.2f" % (value * 5,)
def postprocess_prediction(self, value: float):
return min((max(value, 0.0)), 1.0)
class CBDTask(BaseTask):
def __init__(self):
self._spec = TaskSpecification("CBD", "classification", 2, 1)
self._spec.no_dev_set = True
self._spec.evaluation_metric = self._spec.binary_f1
def read(self, data_path: str, split: str) -> Iterable[DataExample]:
split_name = "training" if split == "train" else split
file_pattern = "{}_set_clean_only_{}.txt"
text_path = os.path.join(data_path, self._spec.task_path(), file_pattern.format(split_name, "text"))
tags_path = os.path.join(data_path, self._spec.task_path(), file_pattern.format(split_name, "tags"))
normalizer = TextNormalizer(detokenize=False)
with open(text_path, "r", encoding="utf-8") as text_file, open(tags_path, "r", encoding="utf-8") as tags_file:
text_lines = text_file.readlines()
tags_lines = tags_file.readlines()
assert len(text_lines) == len(tags_lines)
for idx in range(len(text_lines)):
text = normalizer.process(text_lines[idx].strip())
text = text.replace("@anonymized_account", "@ użytkownik")
label = tags_lines[idx].strip()
yield DataExample(text, label)
class PolEmoINTask(BaseTask):
def __init__(self):
self._spec = TaskSpecification("POLEMO", "classification", 4, 1)
self._spec.output_dir = "POLEMO-IN"
def read(self, data_path: str, split: str) -> Iterable[DataExample]:
split = split if split == "train" else f"in-{split}"
path = self.get_split_path(data_path, split)
normalizer = TextNormalizer()
with open(path, "r", encoding="utf-8") as input_file:
for line in input_file:
words = line.split()
label = words[-1]
text = " ".join(words[0:-1])
text = text.replace(" em ", "em ").replace(" śmy ", "śmy ").replace(" m ", "m ")
text = normalizer.process(text)
yield DataExample(text, label)
class PolEmoOUTTask(BaseTask):
def __init__(self):
self._spec = TaskSpecification("POLEMO", "classification", 4, 1)
self._spec.output_dir = "POLEMO-OUT"
def read(self, data_path: str, split: str) -> Iterable[DataExample]:
split = split if split == "train" else f"out-{split}"
path = self.get_split_path(data_path, split)
normalizer = TextNormalizer()
with open(path, "r", encoding="utf-8") as input_file:
for line in input_file:
words = line.split()
label = words[-1]
text = " ".join(words[0:-1])
text = text.replace(" em ", "em ").replace(" śmy ", "śmy ").replace(" m ", "m ")
text = normalizer.process(text)
yield DataExample(text, label)
class KLEJTask(BaseTask):
def read(self, data_path: str, split: str) -> Iterable[DataExample]:
input_path = os.path.join(data_path, self.spec().task_path(), split + ".tsv")
has_target = True
if split == "test" and not os.path.exists(input_path):
input_path = os.path.join(data_path, self.spec().task_path(), split + "_features.tsv")
has_target = False
normalizer = self.normalizer()
with open(input_path, "r", encoding="utf-8") as input_file:
header = input_file.readline().strip().split("\t")
for line in input_file:
values = [val.strip() for val in line.split("\t")]
assert len(header) == len(values), values
row = {key: val for key, val in zip(header, values)}
yield self.create_example(row, normalizer, has_target)
def normalizer(self):
return TextNormalizer()
@abstractmethod
def create_example(self, row: Dict, normalizer: TextNormalizer, has_target: bool) -> DataExample:
raise NotImplementedError
class KLEJCBDTask(KLEJTask):
def __init__(self):
self._spec = TaskSpecification("CBD", "classification", 2, 1, "KLEJ")
self._spec.no_dev_set = True
self._spec.evaluation_metric = self._spec.binary_f1
def normalizer(self) -> TextNormalizer:
return TextNormalizer(detokenize=False)
def create_example(self, row: Dict, normalizer: TextNormalizer, has_target: bool) -> DataExample:
text = normalizer.process(row["sentence"].strip())
text = text.replace("@anonymized_account", "@ użytkownik")
return DataExample(text, row["target"].strip() if has_target else None)
class KLEJDYKTask(KLEJTask):
def __init__(self):
self._spec = TaskSpecification("DYK", "classification", 2, 2, "KLEJ")
self._spec.no_dev_set = True
self._spec.evaluation_metric = self._spec.binary_f1
def normalizer(self) -> TextNormalizer:
return TextNormalizer(detokenize=False)
def create_example(self, row: Dict, normalizer: TextNormalizer, has_target: bool) -> DataExample:
text1 = row["question"].strip()
text2 = row["answer"].strip()
return DataExample([text1, text2], row["target"].strip() if has_target else None)
class KLEJPSCTask(KLEJTask):
def __init__(self):
self._spec = TaskSpecification("PSC", "classification", 2, 2, "KLEJ")
self._spec.no_dev_set = True
self._spec.evaluation_metric = self._spec.binary_f1
def normalizer(self) -> TextNormalizer:
return TextNormalizer(detokenize=False)
def create_example(self, row: Dict, normalizer: TextNormalizer, has_target: bool) -> DataExample:
text1 = row["extract_text"].strip()
text2 = row["summary_text"].strip()
return DataExample([text1, text2], row["label"].strip() if has_target else None)
class KLEJPolEmoINTask(KLEJTask):
def __init__(self):
self._spec = TaskSpecification("POLEMO2.0-IN", "classification", 4, 1, "KLEJ")
def create_example(self, row: Dict, normalizer: TextNormalizer, has_target: bool) -> DataExample:
text= normalizer.process(row["sentence"].strip())
return DataExample(text, row["target"].strip() if has_target else None)
class KLEJPolEmoOUTTask(KLEJTask):
def __init__(self):
self._spec = TaskSpecification("POLEMO2.0-OUT", "classification", 4, 1, "KLEJ")
self.labels = ("__label__meta_minus_m", "__label__meta_plus_m", "__label__meta_amb", "__label__meta_zero")
def create_example(self, row: Dict, normalizer: TextNormalizer, has_target: bool) -> DataExample:
text= normalizer.process(row["sentence"].strip())
for label in self.labels:
if label in text:
print(text)
return DataExample(text, row["target"].strip() if has_target else None)
class KLEJCDSEntailmentTask(KLEJTask):
def __init__(self):
self._spec = TaskSpecification("CDSC-E", "classification", 3, 2, "KLEJ")
def create_example(self, row: Dict, normalizer: TextNormalizer, has_target: bool) -> DataExample:
text1 = normalizer.process(row["sentence_A"].strip())
text2 = normalizer.process(row["sentence_B"].strip())
return DataExample([text1, text2], row["entailment_judgment"].strip() if has_target else None)
class KLEJCDSRelatednessTask(KLEJTask):
def __init__(self):
self._spec = TaskSpecification("CDSC-R", "regression", 1, 2, "KLEJ")
def create_example(self, row: Dict, normalizer: TextNormalizer, has_target: bool) -> DataExample:
text1 = normalizer.process(row["sentence_A"].strip())
text2 = normalizer.process(row["sentence_B"].strip())
if has_target:
score = float(row["relatedness_score"])
score = "%.5f" % (score / 5.0,)
else: score = None
return DataExample([text1, text2], score)
def format_output(self, value: float):
return "%.2f" % (value * 5,)
def postprocess_prediction(self, value: float):
return min((max(value, 0)), 1)
class KLEJNKJPTask(KLEJTask):
def __init__(self):
self._spec = TaskSpecification("NKJP-NER", "classification", 6, 1, "KLEJ")
def create_example(self, row: Dict, normalizer: TextNormalizer, has_target: bool) -> DataExample:
text = normalizer.process(row["sentence"].strip())
return DataExample(text, row["target"].strip() if has_target else None)
class KLEJECRRegressionTask(KLEJTask):
def __init__(self):
self._spec = TaskSpecification("ECR", "regression", 1, 1, "KLEJ")
self._spec.evaluation_metric = self._spec.wmae
def create_example(self, row: Dict, normalizer: TextNormalizer, has_target: bool) -> DataExample:
text = row["text"].strip()
if has_target:
score = float(row["rating"]) - 1.0
score = "%.5f" % (score / 4.0,)
else: score = None
return DataExample(text, score)
def normalizer(self) -> TextNormalizer:
return TextNormalizer(detokenize=False)
def format_output(self, value: float):
return "%.2f" % (1 + value * 4,)
def postprocess_prediction(self, value: float):
score = min(max(1 + value * 4, 0), 5)
score = round(score)
return (score - 1.0) / 4.0
class KLEJECRClassificationTask(KLEJTask):
def __init__(self):
self._spec = TaskSpecification("ECR", "classification", 5, 1, "KLEJ")
self._spec.evaluation_metric = self._spec.wmae
def normalizer(self) -> TextNormalizer:
return TextNormalizer(detokenize=False)
def create_example(self, row: Dict, normalizer: TextNormalizer, has_target: bool) -> DataExample:
text = row["text"].strip()
return DataExample(text, row["rating"].strip() if has_target else None)
class GLUETask(BaseTask):
def read_data_file(self, data_path: str, split: str, file_name: str, has_header: bool):
input_path = os.path.join(data_path, self.spec().task_path(), file_name)
normalizer = self.normalizer()
with open(input_path, "r", encoding="utf-8") as input_file:
if has_header:
_ = input_file.readline().strip().split("\t")
for line in input_file:
row = [val.strip() for val in line.split("\t")]
yield self.create_example(row, normalizer, split)
def normalizer(self):
return TextNormalizer(detokenize=False, lang="en")
@abstractmethod
def create_example(self, row: List[str], normalizer: TextNormalizer, split: str) -> DataExample:
raise NotImplementedError
class GLUECoLATask(GLUETask):
def __init__(self) -> None:
self._spec = TaskSpecification("CoLA", "classification", 2, 1, "GLUE")
self._spec.evaluation_metric = self._spec.mcc
def read(self, data_path: str, split: str) -> Iterable[DataExample]:
return self.read_data_file(data_path, split, split + ".tsv", split != "test")
def create_example(self, row: List[str], normalizer: TextNormalizer, split: str) -> DataExample:
text = row[3 if split != "test" else 1].strip()
label = row[1] if split != "test" else None
return DataExample(text, label)
class GLUEQQPTask(GLUETask):
def __init__(self):
self._spec = TaskSpecification("QQP", "classification", 2, 2, "GLUE")
self._spec.evaluation_metric = self._spec.binary_f1
def read(self, data_path: str, split: str) -> Iterable[DataExample]:
return self.read_data_file(data_path, split, split + ".tsv", True)
def create_example(self, row: List[str], normalizer: TextNormalizer, split: str) -> DataExample:
text1 = row[3 if split != "test" else 1].strip()
text2 = row[4 if split != "test" else 2].strip()
label = row[5] if split != "test" else None
return DataExample([text1, text2], label)
class GLUEMNLIMatchedTask(GLUETask):
def __init__(self):
self._spec = TaskSpecification("MNLI", "classification", 3, 2, "GLUE")
self._spec.output_dir = "MNLI-Matched"
self._spec.evaluation_metric = self._spec.accuracy
def read(self, data_path: str, split: str) -> Iterable[DataExample]:
file_name = "train.tsv" if split == "train" else split + "_matched.tsv"
return self.read_data_file(data_path, split, file_name, True)
def create_example(self, row: List[str], normalizer: TextNormalizer, split: str) -> DataExample:
text1 = row[8].strip()
text2 = row[9].strip()
label = row[11] if split == "train" else row[15] if split == "dev" else None
return DataExample([text1, text2], label)
class GLUEMNLIMismatchedTask(GLUETask):
def __init__(self):
self._spec = TaskSpecification("MNLI", "classification", 3, 2, "GLUE")
self._spec.output_dir = "MNLI-Mismatched"
self._spec.evaluation_metric = self._spec.accuracy
def read(self, data_path: str, split: str) -> Iterable[DataExample]:
file_name = "train.tsv" if split == "train" else split + "_mismatched.tsv"
return self.read_data_file(data_path, split, file_name, True)
def create_example(self, row: List[str], normalizer: TextNormalizer, split: str) -> DataExample:
text1 = row[8].strip()
text2 = row[9].strip()
label = row[11] if split == "train" else row[15] if split == "dev" else None
return DataExample([text1, text2], label)
class GLUEQNLITask(GLUETask):
def __init__(self):
self._spec = TaskSpecification("QNLI", "classification", 3, 2, "GLUE")
self._spec.evaluation_metric = self._spec.accuracy
def read(self, data_path: str, split: str) -> Iterable[DataExample]:
return self.read_data_file(data_path, split, split + ".tsv", True)
def create_example(self, row: List[str], normalizer: TextNormalizer, split: str) -> DataExample:
text1 = row[1].strip()
text2 = row[2].strip()
label = row[3] if split != "test" else None
return DataExample([text1, text2], label)
class GLUEMRPCTask(GLUETask):
def __init__(self):
self._spec = TaskSpecification("MRPC", "classification", 2, 2, "GLUE")
self._spec.no_dev_set = True
self._spec.evaluation_metric = self._spec.binary_f1
def read(self, data_path: str, split: str) -> Iterable[DataExample]:
return self.read_data_file(data_path, split, "msr_paraphrase_" + split + ".txt", True)
def create_example(self, row: List[str], normalizer: TextNormalizer, split: str) -> DataExample:
text1 = row[3].strip()
text2 = row[4].strip()
label = row[0]
return DataExample([text1, text2], label)
class GLUERTETask(GLUETask):
def __init__(self):
self._spec = TaskSpecification("RTE", "classification", 2, 2, "GLUE")
self._spec.evaluation_metric = self._spec.accuracy
def read(self, data_path: str, split: str) -> Iterable[DataExample]:
return self.read_data_file(data_path, split, split + ".tsv", True)
def create_example(self, row: List[str], normalizer: TextNormalizer, split: str) -> DataExample:
text1 = row[1].strip()
text2 = row[2].strip()
label = row[3] if split != "test" else None
return DataExample([text1, text2], label)
class GLUESTSBTask(GLUETask):
def __init__(self):
self._spec = TaskSpecification("STS-B", "regression", 1, 2, "GLUE")
self._spec.evaluation_metric = self._spec.corr
def read(self, data_path: str, split: str) -> Iterable[DataExample]:
return self.read_data_file(data_path, split, split + ".tsv", True)
def create_example(self, row: List[str], normalizer: TextNormalizer, split: str) -> DataExample:
text1 = row[7].strip()
text2 = row[8].strip()
if split != "test":
score = float(row[9]) - 1.0
score = "%.5f" % (score / 4.0,)
else:
score = None
return DataExample([text1, text2], score)
def format_output(self, value: float):
return "%.2f" % (1 + value * 4,)
def postprocess_prediction(self, value: float):
score = min(max(1 + value * 4, 0), 5)
score = round(score)
return (score - 1.0) / 4.0
class GLUESST2Task(GLUETask):
def __init__(self):
self._spec = TaskSpecification("SST-2", "classification", 2, 1, "GLUE")
self._spec.evaluation_metric = self._spec.accuracy
def read(self, data_path: str, split: str) -> Iterable[DataExample]:
return self.read_data_file(data_path, split, split + ".tsv", True)
def create_example(self, row: List[str], normalizer: TextNormalizer, split: str) -> DataExample:
text = row[0 if split != "test" else 1].strip()
label = row[1] if split != "test" else None
return DataExample(text, label)
class GLUEDiagnosticsTask(GLUETask):
def __init__(self):
self._spec = TaskSpecification("AX", "classification", 3, 2, "GLUE")
self._spec.no_dev_set = True
self._spec.evaluation_metric = self._spec.mcc
def read(self, data_path: str, split: str) -> Iterable[DataExample]:
assert split in ("train", "test")
if split == "train":
return self.read_data_file(data_path, split, "../MNLI/train.tsv", True)
elif split == "test":
return self.read_data_file(data_path, split, "AX.tsv", True)
def create_example(self, row: List[str], normalizer: TextNormalizer, split: str) -> DataExample:
if split == "train":
text1 = row[8].strip()
text2 = row[9].strip()
label = row[11]
else:
text1 = row[1].strip()
text2 = row[2].strip()
label = None
return DataExample([text1, text2], label)
class PPCTask(BaseTask):
def __init__(self):
self._spec = TaskSpecification("PPC", "classification", 3, 2)
self._spec.evaluation_metric = self._spec.accuracy
def read(self, data_path: str, split: str) -> Iterable[DataExample]:
if split == "dev": split = "test"
split_path = self.get_split_path(data_path, split, extension="jsonl")
with open(split_path, "r", encoding="utf-8") as input_file:
for line in input_file:
obj = json.loads(line.strip())
sent1, sent2, label = obj["sent1"], obj["sent2"], obj["label"]
yield DataExample([sent1, sent2], label)
TASKS = {
# Polish tasks
"WCCRS_HOTELS": WCCRSHotelsTask,
"WCCRS_MEDICINE": WCCRSMedicineTask,
"SICK-E": SICKEntailmentTask,
"SICK-R": SICKRelatednessTask,
"8TAGS": EightTagsTask,
"KLEJ-NKJP": KLEJNKJPTask,
"KLEJ-CDS-E": KLEJCDSEntailmentTask,
"KLEJ-CDS-R": KLEJCDSRelatednessTask,
"KLEJ-CBD": KLEJCBDTask,
"KLEJ-POLEMO-IN": KLEJPolEmoINTask,
"KLEJ-POLEMO-OUT": KLEJPolEmoOUTTask,
"KLEJ-DYK": KLEJDYKTask,
"KLEJ-PSC": KLEJPSCTask,
"KLEJ-ECR": KLEJECRRegressionTask,
"PPC": PPCTask,
# English tasks
"GLUE-COLA": GLUECoLATask,
"GLUE-MNLI-MA": GLUEMNLIMatchedTask,
"GLUE-MNLI-MI": GLUEMNLIMismatchedTask,
"GLUE-QQP": GLUEQQPTask,
"GLUE-QNLI": GLUEQNLITask,
"GLUE-MRPC": GLUEMRPCTask,
"GLUE-RTE": GLUERTETask,
"GLUE-STS-B": GLUESTSBTask,
"GLUE-SST-2": GLUESST2Task,
"GLUE-AX": GLUEDiagnosticsTask
}