snli-zh / snli-zh.py
shibing624's picture
Update snli-zh.py
9e4b871
# coding=utf-8
# Copyright 2020 The TensorFlow Datasets Authors and the HuggingFace Datasets Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Lint as: python3
"""The Stanford Natural Language Inference (SNLI) Corpus.
translate to chinese:https://github.com/liuhuanyong
upload: https://github.com/shibing624
"""
import csv
import os
import datasets
_CITATION = """\
@inproceedings{snli:emnlp2015,
Author = {Bowman, Samuel R. and Angeli, Gabor and Potts, Christopher, and Manning, Christopher D.},
Booktitle = {Proceedings of the 2015 Conference on Empirical Methods in Natural Language Processing (EMNLP)},
Publisher = {Association for Computational Linguistics},
Title = {A large annotated corpus for learning natural language inference},
Year = {2015}
}
"""
_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://huggingface.co/datasets/shibing624/snli-zh/resolve/main/ChineseTextualInference-train.txt"
class Snli(datasets.GeneratorBasedBuilder):
"""The Stanford Natural Language Inference (SNLI-zh) Corpus."""
BUILDER_CONFIGS = [
datasets.BuilderConfig(
name="plain_text",
version=datasets.Version("1.0.0", ""),
description="Plain text import of SNLI-zh",
)
]
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_file = dl_manager.download_and_extract(_DATA_URL)
return [
datasets.SplitGenerator(
name=datasets.Split.TRAIN, gen_kwargs={"filepath": dl_file}
),
]
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)
c = 0
for idx, row in enumerate(reader):
premise = row['sentence1']
hypothesis = row['sentence2']
label = row["gold_label"]
if premise and len(premise) > 1 and hypothesis and len(hypothesis) > 1 and label in ["entailment", "neutral", "contradiction"]:
yield c, {
"premise": premise,
"hypothesis": hypothesis,
"label": label,
}
c += 1