holylovenia commited on
Commit
d07490a
1 Parent(s): 7130429

Upload snli_indo.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. snli_indo.py +158 -0
snli_indo.py ADDED
@@ -0,0 +1,158 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ SNLI Indo is derived from the SNLI corpus, where the premise and hypothesis sentences are translated directly from English to Indonesian using the Google Cloud Translation API. The SNLI corpus is divided into three sets, namely train, development, and test set. The translation process is applied to all the premise and hypothesis sentences in all the three sets. This ensures that the number of sentence pairs obtained is the same as the original SNLI dataset, namely 570k sentence pairs. A filtering process is carried out to remove incomplete sentence pairs and those with a gold label `-`. As a result, 569,027 sentence pairs are obtained.
3
+ """
4
+
5
+ from pathlib import Path
6
+ from typing import Dict, List, Tuple
7
+
8
+ import datasets
9
+ import jsonlines
10
+
11
+ from seacrowd.utils import schemas
12
+ from seacrowd.utils.configs import SEACrowdConfig
13
+ from seacrowd.utils.constants import Licenses, Tasks
14
+
15
+ _CITATION = """\
16
+ @article{suwija2023snli,
17
+ author = "Suwija Putra, I Made
18
+ and Siahaan, Daniel
19
+ and Saikhu, Ahmad",
20
+ title = "SNLI Indo: A recognizing textual entailment dataset in Indonesian derived from the Stanford Natural Language Inference dataset"
21
+ year = "2024",
22
+ journal = "Data in Brief",
23
+ volume = "52",
24
+ pages = "109998",
25
+ publisher = "Elsevier",
26
+ doi = "https://doi.org/10.1016/j.dib.2023.109998",
27
+ url = "https://www.sciencedirect.com/science/article/pii/S2352340923010284",
28
+ }
29
+ """
30
+
31
+ _DATASETNAME = "snli_indo"
32
+
33
+ _DESCRIPTION = """\
34
+ The SNLI Indo dataset is derived from the SNLI corpus by translating each premise and hypothesis sentence from English to Indonesia via the Google Cloud Translation API. Premise sentences are crawled image captions from Flickr, and hypothesis sentences are manually created through crowdsourcing. Five annotators are assigned per sentence pair to label the inference relationship as entailment (true), contradiction (false) or neutral (undetermined).
35
+ """
36
+
37
+ _HOMEPAGE = "https://data.mendeley.com/datasets/k4tjhzs2gd/1"
38
+
39
+ _LANGUAGES = ["ind"]
40
+
41
+ _LICENSE = Licenses.CC_BY_4_0.value
42
+
43
+ _LOCAL = False
44
+
45
+ _URLS = {
46
+ _DATASETNAME: {
47
+ "train": "https://data.mendeley.com/public-files/datasets/k4tjhzs2gd/files/ee45b2bb-e2ea-47b7-bec4-b6653c467d27/file_downloaded",
48
+ "val": "https://data.mendeley.com/public-files/datasets/k4tjhzs2gd/files/5e47db3c-ea84-4c73-9a2f-bfd57b4e2c05/file_downloaded",
49
+ "test": "https://data.mendeley.com/public-files/datasets/k4tjhzs2gd/files/23aff85c-ff72-48b6-aba1-c1dd5dac216b/file_downloaded",
50
+ }
51
+ }
52
+
53
+ _SUPPORTED_TASKS = [Tasks.TEXTUAL_ENTAILMENT]
54
+
55
+ _SOURCE_VERSION = "1.0.0"
56
+
57
+ _SEACROWD_VERSION = "2024.06.20"
58
+
59
+
60
+ class SNLIDataset(datasets.GeneratorBasedBuilder):
61
+ """SNLI Indo is derived from the SNLI corpus, where the premise and hypothesis sentences are translated directly from English to Indonesian using the Google Cloud Translation API. This dataset contains ~570k annotated sentence pairs."""
62
+
63
+ SOURCE_VERSION = datasets.Version(_SOURCE_VERSION)
64
+ SEACROWD_VERSION = datasets.Version(_SEACROWD_VERSION)
65
+
66
+ BUILDER_CONFIGS = [
67
+ SEACrowdConfig(
68
+ name="snli_indo_source",
69
+ version=SOURCE_VERSION,
70
+ description="SNLI Indo source schema",
71
+ schema="source",
72
+ subset_id="snli_indo",
73
+ ),
74
+ SEACrowdConfig(
75
+ name="snli_indo_seacrowd_pairs",
76
+ version=SEACROWD_VERSION,
77
+ description="SNLI Indo SEACrowd schema",
78
+ schema="seacrowd_pairs",
79
+ subset_id="snli_indo",
80
+ ),
81
+ ]
82
+
83
+ DEFAULT_CONFIG_NAME = "snli_source"
84
+ labels = ["kontradiksi", "keterlibatan", "netral"] # ["contradiction", "entailment", "neutral" ]
85
+
86
+ def _info(self) -> datasets.DatasetInfo:
87
+ if self.config.schema == "source":
88
+ features = datasets.Features(
89
+ {
90
+ "premise": datasets.Value("string"),
91
+ "hypothesis": datasets.Value("string"),
92
+ "label": datasets.ClassLabel(names=self.labels),
93
+ }
94
+ )
95
+
96
+ elif self.config.schema == "seacrowd_pairs":
97
+ features = schemas.pairs_features(self.labels)
98
+
99
+ return datasets.DatasetInfo(
100
+ description=_DESCRIPTION,
101
+ features=features,
102
+ homepage=_HOMEPAGE,
103
+ license=_LICENSE,
104
+ citation=_CITATION,
105
+ )
106
+
107
+ def _split_generators(self, dl_manager: datasets.DownloadManager) -> List[datasets.SplitGenerator]:
108
+ """Returns SplitGenerators."""
109
+
110
+ urls = _URLS[_DATASETNAME]
111
+ data_dir = dl_manager.download_and_extract(urls)
112
+
113
+ return [
114
+ datasets.SplitGenerator(
115
+ name=datasets.Split.TRAIN,
116
+ gen_kwargs={
117
+ "filepath": data_dir["train"],
118
+ },
119
+ ),
120
+ datasets.SplitGenerator(
121
+ name=datasets.Split.TEST,
122
+ gen_kwargs={
123
+ "filepath": data_dir["test"],
124
+ },
125
+ ),
126
+ datasets.SplitGenerator(
127
+ name=datasets.Split.VALIDATION,
128
+ gen_kwargs={
129
+ "filepath": data_dir["val"],
130
+ },
131
+ ),
132
+ ]
133
+
134
+ def _generate_examples(self, filepath: Path) -> Tuple[int, Dict]:
135
+ """Yields examples as (key, example) tuples."""
136
+
137
+ if self.config.schema == "source":
138
+ with jsonlines.open(filepath) as f:
139
+ i = -1
140
+ for example in f.iter():
141
+ i += 1
142
+ yield str(i), {
143
+ "premise": example["kalimat1"],
144
+ "hypothesis": example["kalimat2"],
145
+ "label": example["label emas"],
146
+ }
147
+
148
+ elif self.config.schema == "seacrowd_pairs":
149
+ with jsonlines.open(filepath) as f:
150
+ i = -1
151
+ for example in f.iter():
152
+ i += 1
153
+ yield str(i), {
154
+ "id": str(i),
155
+ "text_1": example["kalimat1"],
156
+ "text_2": example["kalimat2"],
157
+ "label": example["label emas"],
158
+ }