|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"""The Stanford Natural Language Inference (SNLI) Corpus.""" |
|
|
|
|
|
import csv |
|
import os |
|
|
|
import datasets |
|
|
|
|
|
_CITATION = """\ |
|
@inproceedings{bowman-etal-2015-large, |
|
title = "A large annotated corpus for learning natural language inference", |
|
author = "Bowman, Samuel R. and |
|
Angeli, Gabor and |
|
Potts, Christopher and |
|
Manning, Christopher D.", |
|
editor = "M{\\`a}rquez, Llu{\\'\\i}s and |
|
Callison-Burch, Chris and |
|
Su, Jian", |
|
booktitle = "Proceedings of the 2015 Conference on Empirical Methods in Natural Language Processing", |
|
month = sep, |
|
year = "2015", |
|
address = "Lisbon, Portugal", |
|
publisher = "Association for Computational Linguistics", |
|
url = "https://aclanthology.org/D15-1075", |
|
doi = "10.18653/v1/D15-1075", |
|
pages = "632--642", |
|
} |
|
""" |
|
|
|
_DESCRIPTION = """\ |
|
The SNLI corpus (version 1.0) is a collection of 570k human-written English |
|
sentence pairs manually labeled for balanced classification with the labels |
|
entailment, contradiction, and neutral, supporting the task of natural language |
|
inference (NLI), also known as recognizing textual entailment (RTE). |
|
""" |
|
|
|
_DATA_URL = "https://nlp.stanford.edu/projects/snli/snli_1.0.zip" |
|
|
|
|
|
class Snli(datasets.GeneratorBasedBuilder): |
|
"""The Stanford Natural Language Inference (SNLI) Corpus.""" |
|
|
|
BUILDER_CONFIGS = [ |
|
datasets.BuilderConfig( |
|
name="plain_text", |
|
version=datasets.Version("1.0.0", ""), |
|
description="Plain text import of SNLI", |
|
) |
|
] |
|
|
|
def _info(self): |
|
return datasets.DatasetInfo( |
|
description=_DESCRIPTION, |
|
features=datasets.Features( |
|
{ |
|
"premise": datasets.Value("string"), |
|
"hypothesis": datasets.Value("string"), |
|
"label": datasets.features.ClassLabel(names=["entailment", "neutral", "contradiction"]), |
|
} |
|
), |
|
|
|
|
|
supervised_keys=None, |
|
homepage="https://nlp.stanford.edu/projects/snli/", |
|
citation=_CITATION, |
|
) |
|
|
|
def _split_generators(self, dl_manager): |
|
dl_dir = dl_manager.download_and_extract(_DATA_URL) |
|
data_dir = os.path.join(dl_dir, "snli_1.0") |
|
return [ |
|
datasets.SplitGenerator( |
|
name=datasets.Split.TEST, gen_kwargs={"filepath": os.path.join(data_dir, "snli_1.0_test.txt")} |
|
), |
|
datasets.SplitGenerator( |
|
name=datasets.Split.VALIDATION, gen_kwargs={"filepath": os.path.join(data_dir, "snli_1.0_dev.txt")} |
|
), |
|
datasets.SplitGenerator( |
|
name=datasets.Split.TRAIN, gen_kwargs={"filepath": os.path.join(data_dir, "snli_1.0_train.txt")} |
|
), |
|
] |
|
|
|
def _generate_examples(self, filepath): |
|
"""This function returns the examples in the raw (text) form.""" |
|
with open(filepath, encoding="utf-8") as f: |
|
reader = csv.DictReader(f, delimiter="\t", quoting=csv.QUOTE_NONE) |
|
for idx, row in enumerate(reader): |
|
label = -1 if row["gold_label"] == "-" else row["gold_label"] |
|
yield idx, { |
|
"premise": row["sentence1"], |
|
"hypothesis": row["sentence2"], |
|
"label": label, |
|
} |
|
|