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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# coding=utf-8
|
| 2 |
+
# Copyright 2020 The TensorFlow Datasets Authors and the HuggingFace Datasets Authors.
|
| 3 |
+
#
|
| 4 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 5 |
+
# you may not use this file except in compliance with the License.
|
| 6 |
+
# You may obtain a copy of the License at
|
| 7 |
+
#
|
| 8 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 9 |
+
#
|
| 10 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 11 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 12 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 13 |
+
# See the License for the specific language governing permissions and
|
| 14 |
+
# limitations under the License.
|
| 15 |
+
|
| 16 |
+
# Lint as: python3
|
| 17 |
+
"""Recast datasets"""
|
| 18 |
+
|
| 19 |
+
from __future__ import absolute_import, division, print_function
|
| 20 |
+
|
| 21 |
+
import csv
|
| 22 |
+
import os
|
| 23 |
+
import textwrap
|
| 24 |
+
|
| 25 |
+
import six
|
| 26 |
+
|
| 27 |
+
import datasets
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
_Recast_CITATION = r"""inproceedings{poliak-etal-2018-collecting,
|
| 31 |
+
title = "Collecting Diverse Natural Language Inference Problems for Sentence Representation Evaluation",
|
| 32 |
+
author = "Poliak, Adam and
|
| 33 |
+
Haldar, Aparajita and
|
| 34 |
+
Rudinger, Rachel and
|
| 35 |
+
Hu, J. Edward and
|
| 36 |
+
Pavlick, Ellie and
|
| 37 |
+
White, Aaron Steven and
|
| 38 |
+
Van Durme, Benjamin",
|
| 39 |
+
booktitle = "Proceedings of the 2018 Conference on Empirical Methods in Natural Language Processing",
|
| 40 |
+
month = oct # "-" # nov,
|
| 41 |
+
year = "2018",
|
| 42 |
+
address = "Brussels, Belgium",
|
| 43 |
+
publisher = "Association for Computational Linguistics",
|
| 44 |
+
url = "https://aclanthology.org/D18-1007",
|
| 45 |
+
doi = "10.18653/v1/D18-1007",
|
| 46 |
+
pages = "67--81",
|
| 47 |
+
abstract = "We present a large-scale collection of diverse natural language inference (NLI) datasets that help provide insight into how well a sentence representation captures distinct types of reasoning. The collection results from recasting 13 existing datasets from 7 semantic phenomena into a common NLI structure, resulting in over half a million labeled context-hypothesis pairs in total. We refer to our collection as the DNC: Diverse Natural Language Inference Collection. The DNC is available online at \url{https://www.decomp.net}, and will grow over time as additional resources are recast and added from novel sources.",
|
| 48 |
+
}
|
| 49 |
+
"""
|
| 50 |
+
|
| 51 |
+
_Recast_DESCRIPTION = """\
|
| 52 |
+
A diverse collection of tasks recasted as natural language inference tasks.
|
| 53 |
+
"""
|
| 54 |
+
|
| 55 |
+
DATA_URL = "https://www.dropbox.com/s/z1mcq6ygfsae0wj/recast.zip?dl=1"
|
| 56 |
+
|
| 57 |
+
TASK_TO_LABELS = {
|
| 58 |
+
"recast_kg_relations": ["1", "2", "3", "4", "5", "6"],
|
| 59 |
+
"recast_puns": ["not-entailed", "entailed"],
|
| 60 |
+
"recast_factuality": ["not-entailed", "entailed"],
|
| 61 |
+
"recast_verbnet": ["not-entailed", "entailed"],
|
| 62 |
+
"recast_verbcorner": ["not-entailed", "entailed"],
|
| 63 |
+
"recast_sentiment": ["not-entailed", "entailed"],
|
| 64 |
+
"recast_megaveridicality": ["not-entailed", "entailed"],
|
| 65 |
+
"recast_ner": ["not-entailed", "entailed"],
|
| 66 |
+
"recast_winogender": ["not-entailed", "entailed"],
|
| 67 |
+
"recast_ner": ["not-entailed", "entailed"],
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
def get_labels(task):
|
| 72 |
+
return TASK_TO_LABELS[task]
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
class RecastConfig(datasets.BuilderConfig):
|
| 76 |
+
"""BuilderConfig for Recast."""
|
| 77 |
+
|
| 78 |
+
def __init__(
|
| 79 |
+
self,
|
| 80 |
+
text_features,
|
| 81 |
+
label_classes=None,
|
| 82 |
+
process_label=lambda x: x,
|
| 83 |
+
**kwargs,
|
| 84 |
+
):
|
| 85 |
+
"""BuilderConfig for Recast.
|
| 86 |
+
Args:
|
| 87 |
+
text_features: `dict[string, string]`, map from the name of the feature
|
| 88 |
+
dict for each text field to the name of the column in the tsv file
|
| 89 |
+
label_column: `string`, name of the column in the tsv file corresponding
|
| 90 |
+
to the label
|
| 91 |
+
data_url: `string`, url to download the zip file from
|
| 92 |
+
data_dir: `string`, the path to the folder containing the tsv files in the
|
| 93 |
+
downloaded zip
|
| 94 |
+
citation: `string`, citation for the data set
|
| 95 |
+
url: `string`, url for information about the data set
|
| 96 |
+
label_classes: `list[string]`, the list of classes if the label is
|
| 97 |
+
categorical. If not provided, then the label will be of type
|
| 98 |
+
`datasets.Value('float32')`.
|
| 99 |
+
process_label: `Function[string, any]`, function taking in the raw value
|
| 100 |
+
of the label and processing it to the form required by the label feature
|
| 101 |
+
**kwargs: keyword arguments forwarded to super.
|
| 102 |
+
"""
|
| 103 |
+
|
| 104 |
+
super(RecastConfig, self).__init__(
|
| 105 |
+
version=datasets.Version("1.0.0", ""), **kwargs
|
| 106 |
+
)
|
| 107 |
+
|
| 108 |
+
self.text_features = text_features
|
| 109 |
+
self.label_column = "label"
|
| 110 |
+
self.label_classes = get_labels(self.name)
|
| 111 |
+
self.data_url = DATA_URL
|
| 112 |
+
self.data_dir = os.path.join("recast", self.name)
|
| 113 |
+
self.citation = textwrap.dedent(_Recast_CITATION)
|
| 114 |
+
self.process_label = lambda x: str(x)
|
| 115 |
+
self.description = ""
|
| 116 |
+
self.url = ""
|
| 117 |
+
|
| 118 |
+
|
| 119 |
+
class Recast(datasets.GeneratorBasedBuilder):
|
| 120 |
+
|
| 121 |
+
"""The General Language Understanding Evaluation (Recast) benchmark."""
|
| 122 |
+
|
| 123 |
+
BUILDER_CONFIG_CLASS = RecastConfig
|
| 124 |
+
|
| 125 |
+
BUILDER_CONFIGS = [
|
| 126 |
+
RecastConfig(
|
| 127 |
+
name="recast_kg_relations",
|
| 128 |
+
text_features={"context": "context", "hypothesis": "hypothesis"},
|
| 129 |
+
),
|
| 130 |
+
RecastConfig(
|
| 131 |
+
name="recast_puns",
|
| 132 |
+
text_features={"context": "context", "hypothesis": "hypothesis"},
|
| 133 |
+
),
|
| 134 |
+
RecastConfig(
|
| 135 |
+
name="recast_factuality",
|
| 136 |
+
text_features={"context": "context", "hypothesis": "hypothesis"},
|
| 137 |
+
),
|
| 138 |
+
RecastConfig(
|
| 139 |
+
name="recast_verbnet",
|
| 140 |
+
text_features={"context": "context", "hypothesis": "hypothesis"},
|
| 141 |
+
),
|
| 142 |
+
RecastConfig(
|
| 143 |
+
name="recast_verbcorner",
|
| 144 |
+
text_features={"context": "context", "hypothesis": "hypothesis"},
|
| 145 |
+
),
|
| 146 |
+
RecastConfig(
|
| 147 |
+
name="recast_ner",
|
| 148 |
+
text_features={"context": "context", "hypothesis": "hypothesis"},
|
| 149 |
+
),
|
| 150 |
+
RecastConfig(
|
| 151 |
+
name="recast_sentiment",
|
| 152 |
+
text_features={"context": "context", "hypothesis": "hypothesis"},
|
| 153 |
+
),
|
| 154 |
+
RecastConfig(
|
| 155 |
+
name="recast_megaveridicality",
|
| 156 |
+
text_features={"context": "context", "hypothesis": "hypothesis"},
|
| 157 |
+
),
|
| 158 |
+
]
|
| 159 |
+
|
| 160 |
+
def _info(self):
|
| 161 |
+
features = {
|
| 162 |
+
text_feature: datasets.Value("string")
|
| 163 |
+
for text_feature in six.iterkeys(self.config.text_features)
|
| 164 |
+
}
|
| 165 |
+
if self.config.label_classes:
|
| 166 |
+
features["label"] = datasets.features.ClassLabel(
|
| 167 |
+
names=self.config.label_classes
|
| 168 |
+
)
|
| 169 |
+
else:
|
| 170 |
+
features["label"] = datasets.Value("float32")
|
| 171 |
+
features["idx"] = datasets.Value("int32")
|
| 172 |
+
return datasets.DatasetInfo(
|
| 173 |
+
description=_Recast_DESCRIPTION,
|
| 174 |
+
features=datasets.Features(features),
|
| 175 |
+
homepage=self.config.url,
|
| 176 |
+
citation=self.config.citation + "\n" + _Recast_CITATION,
|
| 177 |
+
)
|
| 178 |
+
|
| 179 |
+
def _split_generators(self, dl_manager):
|
| 180 |
+
dl_dir = dl_manager.download_and_extract(self.config.data_url)
|
| 181 |
+
data_dir = os.path.join(dl_dir, self.config.data_dir)
|
| 182 |
+
|
| 183 |
+
return [
|
| 184 |
+
datasets.SplitGenerator(
|
| 185 |
+
name=datasets.Split.TRAIN,
|
| 186 |
+
gen_kwargs={
|
| 187 |
+
"data_file": os.path.join(data_dir or "", "train.tsv"),
|
| 188 |
+
"split": "train",
|
| 189 |
+
},
|
| 190 |
+
),
|
| 191 |
+
datasets.SplitGenerator(
|
| 192 |
+
name=datasets.Split.VALIDATION,
|
| 193 |
+
gen_kwargs={
|
| 194 |
+
"data_file": os.path.join(data_dir or "", "dev.tsv"),
|
| 195 |
+
"split": "dev",
|
| 196 |
+
},
|
| 197 |
+
),
|
| 198 |
+
datasets.SplitGenerator(
|
| 199 |
+
name=datasets.Split.TEST,
|
| 200 |
+
gen_kwargs={
|
| 201 |
+
"data_file": os.path.join(data_dir or "", "test.tsv"),
|
| 202 |
+
"split": "test",
|
| 203 |
+
},
|
| 204 |
+
),
|
| 205 |
+
]
|
| 206 |
+
|
| 207 |
+
def _generate_examples(self, data_file, split):
|
| 208 |
+
|
| 209 |
+
process_label = self.config.process_label
|
| 210 |
+
label_classes = self.config.label_classes
|
| 211 |
+
|
| 212 |
+
with open(data_file, encoding="utf8") as f:
|
| 213 |
+
reader = csv.DictReader(f, delimiter="\t", quoting=csv.QUOTE_NONE)
|
| 214 |
+
|
| 215 |
+
for n, row in enumerate(reader):
|
| 216 |
+
|
| 217 |
+
example = {
|
| 218 |
+
feat: row[col]
|
| 219 |
+
for feat, col in six.iteritems(self.config.text_features)
|
| 220 |
+
}
|
| 221 |
+
example["idx"] = n
|
| 222 |
+
|
| 223 |
+
if self.config.label_column in row:
|
| 224 |
+
label = row[self.config.label_column]
|
| 225 |
+
if label_classes and label not in label_classes:
|
| 226 |
+
label = int(label) if label else None
|
| 227 |
+
example["label"] = process_label(label)
|
| 228 |
+
else:
|
| 229 |
+
example["label"] = process_label(-1)
|
| 230 |
+
yield example["idx"], example
|