Commit
•
83ecd0d
1
Parent(s):
079130b
Delete loading script
Browse files
xnli.py
DELETED
@@ -1,211 +0,0 @@
|
|
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 |
-
"""XNLI: The Cross-Lingual NLI Corpus."""
|
18 |
-
|
19 |
-
|
20 |
-
import collections
|
21 |
-
import csv
|
22 |
-
import os
|
23 |
-
from contextlib import ExitStack
|
24 |
-
|
25 |
-
import datasets
|
26 |
-
|
27 |
-
|
28 |
-
_CITATION = """\
|
29 |
-
@InProceedings{conneau2018xnli,
|
30 |
-
author = {Conneau, Alexis
|
31 |
-
and Rinott, Ruty
|
32 |
-
and Lample, Guillaume
|
33 |
-
and Williams, Adina
|
34 |
-
and Bowman, Samuel R.
|
35 |
-
and Schwenk, Holger
|
36 |
-
and Stoyanov, Veselin},
|
37 |
-
title = {XNLI: Evaluating Cross-lingual Sentence Representations},
|
38 |
-
booktitle = {Proceedings of the 2018 Conference on Empirical Methods
|
39 |
-
in Natural Language Processing},
|
40 |
-
year = {2018},
|
41 |
-
publisher = {Association for Computational Linguistics},
|
42 |
-
location = {Brussels, Belgium},
|
43 |
-
}"""
|
44 |
-
|
45 |
-
_DESCRIPTION = """\
|
46 |
-
XNLI is a subset of a few thousand examples from MNLI which has been translated
|
47 |
-
into a 14 different languages (some low-ish resource). As with MNLI, the goal is
|
48 |
-
to predict textual entailment (does sentence A imply/contradict/neither sentence
|
49 |
-
B) and is a classification task (given two sentences, predict one of three
|
50 |
-
labels).
|
51 |
-
"""
|
52 |
-
|
53 |
-
_TRAIN_DATA_URL = "https://dl.fbaipublicfiles.com/XNLI/XNLI-MT-1.0.zip"
|
54 |
-
_TESTVAL_DATA_URL = "https://dl.fbaipublicfiles.com/XNLI/XNLI-1.0.zip"
|
55 |
-
|
56 |
-
_LANGUAGES = ("ar", "bg", "de", "el", "en", "es", "fr", "hi", "ru", "sw", "th", "tr", "ur", "vi", "zh")
|
57 |
-
|
58 |
-
|
59 |
-
class XnliConfig(datasets.BuilderConfig):
|
60 |
-
"""BuilderConfig for XNLI."""
|
61 |
-
|
62 |
-
def __init__(self, language: str, languages=None, **kwargs):
|
63 |
-
"""BuilderConfig for XNLI.
|
64 |
-
|
65 |
-
Args:
|
66 |
-
language: One of ar,bg,de,el,en,es,fr,hi,ru,sw,th,tr,ur,vi,zh, or all_languages
|
67 |
-
**kwargs: keyword arguments forwarded to super.
|
68 |
-
"""
|
69 |
-
super(XnliConfig, self).__init__(**kwargs)
|
70 |
-
self.language = language
|
71 |
-
if language != "all_languages":
|
72 |
-
self.languages = [language]
|
73 |
-
else:
|
74 |
-
self.languages = languages if languages is not None else _LANGUAGES
|
75 |
-
|
76 |
-
|
77 |
-
class Xnli(datasets.GeneratorBasedBuilder):
|
78 |
-
"""XNLI: The Cross-Lingual NLI Corpus. Version 1.0."""
|
79 |
-
|
80 |
-
VERSION = datasets.Version("1.1.0", "")
|
81 |
-
BUILDER_CONFIG_CLASS = XnliConfig
|
82 |
-
BUILDER_CONFIGS = [
|
83 |
-
XnliConfig(
|
84 |
-
name=lang,
|
85 |
-
language=lang,
|
86 |
-
version=datasets.Version("1.1.0", ""),
|
87 |
-
description=f"Plain text import of XNLI for the {lang} language",
|
88 |
-
)
|
89 |
-
for lang in _LANGUAGES
|
90 |
-
] + [
|
91 |
-
XnliConfig(
|
92 |
-
name="all_languages",
|
93 |
-
language="all_languages",
|
94 |
-
version=datasets.Version("1.1.0", ""),
|
95 |
-
description="Plain text import of XNLI for all languages",
|
96 |
-
)
|
97 |
-
]
|
98 |
-
|
99 |
-
def _info(self):
|
100 |
-
if self.config.language == "all_languages":
|
101 |
-
features = datasets.Features(
|
102 |
-
{
|
103 |
-
"premise": datasets.Translation(
|
104 |
-
languages=_LANGUAGES,
|
105 |
-
),
|
106 |
-
"hypothesis": datasets.TranslationVariableLanguages(
|
107 |
-
languages=_LANGUAGES,
|
108 |
-
),
|
109 |
-
"label": datasets.ClassLabel(names=["entailment", "neutral", "contradiction"]),
|
110 |
-
}
|
111 |
-
)
|
112 |
-
else:
|
113 |
-
features = datasets.Features(
|
114 |
-
{
|
115 |
-
"premise": datasets.Value("string"),
|
116 |
-
"hypothesis": datasets.Value("string"),
|
117 |
-
"label": datasets.ClassLabel(names=["entailment", "neutral", "contradiction"]),
|
118 |
-
}
|
119 |
-
)
|
120 |
-
return datasets.DatasetInfo(
|
121 |
-
description=_DESCRIPTION,
|
122 |
-
features=features,
|
123 |
-
# No default supervised_keys (as we have to pass both premise
|
124 |
-
# and hypothesis as input).
|
125 |
-
supervised_keys=None,
|
126 |
-
homepage="https://www.nyu.edu/projects/bowman/xnli/",
|
127 |
-
citation=_CITATION,
|
128 |
-
)
|
129 |
-
|
130 |
-
def _split_generators(self, dl_manager):
|
131 |
-
dl_dirs = dl_manager.download_and_extract(
|
132 |
-
{
|
133 |
-
"train_data": _TRAIN_DATA_URL,
|
134 |
-
"testval_data": _TESTVAL_DATA_URL,
|
135 |
-
}
|
136 |
-
)
|
137 |
-
train_dir = os.path.join(dl_dirs["train_data"], "XNLI-MT-1.0", "multinli")
|
138 |
-
testval_dir = os.path.join(dl_dirs["testval_data"], "XNLI-1.0")
|
139 |
-
return [
|
140 |
-
datasets.SplitGenerator(
|
141 |
-
name=datasets.Split.TRAIN,
|
142 |
-
gen_kwargs={
|
143 |
-
"filepaths": [
|
144 |
-
os.path.join(train_dir, f"multinli.train.{lang}.tsv") for lang in self.config.languages
|
145 |
-
],
|
146 |
-
"data_format": "XNLI-MT",
|
147 |
-
},
|
148 |
-
),
|
149 |
-
datasets.SplitGenerator(
|
150 |
-
name=datasets.Split.TEST,
|
151 |
-
gen_kwargs={"filepaths": [os.path.join(testval_dir, "xnli.test.tsv")], "data_format": "XNLI"},
|
152 |
-
),
|
153 |
-
datasets.SplitGenerator(
|
154 |
-
name=datasets.Split.VALIDATION,
|
155 |
-
gen_kwargs={"filepaths": [os.path.join(testval_dir, "xnli.dev.tsv")], "data_format": "XNLI"},
|
156 |
-
),
|
157 |
-
]
|
158 |
-
|
159 |
-
def _generate_examples(self, data_format, filepaths):
|
160 |
-
"""This function returns the examples in the raw (text) form."""
|
161 |
-
|
162 |
-
if self.config.language == "all_languages":
|
163 |
-
if data_format == "XNLI-MT":
|
164 |
-
with ExitStack() as stack:
|
165 |
-
files = [stack.enter_context(open(filepath, encoding="utf-8")) for filepath in filepaths]
|
166 |
-
readers = [csv.DictReader(file, delimiter="\t", quoting=csv.QUOTE_NONE) for file in files]
|
167 |
-
for row_idx, rows in enumerate(zip(*readers)):
|
168 |
-
yield row_idx, {
|
169 |
-
"premise": {lang: row["premise"] for lang, row in zip(self.config.languages, rows)},
|
170 |
-
"hypothesis": {lang: row["hypo"] for lang, row in zip(self.config.languages, rows)},
|
171 |
-
"label": rows[0]["label"].replace("contradictory", "contradiction"),
|
172 |
-
}
|
173 |
-
else:
|
174 |
-
rows_per_pair_id = collections.defaultdict(list)
|
175 |
-
for filepath in filepaths:
|
176 |
-
with open(filepath, encoding="utf-8") as f:
|
177 |
-
reader = csv.DictReader(f, delimiter="\t", quoting=csv.QUOTE_NONE)
|
178 |
-
for row in reader:
|
179 |
-
rows_per_pair_id[row["pairID"]].append(row)
|
180 |
-
|
181 |
-
for rows in rows_per_pair_id.values():
|
182 |
-
premise = {row["language"]: row["sentence1"] for row in rows}
|
183 |
-
hypothesis = {row["language"]: row["sentence2"] for row in rows}
|
184 |
-
yield rows[0]["pairID"], {
|
185 |
-
"premise": premise,
|
186 |
-
"hypothesis": hypothesis,
|
187 |
-
"label": rows[0]["gold_label"],
|
188 |
-
}
|
189 |
-
else:
|
190 |
-
if data_format == "XNLI-MT":
|
191 |
-
for file_idx, filepath in enumerate(filepaths):
|
192 |
-
file = open(filepath, encoding="utf-8")
|
193 |
-
reader = csv.DictReader(file, delimiter="\t", quoting=csv.QUOTE_NONE)
|
194 |
-
for row_idx, row in enumerate(reader):
|
195 |
-
key = str(file_idx) + "_" + str(row_idx)
|
196 |
-
yield key, {
|
197 |
-
"premise": row["premise"],
|
198 |
-
"hypothesis": row["hypo"],
|
199 |
-
"label": row["label"].replace("contradictory", "contradiction"),
|
200 |
-
}
|
201 |
-
else:
|
202 |
-
for filepath in filepaths:
|
203 |
-
with open(filepath, encoding="utf-8") as f:
|
204 |
-
reader = csv.DictReader(f, delimiter="\t", quoting=csv.QUOTE_NONE)
|
205 |
-
for row in reader:
|
206 |
-
if row["language"] == self.config.language:
|
207 |
-
yield row["pairID"], {
|
208 |
-
"premise": row["sentence1"],
|
209 |
-
"hypothesis": row["sentence2"],
|
210 |
-
"label": row["gold_label"],
|
211 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|