Datasets:
Commit
·
8751b1a
1
Parent(s):
3df8208
Create snli-zh.py
Browse files- snli-zh.py +97 -0
snli-zh.py
ADDED
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
"""The Stanford Natural Language Inference (SNLI) Corpus.
|
18 |
+
|
19 |
+
translate to chinese:https://github.com/liuhuanyong
|
20 |
+
upload: https://github.com/shibing624
|
21 |
+
"""
|
22 |
+
|
23 |
+
|
24 |
+
import csv
|
25 |
+
import os
|
26 |
+
|
27 |
+
import datasets
|
28 |
+
|
29 |
+
|
30 |
+
_CITATION = """\
|
31 |
+
@inproceedings{snli:emnlp2015,
|
32 |
+
Author = {Bowman, Samuel R. and Angeli, Gabor and Potts, Christopher, and Manning, Christopher D.},
|
33 |
+
Booktitle = {Proceedings of the 2015 Conference on Empirical Methods in Natural Language Processing (EMNLP)},
|
34 |
+
Publisher = {Association for Computational Linguistics},
|
35 |
+
Title = {A large annotated corpus for learning natural language inference},
|
36 |
+
Year = {2015}
|
37 |
+
}
|
38 |
+
"""
|
39 |
+
|
40 |
+
_DESCRIPTION = """\
|
41 |
+
The SNLI corpus (version 1.0) is a collection of 570k human-written English
|
42 |
+
sentence pairs manually labeled for balanced classification with the labels
|
43 |
+
entailment, contradiction, and neutral, supporting the task of natural language
|
44 |
+
inference (NLI), also known as recognizing textual entailment (RTE).
|
45 |
+
"""
|
46 |
+
|
47 |
+
_DATA_URL = "https://huggingface.co/datasets/shibing624/snli-zh/resolve/main/ChineseTextualInference-train.txt"
|
48 |
+
|
49 |
+
|
50 |
+
class Snli(datasets.GeneratorBasedBuilder):
|
51 |
+
"""The Stanford Natural Language Inference (SNLI-zh) Corpus."""
|
52 |
+
|
53 |
+
BUILDER_CONFIGS = [
|
54 |
+
datasets.BuilderConfig(
|
55 |
+
name="plain_text",
|
56 |
+
version=datasets.Version("1.0.0", ""),
|
57 |
+
description="Plain text import of SNLI-zh",
|
58 |
+
)
|
59 |
+
]
|
60 |
+
|
61 |
+
def _info(self):
|
62 |
+
return datasets.DatasetInfo(
|
63 |
+
description=_DESCRIPTION,
|
64 |
+
features=datasets.Features(
|
65 |
+
{
|
66 |
+
"premise": datasets.Value("string"),
|
67 |
+
"hypothesis": datasets.Value("string"),
|
68 |
+
"label": datasets.features.ClassLabel(names=["entailment", "neutral", "contradiction"]),
|
69 |
+
}
|
70 |
+
),
|
71 |
+
|
72 |
+
supervised_keys=None,
|
73 |
+
homepage="https://nlp.stanford.edu/projects/snli/",
|
74 |
+
citation=_CITATION,
|
75 |
+
)
|
76 |
+
|
77 |
+
def _split_generators(self, dl_manager):
|
78 |
+
dl_file = dl_manager.download_and_extract(_DATA_URL)
|
79 |
+
|
80 |
+
return [
|
81 |
+
|
82 |
+
datasets.SplitGenerator(
|
83 |
+
name=datasets.Split.TRAIN, gen_kwargs={"filepath": dl_file}
|
84 |
+
),
|
85 |
+
]
|
86 |
+
|
87 |
+
def _generate_examples(self, filepath):
|
88 |
+
"""This function returns the examples in the raw (text) form."""
|
89 |
+
with open(filepath, encoding="utf-8") as f:
|
90 |
+
reader = csv.DictReader(f, delimiter="\t", quoting=csv.QUOTE_NONE)
|
91 |
+
for idx, row in enumerate(reader):
|
92 |
+
label = -1 if row["gold_label"] == "-" else row["gold_label"]
|
93 |
+
yield idx, {
|
94 |
+
"premise": row["sentence1"],
|
95 |
+
"hypothesis": row["sentence2"],
|
96 |
+
"label": label,
|
97 |
+
}
|