Divyanshu commited on
Commit
6d1459e
·
1 Parent(s): 56a6b2d

added loading script

Browse files
This view is limited to 50 files because it contains too many changes.   See raw diff
Files changed (50) hide show
  1. .history/indicxnli_20220823214315.py +203 -0
  2. .history/indicxnli_20220823214334.py +200 -0
  3. .history/indicxnli_20220823220704.py +201 -0
  4. .history/indicxnli_20220823220708.py +201 -0
  5. .history/indicxnli_20220823220724.py +201 -0
  6. .history/indicxnli_20220823220725.py +201 -0
  7. .history/indicxnli_20220823220728.py +201 -0
  8. .history/indicxnli_20220823220732.py +201 -0
  9. .history/indicxnli_20220823220735.py +201 -0
  10. .history/indicxnli_20220823220738.py +201 -0
  11. .history/indicxnli_20220823220742.py +201 -0
  12. .history/indicxnli_20220823221124.py +201 -0
  13. .history/indicxnli_20220823221128.py +202 -0
  14. .history/indicxnli_20220823221142.py +202 -0
  15. .history/indicxnli_20220823221147.py +202 -0
  16. .history/indicxnli_20220823221200.py +203 -0
  17. .history/indicxnli_20220823221210.py +203 -0
  18. .history/indicxnli_20220823221213.py +203 -0
  19. .history/indicxnli_20220823221223.py +203 -0
  20. .history/indicxnli_20220823221227.py +203 -0
  21. .history/indicxnli_20220823221233.py +203 -0
  22. .history/indicxnli_20220823221235.py +203 -0
  23. .history/indicxnli_20220823221240.py +203 -0
  24. .history/indicxnli_20220823221242.py +203 -0
  25. .history/indicxnli_20220823221316.py +203 -0
  26. .history/indicxnli_20220823221318.py +203 -0
  27. .history/indicxnli_20220823221321.py +203 -0
  28. .history/indicxnli_20220823221324.py +203 -0
  29. .history/indicxnli_20220823221328.py +203 -0
  30. .history/indicxnli_20220823221331.py +203 -0
  31. .history/indicxnli_20220823221333.py +203 -0
  32. .history/indicxnli_20220823221334.py +203 -0
  33. .history/indicxnli_20220823221336.py +203 -0
  34. .history/indicxnli_20220823221338.py +203 -0
  35. .history/indicxnli_20220823221339.py +203 -0
  36. .history/indicxnli_20220823221341.py +203 -0
  37. .history/indicxnli_20220823221351.py +203 -0
  38. .history/indicxnli_20220823221357.py +203 -0
  39. .history/indicxnli_20220823221400.py +203 -0
  40. .history/indicxnli_20220823221408.py +203 -0
  41. .history/indicxnli_20220823221440.py +203 -0
  42. .history/indicxnli_20220823221501.py +203 -0
  43. .history/indicxnli_20220823221505.py +203 -0
  44. .history/indicxnli_20220823221601.py +203 -0
  45. .history/indicxnli_20220823221611.py +203 -0
  46. .history/indicxnli_20220823221621.py +203 -0
  47. .history/indicxnli_20220823221623.py +203 -0
  48. .history/indicxnli_20220823221950.py +151 -0
  49. .history/indicxnli_20220823221952.py +151 -0
  50. .history/indicxnli_20220823221954.py +151 -0
.history/indicxnli_20220823214315.py ADDED
@@ -0,0 +1,203 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ @misc{https://doi.org/10.48550/arxiv.2204.08776,
30
+ doi = {10.48550/ARXIV.2204.08776},
31
+
32
+ url = {https://arxiv.org/abs/2204.08776},
33
+
34
+ author = {Aggarwal, Divyanshu and Gupta, Vivek and Kunchukuttan, Anoop},
35
+
36
+ keywords = {Computation and Language (cs.CL), Artificial Intelligence (cs.AI), FOS: Computer and information sciences, FOS: Computer and information sciences},
37
+
38
+ title = {IndicXNLI: Evaluating Multilingual Inference for Indian Languages},
39
+
40
+ publisher = {arXiv},
41
+
42
+ year = {2022},
43
+
44
+ copyright = {Creative Commons Attribution 4.0 International}
45
+ }
46
+ }"""
47
+
48
+ _DESCRIPTION = """\
49
+ IndicXNLI is a translated version of XNLI to 11 Indic Languages. As with XNLI, the goal is
50
+ to predict textual entailment (does sentence A imply/contradict/neither sentence
51
+ B) and is a classification task (given two sentences, predict one of three
52
+ labels).
53
+ """
54
+
55
+ # _TRAIN_DATA_URL = "https://dl.fbaipublicfiles.com/XNLI/XNLI-MT-1.0.zip"
56
+ # _TESTVAL_DATA_URL = "https://dl.fbaipublicfiles.com/XNLI/XNLI-1.0.zip"
57
+
58
+ _LANGUAGES = (
59
+ 'hi',
60
+ 'bn',
61
+ 'mr',
62
+ 'as',
63
+ 'ta',
64
+ 'te',
65
+ 'or',
66
+ 'ml',
67
+ 'pa',
68
+ 'gu',
69
+ 'kn'
70
+ )
71
+
72
+
73
+ class IndicxnliConfig(datasets.BuilderConfig):
74
+ """BuilderConfig for XNLI."""
75
+
76
+ def __init__(self, language: str, **kwargs):
77
+ """BuilderConfig for XNLI.
78
+
79
+ Args:
80
+ language: One of hi, bn, mr, as, ta, te, or, ml, pa, gu, kn
81
+ **kwargs: keyword arguments forwarded to super.
82
+ """
83
+ super(IndicxnliConfig, self).__init__(**kwargs)
84
+ self.language = language
85
+
86
+
87
+ class Indicxnli(datasets.GeneratorBasedBuilder):
88
+ """XNLI: The Cross-Lingual NLI Corpus. Version 1.0."""
89
+
90
+ VERSION = datasets.Version("1.1.0", "")
91
+ BUILDER_CONFIG_CLASS = IndicxnliConfig
92
+ BUILDER_CONFIGS = [
93
+ IndicxnliConfig(
94
+ name=lang,
95
+ language=lang,
96
+ version=datasets.Version("1.1.0", ""),
97
+ description=f"Plain text import of IndicXNLI for the {lang} language",
98
+ )
99
+ for lang in _LANGUAGES
100
+ ]
101
+
102
+ def _info(self):
103
+ features = datasets.Features(
104
+ {
105
+ "premise": datasets.Value("string"),
106
+ "hypothesis": datasets.Value("string"),
107
+ "label": datasets.ClassLabel(names=["entailment", "neutral", "contradiction"]),
108
+ }
109
+ )
110
+ return datasets.DatasetInfo(
111
+ description=_DESCRIPTION,
112
+ features=features,
113
+ # No default supervised_keys (as we have to pass both premise
114
+ # and hypothesis as input).
115
+ supervised_keys=None,
116
+ homepage="https://www.nyu.edu/projects/bowman/xnli/",
117
+ citation=_CITATION,
118
+ )
119
+
120
+ def _split_generators(self, dl_manager):
121
+
122
+ return [
123
+ datasets.SplitGenerator(
124
+ name=datasets.Split.TRAIN,
125
+ gen_kwargs={
126
+ "filepaths": [
127
+ os.path.join(train_dir, f"multinli.train.{lang}.tsv") for lang in self.config.languages
128
+ ],
129
+ "data_format": "XNLI-MT",
130
+ },
131
+ ),
132
+ datasets.SplitGenerator(
133
+ name=datasets.Split.TEST,
134
+ gen_kwargs={"filepaths": [os.path.join(
135
+ testval_dir, "xnli.test.tsv")], "data_format": "XNLI"},
136
+ ),
137
+ datasets.SplitGenerator(
138
+ name=datasets.Split.VALIDATION,
139
+ gen_kwargs={"filepaths": [os.path.join(
140
+ testval_dir, "xnli.dev.tsv")], "data_format": "XNLI"},
141
+ ),
142
+ ]
143
+
144
+ def _generate_examples(self, data_format, filepaths):
145
+ """This function returns the examples in the raw (text) form."""
146
+
147
+ if self.config.language == "all_languages":
148
+ if data_format == "XNLI-MT":
149
+ with ExitStack() as stack:
150
+ files = [stack.enter_context(
151
+ open(filepath, encoding="utf-8")) for filepath in filepaths]
152
+ readers = [csv.DictReader(
153
+ file, delimiter="\t", quoting=csv.QUOTE_NONE) for file in files]
154
+ for row_idx, rows in enumerate(zip(*readers)):
155
+ yield row_idx, {
156
+ "premise": {lang: row["premise"] for lang, row in zip(self.config.languages, rows)},
157
+ "hypothesis": {lang: row["hypo"] for lang, row in zip(self.config.languages, rows)},
158
+ "label": rows[0]["label"].replace("contradictory", "contradiction"),
159
+ }
160
+ else:
161
+ rows_per_pair_id = collections.defaultdict(list)
162
+ for filepath in filepaths:
163
+ with open(filepath, encoding="utf-8") as f:
164
+ reader = csv.DictReader(
165
+ f, delimiter="\t", quoting=csv.QUOTE_NONE)
166
+ for row in reader:
167
+ rows_per_pair_id[row["pairID"]].append(row)
168
+
169
+ for rows in rows_per_pair_id.values():
170
+ premise = {row["language"]: row["sentence1"]
171
+ for row in rows}
172
+ hypothesis = {row["language"]: row["sentence2"]
173
+ for row in rows}
174
+ yield rows[0]["pairID"], {
175
+ "premise": premise,
176
+ "hypothesis": hypothesis,
177
+ "label": rows[0]["gold_label"],
178
+ }
179
+ else:
180
+ if data_format == "XNLI-MT":
181
+ for file_idx, filepath in enumerate(filepaths):
182
+ file = open(filepath, encoding="utf-8")
183
+ reader = csv.DictReader(
184
+ file, delimiter="\t", quoting=csv.QUOTE_NONE)
185
+ for row_idx, row in enumerate(reader):
186
+ key = str(file_idx) + "_" + str(row_idx)
187
+ yield key, {
188
+ "premise": row["premise"],
189
+ "hypothesis": row["hypo"],
190
+ "label": row["label"].replace("contradictory", "contradiction"),
191
+ }
192
+ else:
193
+ for filepath in filepaths:
194
+ with open(filepath, encoding="utf-8") as f:
195
+ reader = csv.DictReader(
196
+ f, delimiter="\t", quoting=csv.QUOTE_NONE)
197
+ for row in reader:
198
+ if row["language"] == self.config.language:
199
+ yield row["pairID"], {
200
+ "premise": row["sentence1"],
201
+ "hypothesis": row["sentence2"],
202
+ "label": row["gold_label"],
203
+ }
.history/indicxnli_20220823214334.py ADDED
@@ -0,0 +1,200 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ @misc{https://doi.org/10.48550/arxiv.2204.08776,
30
+ doi = {10.48550/ARXIV.2204.08776},
31
+
32
+ url = {https://arxiv.org/abs/2204.08776},
33
+
34
+ author = {Aggarwal, Divyanshu and Gupta, Vivek and Kunchukuttan, Anoop},
35
+
36
+ keywords = {Computation and Language (cs.CL), Artificial Intelligence (cs.AI), FOS: Computer and information sciences, FOS: Computer and information sciences},
37
+
38
+ title = {IndicXNLI: Evaluating Multilingual Inference for Indian Languages},
39
+
40
+ publisher = {arXiv},
41
+
42
+ year = {2022},
43
+
44
+ copyright = {Creative Commons Attribution 4.0 International}
45
+ }
46
+ }"""
47
+
48
+ _DESCRIPTION = """\
49
+ IndicXNLI is a translated version of XNLI to 11 Indic Languages. As with XNLI, the goal is
50
+ to predict textual entailment (does sentence A imply/contradict/neither sentence
51
+ B) and is a classification task (given two sentences, predict one of three
52
+ labels).
53
+ """
54
+
55
+ _LANGUAGES = (
56
+ 'hi',
57
+ 'bn',
58
+ 'mr',
59
+ 'as',
60
+ 'ta',
61
+ 'te',
62
+ 'or',
63
+ 'ml',
64
+ 'pa',
65
+ 'gu',
66
+ 'kn'
67
+ )
68
+
69
+
70
+ class IndicxnliConfig(datasets.BuilderConfig):
71
+ """BuilderConfig for XNLI."""
72
+
73
+ def __init__(self, language: str, **kwargs):
74
+ """BuilderConfig for XNLI.
75
+
76
+ Args:
77
+ language: One of hi, bn, mr, as, ta, te, or, ml, pa, gu, kn
78
+ **kwargs: keyword arguments forwarded to super.
79
+ """
80
+ super(IndicxnliConfig, self).__init__(**kwargs)
81
+ self.language = language
82
+
83
+
84
+ class Indicxnli(datasets.GeneratorBasedBuilder):
85
+ """XNLI: The Cross-Lingual NLI Corpus. Version 1.0."""
86
+
87
+ VERSION = datasets.Version("1.1.0", "")
88
+ BUILDER_CONFIG_CLASS = IndicxnliConfig
89
+ BUILDER_CONFIGS = [
90
+ IndicxnliConfig(
91
+ name=lang,
92
+ language=lang,
93
+ version=datasets.Version("1.1.0", ""),
94
+ description=f"Plain text import of IndicXNLI for the {lang} language",
95
+ )
96
+ for lang in _LANGUAGES
97
+ ]
98
+
99
+ def _info(self):
100
+ features = datasets.Features(
101
+ {
102
+ "premise": datasets.Value("string"),
103
+ "hypothesis": datasets.Value("string"),
104
+ "label": datasets.ClassLabel(names=["entailment", "neutral", "contradiction"]),
105
+ }
106
+ )
107
+ return datasets.DatasetInfo(
108
+ description=_DESCRIPTION,
109
+ features=features,
110
+ # No default supervised_keys (as we have to pass both premise
111
+ # and hypothesis as input).
112
+ supervised_keys=None,
113
+ homepage="https://www.nyu.edu/projects/bowman/xnli/",
114
+ citation=_CITATION,
115
+ )
116
+
117
+ def _split_generators(self, dl_manager):
118
+
119
+ return [
120
+ datasets.SplitGenerator(
121
+ name=datasets.Split.TRAIN,
122
+ gen_kwargs={
123
+ "filepaths": [
124
+ os.path.join(train_dir, f"multinli.train.{lang}.tsv") for lang in self.config.languages
125
+ ],
126
+ "data_format": "XNLI-MT",
127
+ },
128
+ ),
129
+ datasets.SplitGenerator(
130
+ name=datasets.Split.TEST,
131
+ gen_kwargs={"filepaths": [os.path.join(
132
+ testval_dir, "xnli.test.tsv")], "data_format": "XNLI"},
133
+ ),
134
+ datasets.SplitGenerator(
135
+ name=datasets.Split.VALIDATION,
136
+ gen_kwargs={"filepaths": [os.path.join(
137
+ testval_dir, "xnli.dev.tsv")], "data_format": "XNLI"},
138
+ ),
139
+ ]
140
+
141
+ def _generate_examples(self, data_format, filepaths):
142
+ """This function returns the examples in the raw (text) form."""
143
+
144
+ if self.config.language == "all_languages":
145
+ if data_format == "XNLI-MT":
146
+ with ExitStack() as stack:
147
+ files = [stack.enter_context(
148
+ open(filepath, encoding="utf-8")) for filepath in filepaths]
149
+ readers = [csv.DictReader(
150
+ file, delimiter="\t", quoting=csv.QUOTE_NONE) for file in files]
151
+ for row_idx, rows in enumerate(zip(*readers)):
152
+ yield row_idx, {
153
+ "premise": {lang: row["premise"] for lang, row in zip(self.config.languages, rows)},
154
+ "hypothesis": {lang: row["hypo"] for lang, row in zip(self.config.languages, rows)},
155
+ "label": rows[0]["label"].replace("contradictory", "contradiction"),
156
+ }
157
+ else:
158
+ rows_per_pair_id = collections.defaultdict(list)
159
+ for filepath in filepaths:
160
+ with open(filepath, encoding="utf-8") as f:
161
+ reader = csv.DictReader(
162
+ f, delimiter="\t", quoting=csv.QUOTE_NONE)
163
+ for row in reader:
164
+ rows_per_pair_id[row["pairID"]].append(row)
165
+
166
+ for rows in rows_per_pair_id.values():
167
+ premise = {row["language"]: row["sentence1"]
168
+ for row in rows}
169
+ hypothesis = {row["language"]: row["sentence2"]
170
+ for row in rows}
171
+ yield rows[0]["pairID"], {
172
+ "premise": premise,
173
+ "hypothesis": hypothesis,
174
+ "label": rows[0]["gold_label"],
175
+ }
176
+ else:
177
+ if data_format == "XNLI-MT":
178
+ for file_idx, filepath in enumerate(filepaths):
179
+ file = open(filepath, encoding="utf-8")
180
+ reader = csv.DictReader(
181
+ file, delimiter="\t", quoting=csv.QUOTE_NONE)
182
+ for row_idx, row in enumerate(reader):
183
+ key = str(file_idx) + "_" + str(row_idx)
184
+ yield key, {
185
+ "premise": row["premise"],
186
+ "hypothesis": row["hypo"],
187
+ "label": row["label"].replace("contradictory", "contradiction"),
188
+ }
189
+ else:
190
+ for filepath in filepaths:
191
+ with open(filepath, encoding="utf-8") as f:
192
+ reader = csv.DictReader(
193
+ f, delimiter="\t", quoting=csv.QUOTE_NONE)
194
+ for row in reader:
195
+ if row["language"] == self.config.language:
196
+ yield row["pairID"], {
197
+ "premise": row["sentence1"],
198
+ "hypothesis": row["sentence2"],
199
+ "label": row["gold_label"],
200
+ }
.history/indicxnli_20220823220704.py ADDED
@@ -0,0 +1,201 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ @misc{https://doi.org/10.48550/arxiv.2204.08776,
30
+ doi = {10.48550/ARXIV.2204.08776},
31
+
32
+ url = {https://arxiv.org/abs/2204.08776},
33
+
34
+ author = {Aggarwal, Divyanshu and Gupta, Vivek and Kunchukuttan, Anoop},
35
+
36
+ keywords = {Computation and Language (cs.CL), Artificial Intelligence (cs.AI), FOS: Computer and information sciences, FOS: Computer and information sciences},
37
+
38
+ title = {IndicXNLI: Evaluating Multilingual Inference for Indian Languages},
39
+
40
+ publisher = {arXiv},
41
+
42
+ year = {2022},
43
+
44
+ copyright = {Creative Commons Attribution 4.0 International}
45
+ }
46
+ }"""
47
+
48
+ _DESCRIPTION = """\
49
+ IndicXNLI is a translated version of XNLI to 11 Indic Languages. As with XNLI, the goal is
50
+ to predict textual entailment (does sentence A imply/contradict/neither sentence
51
+ B) and is a classification task (given two sentences, predict one of three
52
+ labels).
53
+ """
54
+
55
+ _LANGUAGES = (
56
+ 'hi',
57
+ 'bn',
58
+ 'mr',
59
+ 'as',
60
+ 'ta',
61
+ 'te',
62
+ 'or',
63
+ 'ml',
64
+ 'pa',
65
+ 'gu',
66
+ 'kn'
67
+ )
68
+
69
+
70
+ class IndicxnliConfig(datasets.BuilderConfig):
71
+ """BuilderConfig for XNLI."""
72
+
73
+ def __init__(self, language: str, **kwargs):
74
+ """BuilderConfig for XNLI.
75
+
76
+ Args:
77
+ language: One of hi, bn, mr, as, ta, te, or, ml, pa, gu, kn
78
+ **kwargs: keyword arguments forwarded to super.
79
+ """
80
+ super(IndicxnliConfig, self).__init__(**kwargs)
81
+ self.language = language
82
+
83
+
84
+ class Indicxnli(datasets.GeneratorBasedBuilder):
85
+ """XNLI: The Cross-Lingual NLI Corpus. Version 1.0."""
86
+
87
+ VERSION = datasets.Version("1.1.0", "")
88
+ BUILDER_CONFIG_CLASS = IndicxnliConfig
89
+ BUILDER_CONFIGS = [
90
+ IndicxnliConfig(
91
+ name=lang,
92
+ language=lang,
93
+ version=datasets.Version("1.1.0", ""),
94
+ description=f"Plain text import of IndicXNLI for the {lang} language",
95
+ )
96
+ for lang in _LANGUAGES
97
+ ]
98
+
99
+ def _info(self):
100
+ features = datasets.Features(
101
+ {
102
+ "premise": datasets.Value("string"),
103
+ "hypothesis": datasets.Value("string"),
104
+ "label": datasets.ClassLabel(names=["entailment", "neutral", "contradiction"]),
105
+ }
106
+ )
107
+ return datasets.DatasetInfo(
108
+ description=_DESCRIPTION,
109
+ features=features,
110
+ # No default supervised_keys (as we have to pass both premise
111
+ # and hypothesis as input).
112
+ supervised_keys=None,
113
+ homepage="https://www.nyu.edu/projects/bowman/xnli/",
114
+ citation=_CITATION,
115
+ )
116
+
117
+ def _split_generators(self, dl_manager):
118
+
119
+
120
+ return [
121
+ datasets.SplitGenerator(
122
+ name=datasets.Split.TRAIN,
123
+ gen_kwargs={
124
+ "filepaths": [
125
+ os.path.join(train_dir, f"multinli.train.{lang}.tsv") for lang in self.config.languages
126
+ ],
127
+ "data_format": "XNLI-MT",
128
+ },
129
+ ),
130
+ datasets.SplitGenerator(
131
+ name=datasets.Split.TEST,
132
+ gen_kwargs={"filepaths": [os.path.join(
133
+ testval_dir, "xnli.test.tsv")], "data_format": "XNLI"},
134
+ ),
135
+ datasets.SplitGenerator(
136
+ name=datasets.Split.VALIDATION,
137
+ gen_kwargs={"filepaths": [os.path.join(
138
+ testval_dir, "xnli.dev.tsv")], "data_format": "XNLI"},
139
+ ),
140
+ ]
141
+
142
+ def _generate_examples(self, data_format, filepaths):
143
+ """This function returns the examples in the raw (text) form."""
144
+
145
+ if self.config.language == "all_languages":
146
+ if data_format == "XNLI-MT":
147
+ with ExitStack() as stack:
148
+ files = [stack.enter_context(
149
+ open(filepath, encoding="utf-8")) for filepath in filepaths]
150
+ readers = [csv.DictReader(
151
+ file, delimiter="\t", quoting=csv.QUOTE_NONE) for file in files]
152
+ for row_idx, rows in enumerate(zip(*readers)):
153
+ yield row_idx, {
154
+ "premise": {lang: row["premise"] for lang, row in zip(self.config.languages, rows)},
155
+ "hypothesis": {lang: row["hypo"] for lang, row in zip(self.config.languages, rows)},
156
+ "label": rows[0]["label"].replace("contradictory", "contradiction"),
157
+ }
158
+ else:
159
+ rows_per_pair_id = collections.defaultdict(list)
160
+ for filepath in filepaths:
161
+ with open(filepath, encoding="utf-8") as f:
162
+ reader = csv.DictReader(
163
+ f, delimiter="\t", quoting=csv.QUOTE_NONE)
164
+ for row in reader:
165
+ rows_per_pair_id[row["pairID"]].append(row)
166
+
167
+ for rows in rows_per_pair_id.values():
168
+ premise = {row["language"]: row["sentence1"]
169
+ for row in rows}
170
+ hypothesis = {row["language"]: row["sentence2"]
171
+ for row in rows}
172
+ yield rows[0]["pairID"], {
173
+ "premise": premise,
174
+ "hypothesis": hypothesis,
175
+ "label": rows[0]["gold_label"],
176
+ }
177
+ else:
178
+ if data_format == "XNLI-MT":
179
+ for file_idx, filepath in enumerate(filepaths):
180
+ file = open(filepath, encoding="utf-8")
181
+ reader = csv.DictReader(
182
+ file, delimiter="\t", quoting=csv.QUOTE_NONE)
183
+ for row_idx, row in enumerate(reader):
184
+ key = str(file_idx) + "_" + str(row_idx)
185
+ yield key, {
186
+ "premise": row["premise"],
187
+ "hypothesis": row["hypo"],
188
+ "label": row["label"].replace("contradictory", "contradiction"),
189
+ }
190
+ else:
191
+ for filepath in filepaths:
192
+ with open(filepath, encoding="utf-8") as f:
193
+ reader = csv.DictReader(
194
+ f, delimiter="\t", quoting=csv.QUOTE_NONE)
195
+ for row in reader:
196
+ if row["language"] == self.config.language:
197
+ yield row["pairID"], {
198
+ "premise": row["sentence1"],
199
+ "hypothesis": row["sentence2"],
200
+ "label": row["gold_label"],
201
+ }
.history/indicxnli_20220823220708.py ADDED
@@ -0,0 +1,201 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ @misc{https://doi.org/10.48550/arxiv.2204.08776,
30
+ doi = {10.48550/ARXIV.2204.08776},
31
+
32
+ url = {https://arxiv.org/abs/2204.08776},
33
+
34
+ author = {Aggarwal, Divyanshu and Gupta, Vivek and Kunchukuttan, Anoop},
35
+
36
+ keywords = {Computation and Language (cs.CL), Artificial Intelligence (cs.AI), FOS: Computer and information sciences, FOS: Computer and information sciences},
37
+
38
+ title = {IndicXNLI: Evaluating Multilingual Inference for Indian Languages},
39
+
40
+ publisher = {arXiv},
41
+
42
+ year = {2022},
43
+
44
+ copyright = {Creative Commons Attribution 4.0 International}
45
+ }
46
+ }"""
47
+
48
+ _DESCRIPTION = """\
49
+ IndicXNLI is a translated version of XNLI to 11 Indic Languages. As with XNLI, the goal is
50
+ to predict textual entailment (does sentence A imply/contradict/neither sentence
51
+ B) and is a classification task (given two sentences, predict one of three
52
+ labels).
53
+ """
54
+
55
+ _LANGUAGES = (
56
+ 'hi',
57
+ 'bn',
58
+ 'mr',
59
+ 'as',
60
+ 'ta',
61
+ 'te',
62
+ 'or',
63
+ 'ml',
64
+ 'pa',
65
+ 'gu',
66
+ 'kn'
67
+ )
68
+
69
+
70
+ class IndicxnliConfig(datasets.BuilderConfig):
71
+ """BuilderConfig for XNLI."""
72
+
73
+ def __init__(self, language: str, **kwargs):
74
+ """BuilderConfig for XNLI.
75
+
76
+ Args:
77
+ language: One of hi, bn, mr, as, ta, te, or, ml, pa, gu, kn
78
+ **kwargs: keyword arguments forwarded to super.
79
+ """
80
+ super(IndicxnliConfig, self).__init__(**kwargs)
81
+ self.language = language
82
+
83
+
84
+ class Indicxnli(datasets.GeneratorBasedBuilder):
85
+ """XNLI: The Cross-Lingual NLI Corpus. Version 1.0."""
86
+
87
+ VERSION = datasets.Version("1.1.0", "")
88
+ BUILDER_CONFIG_CLASS = IndicxnliConfig
89
+ BUILDER_CONFIGS = [
90
+ IndicxnliConfig(
91
+ name=lang,
92
+ language=lang,
93
+ version=datasets.Version("1.1.0", ""),
94
+ description=f"Plain text import of IndicXNLI for the {lang} language",
95
+ )
96
+ for lang in _LANGUAGES
97
+ ]
98
+
99
+ def _info(self):
100
+ features = datasets.Features(
101
+ {
102
+ "premise": datasets.Value("string"),
103
+ "hypothesis": datasets.Value("string"),
104
+ "label": datasets.ClassLabel(names=["entailment", "neutral", "contradiction"]),
105
+ }
106
+ )
107
+ return datasets.DatasetInfo(
108
+ description=_DESCRIPTION,
109
+ features=features,
110
+ # No default supervised_keys (as we have to pass both premise
111
+ # and hypothesis as input).
112
+ supervised_keys=None,
113
+ homepage="https://www.nyu.edu/projects/bowman/xnli/",
114
+ citation=_CITATION,
115
+ )
116
+
117
+ def _split_generators(self, dl_manager):
118
+ with open()
119
+
120
+ return [
121
+ datasets.SplitGenerator(
122
+ name=datasets.Split.TRAIN,
123
+ gen_kwargs={
124
+ "filepaths": [
125
+ os.path.join(train_dir, f"multinli.train.{lang}.tsv") for lang in self.config.languages
126
+ ],
127
+ "data_format": "XNLI-MT",
128
+ },
129
+ ),
130
+ datasets.SplitGenerator(
131
+ name=datasets.Split.TEST,
132
+ gen_kwargs={"filepaths": [os.path.join(
133
+ testval_dir, "xnli.test.tsv")], "data_format": "XNLI"},
134
+ ),
135
+ datasets.SplitGenerator(
136
+ name=datasets.Split.VALIDATION,
137
+ gen_kwargs={"filepaths": [os.path.join(
138
+ testval_dir, "xnli.dev.tsv")], "data_format": "XNLI"},
139
+ ),
140
+ ]
141
+
142
+ def _generate_examples(self, data_format, filepaths):
143
+ """This function returns the examples in the raw (text) form."""
144
+
145
+ if self.config.language == "all_languages":
146
+ if data_format == "XNLI-MT":
147
+ with ExitStack() as stack:
148
+ files = [stack.enter_context(
149
+ open(filepath, encoding="utf-8")) for filepath in filepaths]
150
+ readers = [csv.DictReader(
151
+ file, delimiter="\t", quoting=csv.QUOTE_NONE) for file in files]
152
+ for row_idx, rows in enumerate(zip(*readers)):
153
+ yield row_idx, {
154
+ "premise": {lang: row["premise"] for lang, row in zip(self.config.languages, rows)},
155
+ "hypothesis": {lang: row["hypo"] for lang, row in zip(self.config.languages, rows)},
156
+ "label": rows[0]["label"].replace("contradictory", "contradiction"),
157
+ }
158
+ else:
159
+ rows_per_pair_id = collections.defaultdict(list)
160
+ for filepath in filepaths:
161
+ with open(filepath, encoding="utf-8") as f:
162
+ reader = csv.DictReader(
163
+ f, delimiter="\t", quoting=csv.QUOTE_NONE)
164
+ for row in reader:
165
+ rows_per_pair_id[row["pairID"]].append(row)
166
+
167
+ for rows in rows_per_pair_id.values():
168
+ premise = {row["language"]: row["sentence1"]
169
+ for row in rows}
170
+ hypothesis = {row["language"]: row["sentence2"]
171
+ for row in rows}
172
+ yield rows[0]["pairID"], {
173
+ "premise": premise,
174
+ "hypothesis": hypothesis,
175
+ "label": rows[0]["gold_label"],
176
+ }
177
+ else:
178
+ if data_format == "XNLI-MT":
179
+ for file_idx, filepath in enumerate(filepaths):
180
+ file = open(filepath, encoding="utf-8")
181
+ reader = csv.DictReader(
182
+ file, delimiter="\t", quoting=csv.QUOTE_NONE)
183
+ for row_idx, row in enumerate(reader):
184
+ key = str(file_idx) + "_" + str(row_idx)
185
+ yield key, {
186
+ "premise": row["premise"],
187
+ "hypothesis": row["hypo"],
188
+ "label": row["label"].replace("contradictory", "contradiction"),
189
+ }
190
+ else:
191
+ for filepath in filepaths:
192
+ with open(filepath, encoding="utf-8") as f:
193
+ reader = csv.DictReader(
194
+ f, delimiter="\t", quoting=csv.QUOTE_NONE)
195
+ for row in reader:
196
+ if row["language"] == self.config.language:
197
+ yield row["pairID"], {
198
+ "premise": row["sentence1"],
199
+ "hypothesis": row["sentence2"],
200
+ "label": row["gold_label"],
201
+ }
.history/indicxnli_20220823220724.py ADDED
@@ -0,0 +1,201 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ @misc{https://doi.org/10.48550/arxiv.2204.08776,
30
+ doi = {10.48550/ARXIV.2204.08776},
31
+
32
+ url = {https://arxiv.org/abs/2204.08776},
33
+
34
+ author = {Aggarwal, Divyanshu and Gupta, Vivek and Kunchukuttan, Anoop},
35
+
36
+ keywords = {Computation and Language (cs.CL), Artificial Intelligence (cs.AI), FOS: Computer and information sciences, FOS: Computer and information sciences},
37
+
38
+ title = {IndicXNLI: Evaluating Multilingual Inference for Indian Languages},
39
+
40
+ publisher = {arXiv},
41
+
42
+ year = {2022},
43
+
44
+ copyright = {Creative Commons Attribution 4.0 International}
45
+ }
46
+ }"""
47
+
48
+ _DESCRIPTION = """\
49
+ IndicXNLI is a translated version of XNLI to 11 Indic Languages. As with XNLI, the goal is
50
+ to predict textual entailment (does sentence A imply/contradict/neither sentence
51
+ B) and is a classification task (given two sentences, predict one of three
52
+ labels).
53
+ """
54
+
55
+ _LANGUAGES = (
56
+ 'hi',
57
+ 'bn',
58
+ 'mr',
59
+ 'as',
60
+ 'ta',
61
+ 'te',
62
+ 'or',
63
+ 'ml',
64
+ 'pa',
65
+ 'gu',
66
+ 'kn'
67
+ )
68
+
69
+
70
+ class IndicxnliConfig(datasets.BuilderConfig):
71
+ """BuilderConfig for XNLI."""
72
+
73
+ def __init__(self, language: str, **kwargs):
74
+ """BuilderConfig for XNLI.
75
+
76
+ Args:
77
+ language: One of hi, bn, mr, as, ta, te, or, ml, pa, gu, kn
78
+ **kwargs: keyword arguments forwarded to super.
79
+ """
80
+ super(IndicxnliConfig, self).__init__(**kwargs)
81
+ self.language = language
82
+
83
+
84
+ class Indicxnli(datasets.GeneratorBasedBuilder):
85
+ """XNLI: The Cross-Lingual NLI Corpus. Version 1.0."""
86
+
87
+ VERSION = datasets.Version("1.1.0", "")
88
+ BUILDER_CONFIG_CLASS = IndicxnliConfig
89
+ BUILDER_CONFIGS = [
90
+ IndicxnliConfig(
91
+ name=lang,
92
+ language=lang,
93
+ version=datasets.Version("1.1.0", ""),
94
+ description=f"Plain text import of IndicXNLI for the {lang} language",
95
+ )
96
+ for lang in _LANGUAGES
97
+ ]
98
+
99
+ def _info(self):
100
+ features = datasets.Features(
101
+ {
102
+ "premise": datasets.Value("string"),
103
+ "hypothesis": datasets.Value("string"),
104
+ "label": datasets.ClassLabel(names=["entailment", "neutral", "contradiction"]),
105
+ }
106
+ )
107
+ return datasets.DatasetInfo(
108
+ description=_DESCRIPTION,
109
+ features=features,
110
+ # No default supervised_keys (as we have to pass both premise
111
+ # and hypothesis as input).
112
+ supervised_keys=None,
113
+ homepage="https://www.nyu.edu/projects/bowman/xnli/",
114
+ citation=_CITATION,
115
+ )
116
+
117
+ def _split_generators(self, dl_manager):
118
+ with open('forward/train')
119
+
120
+ return [
121
+ datasets.SplitGenerator(
122
+ name=datasets.Split.TRAIN,
123
+ gen_kwargs={
124
+ "filepaths": [
125
+ os.path.join(train_dir, f"multinli.train.{lang}.tsv") for lang in self.config.languages
126
+ ],
127
+ "data_format": "XNLI-MT",
128
+ },
129
+ ),
130
+ datasets.SplitGenerator(
131
+ name=datasets.Split.TEST,
132
+ gen_kwargs={"filepaths": [os.path.join(
133
+ testval_dir, "xnli.test.tsv")], "data_format": "XNLI"},
134
+ ),
135
+ datasets.SplitGenerator(
136
+ name=datasets.Split.VALIDATION,
137
+ gen_kwargs={"filepaths": [os.path.join(
138
+ testval_dir, "xnli.dev.tsv")], "data_format": "XNLI"},
139
+ ),
140
+ ]
141
+
142
+ def _generate_examples(self, data_format, filepaths):
143
+ """This function returns the examples in the raw (text) form."""
144
+
145
+ if self.config.language == "all_languages":
146
+ if data_format == "XNLI-MT":
147
+ with ExitStack() as stack:
148
+ files = [stack.enter_context(
149
+ open(filepath, encoding="utf-8")) for filepath in filepaths]
150
+ readers = [csv.DictReader(
151
+ file, delimiter="\t", quoting=csv.QUOTE_NONE) for file in files]
152
+ for row_idx, rows in enumerate(zip(*readers)):
153
+ yield row_idx, {
154
+ "premise": {lang: row["premise"] for lang, row in zip(self.config.languages, rows)},
155
+ "hypothesis": {lang: row["hypo"] for lang, row in zip(self.config.languages, rows)},
156
+ "label": rows[0]["label"].replace("contradictory", "contradiction"),
157
+ }
158
+ else:
159
+ rows_per_pair_id = collections.defaultdict(list)
160
+ for filepath in filepaths:
161
+ with open(filepath, encoding="utf-8") as f:
162
+ reader = csv.DictReader(
163
+ f, delimiter="\t", quoting=csv.QUOTE_NONE)
164
+ for row in reader:
165
+ rows_per_pair_id[row["pairID"]].append(row)
166
+
167
+ for rows in rows_per_pair_id.values():
168
+ premise = {row["language"]: row["sentence1"]
169
+ for row in rows}
170
+ hypothesis = {row["language"]: row["sentence2"]
171
+ for row in rows}
172
+ yield rows[0]["pairID"], {
173
+ "premise": premise,
174
+ "hypothesis": hypothesis,
175
+ "label": rows[0]["gold_label"],
176
+ }
177
+ else:
178
+ if data_format == "XNLI-MT":
179
+ for file_idx, filepath in enumerate(filepaths):
180
+ file = open(filepath, encoding="utf-8")
181
+ reader = csv.DictReader(
182
+ file, delimiter="\t", quoting=csv.QUOTE_NONE)
183
+ for row_idx, row in enumerate(reader):
184
+ key = str(file_idx) + "_" + str(row_idx)
185
+ yield key, {
186
+ "premise": row["premise"],
187
+ "hypothesis": row["hypo"],
188
+ "label": row["label"].replace("contradictory", "contradiction"),
189
+ }
190
+ else:
191
+ for filepath in filepaths:
192
+ with open(filepath, encoding="utf-8") as f:
193
+ reader = csv.DictReader(
194
+ f, delimiter="\t", quoting=csv.QUOTE_NONE)
195
+ for row in reader:
196
+ if row["language"] == self.config.language:
197
+ yield row["pairID"], {
198
+ "premise": row["sentence1"],
199
+ "hypothesis": row["sentence2"],
200
+ "label": row["gold_label"],
201
+ }
.history/indicxnli_20220823220725.py ADDED
@@ -0,0 +1,201 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ @misc{https://doi.org/10.48550/arxiv.2204.08776,
30
+ doi = {10.48550/ARXIV.2204.08776},
31
+
32
+ url = {https://arxiv.org/abs/2204.08776},
33
+
34
+ author = {Aggarwal, Divyanshu and Gupta, Vivek and Kunchukuttan, Anoop},
35
+
36
+ keywords = {Computation and Language (cs.CL), Artificial Intelligence (cs.AI), FOS: Computer and information sciences, FOS: Computer and information sciences},
37
+
38
+ title = {IndicXNLI: Evaluating Multilingual Inference for Indian Languages},
39
+
40
+ publisher = {arXiv},
41
+
42
+ year = {2022},
43
+
44
+ copyright = {Creative Commons Attribution 4.0 International}
45
+ }
46
+ }"""
47
+
48
+ _DESCRIPTION = """\
49
+ IndicXNLI is a translated version of XNLI to 11 Indic Languages. As with XNLI, the goal is
50
+ to predict textual entailment (does sentence A imply/contradict/neither sentence
51
+ B) and is a classification task (given two sentences, predict one of three
52
+ labels).
53
+ """
54
+
55
+ _LANGUAGES = (
56
+ 'hi',
57
+ 'bn',
58
+ 'mr',
59
+ 'as',
60
+ 'ta',
61
+ 'te',
62
+ 'or',
63
+ 'ml',
64
+ 'pa',
65
+ 'gu',
66
+ 'kn'
67
+ )
68
+
69
+
70
+ class IndicxnliConfig(datasets.BuilderConfig):
71
+ """BuilderConfig for XNLI."""
72
+
73
+ def __init__(self, language: str, **kwargs):
74
+ """BuilderConfig for XNLI.
75
+
76
+ Args:
77
+ language: One of hi, bn, mr, as, ta, te, or, ml, pa, gu, kn
78
+ **kwargs: keyword arguments forwarded to super.
79
+ """
80
+ super(IndicxnliConfig, self).__init__(**kwargs)
81
+ self.language = language
82
+
83
+
84
+ class Indicxnli(datasets.GeneratorBasedBuilder):
85
+ """XNLI: The Cross-Lingual NLI Corpus. Version 1.0."""
86
+
87
+ VERSION = datasets.Version("1.1.0", "")
88
+ BUILDER_CONFIG_CLASS = IndicxnliConfig
89
+ BUILDER_CONFIGS = [
90
+ IndicxnliConfig(
91
+ name=lang,
92
+ language=lang,
93
+ version=datasets.Version("1.1.0", ""),
94
+ description=f"Plain text import of IndicXNLI for the {lang} language",
95
+ )
96
+ for lang in _LANGUAGES
97
+ ]
98
+
99
+ def _info(self):
100
+ features = datasets.Features(
101
+ {
102
+ "premise": datasets.Value("string"),
103
+ "hypothesis": datasets.Value("string"),
104
+ "label": datasets.ClassLabel(names=["entailment", "neutral", "contradiction"]),
105
+ }
106
+ )
107
+ return datasets.DatasetInfo(
108
+ description=_DESCRIPTION,
109
+ features=features,
110
+ # No default supervised_keys (as we have to pass both premise
111
+ # and hypothesis as input).
112
+ supervised_keys=None,
113
+ homepage="https://www.nyu.edu/projects/bowman/xnli/",
114
+ citation=_CITATION,
115
+ )
116
+
117
+ def _split_generators(self, dl_manager):
118
+ with open('forward/train', )
119
+
120
+ return [
121
+ datasets.SplitGenerator(
122
+ name=datasets.Split.TRAIN,
123
+ gen_kwargs={
124
+ "filepaths": [
125
+ os.path.join(train_dir, f"multinli.train.{lang}.tsv") for lang in self.config.languages
126
+ ],
127
+ "data_format": "XNLI-MT",
128
+ },
129
+ ),
130
+ datasets.SplitGenerator(
131
+ name=datasets.Split.TEST,
132
+ gen_kwargs={"filepaths": [os.path.join(
133
+ testval_dir, "xnli.test.tsv")], "data_format": "XNLI"},
134
+ ),
135
+ datasets.SplitGenerator(
136
+ name=datasets.Split.VALIDATION,
137
+ gen_kwargs={"filepaths": [os.path.join(
138
+ testval_dir, "xnli.dev.tsv")], "data_format": "XNLI"},
139
+ ),
140
+ ]
141
+
142
+ def _generate_examples(self, data_format, filepaths):
143
+ """This function returns the examples in the raw (text) form."""
144
+
145
+ if self.config.language == "all_languages":
146
+ if data_format == "XNLI-MT":
147
+ with ExitStack() as stack:
148
+ files = [stack.enter_context(
149
+ open(filepath, encoding="utf-8")) for filepath in filepaths]
150
+ readers = [csv.DictReader(
151
+ file, delimiter="\t", quoting=csv.QUOTE_NONE) for file in files]
152
+ for row_idx, rows in enumerate(zip(*readers)):
153
+ yield row_idx, {
154
+ "premise": {lang: row["premise"] for lang, row in zip(self.config.languages, rows)},
155
+ "hypothesis": {lang: row["hypo"] for lang, row in zip(self.config.languages, rows)},
156
+ "label": rows[0]["label"].replace("contradictory", "contradiction"),
157
+ }
158
+ else:
159
+ rows_per_pair_id = collections.defaultdict(list)
160
+ for filepath in filepaths:
161
+ with open(filepath, encoding="utf-8") as f:
162
+ reader = csv.DictReader(
163
+ f, delimiter="\t", quoting=csv.QUOTE_NONE)
164
+ for row in reader:
165
+ rows_per_pair_id[row["pairID"]].append(row)
166
+
167
+ for rows in rows_per_pair_id.values():
168
+ premise = {row["language"]: row["sentence1"]
169
+ for row in rows}
170
+ hypothesis = {row["language"]: row["sentence2"]
171
+ for row in rows}
172
+ yield rows[0]["pairID"], {
173
+ "premise": premise,
174
+ "hypothesis": hypothesis,
175
+ "label": rows[0]["gold_label"],
176
+ }
177
+ else:
178
+ if data_format == "XNLI-MT":
179
+ for file_idx, filepath in enumerate(filepaths):
180
+ file = open(filepath, encoding="utf-8")
181
+ reader = csv.DictReader(
182
+ file, delimiter="\t", quoting=csv.QUOTE_NONE)
183
+ for row_idx, row in enumerate(reader):
184
+ key = str(file_idx) + "_" + str(row_idx)
185
+ yield key, {
186
+ "premise": row["premise"],
187
+ "hypothesis": row["hypo"],
188
+ "label": row["label"].replace("contradictory", "contradiction"),
189
+ }
190
+ else:
191
+ for filepath in filepaths:
192
+ with open(filepath, encoding="utf-8") as f:
193
+ reader = csv.DictReader(
194
+ f, delimiter="\t", quoting=csv.QUOTE_NONE)
195
+ for row in reader:
196
+ if row["language"] == self.config.language:
197
+ yield row["pairID"], {
198
+ "premise": row["sentence1"],
199
+ "hypothesis": row["sentence2"],
200
+ "label": row["gold_label"],
201
+ }
.history/indicxnli_20220823220728.py ADDED
@@ -0,0 +1,201 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ @misc{https://doi.org/10.48550/arxiv.2204.08776,
30
+ doi = {10.48550/ARXIV.2204.08776},
31
+
32
+ url = {https://arxiv.org/abs/2204.08776},
33
+
34
+ author = {Aggarwal, Divyanshu and Gupta, Vivek and Kunchukuttan, Anoop},
35
+
36
+ keywords = {Computation and Language (cs.CL), Artificial Intelligence (cs.AI), FOS: Computer and information sciences, FOS: Computer and information sciences},
37
+
38
+ title = {IndicXNLI: Evaluating Multilingual Inference for Indian Languages},
39
+
40
+ publisher = {arXiv},
41
+
42
+ year = {2022},
43
+
44
+ copyright = {Creative Commons Attribution 4.0 International}
45
+ }
46
+ }"""
47
+
48
+ _DESCRIPTION = """\
49
+ IndicXNLI is a translated version of XNLI to 11 Indic Languages. As with XNLI, the goal is
50
+ to predict textual entailment (does sentence A imply/contradict/neither sentence
51
+ B) and is a classification task (given two sentences, predict one of three
52
+ labels).
53
+ """
54
+
55
+ _LANGUAGES = (
56
+ 'hi',
57
+ 'bn',
58
+ 'mr',
59
+ 'as',
60
+ 'ta',
61
+ 'te',
62
+ 'or',
63
+ 'ml',
64
+ 'pa',
65
+ 'gu',
66
+ 'kn'
67
+ )
68
+
69
+
70
+ class IndicxnliConfig(datasets.BuilderConfig):
71
+ """BuilderConfig for XNLI."""
72
+
73
+ def __init__(self, language: str, **kwargs):
74
+ """BuilderConfig for XNLI.
75
+
76
+ Args:
77
+ language: One of hi, bn, mr, as, ta, te, or, ml, pa, gu, kn
78
+ **kwargs: keyword arguments forwarded to super.
79
+ """
80
+ super(IndicxnliConfig, self).__init__(**kwargs)
81
+ self.language = language
82
+
83
+
84
+ class Indicxnli(datasets.GeneratorBasedBuilder):
85
+ """XNLI: The Cross-Lingual NLI Corpus. Version 1.0."""
86
+
87
+ VERSION = datasets.Version("1.1.0", "")
88
+ BUILDER_CONFIG_CLASS = IndicxnliConfig
89
+ BUILDER_CONFIGS = [
90
+ IndicxnliConfig(
91
+ name=lang,
92
+ language=lang,
93
+ version=datasets.Version("1.1.0", ""),
94
+ description=f"Plain text import of IndicXNLI for the {lang} language",
95
+ )
96
+ for lang in _LANGUAGES
97
+ ]
98
+
99
+ def _info(self):
100
+ features = datasets.Features(
101
+ {
102
+ "premise": datasets.Value("string"),
103
+ "hypothesis": datasets.Value("string"),
104
+ "label": datasets.ClassLabel(names=["entailment", "neutral", "contradiction"]),
105
+ }
106
+ )
107
+ return datasets.DatasetInfo(
108
+ description=_DESCRIPTION,
109
+ features=features,
110
+ # No default supervised_keys (as we have to pass both premise
111
+ # and hypothesis as input).
112
+ supervised_keys=None,
113
+ homepage="https://www.nyu.edu/projects/bowman/xnli/",
114
+ citation=_CITATION,
115
+ )
116
+
117
+ def _split_generators(self, dl_manager):
118
+ with open('forward/train', 'r')
119
+
120
+ return [
121
+ datasets.SplitGenerator(
122
+ name=datasets.Split.TRAIN,
123
+ gen_kwargs={
124
+ "filepaths": [
125
+ os.path.join(train_dir, f"multinli.train.{lang}.tsv") for lang in self.config.languages
126
+ ],
127
+ "data_format": "XNLI-MT",
128
+ },
129
+ ),
130
+ datasets.SplitGenerator(
131
+ name=datasets.Split.TEST,
132
+ gen_kwargs={"filepaths": [os.path.join(
133
+ testval_dir, "xnli.test.tsv")], "data_format": "XNLI"},
134
+ ),
135
+ datasets.SplitGenerator(
136
+ name=datasets.Split.VALIDATION,
137
+ gen_kwargs={"filepaths": [os.path.join(
138
+ testval_dir, "xnli.dev.tsv")], "data_format": "XNLI"},
139
+ ),
140
+ ]
141
+
142
+ def _generate_examples(self, data_format, filepaths):
143
+ """This function returns the examples in the raw (text) form."""
144
+
145
+ if self.config.language == "all_languages":
146
+ if data_format == "XNLI-MT":
147
+ with ExitStack() as stack:
148
+ files = [stack.enter_context(
149
+ open(filepath, encoding="utf-8")) for filepath in filepaths]
150
+ readers = [csv.DictReader(
151
+ file, delimiter="\t", quoting=csv.QUOTE_NONE) for file in files]
152
+ for row_idx, rows in enumerate(zip(*readers)):
153
+ yield row_idx, {
154
+ "premise": {lang: row["premise"] for lang, row in zip(self.config.languages, rows)},
155
+ "hypothesis": {lang: row["hypo"] for lang, row in zip(self.config.languages, rows)},
156
+ "label": rows[0]["label"].replace("contradictory", "contradiction"),
157
+ }
158
+ else:
159
+ rows_per_pair_id = collections.defaultdict(list)
160
+ for filepath in filepaths:
161
+ with open(filepath, encoding="utf-8") as f:
162
+ reader = csv.DictReader(
163
+ f, delimiter="\t", quoting=csv.QUOTE_NONE)
164
+ for row in reader:
165
+ rows_per_pair_id[row["pairID"]].append(row)
166
+
167
+ for rows in rows_per_pair_id.values():
168
+ premise = {row["language"]: row["sentence1"]
169
+ for row in rows}
170
+ hypothesis = {row["language"]: row["sentence2"]
171
+ for row in rows}
172
+ yield rows[0]["pairID"], {
173
+ "premise": premise,
174
+ "hypothesis": hypothesis,
175
+ "label": rows[0]["gold_label"],
176
+ }
177
+ else:
178
+ if data_format == "XNLI-MT":
179
+ for file_idx, filepath in enumerate(filepaths):
180
+ file = open(filepath, encoding="utf-8")
181
+ reader = csv.DictReader(
182
+ file, delimiter="\t", quoting=csv.QUOTE_NONE)
183
+ for row_idx, row in enumerate(reader):
184
+ key = str(file_idx) + "_" + str(row_idx)
185
+ yield key, {
186
+ "premise": row["premise"],
187
+ "hypothesis": row["hypo"],
188
+ "label": row["label"].replace("contradictory", "contradiction"),
189
+ }
190
+ else:
191
+ for filepath in filepaths:
192
+ with open(filepath, encoding="utf-8") as f:
193
+ reader = csv.DictReader(
194
+ f, delimiter="\t", quoting=csv.QUOTE_NONE)
195
+ for row in reader:
196
+ if row["language"] == self.config.language:
197
+ yield row["pairID"], {
198
+ "premise": row["sentence1"],
199
+ "hypothesis": row["sentence2"],
200
+ "label": row["gold_label"],
201
+ }
.history/indicxnli_20220823220732.py ADDED
@@ -0,0 +1,201 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ @misc{https://doi.org/10.48550/arxiv.2204.08776,
30
+ doi = {10.48550/ARXIV.2204.08776},
31
+
32
+ url = {https://arxiv.org/abs/2204.08776},
33
+
34
+ author = {Aggarwal, Divyanshu and Gupta, Vivek and Kunchukuttan, Anoop},
35
+
36
+ keywords = {Computation and Language (cs.CL), Artificial Intelligence (cs.AI), FOS: Computer and information sciences, FOS: Computer and information sciences},
37
+
38
+ title = {IndicXNLI: Evaluating Multilingual Inference for Indian Languages},
39
+
40
+ publisher = {arXiv},
41
+
42
+ year = {2022},
43
+
44
+ copyright = {Creative Commons Attribution 4.0 International}
45
+ }
46
+ }"""
47
+
48
+ _DESCRIPTION = """\
49
+ IndicXNLI is a translated version of XNLI to 11 Indic Languages. As with XNLI, the goal is
50
+ to predict textual entailment (does sentence A imply/contradict/neither sentence
51
+ B) and is a classification task (given two sentences, predict one of three
52
+ labels).
53
+ """
54
+
55
+ _LANGUAGES = (
56
+ 'hi',
57
+ 'bn',
58
+ 'mr',
59
+ 'as',
60
+ 'ta',
61
+ 'te',
62
+ 'or',
63
+ 'ml',
64
+ 'pa',
65
+ 'gu',
66
+ 'kn'
67
+ )
68
+
69
+
70
+ class IndicxnliConfig(datasets.BuilderConfig):
71
+ """BuilderConfig for XNLI."""
72
+
73
+ def __init__(self, language: str, **kwargs):
74
+ """BuilderConfig for XNLI.
75
+
76
+ Args:
77
+ language: One of hi, bn, mr, as, ta, te, or, ml, pa, gu, kn
78
+ **kwargs: keyword arguments forwarded to super.
79
+ """
80
+ super(IndicxnliConfig, self).__init__(**kwargs)
81
+ self.language = language
82
+
83
+
84
+ class Indicxnli(datasets.GeneratorBasedBuilder):
85
+ """XNLI: The Cross-Lingual NLI Corpus. Version 1.0."""
86
+
87
+ VERSION = datasets.Version("1.1.0", "")
88
+ BUILDER_CONFIG_CLASS = IndicxnliConfig
89
+ BUILDER_CONFIGS = [
90
+ IndicxnliConfig(
91
+ name=lang,
92
+ language=lang,
93
+ version=datasets.Version("1.1.0", ""),
94
+ description=f"Plain text import of IndicXNLI for the {lang} language",
95
+ )
96
+ for lang in _LANGUAGES
97
+ ]
98
+
99
+ def _info(self):
100
+ features = datasets.Features(
101
+ {
102
+ "premise": datasets.Value("string"),
103
+ "hypothesis": datasets.Value("string"),
104
+ "label": datasets.ClassLabel(names=["entailment", "neutral", "contradiction"]),
105
+ }
106
+ )
107
+ return datasets.DatasetInfo(
108
+ description=_DESCRIPTION,
109
+ features=features,
110
+ # No default supervised_keys (as we have to pass both premise
111
+ # and hypothesis as input).
112
+ supervised_keys=None,
113
+ homepage="https://www.nyu.edu/projects/bowman/xnli/",
114
+ citation=_CITATION,
115
+ )
116
+
117
+ def _split_generators(self, dl_manager):
118
+ with open('forward/train/', 'r')
119
+
120
+ return [
121
+ datasets.SplitGenerator(
122
+ name=datasets.Split.TRAIN,
123
+ gen_kwargs={
124
+ "filepaths": [
125
+ os.path.join(train_dir, f"multinli.train.{lang}.tsv") for lang in self.config.languages
126
+ ],
127
+ "data_format": "XNLI-MT",
128
+ },
129
+ ),
130
+ datasets.SplitGenerator(
131
+ name=datasets.Split.TEST,
132
+ gen_kwargs={"filepaths": [os.path.join(
133
+ testval_dir, "xnli.test.tsv")], "data_format": "XNLI"},
134
+ ),
135
+ datasets.SplitGenerator(
136
+ name=datasets.Split.VALIDATION,
137
+ gen_kwargs={"filepaths": [os.path.join(
138
+ testval_dir, "xnli.dev.tsv")], "data_format": "XNLI"},
139
+ ),
140
+ ]
141
+
142
+ def _generate_examples(self, data_format, filepaths):
143
+ """This function returns the examples in the raw (text) form."""
144
+
145
+ if self.config.language == "all_languages":
146
+ if data_format == "XNLI-MT":
147
+ with ExitStack() as stack:
148
+ files = [stack.enter_context(
149
+ open(filepath, encoding="utf-8")) for filepath in filepaths]
150
+ readers = [csv.DictReader(
151
+ file, delimiter="\t", quoting=csv.QUOTE_NONE) for file in files]
152
+ for row_idx, rows in enumerate(zip(*readers)):
153
+ yield row_idx, {
154
+ "premise": {lang: row["premise"] for lang, row in zip(self.config.languages, rows)},
155
+ "hypothesis": {lang: row["hypo"] for lang, row in zip(self.config.languages, rows)},
156
+ "label": rows[0]["label"].replace("contradictory", "contradiction"),
157
+ }
158
+ else:
159
+ rows_per_pair_id = collections.defaultdict(list)
160
+ for filepath in filepaths:
161
+ with open(filepath, encoding="utf-8") as f:
162
+ reader = csv.DictReader(
163
+ f, delimiter="\t", quoting=csv.QUOTE_NONE)
164
+ for row in reader:
165
+ rows_per_pair_id[row["pairID"]].append(row)
166
+
167
+ for rows in rows_per_pair_id.values():
168
+ premise = {row["language"]: row["sentence1"]
169
+ for row in rows}
170
+ hypothesis = {row["language"]: row["sentence2"]
171
+ for row in rows}
172
+ yield rows[0]["pairID"], {
173
+ "premise": premise,
174
+ "hypothesis": hypothesis,
175
+ "label": rows[0]["gold_label"],
176
+ }
177
+ else:
178
+ if data_format == "XNLI-MT":
179
+ for file_idx, filepath in enumerate(filepaths):
180
+ file = open(filepath, encoding="utf-8")
181
+ reader = csv.DictReader(
182
+ file, delimiter="\t", quoting=csv.QUOTE_NONE)
183
+ for row_idx, row in enumerate(reader):
184
+ key = str(file_idx) + "_" + str(row_idx)
185
+ yield key, {
186
+ "premise": row["premise"],
187
+ "hypothesis": row["hypo"],
188
+ "label": row["label"].replace("contradictory", "contradiction"),
189
+ }
190
+ else:
191
+ for filepath in filepaths:
192
+ with open(filepath, encoding="utf-8") as f:
193
+ reader = csv.DictReader(
194
+ f, delimiter="\t", quoting=csv.QUOTE_NONE)
195
+ for row in reader:
196
+ if row["language"] == self.config.language:
197
+ yield row["pairID"], {
198
+ "premise": row["sentence1"],
199
+ "hypothesis": row["sentence2"],
200
+ "label": row["gold_label"],
201
+ }
.history/indicxnli_20220823220735.py ADDED
@@ -0,0 +1,201 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ @misc{https://doi.org/10.48550/arxiv.2204.08776,
30
+ doi = {10.48550/ARXIV.2204.08776},
31
+
32
+ url = {https://arxiv.org/abs/2204.08776},
33
+
34
+ author = {Aggarwal, Divyanshu and Gupta, Vivek and Kunchukuttan, Anoop},
35
+
36
+ keywords = {Computation and Language (cs.CL), Artificial Intelligence (cs.AI), FOS: Computer and information sciences, FOS: Computer and information sciences},
37
+
38
+ title = {IndicXNLI: Evaluating Multilingual Inference for Indian Languages},
39
+
40
+ publisher = {arXiv},
41
+
42
+ year = {2022},
43
+
44
+ copyright = {Creative Commons Attribution 4.0 International}
45
+ }
46
+ }"""
47
+
48
+ _DESCRIPTION = """\
49
+ IndicXNLI is a translated version of XNLI to 11 Indic Languages. As with XNLI, the goal is
50
+ to predict textual entailment (does sentence A imply/contradict/neither sentence
51
+ B) and is a classification task (given two sentences, predict one of three
52
+ labels).
53
+ """
54
+
55
+ _LANGUAGES = (
56
+ 'hi',
57
+ 'bn',
58
+ 'mr',
59
+ 'as',
60
+ 'ta',
61
+ 'te',
62
+ 'or',
63
+ 'ml',
64
+ 'pa',
65
+ 'gu',
66
+ 'kn'
67
+ )
68
+
69
+
70
+ class IndicxnliConfig(datasets.BuilderConfig):
71
+ """BuilderConfig for XNLI."""
72
+
73
+ def __init__(self, language: str, **kwargs):
74
+ """BuilderConfig for XNLI.
75
+
76
+ Args:
77
+ language: One of hi, bn, mr, as, ta, te, or, ml, pa, gu, kn
78
+ **kwargs: keyword arguments forwarded to super.
79
+ """
80
+ super(IndicxnliConfig, self).__init__(**kwargs)
81
+ self.language = language
82
+
83
+
84
+ class Indicxnli(datasets.GeneratorBasedBuilder):
85
+ """XNLI: The Cross-Lingual NLI Corpus. Version 1.0."""
86
+
87
+ VERSION = datasets.Version("1.1.0", "")
88
+ BUILDER_CONFIG_CLASS = IndicxnliConfig
89
+ BUILDER_CONFIGS = [
90
+ IndicxnliConfig(
91
+ name=lang,
92
+ language=lang,
93
+ version=datasets.Version("1.1.0", ""),
94
+ description=f"Plain text import of IndicXNLI for the {lang} language",
95
+ )
96
+ for lang in _LANGUAGES
97
+ ]
98
+
99
+ def _info(self):
100
+ features = datasets.Features(
101
+ {
102
+ "premise": datasets.Value("string"),
103
+ "hypothesis": datasets.Value("string"),
104
+ "label": datasets.ClassLabel(names=["entailment", "neutral", "contradiction"]),
105
+ }
106
+ )
107
+ return datasets.DatasetInfo(
108
+ description=_DESCRIPTION,
109
+ features=features,
110
+ # No default supervised_keys (as we have to pass both premise
111
+ # and hypothesis as input).
112
+ supervised_keys=None,
113
+ homepage="https://www.nyu.edu/projects/bowman/xnli/",
114
+ citation=_CITATION,
115
+ )
116
+
117
+ def _split_generators(self, dl_manager):
118
+ with open('forward/train/{}', 'r')
119
+
120
+ return [
121
+ datasets.SplitGenerator(
122
+ name=datasets.Split.TRAIN,
123
+ gen_kwargs={
124
+ "filepaths": [
125
+ os.path.join(train_dir, f"multinli.train.{lang}.tsv") for lang in self.config.languages
126
+ ],
127
+ "data_format": "XNLI-MT",
128
+ },
129
+ ),
130
+ datasets.SplitGenerator(
131
+ name=datasets.Split.TEST,
132
+ gen_kwargs={"filepaths": [os.path.join(
133
+ testval_dir, "xnli.test.tsv")], "data_format": "XNLI"},
134
+ ),
135
+ datasets.SplitGenerator(
136
+ name=datasets.Split.VALIDATION,
137
+ gen_kwargs={"filepaths": [os.path.join(
138
+ testval_dir, "xnli.dev.tsv")], "data_format": "XNLI"},
139
+ ),
140
+ ]
141
+
142
+ def _generate_examples(self, data_format, filepaths):
143
+ """This function returns the examples in the raw (text) form."""
144
+
145
+ if self.config.language == "all_languages":
146
+ if data_format == "XNLI-MT":
147
+ with ExitStack() as stack:
148
+ files = [stack.enter_context(
149
+ open(filepath, encoding="utf-8")) for filepath in filepaths]
150
+ readers = [csv.DictReader(
151
+ file, delimiter="\t", quoting=csv.QUOTE_NONE) for file in files]
152
+ for row_idx, rows in enumerate(zip(*readers)):
153
+ yield row_idx, {
154
+ "premise": {lang: row["premise"] for lang, row in zip(self.config.languages, rows)},
155
+ "hypothesis": {lang: row["hypo"] for lang, row in zip(self.config.languages, rows)},
156
+ "label": rows[0]["label"].replace("contradictory", "contradiction"),
157
+ }
158
+ else:
159
+ rows_per_pair_id = collections.defaultdict(list)
160
+ for filepath in filepaths:
161
+ with open(filepath, encoding="utf-8") as f:
162
+ reader = csv.DictReader(
163
+ f, delimiter="\t", quoting=csv.QUOTE_NONE)
164
+ for row in reader:
165
+ rows_per_pair_id[row["pairID"]].append(row)
166
+
167
+ for rows in rows_per_pair_id.values():
168
+ premise = {row["language"]: row["sentence1"]
169
+ for row in rows}
170
+ hypothesis = {row["language"]: row["sentence2"]
171
+ for row in rows}
172
+ yield rows[0]["pairID"], {
173
+ "premise": premise,
174
+ "hypothesis": hypothesis,
175
+ "label": rows[0]["gold_label"],
176
+ }
177
+ else:
178
+ if data_format == "XNLI-MT":
179
+ for file_idx, filepath in enumerate(filepaths):
180
+ file = open(filepath, encoding="utf-8")
181
+ reader = csv.DictReader(
182
+ file, delimiter="\t", quoting=csv.QUOTE_NONE)
183
+ for row_idx, row in enumerate(reader):
184
+ key = str(file_idx) + "_" + str(row_idx)
185
+ yield key, {
186
+ "premise": row["premise"],
187
+ "hypothesis": row["hypo"],
188
+ "label": row["label"].replace("contradictory", "contradiction"),
189
+ }
190
+ else:
191
+ for filepath in filepaths:
192
+ with open(filepath, encoding="utf-8") as f:
193
+ reader = csv.DictReader(
194
+ f, delimiter="\t", quoting=csv.QUOTE_NONE)
195
+ for row in reader:
196
+ if row["language"] == self.config.language:
197
+ yield row["pairID"], {
198
+ "premise": row["sentence1"],
199
+ "hypothesis": row["sentence2"],
200
+ "label": row["gold_label"],
201
+ }
.history/indicxnli_20220823220738.py ADDED
@@ -0,0 +1,201 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ @misc{https://doi.org/10.48550/arxiv.2204.08776,
30
+ doi = {10.48550/ARXIV.2204.08776},
31
+
32
+ url = {https://arxiv.org/abs/2204.08776},
33
+
34
+ author = {Aggarwal, Divyanshu and Gupta, Vivek and Kunchukuttan, Anoop},
35
+
36
+ keywords = {Computation and Language (cs.CL), Artificial Intelligence (cs.AI), FOS: Computer and information sciences, FOS: Computer and information sciences},
37
+
38
+ title = {IndicXNLI: Evaluating Multilingual Inference for Indian Languages},
39
+
40
+ publisher = {arXiv},
41
+
42
+ year = {2022},
43
+
44
+ copyright = {Creative Commons Attribution 4.0 International}
45
+ }
46
+ }"""
47
+
48
+ _DESCRIPTION = """\
49
+ IndicXNLI is a translated version of XNLI to 11 Indic Languages. As with XNLI, the goal is
50
+ to predict textual entailment (does sentence A imply/contradict/neither sentence
51
+ B) and is a classification task (given two sentences, predict one of three
52
+ labels).
53
+ """
54
+
55
+ _LANGUAGES = (
56
+ 'hi',
57
+ 'bn',
58
+ 'mr',
59
+ 'as',
60
+ 'ta',
61
+ 'te',
62
+ 'or',
63
+ 'ml',
64
+ 'pa',
65
+ 'gu',
66
+ 'kn'
67
+ )
68
+
69
+
70
+ class IndicxnliConfig(datasets.BuilderConfig):
71
+ """BuilderConfig for XNLI."""
72
+
73
+ def __init__(self, language: str, **kwargs):
74
+ """BuilderConfig for XNLI.
75
+
76
+ Args:
77
+ language: One of hi, bn, mr, as, ta, te, or, ml, pa, gu, kn
78
+ **kwargs: keyword arguments forwarded to super.
79
+ """
80
+ super(IndicxnliConfig, self).__init__(**kwargs)
81
+ self.language = language
82
+
83
+
84
+ class Indicxnli(datasets.GeneratorBasedBuilder):
85
+ """XNLI: The Cross-Lingual NLI Corpus. Version 1.0."""
86
+
87
+ VERSION = datasets.Version("1.1.0", "")
88
+ BUILDER_CONFIG_CLASS = IndicxnliConfig
89
+ BUILDER_CONFIGS = [
90
+ IndicxnliConfig(
91
+ name=lang,
92
+ language=lang,
93
+ version=datasets.Version("1.1.0", ""),
94
+ description=f"Plain text import of IndicXNLI for the {lang} language",
95
+ )
96
+ for lang in _LANGUAGES
97
+ ]
98
+
99
+ def _info(self):
100
+ features = datasets.Features(
101
+ {
102
+ "premise": datasets.Value("string"),
103
+ "hypothesis": datasets.Value("string"),
104
+ "label": datasets.ClassLabel(names=["entailment", "neutral", "contradiction"]),
105
+ }
106
+ )
107
+ return datasets.DatasetInfo(
108
+ description=_DESCRIPTION,
109
+ features=features,
110
+ # No default supervised_keys (as we have to pass both premise
111
+ # and hypothesis as input).
112
+ supervised_keys=None,
113
+ homepage="https://www.nyu.edu/projects/bowman/xnli/",
114
+ citation=_CITATION,
115
+ )
116
+
117
+ def _split_generators(self, dl_manager):
118
+ with open(f'forward/train/{}', 'r')
119
+
120
+ return [
121
+ datasets.SplitGenerator(
122
+ name=datasets.Split.TRAIN,
123
+ gen_kwargs={
124
+ "filepaths": [
125
+ os.path.join(train_dir, f"multinli.train.{lang}.tsv") for lang in self.config.languages
126
+ ],
127
+ "data_format": "XNLI-MT",
128
+ },
129
+ ),
130
+ datasets.SplitGenerator(
131
+ name=datasets.Split.TEST,
132
+ gen_kwargs={"filepaths": [os.path.join(
133
+ testval_dir, "xnli.test.tsv")], "data_format": "XNLI"},
134
+ ),
135
+ datasets.SplitGenerator(
136
+ name=datasets.Split.VALIDATION,
137
+ gen_kwargs={"filepaths": [os.path.join(
138
+ testval_dir, "xnli.dev.tsv")], "data_format": "XNLI"},
139
+ ),
140
+ ]
141
+
142
+ def _generate_examples(self, data_format, filepaths):
143
+ """This function returns the examples in the raw (text) form."""
144
+
145
+ if self.config.language == "all_languages":
146
+ if data_format == "XNLI-MT":
147
+ with ExitStack() as stack:
148
+ files = [stack.enter_context(
149
+ open(filepath, encoding="utf-8")) for filepath in filepaths]
150
+ readers = [csv.DictReader(
151
+ file, delimiter="\t", quoting=csv.QUOTE_NONE) for file in files]
152
+ for row_idx, rows in enumerate(zip(*readers)):
153
+ yield row_idx, {
154
+ "premise": {lang: row["premise"] for lang, row in zip(self.config.languages, rows)},
155
+ "hypothesis": {lang: row["hypo"] for lang, row in zip(self.config.languages, rows)},
156
+ "label": rows[0]["label"].replace("contradictory", "contradiction"),
157
+ }
158
+ else:
159
+ rows_per_pair_id = collections.defaultdict(list)
160
+ for filepath in filepaths:
161
+ with open(filepath, encoding="utf-8") as f:
162
+ reader = csv.DictReader(
163
+ f, delimiter="\t", quoting=csv.QUOTE_NONE)
164
+ for row in reader:
165
+ rows_per_pair_id[row["pairID"]].append(row)
166
+
167
+ for rows in rows_per_pair_id.values():
168
+ premise = {row["language"]: row["sentence1"]
169
+ for row in rows}
170
+ hypothesis = {row["language"]: row["sentence2"]
171
+ for row in rows}
172
+ yield rows[0]["pairID"], {
173
+ "premise": premise,
174
+ "hypothesis": hypothesis,
175
+ "label": rows[0]["gold_label"],
176
+ }
177
+ else:
178
+ if data_format == "XNLI-MT":
179
+ for file_idx, filepath in enumerate(filepaths):
180
+ file = open(filepath, encoding="utf-8")
181
+ reader = csv.DictReader(
182
+ file, delimiter="\t", quoting=csv.QUOTE_NONE)
183
+ for row_idx, row in enumerate(reader):
184
+ key = str(file_idx) + "_" + str(row_idx)
185
+ yield key, {
186
+ "premise": row["premise"],
187
+ "hypothesis": row["hypo"],
188
+ "label": row["label"].replace("contradictory", "contradiction"),
189
+ }
190
+ else:
191
+ for filepath in filepaths:
192
+ with open(filepath, encoding="utf-8") as f:
193
+ reader = csv.DictReader(
194
+ f, delimiter="\t", quoting=csv.QUOTE_NONE)
195
+ for row in reader:
196
+ if row["language"] == self.config.language:
197
+ yield row["pairID"], {
198
+ "premise": row["sentence1"],
199
+ "hypothesis": row["sentence2"],
200
+ "label": row["gold_label"],
201
+ }
.history/indicxnli_20220823220742.py ADDED
@@ -0,0 +1,201 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ @misc{https://doi.org/10.48550/arxiv.2204.08776,
30
+ doi = {10.48550/ARXIV.2204.08776},
31
+
32
+ url = {https://arxiv.org/abs/2204.08776},
33
+
34
+ author = {Aggarwal, Divyanshu and Gupta, Vivek and Kunchukuttan, Anoop},
35
+
36
+ keywords = {Computation and Language (cs.CL), Artificial Intelligence (cs.AI), FOS: Computer and information sciences, FOS: Computer and information sciences},
37
+
38
+ title = {IndicXNLI: Evaluating Multilingual Inference for Indian Languages},
39
+
40
+ publisher = {arXiv},
41
+
42
+ year = {2022},
43
+
44
+ copyright = {Creative Commons Attribution 4.0 International}
45
+ }
46
+ }"""
47
+
48
+ _DESCRIPTION = """\
49
+ IndicXNLI is a translated version of XNLI to 11 Indic Languages. As with XNLI, the goal is
50
+ to predict textual entailment (does sentence A imply/contradict/neither sentence
51
+ B) and is a classification task (given two sentences, predict one of three
52
+ labels).
53
+ """
54
+
55
+ _LANGUAGES = (
56
+ 'hi',
57
+ 'bn',
58
+ 'mr',
59
+ 'as',
60
+ 'ta',
61
+ 'te',
62
+ 'or',
63
+ 'ml',
64
+ 'pa',
65
+ 'gu',
66
+ 'kn'
67
+ )
68
+
69
+
70
+ class IndicxnliConfig(datasets.BuilderConfig):
71
+ """BuilderConfig for XNLI."""
72
+
73
+ def __init__(self, language: str, **kwargs):
74
+ """BuilderConfig for XNLI.
75
+
76
+ Args:
77
+ language: One of hi, bn, mr, as, ta, te, or, ml, pa, gu, kn
78
+ **kwargs: keyword arguments forwarded to super.
79
+ """
80
+ super(IndicxnliConfig, self).__init__(**kwargs)
81
+ self.language = language
82
+
83
+
84
+ class Indicxnli(datasets.GeneratorBasedBuilder):
85
+ """XNLI: The Cross-Lingual NLI Corpus. Version 1.0."""
86
+
87
+ VERSION = datasets.Version("1.1.0", "")
88
+ BUILDER_CONFIG_CLASS = IndicxnliConfig
89
+ BUILDER_CONFIGS = [
90
+ IndicxnliConfig(
91
+ name=lang,
92
+ language=lang,
93
+ version=datasets.Version("1.1.0", ""),
94
+ description=f"Plain text import of IndicXNLI for the {lang} language",
95
+ )
96
+ for lang in _LANGUAGES
97
+ ]
98
+
99
+ def _info(self):
100
+ features = datasets.Features(
101
+ {
102
+ "premise": datasets.Value("string"),
103
+ "hypothesis": datasets.Value("string"),
104
+ "label": datasets.ClassLabel(names=["entailment", "neutral", "contradiction"]),
105
+ }
106
+ )
107
+ return datasets.DatasetInfo(
108
+ description=_DESCRIPTION,
109
+ features=features,
110
+ # No default supervised_keys (as we have to pass both premise
111
+ # and hypothesis as input).
112
+ supervised_keys=None,
113
+ homepage="https://www.nyu.edu/projects/bowman/xnli/",
114
+ citation=_CITATION,
115
+ )
116
+
117
+ def _split_generators(self, dl_manager):
118
+ with open(f'forward/train/{language}', 'r')
119
+
120
+ return [
121
+ datasets.SplitGenerator(
122
+ name=datasets.Split.TRAIN,
123
+ gen_kwargs={
124
+ "filepaths": [
125
+ os.path.join(train_dir, f"multinli.train.{lang}.tsv") for lang in self.config.languages
126
+ ],
127
+ "data_format": "XNLI-MT",
128
+ },
129
+ ),
130
+ datasets.SplitGenerator(
131
+ name=datasets.Split.TEST,
132
+ gen_kwargs={"filepaths": [os.path.join(
133
+ testval_dir, "xnli.test.tsv")], "data_format": "XNLI"},
134
+ ),
135
+ datasets.SplitGenerator(
136
+ name=datasets.Split.VALIDATION,
137
+ gen_kwargs={"filepaths": [os.path.join(
138
+ testval_dir, "xnli.dev.tsv")], "data_format": "XNLI"},
139
+ ),
140
+ ]
141
+
142
+ def _generate_examples(self, data_format, filepaths):
143
+ """This function returns the examples in the raw (text) form."""
144
+
145
+ if self.config.language == "all_languages":
146
+ if data_format == "XNLI-MT":
147
+ with ExitStack() as stack:
148
+ files = [stack.enter_context(
149
+ open(filepath, encoding="utf-8")) for filepath in filepaths]
150
+ readers = [csv.DictReader(
151
+ file, delimiter="\t", quoting=csv.QUOTE_NONE) for file in files]
152
+ for row_idx, rows in enumerate(zip(*readers)):
153
+ yield row_idx, {
154
+ "premise": {lang: row["premise"] for lang, row in zip(self.config.languages, rows)},
155
+ "hypothesis": {lang: row["hypo"] for lang, row in zip(self.config.languages, rows)},
156
+ "label": rows[0]["label"].replace("contradictory", "contradiction"),
157
+ }
158
+ else:
159
+ rows_per_pair_id = collections.defaultdict(list)
160
+ for filepath in filepaths:
161
+ with open(filepath, encoding="utf-8") as f:
162
+ reader = csv.DictReader(
163
+ f, delimiter="\t", quoting=csv.QUOTE_NONE)
164
+ for row in reader:
165
+ rows_per_pair_id[row["pairID"]].append(row)
166
+
167
+ for rows in rows_per_pair_id.values():
168
+ premise = {row["language"]: row["sentence1"]
169
+ for row in rows}
170
+ hypothesis = {row["language"]: row["sentence2"]
171
+ for row in rows}
172
+ yield rows[0]["pairID"], {
173
+ "premise": premise,
174
+ "hypothesis": hypothesis,
175
+ "label": rows[0]["gold_label"],
176
+ }
177
+ else:
178
+ if data_format == "XNLI-MT":
179
+ for file_idx, filepath in enumerate(filepaths):
180
+ file = open(filepath, encoding="utf-8")
181
+ reader = csv.DictReader(
182
+ file, delimiter="\t", quoting=csv.QUOTE_NONE)
183
+ for row_idx, row in enumerate(reader):
184
+ key = str(file_idx) + "_" + str(row_idx)
185
+ yield key, {
186
+ "premise": row["premise"],
187
+ "hypothesis": row["hypo"],
188
+ "label": row["label"].replace("contradictory", "contradiction"),
189
+ }
190
+ else:
191
+ for filepath in filepaths:
192
+ with open(filepath, encoding="utf-8") as f:
193
+ reader = csv.DictReader(
194
+ f, delimiter="\t", quoting=csv.QUOTE_NONE)
195
+ for row in reader:
196
+ if row["language"] == self.config.language:
197
+ yield row["pairID"], {
198
+ "premise": row["sentence1"],
199
+ "hypothesis": row["sentence2"],
200
+ "label": row["gold_label"],
201
+ }
.history/indicxnli_20220823221124.py ADDED
@@ -0,0 +1,201 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ @misc{https://doi.org/10.48550/arxiv.2204.08776,
30
+ doi = {10.48550/ARXIV.2204.08776},
31
+
32
+ url = {https://arxiv.org/abs/2204.08776},
33
+
34
+ author = {Aggarwal, Divyanshu and Gupta, Vivek and Kunchukuttan, Anoop},
35
+
36
+ keywords = {Computation and Language (cs.CL), Artificial Intelligence (cs.AI), FOS: Computer and information sciences, FOS: Computer and information sciences},
37
+
38
+ title = {IndicXNLI: Evaluating Multilingual Inference for Indian Languages},
39
+
40
+ publisher = {arXiv},
41
+
42
+ year = {2022},
43
+
44
+ copyright = {Creative Commons Attribution 4.0 International}
45
+ }
46
+ }"""
47
+
48
+ _DESCRIPTION = """\
49
+ IndicXNLI is a translated version of XNLI to 11 Indic Languages. As with XNLI, the goal is
50
+ to predict textual entailment (does sentence A imply/contradict/neither sentence
51
+ B) and is a classification task (given two sentences, predict one of three
52
+ labels).
53
+ """
54
+
55
+ _LANGUAGES = (
56
+ 'hi',
57
+ 'bn',
58
+ 'mr',
59
+ 'as',
60
+ 'ta',
61
+ 'te',
62
+ 'or',
63
+ 'ml',
64
+ 'pa',
65
+ 'gu',
66
+ 'kn'
67
+ )
68
+
69
+
70
+ class IndicxnliConfig(datasets.BuilderConfig):
71
+ """BuilderConfig for XNLI."""
72
+
73
+ def __init__(self, language: str, **kwargs):
74
+ """BuilderConfig for XNLI.
75
+
76
+ Args:
77
+ language: One of hi, bn, mr, as, ta, te, or, ml, pa, gu, kn
78
+ **kwargs: keyword arguments forwarded to super.
79
+ """
80
+ super(IndicxnliConfig, self).__init__(**kwargs)
81
+ self.language = language
82
+
83
+
84
+ class Indicxnli(datasets.GeneratorBasedBuilder):
85
+ """XNLI: The Cross-Lingual NLI Corpus. Version 1.0."""
86
+
87
+ VERSION = datasets.Version("1.1.0", "")
88
+ BUILDER_CONFIG_CLASS = IndicxnliConfig
89
+ BUILDER_CONFIGS = [
90
+ IndicxnliConfig(
91
+ name=lang,
92
+ language=lang,
93
+ version=datasets.Version("1.1.0", ""),
94
+ description=f"Plain text import of IndicXNLI for the {lang} language",
95
+ )
96
+ for lang in _LANGUAGES
97
+ ]
98
+
99
+ def _info(self):
100
+ features = datasets.Features(
101
+ {
102
+ "premise": datasets.Value("string"),
103
+ "hypothesis": datasets.Value("string"),
104
+ "label": datasets.ClassLabel(names=["entailment", "neutral", "contradiction"]),
105
+ }
106
+ )
107
+ return datasets.DatasetInfo(
108
+ description=_DESCRIPTION,
109
+ features=features,
110
+ # No default supervised_keys (as we have to pass both premise
111
+ # and hypothesis as input).
112
+ supervised_keys=None,
113
+ homepage="https://www.nyu.edu/projects/bowman/xnli/",
114
+ citation=_CITATION,
115
+ )
116
+
117
+ def _split_generators(self, dl_manager):
118
+ with open(f'forward/train/{self.config.language}', 'r')
119
+
120
+ return [
121
+ datasets.SplitGenerator(
122
+ name=datasets.Split.TRAIN,
123
+ gen_kwargs={
124
+ "filepaths": [
125
+ os.path.join(train_dir, f"multinli.train.{lang}.tsv") for lang in self.config.languages
126
+ ],
127
+ "data_format": "XNLI-MT",
128
+ },
129
+ ),
130
+ datasets.SplitGenerator(
131
+ name=datasets.Split.TEST,
132
+ gen_kwargs={"filepaths": [os.path.join(
133
+ testval_dir, "xnli.test.tsv")], "data_format": "XNLI"},
134
+ ),
135
+ datasets.SplitGenerator(
136
+ name=datasets.Split.VALIDATION,
137
+ gen_kwargs={"filepaths": [os.path.join(
138
+ testval_dir, "xnli.dev.tsv")], "data_format": "XNLI"},
139
+ ),
140
+ ]
141
+
142
+ def _generate_examples(self, data_format, filepaths):
143
+ """This function returns the examples in the raw (text) form."""
144
+
145
+ if self.config.language == "all_languages":
146
+ if data_format == "XNLI-MT":
147
+ with ExitStack() as stack:
148
+ files = [stack.enter_context(
149
+ open(filepath, encoding="utf-8")) for filepath in filepaths]
150
+ readers = [csv.DictReader(
151
+ file, delimiter="\t", quoting=csv.QUOTE_NONE) for file in files]
152
+ for row_idx, rows in enumerate(zip(*readers)):
153
+ yield row_idx, {
154
+ "premise": {lang: row["premise"] for lang, row in zip(self.config.languages, rows)},
155
+ "hypothesis": {lang: row["hypo"] for lang, row in zip(self.config.languages, rows)},
156
+ "label": rows[0]["label"].replace("contradictory", "contradiction"),
157
+ }
158
+ else:
159
+ rows_per_pair_id = collections.defaultdict(list)
160
+ for filepath in filepaths:
161
+ with open(filepath, encoding="utf-8") as f:
162
+ reader = csv.DictReader(
163
+ f, delimiter="\t", quoting=csv.QUOTE_NONE)
164
+ for row in reader:
165
+ rows_per_pair_id[row["pairID"]].append(row)
166
+
167
+ for rows in rows_per_pair_id.values():
168
+ premise = {row["language"]: row["sentence1"]
169
+ for row in rows}
170
+ hypothesis = {row["language"]: row["sentence2"]
171
+ for row in rows}
172
+ yield rows[0]["pairID"], {
173
+ "premise": premise,
174
+ "hypothesis": hypothesis,
175
+ "label": rows[0]["gold_label"],
176
+ }
177
+ else:
178
+ if data_format == "XNLI-MT":
179
+ for file_idx, filepath in enumerate(filepaths):
180
+ file = open(filepath, encoding="utf-8")
181
+ reader = csv.DictReader(
182
+ file, delimiter="\t", quoting=csv.QUOTE_NONE)
183
+ for row_idx, row in enumerate(reader):
184
+ key = str(file_idx) + "_" + str(row_idx)
185
+ yield key, {
186
+ "premise": row["premise"],
187
+ "hypothesis": row["hypo"],
188
+ "label": row["label"].replace("contradictory", "contradiction"),
189
+ }
190
+ else:
191
+ for filepath in filepaths:
192
+ with open(filepath, encoding="utf-8") as f:
193
+ reader = csv.DictReader(
194
+ f, delimiter="\t", quoting=csv.QUOTE_NONE)
195
+ for row in reader:
196
+ if row["language"] == self.config.language:
197
+ yield row["pairID"], {
198
+ "premise": row["sentence1"],
199
+ "hypothesis": row["sentence2"],
200
+ "label": row["gold_label"],
201
+ }
.history/indicxnli_20220823221128.py ADDED
@@ -0,0 +1,202 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ @misc{https://doi.org/10.48550/arxiv.2204.08776,
30
+ doi = {10.48550/ARXIV.2204.08776},
31
+
32
+ url = {https://arxiv.org/abs/2204.08776},
33
+
34
+ author = {Aggarwal, Divyanshu and Gupta, Vivek and Kunchukuttan, Anoop},
35
+
36
+ keywords = {Computation and Language (cs.CL), Artificial Intelligence (cs.AI), FOS: Computer and information sciences, FOS: Computer and information sciences},
37
+
38
+ title = {IndicXNLI: Evaluating Multilingual Inference for Indian Languages},
39
+
40
+ publisher = {arXiv},
41
+
42
+ year = {2022},
43
+
44
+ copyright = {Creative Commons Attribution 4.0 International}
45
+ }
46
+ }"""
47
+
48
+ _DESCRIPTION = """\
49
+ IndicXNLI is a translated version of XNLI to 11 Indic Languages. As with XNLI, the goal is
50
+ to predict textual entailment (does sentence A imply/contradict/neither sentence
51
+ B) and is a classification task (given two sentences, predict one of three
52
+ labels).
53
+ """
54
+
55
+ _LANGUAGES = (
56
+ 'hi',
57
+ 'bn',
58
+ 'mr',
59
+ 'as',
60
+ 'ta',
61
+ 'te',
62
+ 'or',
63
+ 'ml',
64
+ 'pa',
65
+ 'gu',
66
+ 'kn'
67
+ )
68
+
69
+
70
+ class IndicxnliConfig(datasets.BuilderConfig):
71
+ """BuilderConfig for XNLI."""
72
+
73
+ def __init__(self, language: str, **kwargs):
74
+ """BuilderConfig for XNLI.
75
+
76
+ Args:
77
+ language: One of hi, bn, mr, as, ta, te, or, ml, pa, gu, kn
78
+ **kwargs: keyword arguments forwarded to super.
79
+ """
80
+ super(IndicxnliConfig, self).__init__(**kwargs)
81
+ self.language = language
82
+
83
+
84
+ class Indicxnli(datasets.GeneratorBasedBuilder):
85
+ """XNLI: The Cross-Lingual NLI Corpus. Version 1.0."""
86
+
87
+ VERSION = datasets.Version("1.1.0", "")
88
+ BUILDER_CONFIG_CLASS = IndicxnliConfig
89
+ BUILDER_CONFIGS = [
90
+ IndicxnliConfig(
91
+ name=lang,
92
+ language=lang,
93
+ version=datasets.Version("1.1.0", ""),
94
+ description=f"Plain text import of IndicXNLI for the {lang} language",
95
+ )
96
+ for lang in _LANGUAGES
97
+ ]
98
+
99
+ def _info(self):
100
+ features = datasets.Features(
101
+ {
102
+ "premise": datasets.Value("string"),
103
+ "hypothesis": datasets.Value("string"),
104
+ "label": datasets.ClassLabel(names=["entailment", "neutral", "contradiction"]),
105
+ }
106
+ )
107
+ return datasets.DatasetInfo(
108
+ description=_DESCRIPTION,
109
+ features=features,
110
+ # No default supervised_keys (as we have to pass both premise
111
+ # and hypothesis as input).
112
+ supervised_keys=None,
113
+ homepage="https://www.nyu.edu/projects/bowman/xnli/",
114
+ citation=_CITATION,
115
+ )
116
+
117
+ def _split_generators(self, dl_manager):
118
+ with open(f'forward/train/{self.config.language}', 'r') as f:
119
+
120
+
121
+ return [
122
+ datasets.SplitGenerator(
123
+ name=datasets.Split.TRAIN,
124
+ gen_kwargs={
125
+ "filepaths": [
126
+ os.path.join(train_dir, f"multinli.train.{lang}.tsv") for lang in self.config.languages
127
+ ],
128
+ "data_format": "XNLI-MT",
129
+ },
130
+ ),
131
+ datasets.SplitGenerator(
132
+ name=datasets.Split.TEST,
133
+ gen_kwargs={"filepaths": [os.path.join(
134
+ testval_dir, "xnli.test.tsv")], "data_format": "XNLI"},
135
+ ),
136
+ datasets.SplitGenerator(
137
+ name=datasets.Split.VALIDATION,
138
+ gen_kwargs={"filepaths": [os.path.join(
139
+ testval_dir, "xnli.dev.tsv")], "data_format": "XNLI"},
140
+ ),
141
+ ]
142
+
143
+ def _generate_examples(self, data_format, filepaths):
144
+ """This function returns the examples in the raw (text) form."""
145
+
146
+ if self.config.language == "all_languages":
147
+ if data_format == "XNLI-MT":
148
+ with ExitStack() as stack:
149
+ files = [stack.enter_context(
150
+ open(filepath, encoding="utf-8")) for filepath in filepaths]
151
+ readers = [csv.DictReader(
152
+ file, delimiter="\t", quoting=csv.QUOTE_NONE) for file in files]
153
+ for row_idx, rows in enumerate(zip(*readers)):
154
+ yield row_idx, {
155
+ "premise": {lang: row["premise"] for lang, row in zip(self.config.languages, rows)},
156
+ "hypothesis": {lang: row["hypo"] for lang, row in zip(self.config.languages, rows)},
157
+ "label": rows[0]["label"].replace("contradictory", "contradiction"),
158
+ }
159
+ else:
160
+ rows_per_pair_id = collections.defaultdict(list)
161
+ for filepath in filepaths:
162
+ with open(filepath, encoding="utf-8") as f:
163
+ reader = csv.DictReader(
164
+ f, delimiter="\t", quoting=csv.QUOTE_NONE)
165
+ for row in reader:
166
+ rows_per_pair_id[row["pairID"]].append(row)
167
+
168
+ for rows in rows_per_pair_id.values():
169
+ premise = {row["language"]: row["sentence1"]
170
+ for row in rows}
171
+ hypothesis = {row["language"]: row["sentence2"]
172
+ for row in rows}
173
+ yield rows[0]["pairID"], {
174
+ "premise": premise,
175
+ "hypothesis": hypothesis,
176
+ "label": rows[0]["gold_label"],
177
+ }
178
+ else:
179
+ if data_format == "XNLI-MT":
180
+ for file_idx, filepath in enumerate(filepaths):
181
+ file = open(filepath, encoding="utf-8")
182
+ reader = csv.DictReader(
183
+ file, delimiter="\t", quoting=csv.QUOTE_NONE)
184
+ for row_idx, row in enumerate(reader):
185
+ key = str(file_idx) + "_" + str(row_idx)
186
+ yield key, {
187
+ "premise": row["premise"],
188
+ "hypothesis": row["hypo"],
189
+ "label": row["label"].replace("contradictory", "contradiction"),
190
+ }
191
+ else:
192
+ for filepath in filepaths:
193
+ with open(filepath, encoding="utf-8") as f:
194
+ reader = csv.DictReader(
195
+ f, delimiter="\t", quoting=csv.QUOTE_NONE)
196
+ for row in reader:
197
+ if row["language"] == self.config.language:
198
+ yield row["pairID"], {
199
+ "premise": row["sentence1"],
200
+ "hypothesis": row["sentence2"],
201
+ "label": row["gold_label"],
202
+ }
.history/indicxnli_20220823221142.py ADDED
@@ -0,0 +1,202 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ @misc{https://doi.org/10.48550/arxiv.2204.08776,
30
+ doi = {10.48550/ARXIV.2204.08776},
31
+
32
+ url = {https://arxiv.org/abs/2204.08776},
33
+
34
+ author = {Aggarwal, Divyanshu and Gupta, Vivek and Kunchukuttan, Anoop},
35
+
36
+ keywords = {Computation and Language (cs.CL), Artificial Intelligence (cs.AI), FOS: Computer and information sciences, FOS: Computer and information sciences},
37
+
38
+ title = {IndicXNLI: Evaluating Multilingual Inference for Indian Languages},
39
+
40
+ publisher = {arXiv},
41
+
42
+ year = {2022},
43
+
44
+ copyright = {Creative Commons Attribution 4.0 International}
45
+ }
46
+ }"""
47
+
48
+ _DESCRIPTION = """\
49
+ IndicXNLI is a translated version of XNLI to 11 Indic Languages. As with XNLI, the goal is
50
+ to predict textual entailment (does sentence A imply/contradict/neither sentence
51
+ B) and is a classification task (given two sentences, predict one of three
52
+ labels).
53
+ """
54
+
55
+ _LANGUAGES = (
56
+ 'hi',
57
+ 'bn',
58
+ 'mr',
59
+ 'as',
60
+ 'ta',
61
+ 'te',
62
+ 'or',
63
+ 'ml',
64
+ 'pa',
65
+ 'gu',
66
+ 'kn'
67
+ )
68
+
69
+
70
+ class IndicxnliConfig(datasets.BuilderConfig):
71
+ """BuilderConfig for XNLI."""
72
+
73
+ def __init__(self, language: str, **kwargs):
74
+ """BuilderConfig for XNLI.
75
+
76
+ Args:
77
+ language: One of hi, bn, mr, as, ta, te, or, ml, pa, gu, kn
78
+ **kwargs: keyword arguments forwarded to super.
79
+ """
80
+ super(IndicxnliConfig, self).__init__(**kwargs)
81
+ self.language = language
82
+
83
+
84
+ class Indicxnli(datasets.GeneratorBasedBuilder):
85
+ """XNLI: The Cross-Lingual NLI Corpus. Version 1.0."""
86
+
87
+ VERSION = datasets.Version("1.1.0", "")
88
+ BUILDER_CONFIG_CLASS = IndicxnliConfig
89
+ BUILDER_CONFIGS = [
90
+ IndicxnliConfig(
91
+ name=lang,
92
+ language=lang,
93
+ version=datasets.Version("1.1.0", ""),
94
+ description=f"Plain text import of IndicXNLI for the {lang} language",
95
+ )
96
+ for lang in _LANGUAGES
97
+ ]
98
+
99
+ def _info(self):
100
+ features = datasets.Features(
101
+ {
102
+ "premise": datasets.Value("string"),
103
+ "hypothesis": datasets.Value("string"),
104
+ "label": datasets.ClassLabel(names=["entailment", "neutral", "contradiction"]),
105
+ }
106
+ )
107
+ return datasets.DatasetInfo(
108
+ description=_DESCRIPTION,
109
+ features=features,
110
+ # No default supervised_keys (as we have to pass both premise
111
+ # and hypothesis as input).
112
+ supervised_keys=None,
113
+ homepage="https://www.nyu.edu/projects/bowman/xnli/",
114
+ citation=_CITATION,
115
+ )
116
+
117
+ def _split_generators(self, dl_manager):
118
+ with open(f'forward/train/xnli_{self.config.language}', 'r') as f:
119
+
120
+
121
+ return [
122
+ datasets.SplitGenerator(
123
+ name=datasets.Split.TRAIN,
124
+ gen_kwargs={
125
+ "filepaths": [
126
+ os.path.join(train_dir, f"multinli.train.{lang}.tsv") for lang in self.config.languages
127
+ ],
128
+ "data_format": "XNLI-MT",
129
+ },
130
+ ),
131
+ datasets.SplitGenerator(
132
+ name=datasets.Split.TEST,
133
+ gen_kwargs={"filepaths": [os.path.join(
134
+ testval_dir, "xnli.test.tsv")], "data_format": "XNLI"},
135
+ ),
136
+ datasets.SplitGenerator(
137
+ name=datasets.Split.VALIDATION,
138
+ gen_kwargs={"filepaths": [os.path.join(
139
+ testval_dir, "xnli.dev.tsv")], "data_format": "XNLI"},
140
+ ),
141
+ ]
142
+
143
+ def _generate_examples(self, data_format, filepaths):
144
+ """This function returns the examples in the raw (text) form."""
145
+
146
+ if self.config.language == "all_languages":
147
+ if data_format == "XNLI-MT":
148
+ with ExitStack() as stack:
149
+ files = [stack.enter_context(
150
+ open(filepath, encoding="utf-8")) for filepath in filepaths]
151
+ readers = [csv.DictReader(
152
+ file, delimiter="\t", quoting=csv.QUOTE_NONE) for file in files]
153
+ for row_idx, rows in enumerate(zip(*readers)):
154
+ yield row_idx, {
155
+ "premise": {lang: row["premise"] for lang, row in zip(self.config.languages, rows)},
156
+ "hypothesis": {lang: row["hypo"] for lang, row in zip(self.config.languages, rows)},
157
+ "label": rows[0]["label"].replace("contradictory", "contradiction"),
158
+ }
159
+ else:
160
+ rows_per_pair_id = collections.defaultdict(list)
161
+ for filepath in filepaths:
162
+ with open(filepath, encoding="utf-8") as f:
163
+ reader = csv.DictReader(
164
+ f, delimiter="\t", quoting=csv.QUOTE_NONE)
165
+ for row in reader:
166
+ rows_per_pair_id[row["pairID"]].append(row)
167
+
168
+ for rows in rows_per_pair_id.values():
169
+ premise = {row["language"]: row["sentence1"]
170
+ for row in rows}
171
+ hypothesis = {row["language"]: row["sentence2"]
172
+ for row in rows}
173
+ yield rows[0]["pairID"], {
174
+ "premise": premise,
175
+ "hypothesis": hypothesis,
176
+ "label": rows[0]["gold_label"],
177
+ }
178
+ else:
179
+ if data_format == "XNLI-MT":
180
+ for file_idx, filepath in enumerate(filepaths):
181
+ file = open(filepath, encoding="utf-8")
182
+ reader = csv.DictReader(
183
+ file, delimiter="\t", quoting=csv.QUOTE_NONE)
184
+ for row_idx, row in enumerate(reader):
185
+ key = str(file_idx) + "_" + str(row_idx)
186
+ yield key, {
187
+ "premise": row["premise"],
188
+ "hypothesis": row["hypo"],
189
+ "label": row["label"].replace("contradictory", "contradiction"),
190
+ }
191
+ else:
192
+ for filepath in filepaths:
193
+ with open(filepath, encoding="utf-8") as f:
194
+ reader = csv.DictReader(
195
+ f, delimiter="\t", quoting=csv.QUOTE_NONE)
196
+ for row in reader:
197
+ if row["language"] == self.config.language:
198
+ yield row["pairID"], {
199
+ "premise": row["sentence1"],
200
+ "hypothesis": row["sentence2"],
201
+ "label": row["gold_label"],
202
+ }
.history/indicxnli_20220823221147.py ADDED
@@ -0,0 +1,202 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ @misc{https://doi.org/10.48550/arxiv.2204.08776,
30
+ doi = {10.48550/ARXIV.2204.08776},
31
+
32
+ url = {https://arxiv.org/abs/2204.08776},
33
+
34
+ author = {Aggarwal, Divyanshu and Gupta, Vivek and Kunchukuttan, Anoop},
35
+
36
+ keywords = {Computation and Language (cs.CL), Artificial Intelligence (cs.AI), FOS: Computer and information sciences, FOS: Computer and information sciences},
37
+
38
+ title = {IndicXNLI: Evaluating Multilingual Inference for Indian Languages},
39
+
40
+ publisher = {arXiv},
41
+
42
+ year = {2022},
43
+
44
+ copyright = {Creative Commons Attribution 4.0 International}
45
+ }
46
+ }"""
47
+
48
+ _DESCRIPTION = """\
49
+ IndicXNLI is a translated version of XNLI to 11 Indic Languages. As with XNLI, the goal is
50
+ to predict textual entailment (does sentence A imply/contradict/neither sentence
51
+ B) and is a classification task (given two sentences, predict one of three
52
+ labels).
53
+ """
54
+
55
+ _LANGUAGES = (
56
+ 'hi',
57
+ 'bn',
58
+ 'mr',
59
+ 'as',
60
+ 'ta',
61
+ 'te',
62
+ 'or',
63
+ 'ml',
64
+ 'pa',
65
+ 'gu',
66
+ 'kn'
67
+ )
68
+
69
+
70
+ class IndicxnliConfig(datasets.BuilderConfig):
71
+ """BuilderConfig for XNLI."""
72
+
73
+ def __init__(self, language: str, **kwargs):
74
+ """BuilderConfig for XNLI.
75
+
76
+ Args:
77
+ language: One of hi, bn, mr, as, ta, te, or, ml, pa, gu, kn
78
+ **kwargs: keyword arguments forwarded to super.
79
+ """
80
+ super(IndicxnliConfig, self).__init__(**kwargs)
81
+ self.language = language
82
+
83
+
84
+ class Indicxnli(datasets.GeneratorBasedBuilder):
85
+ """XNLI: The Cross-Lingual NLI Corpus. Version 1.0."""
86
+
87
+ VERSION = datasets.Version("1.1.0", "")
88
+ BUILDER_CONFIG_CLASS = IndicxnliConfig
89
+ BUILDER_CONFIGS = [
90
+ IndicxnliConfig(
91
+ name=lang,
92
+ language=lang,
93
+ version=datasets.Version("1.1.0", ""),
94
+ description=f"Plain text import of IndicXNLI for the {lang} language",
95
+ )
96
+ for lang in _LANGUAGES
97
+ ]
98
+
99
+ def _info(self):
100
+ features = datasets.Features(
101
+ {
102
+ "premise": datasets.Value("string"),
103
+ "hypothesis": datasets.Value("string"),
104
+ "label": datasets.ClassLabel(names=["entailment", "neutral", "contradiction"]),
105
+ }
106
+ )
107
+ return datasets.DatasetInfo(
108
+ description=_DESCRIPTION,
109
+ features=features,
110
+ # No default supervised_keys (as we have to pass both premise
111
+ # and hypothesis as input).
112
+ supervised_keys=None,
113
+ homepage="https://www.nyu.edu/projects/bowman/xnli/",
114
+ citation=_CITATION,
115
+ )
116
+
117
+ def _split_generators(self, dl_manager):
118
+ with open(f'forward/train/xnli_{self.config.language}.json', 'r') as f:
119
+
120
+
121
+ return [
122
+ datasets.SplitGenerator(
123
+ name=datasets.Split.TRAIN,
124
+ gen_kwargs={
125
+ "filepaths": [
126
+ os.path.join(train_dir, f"multinli.train.{lang}.tsv") for lang in self.config.languages
127
+ ],
128
+ "data_format": "XNLI-MT",
129
+ },
130
+ ),
131
+ datasets.SplitGenerator(
132
+ name=datasets.Split.TEST,
133
+ gen_kwargs={"filepaths": [os.path.join(
134
+ testval_dir, "xnli.test.tsv")], "data_format": "XNLI"},
135
+ ),
136
+ datasets.SplitGenerator(
137
+ name=datasets.Split.VALIDATION,
138
+ gen_kwargs={"filepaths": [os.path.join(
139
+ testval_dir, "xnli.dev.tsv")], "data_format": "XNLI"},
140
+ ),
141
+ ]
142
+
143
+ def _generate_examples(self, data_format, filepaths):
144
+ """This function returns the examples in the raw (text) form."""
145
+
146
+ if self.config.language == "all_languages":
147
+ if data_format == "XNLI-MT":
148
+ with ExitStack() as stack:
149
+ files = [stack.enter_context(
150
+ open(filepath, encoding="utf-8")) for filepath in filepaths]
151
+ readers = [csv.DictReader(
152
+ file, delimiter="\t", quoting=csv.QUOTE_NONE) for file in files]
153
+ for row_idx, rows in enumerate(zip(*readers)):
154
+ yield row_idx, {
155
+ "premise": {lang: row["premise"] for lang, row in zip(self.config.languages, rows)},
156
+ "hypothesis": {lang: row["hypo"] for lang, row in zip(self.config.languages, rows)},
157
+ "label": rows[0]["label"].replace("contradictory", "contradiction"),
158
+ }
159
+ else:
160
+ rows_per_pair_id = collections.defaultdict(list)
161
+ for filepath in filepaths:
162
+ with open(filepath, encoding="utf-8") as f:
163
+ reader = csv.DictReader(
164
+ f, delimiter="\t", quoting=csv.QUOTE_NONE)
165
+ for row in reader:
166
+ rows_per_pair_id[row["pairID"]].append(row)
167
+
168
+ for rows in rows_per_pair_id.values():
169
+ premise = {row["language"]: row["sentence1"]
170
+ for row in rows}
171
+ hypothesis = {row["language"]: row["sentence2"]
172
+ for row in rows}
173
+ yield rows[0]["pairID"], {
174
+ "premise": premise,
175
+ "hypothesis": hypothesis,
176
+ "label": rows[0]["gold_label"],
177
+ }
178
+ else:
179
+ if data_format == "XNLI-MT":
180
+ for file_idx, filepath in enumerate(filepaths):
181
+ file = open(filepath, encoding="utf-8")
182
+ reader = csv.DictReader(
183
+ file, delimiter="\t", quoting=csv.QUOTE_NONE)
184
+ for row_idx, row in enumerate(reader):
185
+ key = str(file_idx) + "_" + str(row_idx)
186
+ yield key, {
187
+ "premise": row["premise"],
188
+ "hypothesis": row["hypo"],
189
+ "label": row["label"].replace("contradictory", "contradiction"),
190
+ }
191
+ else:
192
+ for filepath in filepaths:
193
+ with open(filepath, encoding="utf-8") as f:
194
+ reader = csv.DictReader(
195
+ f, delimiter="\t", quoting=csv.QUOTE_NONE)
196
+ for row in reader:
197
+ if row["language"] == self.config.language:
198
+ yield row["pairID"], {
199
+ "premise": row["sentence1"],
200
+ "hypothesis": row["sentence2"],
201
+ "label": row["gold_label"],
202
+ }
.history/indicxnli_20220823221200.py ADDED
@@ -0,0 +1,203 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ @misc{https://doi.org/10.48550/arxiv.2204.08776,
30
+ doi = {10.48550/ARXIV.2204.08776},
31
+
32
+ url = {https://arxiv.org/abs/2204.08776},
33
+
34
+ author = {Aggarwal, Divyanshu and Gupta, Vivek and Kunchukuttan, Anoop},
35
+
36
+ keywords = {Computation and Language (cs.CL), Artificial Intelligence (cs.AI), FOS: Computer and information sciences, FOS: Computer and information sciences},
37
+
38
+ title = {IndicXNLI: Evaluating Multilingual Inference for Indian Languages},
39
+
40
+ publisher = {arXiv},
41
+
42
+ year = {2022},
43
+
44
+ copyright = {Creative Commons Attribution 4.0 International}
45
+ }
46
+ }"""
47
+
48
+ _DESCRIPTION = """\
49
+ IndicXNLI is a translated version of XNLI to 11 Indic Languages. As with XNLI, the goal is
50
+ to predict textual entailment (does sentence A imply/contradict/neither sentence
51
+ B) and is a classification task (given two sentences, predict one of three
52
+ labels).
53
+ """
54
+
55
+ _LANGUAGES = (
56
+ 'hi',
57
+ 'bn',
58
+ 'mr',
59
+ 'as',
60
+ 'ta',
61
+ 'te',
62
+ 'or',
63
+ 'ml',
64
+ 'pa',
65
+ 'gu',
66
+ 'kn'
67
+ )
68
+
69
+
70
+ class IndicxnliConfig(datasets.BuilderConfig):
71
+ """BuilderConfig for XNLI."""
72
+
73
+ def __init__(self, language: str, **kwargs):
74
+ """BuilderConfig for XNLI.
75
+
76
+ Args:
77
+ language: One of hi, bn, mr, as, ta, te, or, ml, pa, gu, kn
78
+ **kwargs: keyword arguments forwarded to super.
79
+ """
80
+ super(IndicxnliConfig, self).__init__(**kwargs)
81
+ self.language = language
82
+
83
+
84
+ class Indicxnli(datasets.GeneratorBasedBuilder):
85
+ """XNLI: The Cross-Lingual NLI Corpus. Version 1.0."""
86
+
87
+ VERSION = datasets.Version("1.1.0", "")
88
+ BUILDER_CONFIG_CLASS = IndicxnliConfig
89
+ BUILDER_CONFIGS = [
90
+ IndicxnliConfig(
91
+ name=lang,
92
+ language=lang,
93
+ version=datasets.Version("1.1.0", ""),
94
+ description=f"Plain text import of IndicXNLI for the {lang} language",
95
+ )
96
+ for lang in _LANGUAGES
97
+ ]
98
+
99
+ def _info(self):
100
+ features = datasets.Features(
101
+ {
102
+ "premise": datasets.Value("string"),
103
+ "hypothesis": datasets.Value("string"),
104
+ "label": datasets.ClassLabel(names=["entailment", "neutral", "contradiction"]),
105
+ }
106
+ )
107
+ return datasets.DatasetInfo(
108
+ description=_DESCRIPTION,
109
+ features=features,
110
+ # No default supervised_keys (as we have to pass both premise
111
+ # and hypothesis as input).
112
+ supervised_keys=None,
113
+ homepage="https://www.nyu.edu/projects/bowman/xnli/",
114
+ citation=_CITATION,
115
+ )
116
+
117
+ def _split_generators(self, dl_manager):
118
+ with open(f'forward/train/xnli_{self.config.language}.json', 'r') as f:
119
+
120
+
121
+
122
+ return [
123
+ datasets.SplitGenerator(
124
+ name=datasets.Split.TRAIN,
125
+ gen_kwargs={
126
+ "filepaths": [
127
+ os.path.join(train_dir, f"multinli.train.{lang}.tsv") for lang in self.config.languages
128
+ ],
129
+ "data_format": "XNLI-MT",
130
+ },
131
+ ),
132
+ datasets.SplitGenerator(
133
+ name=datasets.Split.TEST,
134
+ gen_kwargs={"filepaths": [os.path.join(
135
+ testval_dir, "xnli.test.tsv")], "data_format": "XNLI"},
136
+ ),
137
+ datasets.SplitGenerator(
138
+ name=datasets.Split.VALIDATION,
139
+ gen_kwargs={"filepaths": [os.path.join(
140
+ testval_dir, "xnli.dev.tsv")], "data_format": "XNLI"},
141
+ ),
142
+ ]
143
+
144
+ def _generate_examples(self, data_format, filepaths):
145
+ """This function returns the examples in the raw (text) form."""
146
+
147
+ if self.config.language == "all_languages":
148
+ if data_format == "XNLI-MT":
149
+ with ExitStack() as stack:
150
+ files = [stack.enter_context(
151
+ open(filepath, encoding="utf-8")) for filepath in filepaths]
152
+ readers = [csv.DictReader(
153
+ file, delimiter="\t", quoting=csv.QUOTE_NONE) for file in files]
154
+ for row_idx, rows in enumerate(zip(*readers)):
155
+ yield row_idx, {
156
+ "premise": {lang: row["premise"] for lang, row in zip(self.config.languages, rows)},
157
+ "hypothesis": {lang: row["hypo"] for lang, row in zip(self.config.languages, rows)},
158
+ "label": rows[0]["label"].replace("contradictory", "contradiction"),
159
+ }
160
+ else:
161
+ rows_per_pair_id = collections.defaultdict(list)
162
+ for filepath in filepaths:
163
+ with open(filepath, encoding="utf-8") as f:
164
+ reader = csv.DictReader(
165
+ f, delimiter="\t", quoting=csv.QUOTE_NONE)
166
+ for row in reader:
167
+ rows_per_pair_id[row["pairID"]].append(row)
168
+
169
+ for rows in rows_per_pair_id.values():
170
+ premise = {row["language"]: row["sentence1"]
171
+ for row in rows}
172
+ hypothesis = {row["language"]: row["sentence2"]
173
+ for row in rows}
174
+ yield rows[0]["pairID"], {
175
+ "premise": premise,
176
+ "hypothesis": hypothesis,
177
+ "label": rows[0]["gold_label"],
178
+ }
179
+ else:
180
+ if data_format == "XNLI-MT":
181
+ for file_idx, filepath in enumerate(filepaths):
182
+ file = open(filepath, encoding="utf-8")
183
+ reader = csv.DictReader(
184
+ file, delimiter="\t", quoting=csv.QUOTE_NONE)
185
+ for row_idx, row in enumerate(reader):
186
+ key = str(file_idx) + "_" + str(row_idx)
187
+ yield key, {
188
+ "premise": row["premise"],
189
+ "hypothesis": row["hypo"],
190
+ "label": row["label"].replace("contradictory", "contradiction"),
191
+ }
192
+ else:
193
+ for filepath in filepaths:
194
+ with open(filepath, encoding="utf-8") as f:
195
+ reader = csv.DictReader(
196
+ f, delimiter="\t", quoting=csv.QUOTE_NONE)
197
+ for row in reader:
198
+ if row["language"] == self.config.language:
199
+ yield row["pairID"], {
200
+ "premise": row["sentence1"],
201
+ "hypothesis": row["sentence2"],
202
+ "label": row["gold_label"],
203
+ }
.history/indicxnli_20220823221210.py ADDED
@@ -0,0 +1,203 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ @misc{https://doi.org/10.48550/arxiv.2204.08776,
30
+ doi = {10.48550/ARXIV.2204.08776},
31
+
32
+ url = {https://arxiv.org/abs/2204.08776},
33
+
34
+ author = {Aggarwal, Divyanshu and Gupta, Vivek and Kunchukuttan, Anoop},
35
+
36
+ keywords = {Computation and Language (cs.CL), Artificial Intelligence (cs.AI), FOS: Computer and information sciences, FOS: Computer and information sciences},
37
+
38
+ title = {IndicXNLI: Evaluating Multilingual Inference for Indian Languages},
39
+
40
+ publisher = {arXiv},
41
+
42
+ year = {2022},
43
+
44
+ copyright = {Creative Commons Attribution 4.0 International}
45
+ }
46
+ }"""
47
+
48
+ _DESCRIPTION = """\
49
+ IndicXNLI is a translated version of XNLI to 11 Indic Languages. As with XNLI, the goal is
50
+ to predict textual entailment (does sentence A imply/contradict/neither sentence
51
+ B) and is a classification task (given two sentences, predict one of three
52
+ labels).
53
+ """
54
+
55
+ _LANGUAGES = (
56
+ 'hi',
57
+ 'bn',
58
+ 'mr',
59
+ 'as',
60
+ 'ta',
61
+ 'te',
62
+ 'or',
63
+ 'ml',
64
+ 'pa',
65
+ 'gu',
66
+ 'kn'
67
+ )
68
+
69
+
70
+ class IndicxnliConfig(datasets.BuilderConfig):
71
+ """BuilderConfig for XNLI."""
72
+
73
+ def __init__(self, language: str, **kwargs):
74
+ """BuilderConfig for XNLI.
75
+
76
+ Args:
77
+ language: One of hi, bn, mr, as, ta, te, or, ml, pa, gu, kn
78
+ **kwargs: keyword arguments forwarded to super.
79
+ """
80
+ super(IndicxnliConfig, self).__init__(**kwargs)
81
+ self.language = language
82
+
83
+
84
+ class Indicxnli(datasets.GeneratorBasedBuilder):
85
+ """XNLI: The Cross-Lingual NLI Corpus. Version 1.0."""
86
+
87
+ VERSION = datasets.Version("1.1.0", "")
88
+ BUILDER_CONFIG_CLASS = IndicxnliConfig
89
+ BUILDER_CONFIGS = [
90
+ IndicxnliConfig(
91
+ name=lang,
92
+ language=lang,
93
+ version=datasets.Version("1.1.0", ""),
94
+ description=f"Plain text import of IndicXNLI for the {lang} language",
95
+ )
96
+ for lang in _LANGUAGES
97
+ ]
98
+
99
+ def _info(self):
100
+ features = datasets.Features(
101
+ {
102
+ "premise": datasets.Value("string"),
103
+ "hypothesis": datasets.Value("string"),
104
+ "label": datasets.ClassLabel(names=["entailment", "neutral", "contradiction"]),
105
+ }
106
+ )
107
+ return datasets.DatasetInfo(
108
+ description=_DESCRIPTION,
109
+ features=features,
110
+ # No default supervised_keys (as we have to pass both premise
111
+ # and hypothesis as input).
112
+ supervised_keys=None,
113
+ homepage="https://www.nyu.edu/projects/bowman/xnli/",
114
+ citation=_CITATION,
115
+ )
116
+
117
+ def _split_generators(self, dl_manager):
118
+ train_path = f'forward/train/xnli_{self.config.language}.json', 'r') as f:
119
+
120
+
121
+
122
+ return [
123
+ datasets.SplitGenerator(
124
+ name=datasets.Split.TRAIN,
125
+ gen_kwargs={
126
+ "filepaths": [
127
+ os.path.join(train_dir, f"multinli.train.{lang}.tsv") for lang in self.config.languages
128
+ ],
129
+ "data_format": "XNLI-MT",
130
+ },
131
+ ),
132
+ datasets.SplitGenerator(
133
+ name=datasets.Split.TEST,
134
+ gen_kwargs={"filepaths": [os.path.join(
135
+ testval_dir, "xnli.test.tsv")], "data_format": "XNLI"},
136
+ ),
137
+ datasets.SplitGenerator(
138
+ name=datasets.Split.VALIDATION,
139
+ gen_kwargs={"filepaths": [os.path.join(
140
+ testval_dir, "xnli.dev.tsv")], "data_format": "XNLI"},
141
+ ),
142
+ ]
143
+
144
+ def _generate_examples(self, data_format, filepaths):
145
+ """This function returns the examples in the raw (text) form."""
146
+
147
+ if self.config.language == "all_languages":
148
+ if data_format == "XNLI-MT":
149
+ with ExitStack() as stack:
150
+ files = [stack.enter_context(
151
+ open(filepath, encoding="utf-8")) for filepath in filepaths]
152
+ readers = [csv.DictReader(
153
+ file, delimiter="\t", quoting=csv.QUOTE_NONE) for file in files]
154
+ for row_idx, rows in enumerate(zip(*readers)):
155
+ yield row_idx, {
156
+ "premise": {lang: row["premise"] for lang, row in zip(self.config.languages, rows)},
157
+ "hypothesis": {lang: row["hypo"] for lang, row in zip(self.config.languages, rows)},
158
+ "label": rows[0]["label"].replace("contradictory", "contradiction"),
159
+ }
160
+ else:
161
+ rows_per_pair_id = collections.defaultdict(list)
162
+ for filepath in filepaths:
163
+ with open(filepath, encoding="utf-8") as f:
164
+ reader = csv.DictReader(
165
+ f, delimiter="\t", quoting=csv.QUOTE_NONE)
166
+ for row in reader:
167
+ rows_per_pair_id[row["pairID"]].append(row)
168
+
169
+ for rows in rows_per_pair_id.values():
170
+ premise = {row["language"]: row["sentence1"]
171
+ for row in rows}
172
+ hypothesis = {row["language"]: row["sentence2"]
173
+ for row in rows}
174
+ yield rows[0]["pairID"], {
175
+ "premise": premise,
176
+ "hypothesis": hypothesis,
177
+ "label": rows[0]["gold_label"],
178
+ }
179
+ else:
180
+ if data_format == "XNLI-MT":
181
+ for file_idx, filepath in enumerate(filepaths):
182
+ file = open(filepath, encoding="utf-8")
183
+ reader = csv.DictReader(
184
+ file, delimiter="\t", quoting=csv.QUOTE_NONE)
185
+ for row_idx, row in enumerate(reader):
186
+ key = str(file_idx) + "_" + str(row_idx)
187
+ yield key, {
188
+ "premise": row["premise"],
189
+ "hypothesis": row["hypo"],
190
+ "label": row["label"].replace("contradictory", "contradiction"),
191
+ }
192
+ else:
193
+ for filepath in filepaths:
194
+ with open(filepath, encoding="utf-8") as f:
195
+ reader = csv.DictReader(
196
+ f, delimiter="\t", quoting=csv.QUOTE_NONE)
197
+ for row in reader:
198
+ if row["language"] == self.config.language:
199
+ yield row["pairID"], {
200
+ "premise": row["sentence1"],
201
+ "hypothesis": row["sentence2"],
202
+ "label": row["gold_label"],
203
+ }
.history/indicxnli_20220823221213.py ADDED
@@ -0,0 +1,203 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ @misc{https://doi.org/10.48550/arxiv.2204.08776,
30
+ doi = {10.48550/ARXIV.2204.08776},
31
+
32
+ url = {https://arxiv.org/abs/2204.08776},
33
+
34
+ author = {Aggarwal, Divyanshu and Gupta, Vivek and Kunchukuttan, Anoop},
35
+
36
+ keywords = {Computation and Language (cs.CL), Artificial Intelligence (cs.AI), FOS: Computer and information sciences, FOS: Computer and information sciences},
37
+
38
+ title = {IndicXNLI: Evaluating Multilingual Inference for Indian Languages},
39
+
40
+ publisher = {arXiv},
41
+
42
+ year = {2022},
43
+
44
+ copyright = {Creative Commons Attribution 4.0 International}
45
+ }
46
+ }"""
47
+
48
+ _DESCRIPTION = """\
49
+ IndicXNLI is a translated version of XNLI to 11 Indic Languages. As with XNLI, the goal is
50
+ to predict textual entailment (does sentence A imply/contradict/neither sentence
51
+ B) and is a classification task (given two sentences, predict one of three
52
+ labels).
53
+ """
54
+
55
+ _LANGUAGES = (
56
+ 'hi',
57
+ 'bn',
58
+ 'mr',
59
+ 'as',
60
+ 'ta',
61
+ 'te',
62
+ 'or',
63
+ 'ml',
64
+ 'pa',
65
+ 'gu',
66
+ 'kn'
67
+ )
68
+
69
+
70
+ class IndicxnliConfig(datasets.BuilderConfig):
71
+ """BuilderConfig for XNLI."""
72
+
73
+ def __init__(self, language: str, **kwargs):
74
+ """BuilderConfig for XNLI.
75
+
76
+ Args:
77
+ language: One of hi, bn, mr, as, ta, te, or, ml, pa, gu, kn
78
+ **kwargs: keyword arguments forwarded to super.
79
+ """
80
+ super(IndicxnliConfig, self).__init__(**kwargs)
81
+ self.language = language
82
+
83
+
84
+ class Indicxnli(datasets.GeneratorBasedBuilder):
85
+ """XNLI: The Cross-Lingual NLI Corpus. Version 1.0."""
86
+
87
+ VERSION = datasets.Version("1.1.0", "")
88
+ BUILDER_CONFIG_CLASS = IndicxnliConfig
89
+ BUILDER_CONFIGS = [
90
+ IndicxnliConfig(
91
+ name=lang,
92
+ language=lang,
93
+ version=datasets.Version("1.1.0", ""),
94
+ description=f"Plain text import of IndicXNLI for the {lang} language",
95
+ )
96
+ for lang in _LANGUAGES
97
+ ]
98
+
99
+ def _info(self):
100
+ features = datasets.Features(
101
+ {
102
+ "premise": datasets.Value("string"),
103
+ "hypothesis": datasets.Value("string"),
104
+ "label": datasets.ClassLabel(names=["entailment", "neutral", "contradiction"]),
105
+ }
106
+ )
107
+ return datasets.DatasetInfo(
108
+ description=_DESCRIPTION,
109
+ features=features,
110
+ # No default supervised_keys (as we have to pass both premise
111
+ # and hypothesis as input).
112
+ supervised_keys=None,
113
+ homepage="https://www.nyu.edu/projects/bowman/xnli/",
114
+ citation=_CITATION,
115
+ )
116
+
117
+ def _split_generators(self, dl_manager):
118
+ train_path = f'forward/train/xnli_{self.config.language}.json'
119
+
120
+
121
+
122
+ return [
123
+ datasets.SplitGenerator(
124
+ name=datasets.Split.TRAIN,
125
+ gen_kwargs={
126
+ "filepaths": [
127
+ os.path.join(train_dir, f"multinli.train.{lang}.tsv") for lang in self.config.languages
128
+ ],
129
+ "data_format": "XNLI-MT",
130
+ },
131
+ ),
132
+ datasets.SplitGenerator(
133
+ name=datasets.Split.TEST,
134
+ gen_kwargs={"filepaths": [os.path.join(
135
+ testval_dir, "xnli.test.tsv")], "data_format": "XNLI"},
136
+ ),
137
+ datasets.SplitGenerator(
138
+ name=datasets.Split.VALIDATION,
139
+ gen_kwargs={"filepaths": [os.path.join(
140
+ testval_dir, "xnli.dev.tsv")], "data_format": "XNLI"},
141
+ ),
142
+ ]
143
+
144
+ def _generate_examples(self, data_format, filepaths):
145
+ """This function returns the examples in the raw (text) form."""
146
+
147
+ if self.config.language == "all_languages":
148
+ if data_format == "XNLI-MT":
149
+ with ExitStack() as stack:
150
+ files = [stack.enter_context(
151
+ open(filepath, encoding="utf-8")) for filepath in filepaths]
152
+ readers = [csv.DictReader(
153
+ file, delimiter="\t", quoting=csv.QUOTE_NONE) for file in files]
154
+ for row_idx, rows in enumerate(zip(*readers)):
155
+ yield row_idx, {
156
+ "premise": {lang: row["premise"] for lang, row in zip(self.config.languages, rows)},
157
+ "hypothesis": {lang: row["hypo"] for lang, row in zip(self.config.languages, rows)},
158
+ "label": rows[0]["label"].replace("contradictory", "contradiction"),
159
+ }
160
+ else:
161
+ rows_per_pair_id = collections.defaultdict(list)
162
+ for filepath in filepaths:
163
+ with open(filepath, encoding="utf-8") as f:
164
+ reader = csv.DictReader(
165
+ f, delimiter="\t", quoting=csv.QUOTE_NONE)
166
+ for row in reader:
167
+ rows_per_pair_id[row["pairID"]].append(row)
168
+
169
+ for rows in rows_per_pair_id.values():
170
+ premise = {row["language"]: row["sentence1"]
171
+ for row in rows}
172
+ hypothesis = {row["language"]: row["sentence2"]
173
+ for row in rows}
174
+ yield rows[0]["pairID"], {
175
+ "premise": premise,
176
+ "hypothesis": hypothesis,
177
+ "label": rows[0]["gold_label"],
178
+ }
179
+ else:
180
+ if data_format == "XNLI-MT":
181
+ for file_idx, filepath in enumerate(filepaths):
182
+ file = open(filepath, encoding="utf-8")
183
+ reader = csv.DictReader(
184
+ file, delimiter="\t", quoting=csv.QUOTE_NONE)
185
+ for row_idx, row in enumerate(reader):
186
+ key = str(file_idx) + "_" + str(row_idx)
187
+ yield key, {
188
+ "premise": row["premise"],
189
+ "hypothesis": row["hypo"],
190
+ "label": row["label"].replace("contradictory", "contradiction"),
191
+ }
192
+ else:
193
+ for filepath in filepaths:
194
+ with open(filepath, encoding="utf-8") as f:
195
+ reader = csv.DictReader(
196
+ f, delimiter="\t", quoting=csv.QUOTE_NONE)
197
+ for row in reader:
198
+ if row["language"] == self.config.language:
199
+ yield row["pairID"], {
200
+ "premise": row["sentence1"],
201
+ "hypothesis": row["sentence2"],
202
+ "label": row["gold_label"],
203
+ }
.history/indicxnli_20220823221223.py ADDED
@@ -0,0 +1,203 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ @misc{https://doi.org/10.48550/arxiv.2204.08776,
30
+ doi = {10.48550/ARXIV.2204.08776},
31
+
32
+ url = {https://arxiv.org/abs/2204.08776},
33
+
34
+ author = {Aggarwal, Divyanshu and Gupta, Vivek and Kunchukuttan, Anoop},
35
+
36
+ keywords = {Computation and Language (cs.CL), Artificial Intelligence (cs.AI), FOS: Computer and information sciences, FOS: Computer and information sciences},
37
+
38
+ title = {IndicXNLI: Evaluating Multilingual Inference for Indian Languages},
39
+
40
+ publisher = {arXiv},
41
+
42
+ year = {2022},
43
+
44
+ copyright = {Creative Commons Attribution 4.0 International}
45
+ }
46
+ }"""
47
+
48
+ _DESCRIPTION = """\
49
+ IndicXNLI is a translated version of XNLI to 11 Indic Languages. As with XNLI, the goal is
50
+ to predict textual entailment (does sentence A imply/contradict/neither sentence
51
+ B) and is a classification task (given two sentences, predict one of three
52
+ labels).
53
+ """
54
+
55
+ _LANGUAGES = (
56
+ 'hi',
57
+ 'bn',
58
+ 'mr',
59
+ 'as',
60
+ 'ta',
61
+ 'te',
62
+ 'or',
63
+ 'ml',
64
+ 'pa',
65
+ 'gu',
66
+ 'kn'
67
+ )
68
+
69
+
70
+ class IndicxnliConfig(datasets.BuilderConfig):
71
+ """BuilderConfig for XNLI."""
72
+
73
+ def __init__(self, language: str, **kwargs):
74
+ """BuilderConfig for XNLI.
75
+
76
+ Args:
77
+ language: One of hi, bn, mr, as, ta, te, or, ml, pa, gu, kn
78
+ **kwargs: keyword arguments forwarded to super.
79
+ """
80
+ super(IndicxnliConfig, self).__init__(**kwargs)
81
+ self.language = language
82
+
83
+
84
+ class Indicxnli(datasets.GeneratorBasedBuilder):
85
+ """XNLI: The Cross-Lingual NLI Corpus. Version 1.0."""
86
+
87
+ VERSION = datasets.Version("1.1.0", "")
88
+ BUILDER_CONFIG_CLASS = IndicxnliConfig
89
+ BUILDER_CONFIGS = [
90
+ IndicxnliConfig(
91
+ name=lang,
92
+ language=lang,
93
+ version=datasets.Version("1.1.0", ""),
94
+ description=f"Plain text import of IndicXNLI for the {lang} language",
95
+ )
96
+ for lang in _LANGUAGES
97
+ ]
98
+
99
+ def _info(self):
100
+ features = datasets.Features(
101
+ {
102
+ "premise": datasets.Value("string"),
103
+ "hypothesis": datasets.Value("string"),
104
+ "label": datasets.ClassLabel(names=["entailment", "neutral", "contradiction"]),
105
+ }
106
+ )
107
+ return datasets.DatasetInfo(
108
+ description=_DESCRIPTION,
109
+ features=features,
110
+ # No default supervised_keys (as we have to pass both premise
111
+ # and hypothesis as input).
112
+ supervised_keys=None,
113
+ homepage="https://www.nyu.edu/projects/bowman/xnli/",
114
+ citation=_CITATION,
115
+ )
116
+
117
+ def _split_generators(self, dl_manager):
118
+ train_path = f'forward/train/xnli_{self.config.language}.json'
119
+ train_path = f'forward/train/xnli_{self.config.language}.json'
120
+
121
+
122
+ return [
123
+ datasets.SplitGenerator(
124
+ name=datasets.Split.TRAIN,
125
+ gen_kwargs={
126
+ "filepaths": [
127
+ os.path.join(train_dir, f"multinli.train.{lang}.tsv") for lang in self.config.languages
128
+ ],
129
+ "data_format": "XNLI-MT",
130
+ },
131
+ ),
132
+ datasets.SplitGenerator(
133
+ name=datasets.Split.TEST,
134
+ gen_kwargs={"filepaths": [os.path.join(
135
+ testval_dir, "xnli.test.tsv")], "data_format": "XNLI"},
136
+ ),
137
+ datasets.SplitGenerator(
138
+ name=datasets.Split.VALIDATION,
139
+ gen_kwargs={"filepaths": [os.path.join(
140
+ testval_dir, "xnli.dev.tsv")], "data_format": "XNLI"},
141
+ ),
142
+ ]
143
+
144
+ def _generate_examples(self, data_format, filepaths):
145
+ """This function returns the examples in the raw (text) form."""
146
+
147
+ if self.config.language == "all_languages":
148
+ if data_format == "XNLI-MT":
149
+ with ExitStack() as stack:
150
+ files = [stack.enter_context(
151
+ open(filepath, encoding="utf-8")) for filepath in filepaths]
152
+ readers = [csv.DictReader(
153
+ file, delimiter="\t", quoting=csv.QUOTE_NONE) for file in files]
154
+ for row_idx, rows in enumerate(zip(*readers)):
155
+ yield row_idx, {
156
+ "premise": {lang: row["premise"] for lang, row in zip(self.config.languages, rows)},
157
+ "hypothesis": {lang: row["hypo"] for lang, row in zip(self.config.languages, rows)},
158
+ "label": rows[0]["label"].replace("contradictory", "contradiction"),
159
+ }
160
+ else:
161
+ rows_per_pair_id = collections.defaultdict(list)
162
+ for filepath in filepaths:
163
+ with open(filepath, encoding="utf-8") as f:
164
+ reader = csv.DictReader(
165
+ f, delimiter="\t", quoting=csv.QUOTE_NONE)
166
+ for row in reader:
167
+ rows_per_pair_id[row["pairID"]].append(row)
168
+
169
+ for rows in rows_per_pair_id.values():
170
+ premise = {row["language"]: row["sentence1"]
171
+ for row in rows}
172
+ hypothesis = {row["language"]: row["sentence2"]
173
+ for row in rows}
174
+ yield rows[0]["pairID"], {
175
+ "premise": premise,
176
+ "hypothesis": hypothesis,
177
+ "label": rows[0]["gold_label"],
178
+ }
179
+ else:
180
+ if data_format == "XNLI-MT":
181
+ for file_idx, filepath in enumerate(filepaths):
182
+ file = open(filepath, encoding="utf-8")
183
+ reader = csv.DictReader(
184
+ file, delimiter="\t", quoting=csv.QUOTE_NONE)
185
+ for row_idx, row in enumerate(reader):
186
+ key = str(file_idx) + "_" + str(row_idx)
187
+ yield key, {
188
+ "premise": row["premise"],
189
+ "hypothesis": row["hypo"],
190
+ "label": row["label"].replace("contradictory", "contradiction"),
191
+ }
192
+ else:
193
+ for filepath in filepaths:
194
+ with open(filepath, encoding="utf-8") as f:
195
+ reader = csv.DictReader(
196
+ f, delimiter="\t", quoting=csv.QUOTE_NONE)
197
+ for row in reader:
198
+ if row["language"] == self.config.language:
199
+ yield row["pairID"], {
200
+ "premise": row["sentence1"],
201
+ "hypothesis": row["sentence2"],
202
+ "label": row["gold_label"],
203
+ }
.history/indicxnli_20220823221227.py ADDED
@@ -0,0 +1,203 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ @misc{https://doi.org/10.48550/arxiv.2204.08776,
30
+ doi = {10.48550/ARXIV.2204.08776},
31
+
32
+ url = {https://arxiv.org/abs/2204.08776},
33
+
34
+ author = {Aggarwal, Divyanshu and Gupta, Vivek and Kunchukuttan, Anoop},
35
+
36
+ keywords = {Computation and Language (cs.CL), Artificial Intelligence (cs.AI), FOS: Computer and information sciences, FOS: Computer and information sciences},
37
+
38
+ title = {IndicXNLI: Evaluating Multilingual Inference for Indian Languages},
39
+
40
+ publisher = {arXiv},
41
+
42
+ year = {2022},
43
+
44
+ copyright = {Creative Commons Attribution 4.0 International}
45
+ }
46
+ }"""
47
+
48
+ _DESCRIPTION = """\
49
+ IndicXNLI is a translated version of XNLI to 11 Indic Languages. As with XNLI, the goal is
50
+ to predict textual entailment (does sentence A imply/contradict/neither sentence
51
+ B) and is a classification task (given two sentences, predict one of three
52
+ labels).
53
+ """
54
+
55
+ _LANGUAGES = (
56
+ 'hi',
57
+ 'bn',
58
+ 'mr',
59
+ 'as',
60
+ 'ta',
61
+ 'te',
62
+ 'or',
63
+ 'ml',
64
+ 'pa',
65
+ 'gu',
66
+ 'kn'
67
+ )
68
+
69
+
70
+ class IndicxnliConfig(datasets.BuilderConfig):
71
+ """BuilderConfig for XNLI."""
72
+
73
+ def __init__(self, language: str, **kwargs):
74
+ """BuilderConfig for XNLI.
75
+
76
+ Args:
77
+ language: One of hi, bn, mr, as, ta, te, or, ml, pa, gu, kn
78
+ **kwargs: keyword arguments forwarded to super.
79
+ """
80
+ super(IndicxnliConfig, self).__init__(**kwargs)
81
+ self.language = language
82
+
83
+
84
+ class Indicxnli(datasets.GeneratorBasedBuilder):
85
+ """XNLI: The Cross-Lingual NLI Corpus. Version 1.0."""
86
+
87
+ VERSION = datasets.Version("1.1.0", "")
88
+ BUILDER_CONFIG_CLASS = IndicxnliConfig
89
+ BUILDER_CONFIGS = [
90
+ IndicxnliConfig(
91
+ name=lang,
92
+ language=lang,
93
+ version=datasets.Version("1.1.0", ""),
94
+ description=f"Plain text import of IndicXNLI for the {lang} language",
95
+ )
96
+ for lang in _LANGUAGES
97
+ ]
98
+
99
+ def _info(self):
100
+ features = datasets.Features(
101
+ {
102
+ "premise": datasets.Value("string"),
103
+ "hypothesis": datasets.Value("string"),
104
+ "label": datasets.ClassLabel(names=["entailment", "neutral", "contradiction"]),
105
+ }
106
+ )
107
+ return datasets.DatasetInfo(
108
+ description=_DESCRIPTION,
109
+ features=features,
110
+ # No default supervised_keys (as we have to pass both premise
111
+ # and hypothesis as input).
112
+ supervised_keys=None,
113
+ homepage="https://www.nyu.edu/projects/bowman/xnli/",
114
+ citation=_CITATION,
115
+ )
116
+
117
+ def _split_generators(self, dl_manager):
118
+ train_path = f'forward/train/xnli_{self.config.language}.json'
119
+ dev_path = f'forward/train/xnli_{self.config.language}.json'
120
+
121
+
122
+ return [
123
+ datasets.SplitGenerator(
124
+ name=datasets.Split.TRAIN,
125
+ gen_kwargs={
126
+ "filepaths": [
127
+ os.path.join(train_dir, f"multinli.train.{lang}.tsv") for lang in self.config.languages
128
+ ],
129
+ "data_format": "XNLI-MT",
130
+ },
131
+ ),
132
+ datasets.SplitGenerator(
133
+ name=datasets.Split.TEST,
134
+ gen_kwargs={"filepaths": [os.path.join(
135
+ testval_dir, "xnli.test.tsv")], "data_format": "XNLI"},
136
+ ),
137
+ datasets.SplitGenerator(
138
+ name=datasets.Split.VALIDATION,
139
+ gen_kwargs={"filepaths": [os.path.join(
140
+ testval_dir, "xnli.dev.tsv")], "data_format": "XNLI"},
141
+ ),
142
+ ]
143
+
144
+ def _generate_examples(self, data_format, filepaths):
145
+ """This function returns the examples in the raw (text) form."""
146
+
147
+ if self.config.language == "all_languages":
148
+ if data_format == "XNLI-MT":
149
+ with ExitStack() as stack:
150
+ files = [stack.enter_context(
151
+ open(filepath, encoding="utf-8")) for filepath in filepaths]
152
+ readers = [csv.DictReader(
153
+ file, delimiter="\t", quoting=csv.QUOTE_NONE) for file in files]
154
+ for row_idx, rows in enumerate(zip(*readers)):
155
+ yield row_idx, {
156
+ "premise": {lang: row["premise"] for lang, row in zip(self.config.languages, rows)},
157
+ "hypothesis": {lang: row["hypo"] for lang, row in zip(self.config.languages, rows)},
158
+ "label": rows[0]["label"].replace("contradictory", "contradiction"),
159
+ }
160
+ else:
161
+ rows_per_pair_id = collections.defaultdict(list)
162
+ for filepath in filepaths:
163
+ with open(filepath, encoding="utf-8") as f:
164
+ reader = csv.DictReader(
165
+ f, delimiter="\t", quoting=csv.QUOTE_NONE)
166
+ for row in reader:
167
+ rows_per_pair_id[row["pairID"]].append(row)
168
+
169
+ for rows in rows_per_pair_id.values():
170
+ premise = {row["language"]: row["sentence1"]
171
+ for row in rows}
172
+ hypothesis = {row["language"]: row["sentence2"]
173
+ for row in rows}
174
+ yield rows[0]["pairID"], {
175
+ "premise": premise,
176
+ "hypothesis": hypothesis,
177
+ "label": rows[0]["gold_label"],
178
+ }
179
+ else:
180
+ if data_format == "XNLI-MT":
181
+ for file_idx, filepath in enumerate(filepaths):
182
+ file = open(filepath, encoding="utf-8")
183
+ reader = csv.DictReader(
184
+ file, delimiter="\t", quoting=csv.QUOTE_NONE)
185
+ for row_idx, row in enumerate(reader):
186
+ key = str(file_idx) + "_" + str(row_idx)
187
+ yield key, {
188
+ "premise": row["premise"],
189
+ "hypothesis": row["hypo"],
190
+ "label": row["label"].replace("contradictory", "contradiction"),
191
+ }
192
+ else:
193
+ for filepath in filepaths:
194
+ with open(filepath, encoding="utf-8") as f:
195
+ reader = csv.DictReader(
196
+ f, delimiter="\t", quoting=csv.QUOTE_NONE)
197
+ for row in reader:
198
+ if row["language"] == self.config.language:
199
+ yield row["pairID"], {
200
+ "premise": row["sentence1"],
201
+ "hypothesis": row["sentence2"],
202
+ "label": row["gold_label"],
203
+ }
.history/indicxnli_20220823221233.py ADDED
@@ -0,0 +1,203 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ @misc{https://doi.org/10.48550/arxiv.2204.08776,
30
+ doi = {10.48550/ARXIV.2204.08776},
31
+
32
+ url = {https://arxiv.org/abs/2204.08776},
33
+
34
+ author = {Aggarwal, Divyanshu and Gupta, Vivek and Kunchukuttan, Anoop},
35
+
36
+ keywords = {Computation and Language (cs.CL), Artificial Intelligence (cs.AI), FOS: Computer and information sciences, FOS: Computer and information sciences},
37
+
38
+ title = {IndicXNLI: Evaluating Multilingual Inference for Indian Languages},
39
+
40
+ publisher = {arXiv},
41
+
42
+ year = {2022},
43
+
44
+ copyright = {Creative Commons Attribution 4.0 International}
45
+ }
46
+ }"""
47
+
48
+ _DESCRIPTION = """\
49
+ IndicXNLI is a translated version of XNLI to 11 Indic Languages. As with XNLI, the goal is
50
+ to predict textual entailment (does sentence A imply/contradict/neither sentence
51
+ B) and is a classification task (given two sentences, predict one of three
52
+ labels).
53
+ """
54
+
55
+ _LANGUAGES = (
56
+ 'hi',
57
+ 'bn',
58
+ 'mr',
59
+ 'as',
60
+ 'ta',
61
+ 'te',
62
+ 'or',
63
+ 'ml',
64
+ 'pa',
65
+ 'gu',
66
+ 'kn'
67
+ )
68
+
69
+
70
+ class IndicxnliConfig(datasets.BuilderConfig):
71
+ """BuilderConfig for XNLI."""
72
+
73
+ def __init__(self, language: str, **kwargs):
74
+ """BuilderConfig for XNLI.
75
+
76
+ Args:
77
+ language: One of hi, bn, mr, as, ta, te, or, ml, pa, gu, kn
78
+ **kwargs: keyword arguments forwarded to super.
79
+ """
80
+ super(IndicxnliConfig, self).__init__(**kwargs)
81
+ self.language = language
82
+
83
+
84
+ class Indicxnli(datasets.GeneratorBasedBuilder):
85
+ """XNLI: The Cross-Lingual NLI Corpus. Version 1.0."""
86
+
87
+ VERSION = datasets.Version("1.1.0", "")
88
+ BUILDER_CONFIG_CLASS = IndicxnliConfig
89
+ BUILDER_CONFIGS = [
90
+ IndicxnliConfig(
91
+ name=lang,
92
+ language=lang,
93
+ version=datasets.Version("1.1.0", ""),
94
+ description=f"Plain text import of IndicXNLI for the {lang} language",
95
+ )
96
+ for lang in _LANGUAGES
97
+ ]
98
+
99
+ def _info(self):
100
+ features = datasets.Features(
101
+ {
102
+ "premise": datasets.Value("string"),
103
+ "hypothesis": datasets.Value("string"),
104
+ "label": datasets.ClassLabel(names=["entailment", "neutral", "contradiction"]),
105
+ }
106
+ )
107
+ return datasets.DatasetInfo(
108
+ description=_DESCRIPTION,
109
+ features=features,
110
+ # No default supervised_keys (as we have to pass both premise
111
+ # and hypothesis as input).
112
+ supervised_keys=None,
113
+ homepage="https://www.nyu.edu/projects/bowman/xnli/",
114
+ citation=_CITATION,
115
+ )
116
+
117
+ def _split_generators(self, dl_manager):
118
+ train_path = f'forward/train/xnli_{self.config.language}.json'
119
+ dev_path = f'forward/train/xnli_{self.config.language}.json'
120
+ train_path = f'forward/train/xnli_{self.config.language}.json'
121
+
122
+ return [
123
+ datasets.SplitGenerator(
124
+ name=datasets.Split.TRAIN,
125
+ gen_kwargs={
126
+ "filepaths": [
127
+ os.path.join(train_dir, f"multinli.train.{lang}.tsv") for lang in self.config.languages
128
+ ],
129
+ "data_format": "XNLI-MT",
130
+ },
131
+ ),
132
+ datasets.SplitGenerator(
133
+ name=datasets.Split.TEST,
134
+ gen_kwargs={"filepaths": [os.path.join(
135
+ testval_dir, "xnli.test.tsv")], "data_format": "XNLI"},
136
+ ),
137
+ datasets.SplitGenerator(
138
+ name=datasets.Split.VALIDATION,
139
+ gen_kwargs={"filepaths": [os.path.join(
140
+ testval_dir, "xnli.dev.tsv")], "data_format": "XNLI"},
141
+ ),
142
+ ]
143
+
144
+ def _generate_examples(self, data_format, filepaths):
145
+ """This function returns the examples in the raw (text) form."""
146
+
147
+ if self.config.language == "all_languages":
148
+ if data_format == "XNLI-MT":
149
+ with ExitStack() as stack:
150
+ files = [stack.enter_context(
151
+ open(filepath, encoding="utf-8")) for filepath in filepaths]
152
+ readers = [csv.DictReader(
153
+ file, delimiter="\t", quoting=csv.QUOTE_NONE) for file in files]
154
+ for row_idx, rows in enumerate(zip(*readers)):
155
+ yield row_idx, {
156
+ "premise": {lang: row["premise"] for lang, row in zip(self.config.languages, rows)},
157
+ "hypothesis": {lang: row["hypo"] for lang, row in zip(self.config.languages, rows)},
158
+ "label": rows[0]["label"].replace("contradictory", "contradiction"),
159
+ }
160
+ else:
161
+ rows_per_pair_id = collections.defaultdict(list)
162
+ for filepath in filepaths:
163
+ with open(filepath, encoding="utf-8") as f:
164
+ reader = csv.DictReader(
165
+ f, delimiter="\t", quoting=csv.QUOTE_NONE)
166
+ for row in reader:
167
+ rows_per_pair_id[row["pairID"]].append(row)
168
+
169
+ for rows in rows_per_pair_id.values():
170
+ premise = {row["language"]: row["sentence1"]
171
+ for row in rows}
172
+ hypothesis = {row["language"]: row["sentence2"]
173
+ for row in rows}
174
+ yield rows[0]["pairID"], {
175
+ "premise": premise,
176
+ "hypothesis": hypothesis,
177
+ "label": rows[0]["gold_label"],
178
+ }
179
+ else:
180
+ if data_format == "XNLI-MT":
181
+ for file_idx, filepath in enumerate(filepaths):
182
+ file = open(filepath, encoding="utf-8")
183
+ reader = csv.DictReader(
184
+ file, delimiter="\t", quoting=csv.QUOTE_NONE)
185
+ for row_idx, row in enumerate(reader):
186
+ key = str(file_idx) + "_" + str(row_idx)
187
+ yield key, {
188
+ "premise": row["premise"],
189
+ "hypothesis": row["hypo"],
190
+ "label": row["label"].replace("contradictory", "contradiction"),
191
+ }
192
+ else:
193
+ for filepath in filepaths:
194
+ with open(filepath, encoding="utf-8") as f:
195
+ reader = csv.DictReader(
196
+ f, delimiter="\t", quoting=csv.QUOTE_NONE)
197
+ for row in reader:
198
+ if row["language"] == self.config.language:
199
+ yield row["pairID"], {
200
+ "premise": row["sentence1"],
201
+ "hypothesis": row["sentence2"],
202
+ "label": row["gold_label"],
203
+ }
.history/indicxnli_20220823221235.py ADDED
@@ -0,0 +1,203 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ @misc{https://doi.org/10.48550/arxiv.2204.08776,
30
+ doi = {10.48550/ARXIV.2204.08776},
31
+
32
+ url = {https://arxiv.org/abs/2204.08776},
33
+
34
+ author = {Aggarwal, Divyanshu and Gupta, Vivek and Kunchukuttan, Anoop},
35
+
36
+ keywords = {Computation and Language (cs.CL), Artificial Intelligence (cs.AI), FOS: Computer and information sciences, FOS: Computer and information sciences},
37
+
38
+ title = {IndicXNLI: Evaluating Multilingual Inference for Indian Languages},
39
+
40
+ publisher = {arXiv},
41
+
42
+ year = {2022},
43
+
44
+ copyright = {Creative Commons Attribution 4.0 International}
45
+ }
46
+ }"""
47
+
48
+ _DESCRIPTION = """\
49
+ IndicXNLI is a translated version of XNLI to 11 Indic Languages. As with XNLI, the goal is
50
+ to predict textual entailment (does sentence A imply/contradict/neither sentence
51
+ B) and is a classification task (given two sentences, predict one of three
52
+ labels).
53
+ """
54
+
55
+ _LANGUAGES = (
56
+ 'hi',
57
+ 'bn',
58
+ 'mr',
59
+ 'as',
60
+ 'ta',
61
+ 'te',
62
+ 'or',
63
+ 'ml',
64
+ 'pa',
65
+ 'gu',
66
+ 'kn'
67
+ )
68
+
69
+
70
+ class IndicxnliConfig(datasets.BuilderConfig):
71
+ """BuilderConfig for XNLI."""
72
+
73
+ def __init__(self, language: str, **kwargs):
74
+ """BuilderConfig for XNLI.
75
+
76
+ Args:
77
+ language: One of hi, bn, mr, as, ta, te, or, ml, pa, gu, kn
78
+ **kwargs: keyword arguments forwarded to super.
79
+ """
80
+ super(IndicxnliConfig, self).__init__(**kwargs)
81
+ self.language = language
82
+
83
+
84
+ class Indicxnli(datasets.GeneratorBasedBuilder):
85
+ """XNLI: The Cross-Lingual NLI Corpus. Version 1.0."""
86
+
87
+ VERSION = datasets.Version("1.1.0", "")
88
+ BUILDER_CONFIG_CLASS = IndicxnliConfig
89
+ BUILDER_CONFIGS = [
90
+ IndicxnliConfig(
91
+ name=lang,
92
+ language=lang,
93
+ version=datasets.Version("1.1.0", ""),
94
+ description=f"Plain text import of IndicXNLI for the {lang} language",
95
+ )
96
+ for lang in _LANGUAGES
97
+ ]
98
+
99
+ def _info(self):
100
+ features = datasets.Features(
101
+ {
102
+ "premise": datasets.Value("string"),
103
+ "hypothesis": datasets.Value("string"),
104
+ "label": datasets.ClassLabel(names=["entailment", "neutral", "contradiction"]),
105
+ }
106
+ )
107
+ return datasets.DatasetInfo(
108
+ description=_DESCRIPTION,
109
+ features=features,
110
+ # No default supervised_keys (as we have to pass both premise
111
+ # and hypothesis as input).
112
+ supervised_keys=None,
113
+ homepage="https://www.nyu.edu/projects/bowman/xnli/",
114
+ citation=_CITATION,
115
+ )
116
+
117
+ def _split_generators(self, dl_manager):
118
+ train_path = f'forward/train/xnli_{self.config.language}.json'
119
+ dev_path = f'forward/train/xnli_{self.config.language}.json'
120
+ test_path = f'forward/train/xnli_{self.config.language}.json'
121
+
122
+ return [
123
+ datasets.SplitGenerator(
124
+ name=datasets.Split.TRAIN,
125
+ gen_kwargs={
126
+ "filepaths": [
127
+ os.path.join(train_dir, f"multinli.train.{lang}.tsv") for lang in self.config.languages
128
+ ],
129
+ "data_format": "XNLI-MT",
130
+ },
131
+ ),
132
+ datasets.SplitGenerator(
133
+ name=datasets.Split.TEST,
134
+ gen_kwargs={"filepaths": [os.path.join(
135
+ testval_dir, "xnli.test.tsv")], "data_format": "XNLI"},
136
+ ),
137
+ datasets.SplitGenerator(
138
+ name=datasets.Split.VALIDATION,
139
+ gen_kwargs={"filepaths": [os.path.join(
140
+ testval_dir, "xnli.dev.tsv")], "data_format": "XNLI"},
141
+ ),
142
+ ]
143
+
144
+ def _generate_examples(self, data_format, filepaths):
145
+ """This function returns the examples in the raw (text) form."""
146
+
147
+ if self.config.language == "all_languages":
148
+ if data_format == "XNLI-MT":
149
+ with ExitStack() as stack:
150
+ files = [stack.enter_context(
151
+ open(filepath, encoding="utf-8")) for filepath in filepaths]
152
+ readers = [csv.DictReader(
153
+ file, delimiter="\t", quoting=csv.QUOTE_NONE) for file in files]
154
+ for row_idx, rows in enumerate(zip(*readers)):
155
+ yield row_idx, {
156
+ "premise": {lang: row["premise"] for lang, row in zip(self.config.languages, rows)},
157
+ "hypothesis": {lang: row["hypo"] for lang, row in zip(self.config.languages, rows)},
158
+ "label": rows[0]["label"].replace("contradictory", "contradiction"),
159
+ }
160
+ else:
161
+ rows_per_pair_id = collections.defaultdict(list)
162
+ for filepath in filepaths:
163
+ with open(filepath, encoding="utf-8") as f:
164
+ reader = csv.DictReader(
165
+ f, delimiter="\t", quoting=csv.QUOTE_NONE)
166
+ for row in reader:
167
+ rows_per_pair_id[row["pairID"]].append(row)
168
+
169
+ for rows in rows_per_pair_id.values():
170
+ premise = {row["language"]: row["sentence1"]
171
+ for row in rows}
172
+ hypothesis = {row["language"]: row["sentence2"]
173
+ for row in rows}
174
+ yield rows[0]["pairID"], {
175
+ "premise": premise,
176
+ "hypothesis": hypothesis,
177
+ "label": rows[0]["gold_label"],
178
+ }
179
+ else:
180
+ if data_format == "XNLI-MT":
181
+ for file_idx, filepath in enumerate(filepaths):
182
+ file = open(filepath, encoding="utf-8")
183
+ reader = csv.DictReader(
184
+ file, delimiter="\t", quoting=csv.QUOTE_NONE)
185
+ for row_idx, row in enumerate(reader):
186
+ key = str(file_idx) + "_" + str(row_idx)
187
+ yield key, {
188
+ "premise": row["premise"],
189
+ "hypothesis": row["hypo"],
190
+ "label": row["label"].replace("contradictory", "contradiction"),
191
+ }
192
+ else:
193
+ for filepath in filepaths:
194
+ with open(filepath, encoding="utf-8") as f:
195
+ reader = csv.DictReader(
196
+ f, delimiter="\t", quoting=csv.QUOTE_NONE)
197
+ for row in reader:
198
+ if row["language"] == self.config.language:
199
+ yield row["pairID"], {
200
+ "premise": row["sentence1"],
201
+ "hypothesis": row["sentence2"],
202
+ "label": row["gold_label"],
203
+ }
.history/indicxnli_20220823221240.py ADDED
@@ -0,0 +1,203 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ @misc{https://doi.org/10.48550/arxiv.2204.08776,
30
+ doi = {10.48550/ARXIV.2204.08776},
31
+
32
+ url = {https://arxiv.org/abs/2204.08776},
33
+
34
+ author = {Aggarwal, Divyanshu and Gupta, Vivek and Kunchukuttan, Anoop},
35
+
36
+ keywords = {Computation and Language (cs.CL), Artificial Intelligence (cs.AI), FOS: Computer and information sciences, FOS: Computer and information sciences},
37
+
38
+ title = {IndicXNLI: Evaluating Multilingual Inference for Indian Languages},
39
+
40
+ publisher = {arXiv},
41
+
42
+ year = {2022},
43
+
44
+ copyright = {Creative Commons Attribution 4.0 International}
45
+ }
46
+ }"""
47
+
48
+ _DESCRIPTION = """\
49
+ IndicXNLI is a translated version of XNLI to 11 Indic Languages. As with XNLI, the goal is
50
+ to predict textual entailment (does sentence A imply/contradict/neither sentence
51
+ B) and is a classification task (given two sentences, predict one of three
52
+ labels).
53
+ """
54
+
55
+ _LANGUAGES = (
56
+ 'hi',
57
+ 'bn',
58
+ 'mr',
59
+ 'as',
60
+ 'ta',
61
+ 'te',
62
+ 'or',
63
+ 'ml',
64
+ 'pa',
65
+ 'gu',
66
+ 'kn'
67
+ )
68
+
69
+
70
+ class IndicxnliConfig(datasets.BuilderConfig):
71
+ """BuilderConfig for XNLI."""
72
+
73
+ def __init__(self, language: str, **kwargs):
74
+ """BuilderConfig for XNLI.
75
+
76
+ Args:
77
+ language: One of hi, bn, mr, as, ta, te, or, ml, pa, gu, kn
78
+ **kwargs: keyword arguments forwarded to super.
79
+ """
80
+ super(IndicxnliConfig, self).__init__(**kwargs)
81
+ self.language = language
82
+
83
+
84
+ class Indicxnli(datasets.GeneratorBasedBuilder):
85
+ """XNLI: The Cross-Lingual NLI Corpus. Version 1.0."""
86
+
87
+ VERSION = datasets.Version("1.1.0", "")
88
+ BUILDER_CONFIG_CLASS = IndicxnliConfig
89
+ BUILDER_CONFIGS = [
90
+ IndicxnliConfig(
91
+ name=lang,
92
+ language=lang,
93
+ version=datasets.Version("1.1.0", ""),
94
+ description=f"Plain text import of IndicXNLI for the {lang} language",
95
+ )
96
+ for lang in _LANGUAGES
97
+ ]
98
+
99
+ def _info(self):
100
+ features = datasets.Features(
101
+ {
102
+ "premise": datasets.Value("string"),
103
+ "hypothesis": datasets.Value("string"),
104
+ "label": datasets.ClassLabel(names=["entailment", "neutral", "contradiction"]),
105
+ }
106
+ )
107
+ return datasets.DatasetInfo(
108
+ description=_DESCRIPTION,
109
+ features=features,
110
+ # No default supervised_keys (as we have to pass both premise
111
+ # and hypothesis as input).
112
+ supervised_keys=None,
113
+ homepage="https://www.nyu.edu/projects/bowman/xnli/",
114
+ citation=_CITATION,
115
+ )
116
+
117
+ def _split_generators(self, dl_manager):
118
+ train_path = f'forward/train/xnli_{self.config.language}.json'
119
+ dev_path = f'forward/train/xnli_{self.config.language}.json'
120
+ test_path = f'forward/test/xnli_{self.config.language}.json'
121
+
122
+ return [
123
+ datasets.SplitGenerator(
124
+ name=datasets.Split.TRAIN,
125
+ gen_kwargs={
126
+ "filepaths": [
127
+ os.path.join(train_dir, f"multinli.train.{lang}.tsv") for lang in self.config.languages
128
+ ],
129
+ "data_format": "XNLI-MT",
130
+ },
131
+ ),
132
+ datasets.SplitGenerator(
133
+ name=datasets.Split.TEST,
134
+ gen_kwargs={"filepaths": [os.path.join(
135
+ testval_dir, "xnli.test.tsv")], "data_format": "XNLI"},
136
+ ),
137
+ datasets.SplitGenerator(
138
+ name=datasets.Split.VALIDATION,
139
+ gen_kwargs={"filepaths": [os.path.join(
140
+ testval_dir, "xnli.dev.tsv")], "data_format": "XNLI"},
141
+ ),
142
+ ]
143
+
144
+ def _generate_examples(self, data_format, filepaths):
145
+ """This function returns the examples in the raw (text) form."""
146
+
147
+ if self.config.language == "all_languages":
148
+ if data_format == "XNLI-MT":
149
+ with ExitStack() as stack:
150
+ files = [stack.enter_context(
151
+ open(filepath, encoding="utf-8")) for filepath in filepaths]
152
+ readers = [csv.DictReader(
153
+ file, delimiter="\t", quoting=csv.QUOTE_NONE) for file in files]
154
+ for row_idx, rows in enumerate(zip(*readers)):
155
+ yield row_idx, {
156
+ "premise": {lang: row["premise"] for lang, row in zip(self.config.languages, rows)},
157
+ "hypothesis": {lang: row["hypo"] for lang, row in zip(self.config.languages, rows)},
158
+ "label": rows[0]["label"].replace("contradictory", "contradiction"),
159
+ }
160
+ else:
161
+ rows_per_pair_id = collections.defaultdict(list)
162
+ for filepath in filepaths:
163
+ with open(filepath, encoding="utf-8") as f:
164
+ reader = csv.DictReader(
165
+ f, delimiter="\t", quoting=csv.QUOTE_NONE)
166
+ for row in reader:
167
+ rows_per_pair_id[row["pairID"]].append(row)
168
+
169
+ for rows in rows_per_pair_id.values():
170
+ premise = {row["language"]: row["sentence1"]
171
+ for row in rows}
172
+ hypothesis = {row["language"]: row["sentence2"]
173
+ for row in rows}
174
+ yield rows[0]["pairID"], {
175
+ "premise": premise,
176
+ "hypothesis": hypothesis,
177
+ "label": rows[0]["gold_label"],
178
+ }
179
+ else:
180
+ if data_format == "XNLI-MT":
181
+ for file_idx, filepath in enumerate(filepaths):
182
+ file = open(filepath, encoding="utf-8")
183
+ reader = csv.DictReader(
184
+ file, delimiter="\t", quoting=csv.QUOTE_NONE)
185
+ for row_idx, row in enumerate(reader):
186
+ key = str(file_idx) + "_" + str(row_idx)
187
+ yield key, {
188
+ "premise": row["premise"],
189
+ "hypothesis": row["hypo"],
190
+ "label": row["label"].replace("contradictory", "contradiction"),
191
+ }
192
+ else:
193
+ for filepath in filepaths:
194
+ with open(filepath, encoding="utf-8") as f:
195
+ reader = csv.DictReader(
196
+ f, delimiter="\t", quoting=csv.QUOTE_NONE)
197
+ for row in reader:
198
+ if row["language"] == self.config.language:
199
+ yield row["pairID"], {
200
+ "premise": row["sentence1"],
201
+ "hypothesis": row["sentence2"],
202
+ "label": row["gold_label"],
203
+ }
.history/indicxnli_20220823221242.py ADDED
@@ -0,0 +1,203 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ @misc{https://doi.org/10.48550/arxiv.2204.08776,
30
+ doi = {10.48550/ARXIV.2204.08776},
31
+
32
+ url = {https://arxiv.org/abs/2204.08776},
33
+
34
+ author = {Aggarwal, Divyanshu and Gupta, Vivek and Kunchukuttan, Anoop},
35
+
36
+ keywords = {Computation and Language (cs.CL), Artificial Intelligence (cs.AI), FOS: Computer and information sciences, FOS: Computer and information sciences},
37
+
38
+ title = {IndicXNLI: Evaluating Multilingual Inference for Indian Languages},
39
+
40
+ publisher = {arXiv},
41
+
42
+ year = {2022},
43
+
44
+ copyright = {Creative Commons Attribution 4.0 International}
45
+ }
46
+ }"""
47
+
48
+ _DESCRIPTION = """\
49
+ IndicXNLI is a translated version of XNLI to 11 Indic Languages. As with XNLI, the goal is
50
+ to predict textual entailment (does sentence A imply/contradict/neither sentence
51
+ B) and is a classification task (given two sentences, predict one of three
52
+ labels).
53
+ """
54
+
55
+ _LANGUAGES = (
56
+ 'hi',
57
+ 'bn',
58
+ 'mr',
59
+ 'as',
60
+ 'ta',
61
+ 'te',
62
+ 'or',
63
+ 'ml',
64
+ 'pa',
65
+ 'gu',
66
+ 'kn'
67
+ )
68
+
69
+
70
+ class IndicxnliConfig(datasets.BuilderConfig):
71
+ """BuilderConfig for XNLI."""
72
+
73
+ def __init__(self, language: str, **kwargs):
74
+ """BuilderConfig for XNLI.
75
+
76
+ Args:
77
+ language: One of hi, bn, mr, as, ta, te, or, ml, pa, gu, kn
78
+ **kwargs: keyword arguments forwarded to super.
79
+ """
80
+ super(IndicxnliConfig, self).__init__(**kwargs)
81
+ self.language = language
82
+
83
+
84
+ class Indicxnli(datasets.GeneratorBasedBuilder):
85
+ """XNLI: The Cross-Lingual NLI Corpus. Version 1.0."""
86
+
87
+ VERSION = datasets.Version("1.1.0", "")
88
+ BUILDER_CONFIG_CLASS = IndicxnliConfig
89
+ BUILDER_CONFIGS = [
90
+ IndicxnliConfig(
91
+ name=lang,
92
+ language=lang,
93
+ version=datasets.Version("1.1.0", ""),
94
+ description=f"Plain text import of IndicXNLI for the {lang} language",
95
+ )
96
+ for lang in _LANGUAGES
97
+ ]
98
+
99
+ def _info(self):
100
+ features = datasets.Features(
101
+ {
102
+ "premise": datasets.Value("string"),
103
+ "hypothesis": datasets.Value("string"),
104
+ "label": datasets.ClassLabel(names=["entailment", "neutral", "contradiction"]),
105
+ }
106
+ )
107
+ return datasets.DatasetInfo(
108
+ description=_DESCRIPTION,
109
+ features=features,
110
+ # No default supervised_keys (as we have to pass both premise
111
+ # and hypothesis as input).
112
+ supervised_keys=None,
113
+ homepage="https://www.nyu.edu/projects/bowman/xnli/",
114
+ citation=_CITATION,
115
+ )
116
+
117
+ def _split_generators(self, dl_manager):
118
+ train_path = f'forward/train/xnli_{self.config.language}.json'
119
+ dev_path = f'forward/dev/xnli_{self.config.language}.json'
120
+ test_path = f'forward/test/xnli_{self.config.language}.json'
121
+
122
+ return [
123
+ datasets.SplitGenerator(
124
+ name=datasets.Split.TRAIN,
125
+ gen_kwargs={
126
+ "filepaths": [
127
+ os.path.join(train_dir, f"multinli.train.{lang}.tsv") for lang in self.config.languages
128
+ ],
129
+ "data_format": "XNLI-MT",
130
+ },
131
+ ),
132
+ datasets.SplitGenerator(
133
+ name=datasets.Split.TEST,
134
+ gen_kwargs={"filepaths": [os.path.join(
135
+ testval_dir, "xnli.test.tsv")], "data_format": "XNLI"},
136
+ ),
137
+ datasets.SplitGenerator(
138
+ name=datasets.Split.VALIDATION,
139
+ gen_kwargs={"filepaths": [os.path.join(
140
+ testval_dir, "xnli.dev.tsv")], "data_format": "XNLI"},
141
+ ),
142
+ ]
143
+
144
+ def _generate_examples(self, data_format, filepaths):
145
+ """This function returns the examples in the raw (text) form."""
146
+
147
+ if self.config.language == "all_languages":
148
+ if data_format == "XNLI-MT":
149
+ with ExitStack() as stack:
150
+ files = [stack.enter_context(
151
+ open(filepath, encoding="utf-8")) for filepath in filepaths]
152
+ readers = [csv.DictReader(
153
+ file, delimiter="\t", quoting=csv.QUOTE_NONE) for file in files]
154
+ for row_idx, rows in enumerate(zip(*readers)):
155
+ yield row_idx, {
156
+ "premise": {lang: row["premise"] for lang, row in zip(self.config.languages, rows)},
157
+ "hypothesis": {lang: row["hypo"] for lang, row in zip(self.config.languages, rows)},
158
+ "label": rows[0]["label"].replace("contradictory", "contradiction"),
159
+ }
160
+ else:
161
+ rows_per_pair_id = collections.defaultdict(list)
162
+ for filepath in filepaths:
163
+ with open(filepath, encoding="utf-8") as f:
164
+ reader = csv.DictReader(
165
+ f, delimiter="\t", quoting=csv.QUOTE_NONE)
166
+ for row in reader:
167
+ rows_per_pair_id[row["pairID"]].append(row)
168
+
169
+ for rows in rows_per_pair_id.values():
170
+ premise = {row["language"]: row["sentence1"]
171
+ for row in rows}
172
+ hypothesis = {row["language"]: row["sentence2"]
173
+ for row in rows}
174
+ yield rows[0]["pairID"], {
175
+ "premise": premise,
176
+ "hypothesis": hypothesis,
177
+ "label": rows[0]["gold_label"],
178
+ }
179
+ else:
180
+ if data_format == "XNLI-MT":
181
+ for file_idx, filepath in enumerate(filepaths):
182
+ file = open(filepath, encoding="utf-8")
183
+ reader = csv.DictReader(
184
+ file, delimiter="\t", quoting=csv.QUOTE_NONE)
185
+ for row_idx, row in enumerate(reader):
186
+ key = str(file_idx) + "_" + str(row_idx)
187
+ yield key, {
188
+ "premise": row["premise"],
189
+ "hypothesis": row["hypo"],
190
+ "label": row["label"].replace("contradictory", "contradiction"),
191
+ }
192
+ else:
193
+ for filepath in filepaths:
194
+ with open(filepath, encoding="utf-8") as f:
195
+ reader = csv.DictReader(
196
+ f, delimiter="\t", quoting=csv.QUOTE_NONE)
197
+ for row in reader:
198
+ if row["language"] == self.config.language:
199
+ yield row["pairID"], {
200
+ "premise": row["sentence1"],
201
+ "hypothesis": row["sentence2"],
202
+ "label": row["gold_label"],
203
+ }
.history/indicxnli_20220823221316.py ADDED
@@ -0,0 +1,203 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ @misc{https://doi.org/10.48550/arxiv.2204.08776,
30
+ doi = {10.48550/ARXIV.2204.08776},
31
+
32
+ url = {https://arxiv.org/abs/2204.08776},
33
+
34
+ author = {Aggarwal, Divyanshu and Gupta, Vivek and Kunchukuttan, Anoop},
35
+
36
+ keywords = {Computation and Language (cs.CL), Artificial Intelligence (cs.AI), FOS: Computer and information sciences, FOS: Computer and information sciences},
37
+
38
+ title = {IndicXNLI: Evaluating Multilingual Inference for Indian Languages},
39
+
40
+ publisher = {arXiv},
41
+
42
+ year = {2022},
43
+
44
+ copyright = {Creative Commons Attribution 4.0 International}
45
+ }
46
+ }"""
47
+
48
+ _DESCRIPTION = """\
49
+ IndicXNLI is a translated version of XNLI to 11 Indic Languages. As with XNLI, the goal is
50
+ to predict textual entailment (does sentence A imply/contradict/neither sentence
51
+ B) and is a classification task (given two sentences, predict one of three
52
+ labels).
53
+ """
54
+
55
+ _LANGUAGES = (
56
+ 'hi',
57
+ 'bn',
58
+ 'mr',
59
+ 'as',
60
+ 'ta',
61
+ 'te',
62
+ 'or',
63
+ 'ml',
64
+ 'pa',
65
+ 'gu',
66
+ 'kn'
67
+ )
68
+
69
+
70
+ class IndicxnliConfig(datasets.BuilderConfig):
71
+ """BuilderConfig for XNLI."""
72
+
73
+ def __init__(self, language: str, **kwargs):
74
+ """BuilderConfig for XNLI.
75
+
76
+ Args:
77
+ language: One of hi, bn, mr, as, ta, te, or, ml, pa, gu, kn
78
+ **kwargs: keyword arguments forwarded to super.
79
+ """
80
+ super(IndicxnliConfig, self).__init__(**kwargs)
81
+ self.language = language
82
+
83
+
84
+ class Indicxnli(datasets.GeneratorBasedBuilder):
85
+ """XNLI: The Cross-Lingual NLI Corpus. Version 1.0."""
86
+
87
+ VERSION = datasets.Version("1.1.0", "")
88
+ BUILDER_CONFIG_CLASS = IndicxnliConfig
89
+ BUILDER_CONFIGS = [
90
+ IndicxnliConfig(
91
+ name=lang,
92
+ language=lang,
93
+ version=datasets.Version("1.1.0", ""),
94
+ description=f"Plain text import of IndicXNLI for the {lang} language",
95
+ )
96
+ for lang in _LANGUAGES
97
+ ]
98
+
99
+ def _info(self):
100
+ features = datasets.Features(
101
+ {
102
+ "premise": datasets.Value("string"),
103
+ "hypothesis": datasets.Value("string"),
104
+ "label": datasets.ClassLabel(names=["entailment", "neutral", "contradiction"]),
105
+ }
106
+ )
107
+ return datasets.DatasetInfo(
108
+ description=_DESCRIPTION,
109
+ features=features,
110
+ # No default supervised_keys (as we have to pass both premise
111
+ # and hypothesis as input).
112
+ supervised_keys=None,
113
+ homepage="https://www.nyu.edu/projects/bowman/xnli/",
114
+ citation=_CITATION,
115
+ )
116
+
117
+ def _split_generators(self, dl_manager):
118
+ train_dir = f'forward/train/xnli_{self.config.language}.json'
119
+ dev_path = f'forward/dev/xnli_{self.config.language}.json'
120
+ test_path = f'forward/test/xnli_{self.config.language}.json'
121
+
122
+ return [
123
+ datasets.SplitGenerator(
124
+ name=datasets.Split.TRAIN,
125
+ gen_kwargs={
126
+ "filepaths": [
127
+ os.path.join(train_dir, f"multinli.train.{lang}.tsv") for lang in self.config.languages
128
+ ],
129
+ "data_format": "XNLI-MT",
130
+ },
131
+ ),
132
+ datasets.SplitGenerator(
133
+ name=datasets.Split.TEST,
134
+ gen_kwargs={"filepaths": [os.path.join(
135
+ testval_dir, "xnli.test.tsv")], "data_format": "XNLI"},
136
+ ),
137
+ datasets.SplitGenerator(
138
+ name=datasets.Split.VALIDATION,
139
+ gen_kwargs={"filepaths": [os.path.join(
140
+ testval_dir, "xnli.dev.tsv")], "data_format": "XNLI"},
141
+ ),
142
+ ]
143
+
144
+ def _generate_examples(self, data_format, filepaths):
145
+ """This function returns the examples in the raw (text) form."""
146
+
147
+ if self.config.language == "all_languages":
148
+ if data_format == "XNLI-MT":
149
+ with ExitStack() as stack:
150
+ files = [stack.enter_context(
151
+ open(filepath, encoding="utf-8")) for filepath in filepaths]
152
+ readers = [csv.DictReader(
153
+ file, delimiter="\t", quoting=csv.QUOTE_NONE) for file in files]
154
+ for row_idx, rows in enumerate(zip(*readers)):
155
+ yield row_idx, {
156
+ "premise": {lang: row["premise"] for lang, row in zip(self.config.languages, rows)},
157
+ "hypothesis": {lang: row["hypo"] for lang, row in zip(self.config.languages, rows)},
158
+ "label": rows[0]["label"].replace("contradictory", "contradiction"),
159
+ }
160
+ else:
161
+ rows_per_pair_id = collections.defaultdict(list)
162
+ for filepath in filepaths:
163
+ with open(filepath, encoding="utf-8") as f:
164
+ reader = csv.DictReader(
165
+ f, delimiter="\t", quoting=csv.QUOTE_NONE)
166
+ for row in reader:
167
+ rows_per_pair_id[row["pairID"]].append(row)
168
+
169
+ for rows in rows_per_pair_id.values():
170
+ premise = {row["language"]: row["sentence1"]
171
+ for row in rows}
172
+ hypothesis = {row["language"]: row["sentence2"]
173
+ for row in rows}
174
+ yield rows[0]["pairID"], {
175
+ "premise": premise,
176
+ "hypothesis": hypothesis,
177
+ "label": rows[0]["gold_label"],
178
+ }
179
+ else:
180
+ if data_format == "XNLI-MT":
181
+ for file_idx, filepath in enumerate(filepaths):
182
+ file = open(filepath, encoding="utf-8")
183
+ reader = csv.DictReader(
184
+ file, delimiter="\t", quoting=csv.QUOTE_NONE)
185
+ for row_idx, row in enumerate(reader):
186
+ key = str(file_idx) + "_" + str(row_idx)
187
+ yield key, {
188
+ "premise": row["premise"],
189
+ "hypothesis": row["hypo"],
190
+ "label": row["label"].replace("contradictory", "contradiction"),
191
+ }
192
+ else:
193
+ for filepath in filepaths:
194
+ with open(filepath, encoding="utf-8") as f:
195
+ reader = csv.DictReader(
196
+ f, delimiter="\t", quoting=csv.QUOTE_NONE)
197
+ for row in reader:
198
+ if row["language"] == self.config.language:
199
+ yield row["pairID"], {
200
+ "premise": row["sentence1"],
201
+ "hypothesis": row["sentence2"],
202
+ "label": row["gold_label"],
203
+ }
.history/indicxnli_20220823221318.py ADDED
@@ -0,0 +1,203 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ @misc{https://doi.org/10.48550/arxiv.2204.08776,
30
+ doi = {10.48550/ARXIV.2204.08776},
31
+
32
+ url = {https://arxiv.org/abs/2204.08776},
33
+
34
+ author = {Aggarwal, Divyanshu and Gupta, Vivek and Kunchukuttan, Anoop},
35
+
36
+ keywords = {Computation and Language (cs.CL), Artificial Intelligence (cs.AI), FOS: Computer and information sciences, FOS: Computer and information sciences},
37
+
38
+ title = {IndicXNLI: Evaluating Multilingual Inference for Indian Languages},
39
+
40
+ publisher = {arXiv},
41
+
42
+ year = {2022},
43
+
44
+ copyright = {Creative Commons Attribution 4.0 International}
45
+ }
46
+ }"""
47
+
48
+ _DESCRIPTION = """\
49
+ IndicXNLI is a translated version of XNLI to 11 Indic Languages. As with XNLI, the goal is
50
+ to predict textual entailment (does sentence A imply/contradict/neither sentence
51
+ B) and is a classification task (given two sentences, predict one of three
52
+ labels).
53
+ """
54
+
55
+ _LANGUAGES = (
56
+ 'hi',
57
+ 'bn',
58
+ 'mr',
59
+ 'as',
60
+ 'ta',
61
+ 'te',
62
+ 'or',
63
+ 'ml',
64
+ 'pa',
65
+ 'gu',
66
+ 'kn'
67
+ )
68
+
69
+
70
+ class IndicxnliConfig(datasets.BuilderConfig):
71
+ """BuilderConfig for XNLI."""
72
+
73
+ def __init__(self, language: str, **kwargs):
74
+ """BuilderConfig for XNLI.
75
+
76
+ Args:
77
+ language: One of hi, bn, mr, as, ta, te, or, ml, pa, gu, kn
78
+ **kwargs: keyword arguments forwarded to super.
79
+ """
80
+ super(IndicxnliConfig, self).__init__(**kwargs)
81
+ self.language = language
82
+
83
+
84
+ class Indicxnli(datasets.GeneratorBasedBuilder):
85
+ """XNLI: The Cross-Lingual NLI Corpus. Version 1.0."""
86
+
87
+ VERSION = datasets.Version("1.1.0", "")
88
+ BUILDER_CONFIG_CLASS = IndicxnliConfig
89
+ BUILDER_CONFIGS = [
90
+ IndicxnliConfig(
91
+ name=lang,
92
+ language=lang,
93
+ version=datasets.Version("1.1.0", ""),
94
+ description=f"Plain text import of IndicXNLI for the {lang} language",
95
+ )
96
+ for lang in _LANGUAGES
97
+ ]
98
+
99
+ def _info(self):
100
+ features = datasets.Features(
101
+ {
102
+ "premise": datasets.Value("string"),
103
+ "hypothesis": datasets.Value("string"),
104
+ "label": datasets.ClassLabel(names=["entailment", "neutral", "contradiction"]),
105
+ }
106
+ )
107
+ return datasets.DatasetInfo(
108
+ description=_DESCRIPTION,
109
+ features=features,
110
+ # No default supervised_keys (as we have to pass both premise
111
+ # and hypothesis as input).
112
+ supervised_keys=None,
113
+ homepage="https://www.nyu.edu/projects/bowman/xnli/",
114
+ citation=_CITATION,
115
+ )
116
+
117
+ def _split_generators(self, dl_manager):
118
+ train_dir = f'forward/train/xnli_{self.config.language}.json'
119
+ dev_dir = f'forward/dev/xnli_{self.config.language}.json'
120
+ test_path = f'forward/test/xnli_{self.config.language}.json'
121
+
122
+ return [
123
+ datasets.SplitGenerator(
124
+ name=datasets.Split.TRAIN,
125
+ gen_kwargs={
126
+ "filepaths": [
127
+ os.path.join(train_dir, f"multinli.train.{lang}.tsv") for lang in self.config.languages
128
+ ],
129
+ "data_format": "XNLI-MT",
130
+ },
131
+ ),
132
+ datasets.SplitGenerator(
133
+ name=datasets.Split.TEST,
134
+ gen_kwargs={"filepaths": [os.path.join(
135
+ testval_dir, "xnli.test.tsv")], "data_format": "XNLI"},
136
+ ),
137
+ datasets.SplitGenerator(
138
+ name=datasets.Split.VALIDATION,
139
+ gen_kwargs={"filepaths": [os.path.join(
140
+ testval_dir, "xnli.dev.tsv")], "data_format": "XNLI"},
141
+ ),
142
+ ]
143
+
144
+ def _generate_examples(self, data_format, filepaths):
145
+ """This function returns the examples in the raw (text) form."""
146
+
147
+ if self.config.language == "all_languages":
148
+ if data_format == "XNLI-MT":
149
+ with ExitStack() as stack:
150
+ files = [stack.enter_context(
151
+ open(filepath, encoding="utf-8")) for filepath in filepaths]
152
+ readers = [csv.DictReader(
153
+ file, delimiter="\t", quoting=csv.QUOTE_NONE) for file in files]
154
+ for row_idx, rows in enumerate(zip(*readers)):
155
+ yield row_idx, {
156
+ "premise": {lang: row["premise"] for lang, row in zip(self.config.languages, rows)},
157
+ "hypothesis": {lang: row["hypo"] for lang, row in zip(self.config.languages, rows)},
158
+ "label": rows[0]["label"].replace("contradictory", "contradiction"),
159
+ }
160
+ else:
161
+ rows_per_pair_id = collections.defaultdict(list)
162
+ for filepath in filepaths:
163
+ with open(filepath, encoding="utf-8") as f:
164
+ reader = csv.DictReader(
165
+ f, delimiter="\t", quoting=csv.QUOTE_NONE)
166
+ for row in reader:
167
+ rows_per_pair_id[row["pairID"]].append(row)
168
+
169
+ for rows in rows_per_pair_id.values():
170
+ premise = {row["language"]: row["sentence1"]
171
+ for row in rows}
172
+ hypothesis = {row["language"]: row["sentence2"]
173
+ for row in rows}
174
+ yield rows[0]["pairID"], {
175
+ "premise": premise,
176
+ "hypothesis": hypothesis,
177
+ "label": rows[0]["gold_label"],
178
+ }
179
+ else:
180
+ if data_format == "XNLI-MT":
181
+ for file_idx, filepath in enumerate(filepaths):
182
+ file = open(filepath, encoding="utf-8")
183
+ reader = csv.DictReader(
184
+ file, delimiter="\t", quoting=csv.QUOTE_NONE)
185
+ for row_idx, row in enumerate(reader):
186
+ key = str(file_idx) + "_" + str(row_idx)
187
+ yield key, {
188
+ "premise": row["premise"],
189
+ "hypothesis": row["hypo"],
190
+ "label": row["label"].replace("contradictory", "contradiction"),
191
+ }
192
+ else:
193
+ for filepath in filepaths:
194
+ with open(filepath, encoding="utf-8") as f:
195
+ reader = csv.DictReader(
196
+ f, delimiter="\t", quoting=csv.QUOTE_NONE)
197
+ for row in reader:
198
+ if row["language"] == self.config.language:
199
+ yield row["pairID"], {
200
+ "premise": row["sentence1"],
201
+ "hypothesis": row["sentence2"],
202
+ "label": row["gold_label"],
203
+ }
.history/indicxnli_20220823221321.py ADDED
@@ -0,0 +1,203 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ @misc{https://doi.org/10.48550/arxiv.2204.08776,
30
+ doi = {10.48550/ARXIV.2204.08776},
31
+
32
+ url = {https://arxiv.org/abs/2204.08776},
33
+
34
+ author = {Aggarwal, Divyanshu and Gupta, Vivek and Kunchukuttan, Anoop},
35
+
36
+ keywords = {Computation and Language (cs.CL), Artificial Intelligence (cs.AI), FOS: Computer and information sciences, FOS: Computer and information sciences},
37
+
38
+ title = {IndicXNLI: Evaluating Multilingual Inference for Indian Languages},
39
+
40
+ publisher = {arXiv},
41
+
42
+ year = {2022},
43
+
44
+ copyright = {Creative Commons Attribution 4.0 International}
45
+ }
46
+ }"""
47
+
48
+ _DESCRIPTION = """\
49
+ IndicXNLI is a translated version of XNLI to 11 Indic Languages. As with XNLI, the goal is
50
+ to predict textual entailment (does sentence A imply/contradict/neither sentence
51
+ B) and is a classification task (given two sentences, predict one of three
52
+ labels).
53
+ """
54
+
55
+ _LANGUAGES = (
56
+ 'hi',
57
+ 'bn',
58
+ 'mr',
59
+ 'as',
60
+ 'ta',
61
+ 'te',
62
+ 'or',
63
+ 'ml',
64
+ 'pa',
65
+ 'gu',
66
+ 'kn'
67
+ )
68
+
69
+
70
+ class IndicxnliConfig(datasets.BuilderConfig):
71
+ """BuilderConfig for XNLI."""
72
+
73
+ def __init__(self, language: str, **kwargs):
74
+ """BuilderConfig for XNLI.
75
+
76
+ Args:
77
+ language: One of hi, bn, mr, as, ta, te, or, ml, pa, gu, kn
78
+ **kwargs: keyword arguments forwarded to super.
79
+ """
80
+ super(IndicxnliConfig, self).__init__(**kwargs)
81
+ self.language = language
82
+
83
+
84
+ class Indicxnli(datasets.GeneratorBasedBuilder):
85
+ """XNLI: The Cross-Lingual NLI Corpus. Version 1.0."""
86
+
87
+ VERSION = datasets.Version("1.1.0", "")
88
+ BUILDER_CONFIG_CLASS = IndicxnliConfig
89
+ BUILDER_CONFIGS = [
90
+ IndicxnliConfig(
91
+ name=lang,
92
+ language=lang,
93
+ version=datasets.Version("1.1.0", ""),
94
+ description=f"Plain text import of IndicXNLI for the {lang} language",
95
+ )
96
+ for lang in _LANGUAGES
97
+ ]
98
+
99
+ def _info(self):
100
+ features = datasets.Features(
101
+ {
102
+ "premise": datasets.Value("string"),
103
+ "hypothesis": datasets.Value("string"),
104
+ "label": datasets.ClassLabel(names=["entailment", "neutral", "contradiction"]),
105
+ }
106
+ )
107
+ return datasets.DatasetInfo(
108
+ description=_DESCRIPTION,
109
+ features=features,
110
+ # No default supervised_keys (as we have to pass both premise
111
+ # and hypothesis as input).
112
+ supervised_keys=None,
113
+ homepage="https://www.nyu.edu/projects/bowman/xnli/",
114
+ citation=_CITATION,
115
+ )
116
+
117
+ def _split_generators(self, dl_manager):
118
+ train_dir = f'forward/train/xnli_{self.config.language}.json'
119
+ dev_dir = f'forward/dev/xnli_{self.config.language}.json'
120
+ test_dir = f'forward/test/xnli_{self.config.language}.json'
121
+
122
+ return [
123
+ datasets.SplitGenerator(
124
+ name=datasets.Split.TRAIN,
125
+ gen_kwargs={
126
+ "filepaths": [
127
+ os.path.join(train_dir, f"multinli.train.{lang}.tsv") for lang in self.config.languages
128
+ ],
129
+ "data_format": "XNLI-MT",
130
+ },
131
+ ),
132
+ datasets.SplitGenerator(
133
+ name=datasets.Split.TEST,
134
+ gen_kwargs={"filepaths": [os.path.join(
135
+ testval_dir, "xnli.test.tsv")], "data_format": "XNLI"},
136
+ ),
137
+ datasets.SplitGenerator(
138
+ name=datasets.Split.VALIDATION,
139
+ gen_kwargs={"filepaths": [os.path.join(
140
+ testval_dir, "xnli.dev.tsv")], "data_format": "XNLI"},
141
+ ),
142
+ ]
143
+
144
+ def _generate_examples(self, data_format, filepaths):
145
+ """This function returns the examples in the raw (text) form."""
146
+
147
+ if self.config.language == "all_languages":
148
+ if data_format == "XNLI-MT":
149
+ with ExitStack() as stack:
150
+ files = [stack.enter_context(
151
+ open(filepath, encoding="utf-8")) for filepath in filepaths]
152
+ readers = [csv.DictReader(
153
+ file, delimiter="\t", quoting=csv.QUOTE_NONE) for file in files]
154
+ for row_idx, rows in enumerate(zip(*readers)):
155
+ yield row_idx, {
156
+ "premise": {lang: row["premise"] for lang, row in zip(self.config.languages, rows)},
157
+ "hypothesis": {lang: row["hypo"] for lang, row in zip(self.config.languages, rows)},
158
+ "label": rows[0]["label"].replace("contradictory", "contradiction"),
159
+ }
160
+ else:
161
+ rows_per_pair_id = collections.defaultdict(list)
162
+ for filepath in filepaths:
163
+ with open(filepath, encoding="utf-8") as f:
164
+ reader = csv.DictReader(
165
+ f, delimiter="\t", quoting=csv.QUOTE_NONE)
166
+ for row in reader:
167
+ rows_per_pair_id[row["pairID"]].append(row)
168
+
169
+ for rows in rows_per_pair_id.values():
170
+ premise = {row["language"]: row["sentence1"]
171
+ for row in rows}
172
+ hypothesis = {row["language"]: row["sentence2"]
173
+ for row in rows}
174
+ yield rows[0]["pairID"], {
175
+ "premise": premise,
176
+ "hypothesis": hypothesis,
177
+ "label": rows[0]["gold_label"],
178
+ }
179
+ else:
180
+ if data_format == "XNLI-MT":
181
+ for file_idx, filepath in enumerate(filepaths):
182
+ file = open(filepath, encoding="utf-8")
183
+ reader = csv.DictReader(
184
+ file, delimiter="\t", quoting=csv.QUOTE_NONE)
185
+ for row_idx, row in enumerate(reader):
186
+ key = str(file_idx) + "_" + str(row_idx)
187
+ yield key, {
188
+ "premise": row["premise"],
189
+ "hypothesis": row["hypo"],
190
+ "label": row["label"].replace("contradictory", "contradiction"),
191
+ }
192
+ else:
193
+ for filepath in filepaths:
194
+ with open(filepath, encoding="utf-8") as f:
195
+ reader = csv.DictReader(
196
+ f, delimiter="\t", quoting=csv.QUOTE_NONE)
197
+ for row in reader:
198
+ if row["language"] == self.config.language:
199
+ yield row["pairID"], {
200
+ "premise": row["sentence1"],
201
+ "hypothesis": row["sentence2"],
202
+ "label": row["gold_label"],
203
+ }
.history/indicxnli_20220823221324.py ADDED
@@ -0,0 +1,203 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ @misc{https://doi.org/10.48550/arxiv.2204.08776,
30
+ doi = {10.48550/ARXIV.2204.08776},
31
+
32
+ url = {https://arxiv.org/abs/2204.08776},
33
+
34
+ author = {Aggarwal, Divyanshu and Gupta, Vivek and Kunchukuttan, Anoop},
35
+
36
+ keywords = {Computation and Language (cs.CL), Artificial Intelligence (cs.AI), FOS: Computer and information sciences, FOS: Computer and information sciences},
37
+
38
+ title = {IndicXNLI: Evaluating Multilingual Inference for Indian Languages},
39
+
40
+ publisher = {arXiv},
41
+
42
+ year = {2022},
43
+
44
+ copyright = {Creative Commons Attribution 4.0 International}
45
+ }
46
+ }"""
47
+
48
+ _DESCRIPTION = """\
49
+ IndicXNLI is a translated version of XNLI to 11 Indic Languages. As with XNLI, the goal is
50
+ to predict textual entailment (does sentence A imply/contradict/neither sentence
51
+ B) and is a classification task (given two sentences, predict one of three
52
+ labels).
53
+ """
54
+
55
+ _LANGUAGES = (
56
+ 'hi',
57
+ 'bn',
58
+ 'mr',
59
+ 'as',
60
+ 'ta',
61
+ 'te',
62
+ 'or',
63
+ 'ml',
64
+ 'pa',
65
+ 'gu',
66
+ 'kn'
67
+ )
68
+
69
+
70
+ class IndicxnliConfig(datasets.BuilderConfig):
71
+ """BuilderConfig for XNLI."""
72
+
73
+ def __init__(self, language: str, **kwargs):
74
+ """BuilderConfig for XNLI.
75
+
76
+ Args:
77
+ language: One of hi, bn, mr, as, ta, te, or, ml, pa, gu, kn
78
+ **kwargs: keyword arguments forwarded to super.
79
+ """
80
+ super(IndicxnliConfig, self).__init__(**kwargs)
81
+ self.language = language
82
+
83
+
84
+ class Indicxnli(datasets.GeneratorBasedBuilder):
85
+ """XNLI: The Cross-Lingual NLI Corpus. Version 1.0."""
86
+
87
+ VERSION = datasets.Version("1.1.0", "")
88
+ BUILDER_CONFIG_CLASS = IndicxnliConfig
89
+ BUILDER_CONFIGS = [
90
+ IndicxnliConfig(
91
+ name=lang,
92
+ language=lang,
93
+ version=datasets.Version("1.1.0", ""),
94
+ description=f"Plain text import of IndicXNLI for the {lang} language",
95
+ )
96
+ for lang in _LANGUAGES
97
+ ]
98
+
99
+ def _info(self):
100
+ features = datasets.Features(
101
+ {
102
+ "premise": datasets.Value("string"),
103
+ "hypothesis": datasets.Value("string"),
104
+ "label": datasets.ClassLabel(names=["entailment", "neutral", "contradiction"]),
105
+ }
106
+ )
107
+ return datasets.DatasetInfo(
108
+ description=_DESCRIPTION,
109
+ features=features,
110
+ # No default supervised_keys (as we have to pass both premise
111
+ # and hypothesis as input).
112
+ supervised_keys=None,
113
+ homepage="https://www.nyu.edu/projects/bowman/xnli/",
114
+ citation=_CITATION,
115
+ )
116
+
117
+ def _split_generators(self, dl_manager):
118
+ train_dir = f'forward/train/'
119
+ dev_dir = f'forward/dev/xnli_{self.config.language}.json'
120
+ test_dir = f'forward/test/xnli_{self.config.language}.json'
121
+
122
+ return [
123
+ datasets.SplitGenerator(
124
+ name=datasets.Split.TRAIN,
125
+ gen_kwargs={
126
+ "filepaths": [
127
+ os.path.join(train_dir, f"multinli.train.{lang}.tsv") for lang in self.config.languages
128
+ ],
129
+ "data_format": "XNLI-MT",
130
+ },
131
+ ),
132
+ datasets.SplitGenerator(
133
+ name=datasets.Split.TEST,
134
+ gen_kwargs={"filepaths": [os.path.join(
135
+ testval_dir, "xnli.test.tsv")], "data_format": "XNLI"},
136
+ ),
137
+ datasets.SplitGenerator(
138
+ name=datasets.Split.VALIDATION,
139
+ gen_kwargs={"filepaths": [os.path.join(
140
+ testval_dir, "xnli.dev.tsv")], "data_format": "XNLI"},
141
+ ),
142
+ ]
143
+
144
+ def _generate_examples(self, data_format, filepaths):
145
+ """This function returns the examples in the raw (text) form."""
146
+
147
+ if self.config.language == "all_languages":
148
+ if data_format == "XNLI-MT":
149
+ with ExitStack() as stack:
150
+ files = [stack.enter_context(
151
+ open(filepath, encoding="utf-8")) for filepath in filepaths]
152
+ readers = [csv.DictReader(
153
+ file, delimiter="\t", quoting=csv.QUOTE_NONE) for file in files]
154
+ for row_idx, rows in enumerate(zip(*readers)):
155
+ yield row_idx, {
156
+ "premise": {lang: row["premise"] for lang, row in zip(self.config.languages, rows)},
157
+ "hypothesis": {lang: row["hypo"] for lang, row in zip(self.config.languages, rows)},
158
+ "label": rows[0]["label"].replace("contradictory", "contradiction"),
159
+ }
160
+ else:
161
+ rows_per_pair_id = collections.defaultdict(list)
162
+ for filepath in filepaths:
163
+ with open(filepath, encoding="utf-8") as f:
164
+ reader = csv.DictReader(
165
+ f, delimiter="\t", quoting=csv.QUOTE_NONE)
166
+ for row in reader:
167
+ rows_per_pair_id[row["pairID"]].append(row)
168
+
169
+ for rows in rows_per_pair_id.values():
170
+ premise = {row["language"]: row["sentence1"]
171
+ for row in rows}
172
+ hypothesis = {row["language"]: row["sentence2"]
173
+ for row in rows}
174
+ yield rows[0]["pairID"], {
175
+ "premise": premise,
176
+ "hypothesis": hypothesis,
177
+ "label": rows[0]["gold_label"],
178
+ }
179
+ else:
180
+ if data_format == "XNLI-MT":
181
+ for file_idx, filepath in enumerate(filepaths):
182
+ file = open(filepath, encoding="utf-8")
183
+ reader = csv.DictReader(
184
+ file, delimiter="\t", quoting=csv.QUOTE_NONE)
185
+ for row_idx, row in enumerate(reader):
186
+ key = str(file_idx) + "_" + str(row_idx)
187
+ yield key, {
188
+ "premise": row["premise"],
189
+ "hypothesis": row["hypo"],
190
+ "label": row["label"].replace("contradictory", "contradiction"),
191
+ }
192
+ else:
193
+ for filepath in filepaths:
194
+ with open(filepath, encoding="utf-8") as f:
195
+ reader = csv.DictReader(
196
+ f, delimiter="\t", quoting=csv.QUOTE_NONE)
197
+ for row in reader:
198
+ if row["language"] == self.config.language:
199
+ yield row["pairID"], {
200
+ "premise": row["sentence1"],
201
+ "hypothesis": row["sentence2"],
202
+ "label": row["gold_label"],
203
+ }
.history/indicxnli_20220823221328.py ADDED
@@ -0,0 +1,203 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ @misc{https://doi.org/10.48550/arxiv.2204.08776,
30
+ doi = {10.48550/ARXIV.2204.08776},
31
+
32
+ url = {https://arxiv.org/abs/2204.08776},
33
+
34
+ author = {Aggarwal, Divyanshu and Gupta, Vivek and Kunchukuttan, Anoop},
35
+
36
+ keywords = {Computation and Language (cs.CL), Artificial Intelligence (cs.AI), FOS: Computer and information sciences, FOS: Computer and information sciences},
37
+
38
+ title = {IndicXNLI: Evaluating Multilingual Inference for Indian Languages},
39
+
40
+ publisher = {arXiv},
41
+
42
+ year = {2022},
43
+
44
+ copyright = {Creative Commons Attribution 4.0 International}
45
+ }
46
+ }"""
47
+
48
+ _DESCRIPTION = """\
49
+ IndicXNLI is a translated version of XNLI to 11 Indic Languages. As with XNLI, the goal is
50
+ to predict textual entailment (does sentence A imply/contradict/neither sentence
51
+ B) and is a classification task (given two sentences, predict one of three
52
+ labels).
53
+ """
54
+
55
+ _LANGUAGES = (
56
+ 'hi',
57
+ 'bn',
58
+ 'mr',
59
+ 'as',
60
+ 'ta',
61
+ 'te',
62
+ 'or',
63
+ 'ml',
64
+ 'pa',
65
+ 'gu',
66
+ 'kn'
67
+ )
68
+
69
+
70
+ class IndicxnliConfig(datasets.BuilderConfig):
71
+ """BuilderConfig for XNLI."""
72
+
73
+ def __init__(self, language: str, **kwargs):
74
+ """BuilderConfig for XNLI.
75
+
76
+ Args:
77
+ language: One of hi, bn, mr, as, ta, te, or, ml, pa, gu, kn
78
+ **kwargs: keyword arguments forwarded to super.
79
+ """
80
+ super(IndicxnliConfig, self).__init__(**kwargs)
81
+ self.language = language
82
+
83
+
84
+ class Indicxnli(datasets.GeneratorBasedBuilder):
85
+ """XNLI: The Cross-Lingual NLI Corpus. Version 1.0."""
86
+
87
+ VERSION = datasets.Version("1.1.0", "")
88
+ BUILDER_CONFIG_CLASS = IndicxnliConfig
89
+ BUILDER_CONFIGS = [
90
+ IndicxnliConfig(
91
+ name=lang,
92
+ language=lang,
93
+ version=datasets.Version("1.1.0", ""),
94
+ description=f"Plain text import of IndicXNLI for the {lang} language",
95
+ )
96
+ for lang in _LANGUAGES
97
+ ]
98
+
99
+ def _info(self):
100
+ features = datasets.Features(
101
+ {
102
+ "premise": datasets.Value("string"),
103
+ "hypothesis": datasets.Value("string"),
104
+ "label": datasets.ClassLabel(names=["entailment", "neutral", "contradiction"]),
105
+ }
106
+ )
107
+ return datasets.DatasetInfo(
108
+ description=_DESCRIPTION,
109
+ features=features,
110
+ # No default supervised_keys (as we have to pass both premise
111
+ # and hypothesis as input).
112
+ supervised_keys=None,
113
+ homepage="https://www.nyu.edu/projects/bowman/xnli/",
114
+ citation=_CITATION,
115
+ )
116
+
117
+ def _split_generators(self, dl_manager):
118
+ train_dir = f'forward/train/'
119
+ dev_dir = f'forward/dev/'
120
+ test_dir = f'forward/test/xnli_{self.config.language}.json'
121
+
122
+ return [
123
+ datasets.SplitGenerator(
124
+ name=datasets.Split.TRAIN,
125
+ gen_kwargs={
126
+ "filepaths": [
127
+ os.path.join(train_dir, f"multinli.train.{lang}.tsv") for lang in self.config.languages
128
+ ],
129
+ "data_format": "XNLI-MT",
130
+ },
131
+ ),
132
+ datasets.SplitGenerator(
133
+ name=datasets.Split.TEST,
134
+ gen_kwargs={"filepaths": [os.path.join(
135
+ testval_dir, "xnli.test.tsv")], "data_format": "XNLI"},
136
+ ),
137
+ datasets.SplitGenerator(
138
+ name=datasets.Split.VALIDATION,
139
+ gen_kwargs={"filepaths": [os.path.join(
140
+ testval_dir, "xnli.dev.tsv")], "data_format": "XNLI"},
141
+ ),
142
+ ]
143
+
144
+ def _generate_examples(self, data_format, filepaths):
145
+ """This function returns the examples in the raw (text) form."""
146
+
147
+ if self.config.language == "all_languages":
148
+ if data_format == "XNLI-MT":
149
+ with ExitStack() as stack:
150
+ files = [stack.enter_context(
151
+ open(filepath, encoding="utf-8")) for filepath in filepaths]
152
+ readers = [csv.DictReader(
153
+ file, delimiter="\t", quoting=csv.QUOTE_NONE) for file in files]
154
+ for row_idx, rows in enumerate(zip(*readers)):
155
+ yield row_idx, {
156
+ "premise": {lang: row["premise"] for lang, row in zip(self.config.languages, rows)},
157
+ "hypothesis": {lang: row["hypo"] for lang, row in zip(self.config.languages, rows)},
158
+ "label": rows[0]["label"].replace("contradictory", "contradiction"),
159
+ }
160
+ else:
161
+ rows_per_pair_id = collections.defaultdict(list)
162
+ for filepath in filepaths:
163
+ with open(filepath, encoding="utf-8") as f:
164
+ reader = csv.DictReader(
165
+ f, delimiter="\t", quoting=csv.QUOTE_NONE)
166
+ for row in reader:
167
+ rows_per_pair_id[row["pairID"]].append(row)
168
+
169
+ for rows in rows_per_pair_id.values():
170
+ premise = {row["language"]: row["sentence1"]
171
+ for row in rows}
172
+ hypothesis = {row["language"]: row["sentence2"]
173
+ for row in rows}
174
+ yield rows[0]["pairID"], {
175
+ "premise": premise,
176
+ "hypothesis": hypothesis,
177
+ "label": rows[0]["gold_label"],
178
+ }
179
+ else:
180
+ if data_format == "XNLI-MT":
181
+ for file_idx, filepath in enumerate(filepaths):
182
+ file = open(filepath, encoding="utf-8")
183
+ reader = csv.DictReader(
184
+ file, delimiter="\t", quoting=csv.QUOTE_NONE)
185
+ for row_idx, row in enumerate(reader):
186
+ key = str(file_idx) + "_" + str(row_idx)
187
+ yield key, {
188
+ "premise": row["premise"],
189
+ "hypothesis": row["hypo"],
190
+ "label": row["label"].replace("contradictory", "contradiction"),
191
+ }
192
+ else:
193
+ for filepath in filepaths:
194
+ with open(filepath, encoding="utf-8") as f:
195
+ reader = csv.DictReader(
196
+ f, delimiter="\t", quoting=csv.QUOTE_NONE)
197
+ for row in reader:
198
+ if row["language"] == self.config.language:
199
+ yield row["pairID"], {
200
+ "premise": row["sentence1"],
201
+ "hypothesis": row["sentence2"],
202
+ "label": row["gold_label"],
203
+ }
.history/indicxnli_20220823221331.py ADDED
@@ -0,0 +1,203 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ @misc{https://doi.org/10.48550/arxiv.2204.08776,
30
+ doi = {10.48550/ARXIV.2204.08776},
31
+
32
+ url = {https://arxiv.org/abs/2204.08776},
33
+
34
+ author = {Aggarwal, Divyanshu and Gupta, Vivek and Kunchukuttan, Anoop},
35
+
36
+ keywords = {Computation and Language (cs.CL), Artificial Intelligence (cs.AI), FOS: Computer and information sciences, FOS: Computer and information sciences},
37
+
38
+ title = {IndicXNLI: Evaluating Multilingual Inference for Indian Languages},
39
+
40
+ publisher = {arXiv},
41
+
42
+ year = {2022},
43
+
44
+ copyright = {Creative Commons Attribution 4.0 International}
45
+ }
46
+ }"""
47
+
48
+ _DESCRIPTION = """\
49
+ IndicXNLI is a translated version of XNLI to 11 Indic Languages. As with XNLI, the goal is
50
+ to predict textual entailment (does sentence A imply/contradict/neither sentence
51
+ B) and is a classification task (given two sentences, predict one of three
52
+ labels).
53
+ """
54
+
55
+ _LANGUAGES = (
56
+ 'hi',
57
+ 'bn',
58
+ 'mr',
59
+ 'as',
60
+ 'ta',
61
+ 'te',
62
+ 'or',
63
+ 'ml',
64
+ 'pa',
65
+ 'gu',
66
+ 'kn'
67
+ )
68
+
69
+
70
+ class IndicxnliConfig(datasets.BuilderConfig):
71
+ """BuilderConfig for XNLI."""
72
+
73
+ def __init__(self, language: str, **kwargs):
74
+ """BuilderConfig for XNLI.
75
+
76
+ Args:
77
+ language: One of hi, bn, mr, as, ta, te, or, ml, pa, gu, kn
78
+ **kwargs: keyword arguments forwarded to super.
79
+ """
80
+ super(IndicxnliConfig, self).__init__(**kwargs)
81
+ self.language = language
82
+
83
+
84
+ class Indicxnli(datasets.GeneratorBasedBuilder):
85
+ """XNLI: The Cross-Lingual NLI Corpus. Version 1.0."""
86
+
87
+ VERSION = datasets.Version("1.1.0", "")
88
+ BUILDER_CONFIG_CLASS = IndicxnliConfig
89
+ BUILDER_CONFIGS = [
90
+ IndicxnliConfig(
91
+ name=lang,
92
+ language=lang,
93
+ version=datasets.Version("1.1.0", ""),
94
+ description=f"Plain text import of IndicXNLI for the {lang} language",
95
+ )
96
+ for lang in _LANGUAGES
97
+ ]
98
+
99
+ def _info(self):
100
+ features = datasets.Features(
101
+ {
102
+ "premise": datasets.Value("string"),
103
+ "hypothesis": datasets.Value("string"),
104
+ "label": datasets.ClassLabel(names=["entailment", "neutral", "contradiction"]),
105
+ }
106
+ )
107
+ return datasets.DatasetInfo(
108
+ description=_DESCRIPTION,
109
+ features=features,
110
+ # No default supervised_keys (as we have to pass both premise
111
+ # and hypothesis as input).
112
+ supervised_keys=None,
113
+ homepage="https://www.nyu.edu/projects/bowman/xnli/",
114
+ citation=_CITATION,
115
+ )
116
+
117
+ def _split_generators(self, dl_manager):
118
+ train_dir = f'forward/train/'
119
+ dev_dir = f'forward/dev/'
120
+ test_dir = f'forward/test/'
121
+
122
+ return [
123
+ datasets.SplitGenerator(
124
+ name=datasets.Split.TRAIN,
125
+ gen_kwargs={
126
+ "filepaths": [
127
+ os.path.join(train_dir, f"multinli.train.{lang}.tsv") for lang in self.config.languages
128
+ ],
129
+ "data_format": "XNLI-MT",
130
+ },
131
+ ),
132
+ datasets.SplitGenerator(
133
+ name=datasets.Split.TEST,
134
+ gen_kwargs={"filepaths": [os.path.join(
135
+ testval_dir, "xnli.test.tsv")], "data_format": "XNLI"},
136
+ ),
137
+ datasets.SplitGenerator(
138
+ name=datasets.Split.VALIDATION,
139
+ gen_kwargs={"filepaths": [os.path.join(
140
+ testval_dir, "xnli.dev.tsv")], "data_format": "XNLI"},
141
+ ),
142
+ ]
143
+
144
+ def _generate_examples(self, data_format, filepaths):
145
+ """This function returns the examples in the raw (text) form."""
146
+
147
+ if self.config.language == "all_languages":
148
+ if data_format == "XNLI-MT":
149
+ with ExitStack() as stack:
150
+ files = [stack.enter_context(
151
+ open(filepath, encoding="utf-8")) for filepath in filepaths]
152
+ readers = [csv.DictReader(
153
+ file, delimiter="\t", quoting=csv.QUOTE_NONE) for file in files]
154
+ for row_idx, rows in enumerate(zip(*readers)):
155
+ yield row_idx, {
156
+ "premise": {lang: row["premise"] for lang, row in zip(self.config.languages, rows)},
157
+ "hypothesis": {lang: row["hypo"] for lang, row in zip(self.config.languages, rows)},
158
+ "label": rows[0]["label"].replace("contradictory", "contradiction"),
159
+ }
160
+ else:
161
+ rows_per_pair_id = collections.defaultdict(list)
162
+ for filepath in filepaths:
163
+ with open(filepath, encoding="utf-8") as f:
164
+ reader = csv.DictReader(
165
+ f, delimiter="\t", quoting=csv.QUOTE_NONE)
166
+ for row in reader:
167
+ rows_per_pair_id[row["pairID"]].append(row)
168
+
169
+ for rows in rows_per_pair_id.values():
170
+ premise = {row["language"]: row["sentence1"]
171
+ for row in rows}
172
+ hypothesis = {row["language"]: row["sentence2"]
173
+ for row in rows}
174
+ yield rows[0]["pairID"], {
175
+ "premise": premise,
176
+ "hypothesis": hypothesis,
177
+ "label": rows[0]["gold_label"],
178
+ }
179
+ else:
180
+ if data_format == "XNLI-MT":
181
+ for file_idx, filepath in enumerate(filepaths):
182
+ file = open(filepath, encoding="utf-8")
183
+ reader = csv.DictReader(
184
+ file, delimiter="\t", quoting=csv.QUOTE_NONE)
185
+ for row_idx, row in enumerate(reader):
186
+ key = str(file_idx) + "_" + str(row_idx)
187
+ yield key, {
188
+ "premise": row["premise"],
189
+ "hypothesis": row["hypo"],
190
+ "label": row["label"].replace("contradictory", "contradiction"),
191
+ }
192
+ else:
193
+ for filepath in filepaths:
194
+ with open(filepath, encoding="utf-8") as f:
195
+ reader = csv.DictReader(
196
+ f, delimiter="\t", quoting=csv.QUOTE_NONE)
197
+ for row in reader:
198
+ if row["language"] == self.config.language:
199
+ yield row["pairID"], {
200
+ "premise": row["sentence1"],
201
+ "hypothesis": row["sentence2"],
202
+ "label": row["gold_label"],
203
+ }
.history/indicxnli_20220823221333.py ADDED
@@ -0,0 +1,203 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ @misc{https://doi.org/10.48550/arxiv.2204.08776,
30
+ doi = {10.48550/ARXIV.2204.08776},
31
+
32
+ url = {https://arxiv.org/abs/2204.08776},
33
+
34
+ author = {Aggarwal, Divyanshu and Gupta, Vivek and Kunchukuttan, Anoop},
35
+
36
+ keywords = {Computation and Language (cs.CL), Artificial Intelligence (cs.AI), FOS: Computer and information sciences, FOS: Computer and information sciences},
37
+
38
+ title = {IndicXNLI: Evaluating Multilingual Inference for Indian Languages},
39
+
40
+ publisher = {arXiv},
41
+
42
+ year = {2022},
43
+
44
+ copyright = {Creative Commons Attribution 4.0 International}
45
+ }
46
+ }"""
47
+
48
+ _DESCRIPTION = """\
49
+ IndicXNLI is a translated version of XNLI to 11 Indic Languages. As with XNLI, the goal is
50
+ to predict textual entailment (does sentence A imply/contradict/neither sentence
51
+ B) and is a classification task (given two sentences, predict one of three
52
+ labels).
53
+ """
54
+
55
+ _LANGUAGES = (
56
+ 'hi',
57
+ 'bn',
58
+ 'mr',
59
+ 'as',
60
+ 'ta',
61
+ 'te',
62
+ 'or',
63
+ 'ml',
64
+ 'pa',
65
+ 'gu',
66
+ 'kn'
67
+ )
68
+
69
+
70
+ class IndicxnliConfig(datasets.BuilderConfig):
71
+ """BuilderConfig for XNLI."""
72
+
73
+ def __init__(self, language: str, **kwargs):
74
+ """BuilderConfig for XNLI.
75
+
76
+ Args:
77
+ language: One of hi, bn, mr, as, ta, te, or, ml, pa, gu, kn
78
+ **kwargs: keyword arguments forwarded to super.
79
+ """
80
+ super(IndicxnliConfig, self).__init__(**kwargs)
81
+ self.language = language
82
+
83
+
84
+ class Indicxnli(datasets.GeneratorBasedBuilder):
85
+ """XNLI: The Cross-Lingual NLI Corpus. Version 1.0."""
86
+
87
+ VERSION = datasets.Version("1.1.0", "")
88
+ BUILDER_CONFIG_CLASS = IndicxnliConfig
89
+ BUILDER_CONFIGS = [
90
+ IndicxnliConfig(
91
+ name=lang,
92
+ language=lang,
93
+ version=datasets.Version("1.1.0", ""),
94
+ description=f"Plain text import of IndicXNLI for the {lang} language",
95
+ )
96
+ for lang in _LANGUAGES
97
+ ]
98
+
99
+ def _info(self):
100
+ features = datasets.Features(
101
+ {
102
+ "premise": datasets.Value("string"),
103
+ "hypothesis": datasets.Value("string"),
104
+ "label": datasets.ClassLabel(names=["entailment", "neutral", "contradiction"]),
105
+ }
106
+ )
107
+ return datasets.DatasetInfo(
108
+ description=_DESCRIPTION,
109
+ features=features,
110
+ # No default supervised_keys (as we have to pass both premise
111
+ # and hypothesis as input).
112
+ supervised_keys=None,
113
+ homepage="https://www.nyu.edu/projects/bowman/xnli/",
114
+ citation=_CITATION,
115
+ )
116
+
117
+ def _split_generators(self, dl_manager):
118
+ train_dir = f'forward/train/'
119
+ dev_dir = f'forward/dev'
120
+ test_dir = f'forward/test/'
121
+
122
+ return [
123
+ datasets.SplitGenerator(
124
+ name=datasets.Split.TRAIN,
125
+ gen_kwargs={
126
+ "filepaths": [
127
+ os.path.join(train_dir, f"multinli.train.{lang}.tsv") for lang in self.config.languages
128
+ ],
129
+ "data_format": "XNLI-MT",
130
+ },
131
+ ),
132
+ datasets.SplitGenerator(
133
+ name=datasets.Split.TEST,
134
+ gen_kwargs={"filepaths": [os.path.join(
135
+ testval_dir, "xnli.test.tsv")], "data_format": "XNLI"},
136
+ ),
137
+ datasets.SplitGenerator(
138
+ name=datasets.Split.VALIDATION,
139
+ gen_kwargs={"filepaths": [os.path.join(
140
+ testval_dir, "xnli.dev.tsv")], "data_format": "XNLI"},
141
+ ),
142
+ ]
143
+
144
+ def _generate_examples(self, data_format, filepaths):
145
+ """This function returns the examples in the raw (text) form."""
146
+
147
+ if self.config.language == "all_languages":
148
+ if data_format == "XNLI-MT":
149
+ with ExitStack() as stack:
150
+ files = [stack.enter_context(
151
+ open(filepath, encoding="utf-8")) for filepath in filepaths]
152
+ readers = [csv.DictReader(
153
+ file, delimiter="\t", quoting=csv.QUOTE_NONE) for file in files]
154
+ for row_idx, rows in enumerate(zip(*readers)):
155
+ yield row_idx, {
156
+ "premise": {lang: row["premise"] for lang, row in zip(self.config.languages, rows)},
157
+ "hypothesis": {lang: row["hypo"] for lang, row in zip(self.config.languages, rows)},
158
+ "label": rows[0]["label"].replace("contradictory", "contradiction"),
159
+ }
160
+ else:
161
+ rows_per_pair_id = collections.defaultdict(list)
162
+ for filepath in filepaths:
163
+ with open(filepath, encoding="utf-8") as f:
164
+ reader = csv.DictReader(
165
+ f, delimiter="\t", quoting=csv.QUOTE_NONE)
166
+ for row in reader:
167
+ rows_per_pair_id[row["pairID"]].append(row)
168
+
169
+ for rows in rows_per_pair_id.values():
170
+ premise = {row["language"]: row["sentence1"]
171
+ for row in rows}
172
+ hypothesis = {row["language"]: row["sentence2"]
173
+ for row in rows}
174
+ yield rows[0]["pairID"], {
175
+ "premise": premise,
176
+ "hypothesis": hypothesis,
177
+ "label": rows[0]["gold_label"],
178
+ }
179
+ else:
180
+ if data_format == "XNLI-MT":
181
+ for file_idx, filepath in enumerate(filepaths):
182
+ file = open(filepath, encoding="utf-8")
183
+ reader = csv.DictReader(
184
+ file, delimiter="\t", quoting=csv.QUOTE_NONE)
185
+ for row_idx, row in enumerate(reader):
186
+ key = str(file_idx) + "_" + str(row_idx)
187
+ yield key, {
188
+ "premise": row["premise"],
189
+ "hypothesis": row["hypo"],
190
+ "label": row["label"].replace("contradictory", "contradiction"),
191
+ }
192
+ else:
193
+ for filepath in filepaths:
194
+ with open(filepath, encoding="utf-8") as f:
195
+ reader = csv.DictReader(
196
+ f, delimiter="\t", quoting=csv.QUOTE_NONE)
197
+ for row in reader:
198
+ if row["language"] == self.config.language:
199
+ yield row["pairID"], {
200
+ "premise": row["sentence1"],
201
+ "hypothesis": row["sentence2"],
202
+ "label": row["gold_label"],
203
+ }
.history/indicxnli_20220823221334.py ADDED
@@ -0,0 +1,203 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ @misc{https://doi.org/10.48550/arxiv.2204.08776,
30
+ doi = {10.48550/ARXIV.2204.08776},
31
+
32
+ url = {https://arxiv.org/abs/2204.08776},
33
+
34
+ author = {Aggarwal, Divyanshu and Gupta, Vivek and Kunchukuttan, Anoop},
35
+
36
+ keywords = {Computation and Language (cs.CL), Artificial Intelligence (cs.AI), FOS: Computer and information sciences, FOS: Computer and information sciences},
37
+
38
+ title = {IndicXNLI: Evaluating Multilingual Inference for Indian Languages},
39
+
40
+ publisher = {arXiv},
41
+
42
+ year = {2022},
43
+
44
+ copyright = {Creative Commons Attribution 4.0 International}
45
+ }
46
+ }"""
47
+
48
+ _DESCRIPTION = """\
49
+ IndicXNLI is a translated version of XNLI to 11 Indic Languages. As with XNLI, the goal is
50
+ to predict textual entailment (does sentence A imply/contradict/neither sentence
51
+ B) and is a classification task (given two sentences, predict one of three
52
+ labels).
53
+ """
54
+
55
+ _LANGUAGES = (
56
+ 'hi',
57
+ 'bn',
58
+ 'mr',
59
+ 'as',
60
+ 'ta',
61
+ 'te',
62
+ 'or',
63
+ 'ml',
64
+ 'pa',
65
+ 'gu',
66
+ 'kn'
67
+ )
68
+
69
+
70
+ class IndicxnliConfig(datasets.BuilderConfig):
71
+ """BuilderConfig for XNLI."""
72
+
73
+ def __init__(self, language: str, **kwargs):
74
+ """BuilderConfig for XNLI.
75
+
76
+ Args:
77
+ language: One of hi, bn, mr, as, ta, te, or, ml, pa, gu, kn
78
+ **kwargs: keyword arguments forwarded to super.
79
+ """
80
+ super(IndicxnliConfig, self).__init__(**kwargs)
81
+ self.language = language
82
+
83
+
84
+ class Indicxnli(datasets.GeneratorBasedBuilder):
85
+ """XNLI: The Cross-Lingual NLI Corpus. Version 1.0."""
86
+
87
+ VERSION = datasets.Version("1.1.0", "")
88
+ BUILDER_CONFIG_CLASS = IndicxnliConfig
89
+ BUILDER_CONFIGS = [
90
+ IndicxnliConfig(
91
+ name=lang,
92
+ language=lang,
93
+ version=datasets.Version("1.1.0", ""),
94
+ description=f"Plain text import of IndicXNLI for the {lang} language",
95
+ )
96
+ for lang in _LANGUAGES
97
+ ]
98
+
99
+ def _info(self):
100
+ features = datasets.Features(
101
+ {
102
+ "premise": datasets.Value("string"),
103
+ "hypothesis": datasets.Value("string"),
104
+ "label": datasets.ClassLabel(names=["entailment", "neutral", "contradiction"]),
105
+ }
106
+ )
107
+ return datasets.DatasetInfo(
108
+ description=_DESCRIPTION,
109
+ features=features,
110
+ # No default supervised_keys (as we have to pass both premise
111
+ # and hypothesis as input).
112
+ supervised_keys=None,
113
+ homepage="https://www.nyu.edu/projects/bowman/xnli/",
114
+ citation=_CITATION,
115
+ )
116
+
117
+ def _split_generators(self, dl_manager):
118
+ train_dir = f'forward/train/'
119
+ dev_dir = f'forward/dev'
120
+ test_dir = f'forward/test'
121
+
122
+ return [
123
+ datasets.SplitGenerator(
124
+ name=datasets.Split.TRAIN,
125
+ gen_kwargs={
126
+ "filepaths": [
127
+ os.path.join(train_dir, f"multinli.train.{lang}.tsv") for lang in self.config.languages
128
+ ],
129
+ "data_format": "XNLI-MT",
130
+ },
131
+ ),
132
+ datasets.SplitGenerator(
133
+ name=datasets.Split.TEST,
134
+ gen_kwargs={"filepaths": [os.path.join(
135
+ testval_dir, "xnli.test.tsv")], "data_format": "XNLI"},
136
+ ),
137
+ datasets.SplitGenerator(
138
+ name=datasets.Split.VALIDATION,
139
+ gen_kwargs={"filepaths": [os.path.join(
140
+ testval_dir, "xnli.dev.tsv")], "data_format": "XNLI"},
141
+ ),
142
+ ]
143
+
144
+ def _generate_examples(self, data_format, filepaths):
145
+ """This function returns the examples in the raw (text) form."""
146
+
147
+ if self.config.language == "all_languages":
148
+ if data_format == "XNLI-MT":
149
+ with ExitStack() as stack:
150
+ files = [stack.enter_context(
151
+ open(filepath, encoding="utf-8")) for filepath in filepaths]
152
+ readers = [csv.DictReader(
153
+ file, delimiter="\t", quoting=csv.QUOTE_NONE) for file in files]
154
+ for row_idx, rows in enumerate(zip(*readers)):
155
+ yield row_idx, {
156
+ "premise": {lang: row["premise"] for lang, row in zip(self.config.languages, rows)},
157
+ "hypothesis": {lang: row["hypo"] for lang, row in zip(self.config.languages, rows)},
158
+ "label": rows[0]["label"].replace("contradictory", "contradiction"),
159
+ }
160
+ else:
161
+ rows_per_pair_id = collections.defaultdict(list)
162
+ for filepath in filepaths:
163
+ with open(filepath, encoding="utf-8") as f:
164
+ reader = csv.DictReader(
165
+ f, delimiter="\t", quoting=csv.QUOTE_NONE)
166
+ for row in reader:
167
+ rows_per_pair_id[row["pairID"]].append(row)
168
+
169
+ for rows in rows_per_pair_id.values():
170
+ premise = {row["language"]: row["sentence1"]
171
+ for row in rows}
172
+ hypothesis = {row["language"]: row["sentence2"]
173
+ for row in rows}
174
+ yield rows[0]["pairID"], {
175
+ "premise": premise,
176
+ "hypothesis": hypothesis,
177
+ "label": rows[0]["gold_label"],
178
+ }
179
+ else:
180
+ if data_format == "XNLI-MT":
181
+ for file_idx, filepath in enumerate(filepaths):
182
+ file = open(filepath, encoding="utf-8")
183
+ reader = csv.DictReader(
184
+ file, delimiter="\t", quoting=csv.QUOTE_NONE)
185
+ for row_idx, row in enumerate(reader):
186
+ key = str(file_idx) + "_" + str(row_idx)
187
+ yield key, {
188
+ "premise": row["premise"],
189
+ "hypothesis": row["hypo"],
190
+ "label": row["label"].replace("contradictory", "contradiction"),
191
+ }
192
+ else:
193
+ for filepath in filepaths:
194
+ with open(filepath, encoding="utf-8") as f:
195
+ reader = csv.DictReader(
196
+ f, delimiter="\t", quoting=csv.QUOTE_NONE)
197
+ for row in reader:
198
+ if row["language"] == self.config.language:
199
+ yield row["pairID"], {
200
+ "premise": row["sentence1"],
201
+ "hypothesis": row["sentence2"],
202
+ "label": row["gold_label"],
203
+ }
.history/indicxnli_20220823221336.py ADDED
@@ -0,0 +1,203 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ @misc{https://doi.org/10.48550/arxiv.2204.08776,
30
+ doi = {10.48550/ARXIV.2204.08776},
31
+
32
+ url = {https://arxiv.org/abs/2204.08776},
33
+
34
+ author = {Aggarwal, Divyanshu and Gupta, Vivek and Kunchukuttan, Anoop},
35
+
36
+ keywords = {Computation and Language (cs.CL), Artificial Intelligence (cs.AI), FOS: Computer and information sciences, FOS: Computer and information sciences},
37
+
38
+ title = {IndicXNLI: Evaluating Multilingual Inference for Indian Languages},
39
+
40
+ publisher = {arXiv},
41
+
42
+ year = {2022},
43
+
44
+ copyright = {Creative Commons Attribution 4.0 International}
45
+ }
46
+ }"""
47
+
48
+ _DESCRIPTION = """\
49
+ IndicXNLI is a translated version of XNLI to 11 Indic Languages. As with XNLI, the goal is
50
+ to predict textual entailment (does sentence A imply/contradict/neither sentence
51
+ B) and is a classification task (given two sentences, predict one of three
52
+ labels).
53
+ """
54
+
55
+ _LANGUAGES = (
56
+ 'hi',
57
+ 'bn',
58
+ 'mr',
59
+ 'as',
60
+ 'ta',
61
+ 'te',
62
+ 'or',
63
+ 'ml',
64
+ 'pa',
65
+ 'gu',
66
+ 'kn'
67
+ )
68
+
69
+
70
+ class IndicxnliConfig(datasets.BuilderConfig):
71
+ """BuilderConfig for XNLI."""
72
+
73
+ def __init__(self, language: str, **kwargs):
74
+ """BuilderConfig for XNLI.
75
+
76
+ Args:
77
+ language: One of hi, bn, mr, as, ta, te, or, ml, pa, gu, kn
78
+ **kwargs: keyword arguments forwarded to super.
79
+ """
80
+ super(IndicxnliConfig, self).__init__(**kwargs)
81
+ self.language = language
82
+
83
+
84
+ class Indicxnli(datasets.GeneratorBasedBuilder):
85
+ """XNLI: The Cross-Lingual NLI Corpus. Version 1.0."""
86
+
87
+ VERSION = datasets.Version("1.1.0", "")
88
+ BUILDER_CONFIG_CLASS = IndicxnliConfig
89
+ BUILDER_CONFIGS = [
90
+ IndicxnliConfig(
91
+ name=lang,
92
+ language=lang,
93
+ version=datasets.Version("1.1.0", ""),
94
+ description=f"Plain text import of IndicXNLI for the {lang} language",
95
+ )
96
+ for lang in _LANGUAGES
97
+ ]
98
+
99
+ def _info(self):
100
+ features = datasets.Features(
101
+ {
102
+ "premise": datasets.Value("string"),
103
+ "hypothesis": datasets.Value("string"),
104
+ "label": datasets.ClassLabel(names=["entailment", "neutral", "contradiction"]),
105
+ }
106
+ )
107
+ return datasets.DatasetInfo(
108
+ description=_DESCRIPTION,
109
+ features=features,
110
+ # No default supervised_keys (as we have to pass both premise
111
+ # and hypothesis as input).
112
+ supervised_keys=None,
113
+ homepage="https://www.nyu.edu/projects/bowman/xnli/",
114
+ citation=_CITATION,
115
+ )
116
+
117
+ def _split_generators(self, dl_manager):
118
+ train_dir = f'forward/train'
119
+ dev_dir = f'forward/dev'
120
+ test_dir = f'forward/test'
121
+
122
+ return [
123
+ datasets.SplitGenerator(
124
+ name=datasets.Split.TRAIN,
125
+ gen_kwargs={
126
+ "filepaths": [
127
+ os.path.join(train_dir, f"multinli.train.{lang}.tsv") for lang in self.config.languages
128
+ ],
129
+ "data_format": "XNLI-MT",
130
+ },
131
+ ),
132
+ datasets.SplitGenerator(
133
+ name=datasets.Split.TEST,
134
+ gen_kwargs={"filepaths": [os.path.join(
135
+ testval_dir, "xnli.test.tsv")], "data_format": "XNLI"},
136
+ ),
137
+ datasets.SplitGenerator(
138
+ name=datasets.Split.VALIDATION,
139
+ gen_kwargs={"filepaths": [os.path.join(
140
+ testval_dir, "xnli.dev.tsv")], "data_format": "XNLI"},
141
+ ),
142
+ ]
143
+
144
+ def _generate_examples(self, data_format, filepaths):
145
+ """This function returns the examples in the raw (text) form."""
146
+
147
+ if self.config.language == "all_languages":
148
+ if data_format == "XNLI-MT":
149
+ with ExitStack() as stack:
150
+ files = [stack.enter_context(
151
+ open(filepath, encoding="utf-8")) for filepath in filepaths]
152
+ readers = [csv.DictReader(
153
+ file, delimiter="\t", quoting=csv.QUOTE_NONE) for file in files]
154
+ for row_idx, rows in enumerate(zip(*readers)):
155
+ yield row_idx, {
156
+ "premise": {lang: row["premise"] for lang, row in zip(self.config.languages, rows)},
157
+ "hypothesis": {lang: row["hypo"] for lang, row in zip(self.config.languages, rows)},
158
+ "label": rows[0]["label"].replace("contradictory", "contradiction"),
159
+ }
160
+ else:
161
+ rows_per_pair_id = collections.defaultdict(list)
162
+ for filepath in filepaths:
163
+ with open(filepath, encoding="utf-8") as f:
164
+ reader = csv.DictReader(
165
+ f, delimiter="\t", quoting=csv.QUOTE_NONE)
166
+ for row in reader:
167
+ rows_per_pair_id[row["pairID"]].append(row)
168
+
169
+ for rows in rows_per_pair_id.values():
170
+ premise = {row["language"]: row["sentence1"]
171
+ for row in rows}
172
+ hypothesis = {row["language"]: row["sentence2"]
173
+ for row in rows}
174
+ yield rows[0]["pairID"], {
175
+ "premise": premise,
176
+ "hypothesis": hypothesis,
177
+ "label": rows[0]["gold_label"],
178
+ }
179
+ else:
180
+ if data_format == "XNLI-MT":
181
+ for file_idx, filepath in enumerate(filepaths):
182
+ file = open(filepath, encoding="utf-8")
183
+ reader = csv.DictReader(
184
+ file, delimiter="\t", quoting=csv.QUOTE_NONE)
185
+ for row_idx, row in enumerate(reader):
186
+ key = str(file_idx) + "_" + str(row_idx)
187
+ yield key, {
188
+ "premise": row["premise"],
189
+ "hypothesis": row["hypo"],
190
+ "label": row["label"].replace("contradictory", "contradiction"),
191
+ }
192
+ else:
193
+ for filepath in filepaths:
194
+ with open(filepath, encoding="utf-8") as f:
195
+ reader = csv.DictReader(
196
+ f, delimiter="\t", quoting=csv.QUOTE_NONE)
197
+ for row in reader:
198
+ if row["language"] == self.config.language:
199
+ yield row["pairID"], {
200
+ "premise": row["sentence1"],
201
+ "hypothesis": row["sentence2"],
202
+ "label": row["gold_label"],
203
+ }
.history/indicxnli_20220823221338.py ADDED
@@ -0,0 +1,203 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ @misc{https://doi.org/10.48550/arxiv.2204.08776,
30
+ doi = {10.48550/ARXIV.2204.08776},
31
+
32
+ url = {https://arxiv.org/abs/2204.08776},
33
+
34
+ author = {Aggarwal, Divyanshu and Gupta, Vivek and Kunchukuttan, Anoop},
35
+
36
+ keywords = {Computation and Language (cs.CL), Artificial Intelligence (cs.AI), FOS: Computer and information sciences, FOS: Computer and information sciences},
37
+
38
+ title = {IndicXNLI: Evaluating Multilingual Inference for Indian Languages},
39
+
40
+ publisher = {arXiv},
41
+
42
+ year = {2022},
43
+
44
+ copyright = {Creative Commons Attribution 4.0 International}
45
+ }
46
+ }"""
47
+
48
+ _DESCRIPTION = """\
49
+ IndicXNLI is a translated version of XNLI to 11 Indic Languages. As with XNLI, the goal is
50
+ to predict textual entailment (does sentence A imply/contradict/neither sentence
51
+ B) and is a classification task (given two sentences, predict one of three
52
+ labels).
53
+ """
54
+
55
+ _LANGUAGES = (
56
+ 'hi',
57
+ 'bn',
58
+ 'mr',
59
+ 'as',
60
+ 'ta',
61
+ 'te',
62
+ 'or',
63
+ 'ml',
64
+ 'pa',
65
+ 'gu',
66
+ 'kn'
67
+ )
68
+
69
+
70
+ class IndicxnliConfig(datasets.BuilderConfig):
71
+ """BuilderConfig for XNLI."""
72
+
73
+ def __init__(self, language: str, **kwargs):
74
+ """BuilderConfig for XNLI.
75
+
76
+ Args:
77
+ language: One of hi, bn, mr, as, ta, te, or, ml, pa, gu, kn
78
+ **kwargs: keyword arguments forwarded to super.
79
+ """
80
+ super(IndicxnliConfig, self).__init__(**kwargs)
81
+ self.language = language
82
+
83
+
84
+ class Indicxnli(datasets.GeneratorBasedBuilder):
85
+ """XNLI: The Cross-Lingual NLI Corpus. Version 1.0."""
86
+
87
+ VERSION = datasets.Version("1.1.0", "")
88
+ BUILDER_CONFIG_CLASS = IndicxnliConfig
89
+ BUILDER_CONFIGS = [
90
+ IndicxnliConfig(
91
+ name=lang,
92
+ language=lang,
93
+ version=datasets.Version("1.1.0", ""),
94
+ description=f"Plain text import of IndicXNLI for the {lang} language",
95
+ )
96
+ for lang in _LANGUAGES
97
+ ]
98
+
99
+ def _info(self):
100
+ features = datasets.Features(
101
+ {
102
+ "premise": datasets.Value("string"),
103
+ "hypothesis": datasets.Value("string"),
104
+ "label": datasets.ClassLabel(names=["entailment", "neutral", "contradiction"]),
105
+ }
106
+ )
107
+ return datasets.DatasetInfo(
108
+ description=_DESCRIPTION,
109
+ features=features,
110
+ # No default supervised_keys (as we have to pass both premise
111
+ # and hypothesis as input).
112
+ supervised_keys=None,
113
+ homepage="https://www.nyu.edu/projects/bowman/xnli/",
114
+ citation=_CITATION,
115
+ )
116
+
117
+ def _split_generators(self, dl_manager):
118
+ train_dir = 'forward/train'
119
+ dev_dir = f'forward/dev'
120
+ test_dir = f'forward/test'
121
+
122
+ return [
123
+ datasets.SplitGenerator(
124
+ name=datasets.Split.TRAIN,
125
+ gen_kwargs={
126
+ "filepaths": [
127
+ os.path.join(train_dir, f"multinli.train.{lang}.tsv") for lang in self.config.languages
128
+ ],
129
+ "data_format": "XNLI-MT",
130
+ },
131
+ ),
132
+ datasets.SplitGenerator(
133
+ name=datasets.Split.TEST,
134
+ gen_kwargs={"filepaths": [os.path.join(
135
+ testval_dir, "xnli.test.tsv")], "data_format": "XNLI"},
136
+ ),
137
+ datasets.SplitGenerator(
138
+ name=datasets.Split.VALIDATION,
139
+ gen_kwargs={"filepaths": [os.path.join(
140
+ testval_dir, "xnli.dev.tsv")], "data_format": "XNLI"},
141
+ ),
142
+ ]
143
+
144
+ def _generate_examples(self, data_format, filepaths):
145
+ """This function returns the examples in the raw (text) form."""
146
+
147
+ if self.config.language == "all_languages":
148
+ if data_format == "XNLI-MT":
149
+ with ExitStack() as stack:
150
+ files = [stack.enter_context(
151
+ open(filepath, encoding="utf-8")) for filepath in filepaths]
152
+ readers = [csv.DictReader(
153
+ file, delimiter="\t", quoting=csv.QUOTE_NONE) for file in files]
154
+ for row_idx, rows in enumerate(zip(*readers)):
155
+ yield row_idx, {
156
+ "premise": {lang: row["premise"] for lang, row in zip(self.config.languages, rows)},
157
+ "hypothesis": {lang: row["hypo"] for lang, row in zip(self.config.languages, rows)},
158
+ "label": rows[0]["label"].replace("contradictory", "contradiction"),
159
+ }
160
+ else:
161
+ rows_per_pair_id = collections.defaultdict(list)
162
+ for filepath in filepaths:
163
+ with open(filepath, encoding="utf-8") as f:
164
+ reader = csv.DictReader(
165
+ f, delimiter="\t", quoting=csv.QUOTE_NONE)
166
+ for row in reader:
167
+ rows_per_pair_id[row["pairID"]].append(row)
168
+
169
+ for rows in rows_per_pair_id.values():
170
+ premise = {row["language"]: row["sentence1"]
171
+ for row in rows}
172
+ hypothesis = {row["language"]: row["sentence2"]
173
+ for row in rows}
174
+ yield rows[0]["pairID"], {
175
+ "premise": premise,
176
+ "hypothesis": hypothesis,
177
+ "label": rows[0]["gold_label"],
178
+ }
179
+ else:
180
+ if data_format == "XNLI-MT":
181
+ for file_idx, filepath in enumerate(filepaths):
182
+ file = open(filepath, encoding="utf-8")
183
+ reader = csv.DictReader(
184
+ file, delimiter="\t", quoting=csv.QUOTE_NONE)
185
+ for row_idx, row in enumerate(reader):
186
+ key = str(file_idx) + "_" + str(row_idx)
187
+ yield key, {
188
+ "premise": row["premise"],
189
+ "hypothesis": row["hypo"],
190
+ "label": row["label"].replace("contradictory", "contradiction"),
191
+ }
192
+ else:
193
+ for filepath in filepaths:
194
+ with open(filepath, encoding="utf-8") as f:
195
+ reader = csv.DictReader(
196
+ f, delimiter="\t", quoting=csv.QUOTE_NONE)
197
+ for row in reader:
198
+ if row["language"] == self.config.language:
199
+ yield row["pairID"], {
200
+ "premise": row["sentence1"],
201
+ "hypothesis": row["sentence2"],
202
+ "label": row["gold_label"],
203
+ }
.history/indicxnli_20220823221339.py ADDED
@@ -0,0 +1,203 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ @misc{https://doi.org/10.48550/arxiv.2204.08776,
30
+ doi = {10.48550/ARXIV.2204.08776},
31
+
32
+ url = {https://arxiv.org/abs/2204.08776},
33
+
34
+ author = {Aggarwal, Divyanshu and Gupta, Vivek and Kunchukuttan, Anoop},
35
+
36
+ keywords = {Computation and Language (cs.CL), Artificial Intelligence (cs.AI), FOS: Computer and information sciences, FOS: Computer and information sciences},
37
+
38
+ title = {IndicXNLI: Evaluating Multilingual Inference for Indian Languages},
39
+
40
+ publisher = {arXiv},
41
+
42
+ year = {2022},
43
+
44
+ copyright = {Creative Commons Attribution 4.0 International}
45
+ }
46
+ }"""
47
+
48
+ _DESCRIPTION = """\
49
+ IndicXNLI is a translated version of XNLI to 11 Indic Languages. As with XNLI, the goal is
50
+ to predict textual entailment (does sentence A imply/contradict/neither sentence
51
+ B) and is a classification task (given two sentences, predict one of three
52
+ labels).
53
+ """
54
+
55
+ _LANGUAGES = (
56
+ 'hi',
57
+ 'bn',
58
+ 'mr',
59
+ 'as',
60
+ 'ta',
61
+ 'te',
62
+ 'or',
63
+ 'ml',
64
+ 'pa',
65
+ 'gu',
66
+ 'kn'
67
+ )
68
+
69
+
70
+ class IndicxnliConfig(datasets.BuilderConfig):
71
+ """BuilderConfig for XNLI."""
72
+
73
+ def __init__(self, language: str, **kwargs):
74
+ """BuilderConfig for XNLI.
75
+
76
+ Args:
77
+ language: One of hi, bn, mr, as, ta, te, or, ml, pa, gu, kn
78
+ **kwargs: keyword arguments forwarded to super.
79
+ """
80
+ super(IndicxnliConfig, self).__init__(**kwargs)
81
+ self.language = language
82
+
83
+
84
+ class Indicxnli(datasets.GeneratorBasedBuilder):
85
+ """XNLI: The Cross-Lingual NLI Corpus. Version 1.0."""
86
+
87
+ VERSION = datasets.Version("1.1.0", "")
88
+ BUILDER_CONFIG_CLASS = IndicxnliConfig
89
+ BUILDER_CONFIGS = [
90
+ IndicxnliConfig(
91
+ name=lang,
92
+ language=lang,
93
+ version=datasets.Version("1.1.0", ""),
94
+ description=f"Plain text import of IndicXNLI for the {lang} language",
95
+ )
96
+ for lang in _LANGUAGES
97
+ ]
98
+
99
+ def _info(self):
100
+ features = datasets.Features(
101
+ {
102
+ "premise": datasets.Value("string"),
103
+ "hypothesis": datasets.Value("string"),
104
+ "label": datasets.ClassLabel(names=["entailment", "neutral", "contradiction"]),
105
+ }
106
+ )
107
+ return datasets.DatasetInfo(
108
+ description=_DESCRIPTION,
109
+ features=features,
110
+ # No default supervised_keys (as we have to pass both premise
111
+ # and hypothesis as input).
112
+ supervised_keys=None,
113
+ homepage="https://www.nyu.edu/projects/bowman/xnli/",
114
+ citation=_CITATION,
115
+ )
116
+
117
+ def _split_generators(self, dl_manager):
118
+ train_dir = 'forward/train'
119
+ dev_dir = 'forward/dev'
120
+ test_dir = f'forward/test'
121
+
122
+ return [
123
+ datasets.SplitGenerator(
124
+ name=datasets.Split.TRAIN,
125
+ gen_kwargs={
126
+ "filepaths": [
127
+ os.path.join(train_dir, f"multinli.train.{lang}.tsv") for lang in self.config.languages
128
+ ],
129
+ "data_format": "XNLI-MT",
130
+ },
131
+ ),
132
+ datasets.SplitGenerator(
133
+ name=datasets.Split.TEST,
134
+ gen_kwargs={"filepaths": [os.path.join(
135
+ testval_dir, "xnli.test.tsv")], "data_format": "XNLI"},
136
+ ),
137
+ datasets.SplitGenerator(
138
+ name=datasets.Split.VALIDATION,
139
+ gen_kwargs={"filepaths": [os.path.join(
140
+ testval_dir, "xnli.dev.tsv")], "data_format": "XNLI"},
141
+ ),
142
+ ]
143
+
144
+ def _generate_examples(self, data_format, filepaths):
145
+ """This function returns the examples in the raw (text) form."""
146
+
147
+ if self.config.language == "all_languages":
148
+ if data_format == "XNLI-MT":
149
+ with ExitStack() as stack:
150
+ files = [stack.enter_context(
151
+ open(filepath, encoding="utf-8")) for filepath in filepaths]
152
+ readers = [csv.DictReader(
153
+ file, delimiter="\t", quoting=csv.QUOTE_NONE) for file in files]
154
+ for row_idx, rows in enumerate(zip(*readers)):
155
+ yield row_idx, {
156
+ "premise": {lang: row["premise"] for lang, row in zip(self.config.languages, rows)},
157
+ "hypothesis": {lang: row["hypo"] for lang, row in zip(self.config.languages, rows)},
158
+ "label": rows[0]["label"].replace("contradictory", "contradiction"),
159
+ }
160
+ else:
161
+ rows_per_pair_id = collections.defaultdict(list)
162
+ for filepath in filepaths:
163
+ with open(filepath, encoding="utf-8") as f:
164
+ reader = csv.DictReader(
165
+ f, delimiter="\t", quoting=csv.QUOTE_NONE)
166
+ for row in reader:
167
+ rows_per_pair_id[row["pairID"]].append(row)
168
+
169
+ for rows in rows_per_pair_id.values():
170
+ premise = {row["language"]: row["sentence1"]
171
+ for row in rows}
172
+ hypothesis = {row["language"]: row["sentence2"]
173
+ for row in rows}
174
+ yield rows[0]["pairID"], {
175
+ "premise": premise,
176
+ "hypothesis": hypothesis,
177
+ "label": rows[0]["gold_label"],
178
+ }
179
+ else:
180
+ if data_format == "XNLI-MT":
181
+ for file_idx, filepath in enumerate(filepaths):
182
+ file = open(filepath, encoding="utf-8")
183
+ reader = csv.DictReader(
184
+ file, delimiter="\t", quoting=csv.QUOTE_NONE)
185
+ for row_idx, row in enumerate(reader):
186
+ key = str(file_idx) + "_" + str(row_idx)
187
+ yield key, {
188
+ "premise": row["premise"],
189
+ "hypothesis": row["hypo"],
190
+ "label": row["label"].replace("contradictory", "contradiction"),
191
+ }
192
+ else:
193
+ for filepath in filepaths:
194
+ with open(filepath, encoding="utf-8") as f:
195
+ reader = csv.DictReader(
196
+ f, delimiter="\t", quoting=csv.QUOTE_NONE)
197
+ for row in reader:
198
+ if row["language"] == self.config.language:
199
+ yield row["pairID"], {
200
+ "premise": row["sentence1"],
201
+ "hypothesis": row["sentence2"],
202
+ "label": row["gold_label"],
203
+ }
.history/indicxnli_20220823221341.py ADDED
@@ -0,0 +1,203 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ @misc{https://doi.org/10.48550/arxiv.2204.08776,
30
+ doi = {10.48550/ARXIV.2204.08776},
31
+
32
+ url = {https://arxiv.org/abs/2204.08776},
33
+
34
+ author = {Aggarwal, Divyanshu and Gupta, Vivek and Kunchukuttan, Anoop},
35
+
36
+ keywords = {Computation and Language (cs.CL), Artificial Intelligence (cs.AI), FOS: Computer and information sciences, FOS: Computer and information sciences},
37
+
38
+ title = {IndicXNLI: Evaluating Multilingual Inference for Indian Languages},
39
+
40
+ publisher = {arXiv},
41
+
42
+ year = {2022},
43
+
44
+ copyright = {Creative Commons Attribution 4.0 International}
45
+ }
46
+ }"""
47
+
48
+ _DESCRIPTION = """\
49
+ IndicXNLI is a translated version of XNLI to 11 Indic Languages. As with XNLI, the goal is
50
+ to predict textual entailment (does sentence A imply/contradict/neither sentence
51
+ B) and is a classification task (given two sentences, predict one of three
52
+ labels).
53
+ """
54
+
55
+ _LANGUAGES = (
56
+ 'hi',
57
+ 'bn',
58
+ 'mr',
59
+ 'as',
60
+ 'ta',
61
+ 'te',
62
+ 'or',
63
+ 'ml',
64
+ 'pa',
65
+ 'gu',
66
+ 'kn'
67
+ )
68
+
69
+
70
+ class IndicxnliConfig(datasets.BuilderConfig):
71
+ """BuilderConfig for XNLI."""
72
+
73
+ def __init__(self, language: str, **kwargs):
74
+ """BuilderConfig for XNLI.
75
+
76
+ Args:
77
+ language: One of hi, bn, mr, as, ta, te, or, ml, pa, gu, kn
78
+ **kwargs: keyword arguments forwarded to super.
79
+ """
80
+ super(IndicxnliConfig, self).__init__(**kwargs)
81
+ self.language = language
82
+
83
+
84
+ class Indicxnli(datasets.GeneratorBasedBuilder):
85
+ """XNLI: The Cross-Lingual NLI Corpus. Version 1.0."""
86
+
87
+ VERSION = datasets.Version("1.1.0", "")
88
+ BUILDER_CONFIG_CLASS = IndicxnliConfig
89
+ BUILDER_CONFIGS = [
90
+ IndicxnliConfig(
91
+ name=lang,
92
+ language=lang,
93
+ version=datasets.Version("1.1.0", ""),
94
+ description=f"Plain text import of IndicXNLI for the {lang} language",
95
+ )
96
+ for lang in _LANGUAGES
97
+ ]
98
+
99
+ def _info(self):
100
+ features = datasets.Features(
101
+ {
102
+ "premise": datasets.Value("string"),
103
+ "hypothesis": datasets.Value("string"),
104
+ "label": datasets.ClassLabel(names=["entailment", "neutral", "contradiction"]),
105
+ }
106
+ )
107
+ return datasets.DatasetInfo(
108
+ description=_DESCRIPTION,
109
+ features=features,
110
+ # No default supervised_keys (as we have to pass both premise
111
+ # and hypothesis as input).
112
+ supervised_keys=None,
113
+ homepage="https://www.nyu.edu/projects/bowman/xnli/",
114
+ citation=_CITATION,
115
+ )
116
+
117
+ def _split_generators(self, dl_manager):
118
+ train_dir = 'forward/train'
119
+ dev_dir = 'forward/dev'
120
+ test_dir = 'forward/test'
121
+
122
+ return [
123
+ datasets.SplitGenerator(
124
+ name=datasets.Split.TRAIN,
125
+ gen_kwargs={
126
+ "filepaths": [
127
+ os.path.join(train_dir, f"multinli.train.{lang}.tsv") for lang in self.config.languages
128
+ ],
129
+ "data_format": "XNLI-MT",
130
+ },
131
+ ),
132
+ datasets.SplitGenerator(
133
+ name=datasets.Split.TEST,
134
+ gen_kwargs={"filepaths": [os.path.join(
135
+ testval_dir, "xnli.test.tsv")], "data_format": "XNLI"},
136
+ ),
137
+ datasets.SplitGenerator(
138
+ name=datasets.Split.VALIDATION,
139
+ gen_kwargs={"filepaths": [os.path.join(
140
+ testval_dir, "xnli.dev.tsv")], "data_format": "XNLI"},
141
+ ),
142
+ ]
143
+
144
+ def _generate_examples(self, data_format, filepaths):
145
+ """This function returns the examples in the raw (text) form."""
146
+
147
+ if self.config.language == "all_languages":
148
+ if data_format == "XNLI-MT":
149
+ with ExitStack() as stack:
150
+ files = [stack.enter_context(
151
+ open(filepath, encoding="utf-8")) for filepath in filepaths]
152
+ readers = [csv.DictReader(
153
+ file, delimiter="\t", quoting=csv.QUOTE_NONE) for file in files]
154
+ for row_idx, rows in enumerate(zip(*readers)):
155
+ yield row_idx, {
156
+ "premise": {lang: row["premise"] for lang, row in zip(self.config.languages, rows)},
157
+ "hypothesis": {lang: row["hypo"] for lang, row in zip(self.config.languages, rows)},
158
+ "label": rows[0]["label"].replace("contradictory", "contradiction"),
159
+ }
160
+ else:
161
+ rows_per_pair_id = collections.defaultdict(list)
162
+ for filepath in filepaths:
163
+ with open(filepath, encoding="utf-8") as f:
164
+ reader = csv.DictReader(
165
+ f, delimiter="\t", quoting=csv.QUOTE_NONE)
166
+ for row in reader:
167
+ rows_per_pair_id[row["pairID"]].append(row)
168
+
169
+ for rows in rows_per_pair_id.values():
170
+ premise = {row["language"]: row["sentence1"]
171
+ for row in rows}
172
+ hypothesis = {row["language"]: row["sentence2"]
173
+ for row in rows}
174
+ yield rows[0]["pairID"], {
175
+ "premise": premise,
176
+ "hypothesis": hypothesis,
177
+ "label": rows[0]["gold_label"],
178
+ }
179
+ else:
180
+ if data_format == "XNLI-MT":
181
+ for file_idx, filepath in enumerate(filepaths):
182
+ file = open(filepath, encoding="utf-8")
183
+ reader = csv.DictReader(
184
+ file, delimiter="\t", quoting=csv.QUOTE_NONE)
185
+ for row_idx, row in enumerate(reader):
186
+ key = str(file_idx) + "_" + str(row_idx)
187
+ yield key, {
188
+ "premise": row["premise"],
189
+ "hypothesis": row["hypo"],
190
+ "label": row["label"].replace("contradictory", "contradiction"),
191
+ }
192
+ else:
193
+ for filepath in filepaths:
194
+ with open(filepath, encoding="utf-8") as f:
195
+ reader = csv.DictReader(
196
+ f, delimiter="\t", quoting=csv.QUOTE_NONE)
197
+ for row in reader:
198
+ if row["language"] == self.config.language:
199
+ yield row["pairID"], {
200
+ "premise": row["sentence1"],
201
+ "hypothesis": row["sentence2"],
202
+ "label": row["gold_label"],
203
+ }
.history/indicxnli_20220823221351.py ADDED
@@ -0,0 +1,203 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ @misc{https://doi.org/10.48550/arxiv.2204.08776,
30
+ doi = {10.48550/ARXIV.2204.08776},
31
+
32
+ url = {https://arxiv.org/abs/2204.08776},
33
+
34
+ author = {Aggarwal, Divyanshu and Gupta, Vivek and Kunchukuttan, Anoop},
35
+
36
+ keywords = {Computation and Language (cs.CL), Artificial Intelligence (cs.AI), FOS: Computer and information sciences, FOS: Computer and information sciences},
37
+
38
+ title = {IndicXNLI: Evaluating Multilingual Inference for Indian Languages},
39
+
40
+ publisher = {arXiv},
41
+
42
+ year = {2022},
43
+
44
+ copyright = {Creative Commons Attribution 4.0 International}
45
+ }
46
+ }"""
47
+
48
+ _DESCRIPTION = """\
49
+ IndicXNLI is a translated version of XNLI to 11 Indic Languages. As with XNLI, the goal is
50
+ to predict textual entailment (does sentence A imply/contradict/neither sentence
51
+ B) and is a classification task (given two sentences, predict one of three
52
+ labels).
53
+ """
54
+
55
+ _LANGUAGES = (
56
+ 'hi',
57
+ 'bn',
58
+ 'mr',
59
+ 'as',
60
+ 'ta',
61
+ 'te',
62
+ 'or',
63
+ 'ml',
64
+ 'pa',
65
+ 'gu',
66
+ 'kn'
67
+ )
68
+
69
+
70
+ class IndicxnliConfig(datasets.BuilderConfig):
71
+ """BuilderConfig for XNLI."""
72
+
73
+ def __init__(self, language: str, **kwargs):
74
+ """BuilderConfig for XNLI.
75
+
76
+ Args:
77
+ language: One of hi, bn, mr, as, ta, te, or, ml, pa, gu, kn
78
+ **kwargs: keyword arguments forwarded to super.
79
+ """
80
+ super(IndicxnliConfig, self).__init__(**kwargs)
81
+ self.language = language
82
+
83
+
84
+ class Indicxnli(datasets.GeneratorBasedBuilder):
85
+ """XNLI: The Cross-Lingual NLI Corpus. Version 1.0."""
86
+
87
+ VERSION = datasets.Version("1.1.0", "")
88
+ BUILDER_CONFIG_CLASS = IndicxnliConfig
89
+ BUILDER_CONFIGS = [
90
+ IndicxnliConfig(
91
+ name=lang,
92
+ language=lang,
93
+ version=datasets.Version("1.1.0", ""),
94
+ description=f"Plain text import of IndicXNLI for the {lang} language",
95
+ )
96
+ for lang in _LANGUAGES
97
+ ]
98
+
99
+ def _info(self):
100
+ features = datasets.Features(
101
+ {
102
+ "premise": datasets.Value("string"),
103
+ "hypothesis": datasets.Value("string"),
104
+ "label": datasets.ClassLabel(names=["entailment", "neutral", "contradiction"]),
105
+ }
106
+ )
107
+ return datasets.DatasetInfo(
108
+ description=_DESCRIPTION,
109
+ features=features,
110
+ # No default supervised_keys (as we have to pass both premise
111
+ # and hypothesis as input).
112
+ supervised_keys=None,
113
+ homepage="https://www.nyu.edu/projects/bowman/xnli/",
114
+ citation=_CITATION,
115
+ )
116
+
117
+ def _split_generators(self, dl_manager):
118
+ train_dir = 'forward/train'
119
+ dev_dir = 'forward/dev'
120
+ test_dir = 'forward/test'
121
+
122
+ return [
123
+ datasets.SplitGenerator(
124
+ name=datasets.Split.TRAIN,
125
+ gen_kwargs={
126
+ "filepaths": [
127
+ os.path.join(train_dir, f"multinli.train.{lang}.json") for lang in self.config.languages
128
+ ],
129
+ "data_format": "XNLI-MT",
130
+ },
131
+ ),
132
+ datasets.SplitGenerator(
133
+ name=datasets.Split.TEST,
134
+ gen_kwargs={"filepaths": [os.path.join(
135
+ testval_dir, "xnli.test.tsv")], "data_format": "XNLI"},
136
+ ),
137
+ datasets.SplitGenerator(
138
+ name=datasets.Split.VALIDATION,
139
+ gen_kwargs={"filepaths": [os.path.join(
140
+ testval_dir, "xnli.dev.tsv")], "data_format": "XNLI"},
141
+ ),
142
+ ]
143
+
144
+ def _generate_examples(self, data_format, filepaths):
145
+ """This function returns the examples in the raw (text) form."""
146
+
147
+ if self.config.language == "all_languages":
148
+ if data_format == "XNLI-MT":
149
+ with ExitStack() as stack:
150
+ files = [stack.enter_context(
151
+ open(filepath, encoding="utf-8")) for filepath in filepaths]
152
+ readers = [csv.DictReader(
153
+ file, delimiter="\t", quoting=csv.QUOTE_NONE) for file in files]
154
+ for row_idx, rows in enumerate(zip(*readers)):
155
+ yield row_idx, {
156
+ "premise": {lang: row["premise"] for lang, row in zip(self.config.languages, rows)},
157
+ "hypothesis": {lang: row["hypo"] for lang, row in zip(self.config.languages, rows)},
158
+ "label": rows[0]["label"].replace("contradictory", "contradiction"),
159
+ }
160
+ else:
161
+ rows_per_pair_id = collections.defaultdict(list)
162
+ for filepath in filepaths:
163
+ with open(filepath, encoding="utf-8") as f:
164
+ reader = csv.DictReader(
165
+ f, delimiter="\t", quoting=csv.QUOTE_NONE)
166
+ for row in reader:
167
+ rows_per_pair_id[row["pairID"]].append(row)
168
+
169
+ for rows in rows_per_pair_id.values():
170
+ premise = {row["language"]: row["sentence1"]
171
+ for row in rows}
172
+ hypothesis = {row["language"]: row["sentence2"]
173
+ for row in rows}
174
+ yield rows[0]["pairID"], {
175
+ "premise": premise,
176
+ "hypothesis": hypothesis,
177
+ "label": rows[0]["gold_label"],
178
+ }
179
+ else:
180
+ if data_format == "XNLI-MT":
181
+ for file_idx, filepath in enumerate(filepaths):
182
+ file = open(filepath, encoding="utf-8")
183
+ reader = csv.DictReader(
184
+ file, delimiter="\t", quoting=csv.QUOTE_NONE)
185
+ for row_idx, row in enumerate(reader):
186
+ key = str(file_idx) + "_" + str(row_idx)
187
+ yield key, {
188
+ "premise": row["premise"],
189
+ "hypothesis": row["hypo"],
190
+ "label": row["label"].replace("contradictory", "contradiction"),
191
+ }
192
+ else:
193
+ for filepath in filepaths:
194
+ with open(filepath, encoding="utf-8") as f:
195
+ reader = csv.DictReader(
196
+ f, delimiter="\t", quoting=csv.QUOTE_NONE)
197
+ for row in reader:
198
+ if row["language"] == self.config.language:
199
+ yield row["pairID"], {
200
+ "premise": row["sentence1"],
201
+ "hypothesis": row["sentence2"],
202
+ "label": row["gold_label"],
203
+ }
.history/indicxnli_20220823221357.py ADDED
@@ -0,0 +1,203 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ @misc{https://doi.org/10.48550/arxiv.2204.08776,
30
+ doi = {10.48550/ARXIV.2204.08776},
31
+
32
+ url = {https://arxiv.org/abs/2204.08776},
33
+
34
+ author = {Aggarwal, Divyanshu and Gupta, Vivek and Kunchukuttan, Anoop},
35
+
36
+ keywords = {Computation and Language (cs.CL), Artificial Intelligence (cs.AI), FOS: Computer and information sciences, FOS: Computer and information sciences},
37
+
38
+ title = {IndicXNLI: Evaluating Multilingual Inference for Indian Languages},
39
+
40
+ publisher = {arXiv},
41
+
42
+ year = {2022},
43
+
44
+ copyright = {Creative Commons Attribution 4.0 International}
45
+ }
46
+ }"""
47
+
48
+ _DESCRIPTION = """\
49
+ IndicXNLI is a translated version of XNLI to 11 Indic Languages. As with XNLI, the goal is
50
+ to predict textual entailment (does sentence A imply/contradict/neither sentence
51
+ B) and is a classification task (given two sentences, predict one of three
52
+ labels).
53
+ """
54
+
55
+ _LANGUAGES = (
56
+ 'hi',
57
+ 'bn',
58
+ 'mr',
59
+ 'as',
60
+ 'ta',
61
+ 'te',
62
+ 'or',
63
+ 'ml',
64
+ 'pa',
65
+ 'gu',
66
+ 'kn'
67
+ )
68
+
69
+
70
+ class IndicxnliConfig(datasets.BuilderConfig):
71
+ """BuilderConfig for XNLI."""
72
+
73
+ def __init__(self, language: str, **kwargs):
74
+ """BuilderConfig for XNLI.
75
+
76
+ Args:
77
+ language: One of hi, bn, mr, as, ta, te, or, ml, pa, gu, kn
78
+ **kwargs: keyword arguments forwarded to super.
79
+ """
80
+ super(IndicxnliConfig, self).__init__(**kwargs)
81
+ self.language = language
82
+
83
+
84
+ class Indicxnli(datasets.GeneratorBasedBuilder):
85
+ """XNLI: The Cross-Lingual NLI Corpus. Version 1.0."""
86
+
87
+ VERSION = datasets.Version("1.1.0", "")
88
+ BUILDER_CONFIG_CLASS = IndicxnliConfig
89
+ BUILDER_CONFIGS = [
90
+ IndicxnliConfig(
91
+ name=lang,
92
+ language=lang,
93
+ version=datasets.Version("1.1.0", ""),
94
+ description=f"Plain text import of IndicXNLI for the {lang} language",
95
+ )
96
+ for lang in _LANGUAGES
97
+ ]
98
+
99
+ def _info(self):
100
+ features = datasets.Features(
101
+ {
102
+ "premise": datasets.Value("string"),
103
+ "hypothesis": datasets.Value("string"),
104
+ "label": datasets.ClassLabel(names=["entailment", "neutral", "contradiction"]),
105
+ }
106
+ )
107
+ return datasets.DatasetInfo(
108
+ description=_DESCRIPTION,
109
+ features=features,
110
+ # No default supervised_keys (as we have to pass both premise
111
+ # and hypothesis as input).
112
+ supervised_keys=None,
113
+ homepage="https://www.nyu.edu/projects/bowman/xnli/",
114
+ citation=_CITATION,
115
+ )
116
+
117
+ def _split_generators(self, dl_manager):
118
+ train_dir = 'forward/train'
119
+ dev_dir = 'forward/dev'
120
+ test_dir = 'forward/test'
121
+
122
+ return [
123
+ datasets.SplitGenerator(
124
+ name=datasets.Split.TRAIN,
125
+ gen_kwargs={
126
+ "filepaths": [
127
+ os.path.join(train_dir, f"xnli_{lang}.json") for lang in self.config.languages
128
+ ],
129
+ "data_format": "XNLI-MT",
130
+ },
131
+ ),
132
+ datasets.SplitGenerator(
133
+ name=datasets.Split.TEST,
134
+ gen_kwargs={"filepaths": [os.path.join(
135
+ testval_dir, "xnli.test.tsv")], "data_format": "XNLI"},
136
+ ),
137
+ datasets.SplitGenerator(
138
+ name=datasets.Split.VALIDATION,
139
+ gen_kwargs={"filepaths": [os.path.join(
140
+ testval_dir, "xnli.dev.tsv")], "data_format": "XNLI"},
141
+ ),
142
+ ]
143
+
144
+ def _generate_examples(self, data_format, filepaths):
145
+ """This function returns the examples in the raw (text) form."""
146
+
147
+ if self.config.language == "all_languages":
148
+ if data_format == "XNLI-MT":
149
+ with ExitStack() as stack:
150
+ files = [stack.enter_context(
151
+ open(filepath, encoding="utf-8")) for filepath in filepaths]
152
+ readers = [csv.DictReader(
153
+ file, delimiter="\t", quoting=csv.QUOTE_NONE) for file in files]
154
+ for row_idx, rows in enumerate(zip(*readers)):
155
+ yield row_idx, {
156
+ "premise": {lang: row["premise"] for lang, row in zip(self.config.languages, rows)},
157
+ "hypothesis": {lang: row["hypo"] for lang, row in zip(self.config.languages, rows)},
158
+ "label": rows[0]["label"].replace("contradictory", "contradiction"),
159
+ }
160
+ else:
161
+ rows_per_pair_id = collections.defaultdict(list)
162
+ for filepath in filepaths:
163
+ with open(filepath, encoding="utf-8") as f:
164
+ reader = csv.DictReader(
165
+ f, delimiter="\t", quoting=csv.QUOTE_NONE)
166
+ for row in reader:
167
+ rows_per_pair_id[row["pairID"]].append(row)
168
+
169
+ for rows in rows_per_pair_id.values():
170
+ premise = {row["language"]: row["sentence1"]
171
+ for row in rows}
172
+ hypothesis = {row["language"]: row["sentence2"]
173
+ for row in rows}
174
+ yield rows[0]["pairID"], {
175
+ "premise": premise,
176
+ "hypothesis": hypothesis,
177
+ "label": rows[0]["gold_label"],
178
+ }
179
+ else:
180
+ if data_format == "XNLI-MT":
181
+ for file_idx, filepath in enumerate(filepaths):
182
+ file = open(filepath, encoding="utf-8")
183
+ reader = csv.DictReader(
184
+ file, delimiter="\t", quoting=csv.QUOTE_NONE)
185
+ for row_idx, row in enumerate(reader):
186
+ key = str(file_idx) + "_" + str(row_idx)
187
+ yield key, {
188
+ "premise": row["premise"],
189
+ "hypothesis": row["hypo"],
190
+ "label": row["label"].replace("contradictory", "contradiction"),
191
+ }
192
+ else:
193
+ for filepath in filepaths:
194
+ with open(filepath, encoding="utf-8") as f:
195
+ reader = csv.DictReader(
196
+ f, delimiter="\t", quoting=csv.QUOTE_NONE)
197
+ for row in reader:
198
+ if row["language"] == self.config.language:
199
+ yield row["pairID"], {
200
+ "premise": row["sentence1"],
201
+ "hypothesis": row["sentence2"],
202
+ "label": row["gold_label"],
203
+ }
.history/indicxnli_20220823221400.py ADDED
@@ -0,0 +1,203 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ @misc{https://doi.org/10.48550/arxiv.2204.08776,
30
+ doi = {10.48550/ARXIV.2204.08776},
31
+
32
+ url = {https://arxiv.org/abs/2204.08776},
33
+
34
+ author = {Aggarwal, Divyanshu and Gupta, Vivek and Kunchukuttan, Anoop},
35
+
36
+ keywords = {Computation and Language (cs.CL), Artificial Intelligence (cs.AI), FOS: Computer and information sciences, FOS: Computer and information sciences},
37
+
38
+ title = {IndicXNLI: Evaluating Multilingual Inference for Indian Languages},
39
+
40
+ publisher = {arXiv},
41
+
42
+ year = {2022},
43
+
44
+ copyright = {Creative Commons Attribution 4.0 International}
45
+ }
46
+ }"""
47
+
48
+ _DESCRIPTION = """\
49
+ IndicXNLI is a translated version of XNLI to 11 Indic Languages. As with XNLI, the goal is
50
+ to predict textual entailment (does sentence A imply/contradict/neither sentence
51
+ B) and is a classification task (given two sentences, predict one of three
52
+ labels).
53
+ """
54
+
55
+ _LANGUAGES = (
56
+ 'hi',
57
+ 'bn',
58
+ 'mr',
59
+ 'as',
60
+ 'ta',
61
+ 'te',
62
+ 'or',
63
+ 'ml',
64
+ 'pa',
65
+ 'gu',
66
+ 'kn'
67
+ )
68
+
69
+
70
+ class IndicxnliConfig(datasets.BuilderConfig):
71
+ """BuilderConfig for XNLI."""
72
+
73
+ def __init__(self, language: str, **kwargs):
74
+ """BuilderConfig for XNLI.
75
+
76
+ Args:
77
+ language: One of hi, bn, mr, as, ta, te, or, ml, pa, gu, kn
78
+ **kwargs: keyword arguments forwarded to super.
79
+ """
80
+ super(IndicxnliConfig, self).__init__(**kwargs)
81
+ self.language = language
82
+
83
+
84
+ class Indicxnli(datasets.GeneratorBasedBuilder):
85
+ """XNLI: The Cross-Lingual NLI Corpus. Version 1.0."""
86
+
87
+ VERSION = datasets.Version("1.1.0", "")
88
+ BUILDER_CONFIG_CLASS = IndicxnliConfig
89
+ BUILDER_CONFIGS = [
90
+ IndicxnliConfig(
91
+ name=lang,
92
+ language=lang,
93
+ version=datasets.Version("1.1.0", ""),
94
+ description=f"Plain text import of IndicXNLI for the {lang} language",
95
+ )
96
+ for lang in _LANGUAGES
97
+ ]
98
+
99
+ def _info(self):
100
+ features = datasets.Features(
101
+ {
102
+ "premise": datasets.Value("string"),
103
+ "hypothesis": datasets.Value("string"),
104
+ "label": datasets.ClassLabel(names=["entailment", "neutral", "contradiction"]),
105
+ }
106
+ )
107
+ return datasets.DatasetInfo(
108
+ description=_DESCRIPTION,
109
+ features=features,
110
+ # No default supervised_keys (as we have to pass both premise
111
+ # and hypothesis as input).
112
+ supervised_keys=None,
113
+ homepage="https://www.nyu.edu/projects/bowman/xnli/",
114
+ citation=_CITATION,
115
+ )
116
+
117
+ def _split_generators(self, dl_manager):
118
+ train_dir = 'forward/train'
119
+ dev_dir = 'forward/dev'
120
+ test_dir = 'forward/test'
121
+
122
+ return [
123
+ datasets.SplitGenerator(
124
+ name=datasets.Split.TRAIN,
125
+ gen_kwargs={
126
+ "filepaths": [
127
+ os.path.join(train_dir, f"xnli_{lang}.json") for lang in self.config.languages
128
+ ],
129
+ "data_format": "XNLI",
130
+ },
131
+ ),
132
+ datasets.SplitGenerator(
133
+ name=datasets.Split.TEST,
134
+ gen_kwargs={"filepaths": [os.path.join(
135
+ testval_dir, "xnli.test.tsv")], "data_format": "XNLI"},
136
+ ),
137
+ datasets.SplitGenerator(
138
+ name=datasets.Split.VALIDATION,
139
+ gen_kwargs={"filepaths": [os.path.join(
140
+ testval_dir, "xnli.dev.tsv")], "data_format": "XNLI"},
141
+ ),
142
+ ]
143
+
144
+ def _generate_examples(self, data_format, filepaths):
145
+ """This function returns the examples in the raw (text) form."""
146
+
147
+ if self.config.language == "all_languages":
148
+ if data_format == "XNLI-MT":
149
+ with ExitStack() as stack:
150
+ files = [stack.enter_context(
151
+ open(filepath, encoding="utf-8")) for filepath in filepaths]
152
+ readers = [csv.DictReader(
153
+ file, delimiter="\t", quoting=csv.QUOTE_NONE) for file in files]
154
+ for row_idx, rows in enumerate(zip(*readers)):
155
+ yield row_idx, {
156
+ "premise": {lang: row["premise"] for lang, row in zip(self.config.languages, rows)},
157
+ "hypothesis": {lang: row["hypo"] for lang, row in zip(self.config.languages, rows)},
158
+ "label": rows[0]["label"].replace("contradictory", "contradiction"),
159
+ }
160
+ else:
161
+ rows_per_pair_id = collections.defaultdict(list)
162
+ for filepath in filepaths:
163
+ with open(filepath, encoding="utf-8") as f:
164
+ reader = csv.DictReader(
165
+ f, delimiter="\t", quoting=csv.QUOTE_NONE)
166
+ for row in reader:
167
+ rows_per_pair_id[row["pairID"]].append(row)
168
+
169
+ for rows in rows_per_pair_id.values():
170
+ premise = {row["language"]: row["sentence1"]
171
+ for row in rows}
172
+ hypothesis = {row["language"]: row["sentence2"]
173
+ for row in rows}
174
+ yield rows[0]["pairID"], {
175
+ "premise": premise,
176
+ "hypothesis": hypothesis,
177
+ "label": rows[0]["gold_label"],
178
+ }
179
+ else:
180
+ if data_format == "XNLI-MT":
181
+ for file_idx, filepath in enumerate(filepaths):
182
+ file = open(filepath, encoding="utf-8")
183
+ reader = csv.DictReader(
184
+ file, delimiter="\t", quoting=csv.QUOTE_NONE)
185
+ for row_idx, row in enumerate(reader):
186
+ key = str(file_idx) + "_" + str(row_idx)
187
+ yield key, {
188
+ "premise": row["premise"],
189
+ "hypothesis": row["hypo"],
190
+ "label": row["label"].replace("contradictory", "contradiction"),
191
+ }
192
+ else:
193
+ for filepath in filepaths:
194
+ with open(filepath, encoding="utf-8") as f:
195
+ reader = csv.DictReader(
196
+ f, delimiter="\t", quoting=csv.QUOTE_NONE)
197
+ for row in reader:
198
+ if row["language"] == self.config.language:
199
+ yield row["pairID"], {
200
+ "premise": row["sentence1"],
201
+ "hypothesis": row["sentence2"],
202
+ "label": row["gold_label"],
203
+ }
.history/indicxnli_20220823221408.py ADDED
@@ -0,0 +1,203 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ @misc{https://doi.org/10.48550/arxiv.2204.08776,
30
+ doi = {10.48550/ARXIV.2204.08776},
31
+
32
+ url = {https://arxiv.org/abs/2204.08776},
33
+
34
+ author = {Aggarwal, Divyanshu and Gupta, Vivek and Kunchukuttan, Anoop},
35
+
36
+ keywords = {Computation and Language (cs.CL), Artificial Intelligence (cs.AI), FOS: Computer and information sciences, FOS: Computer and information sciences},
37
+
38
+ title = {IndicXNLI: Evaluating Multilingual Inference for Indian Languages},
39
+
40
+ publisher = {arXiv},
41
+
42
+ year = {2022},
43
+
44
+ copyright = {Creative Commons Attribution 4.0 International}
45
+ }
46
+ }"""
47
+
48
+ _DESCRIPTION = """\
49
+ IndicXNLI is a translated version of XNLI to 11 Indic Languages. As with XNLI, the goal is
50
+ to predict textual entailment (does sentence A imply/contradict/neither sentence
51
+ B) and is a classification task (given two sentences, predict one of three
52
+ labels).
53
+ """
54
+
55
+ _LANGUAGES = (
56
+ 'hi',
57
+ 'bn',
58
+ 'mr',
59
+ 'as',
60
+ 'ta',
61
+ 'te',
62
+ 'or',
63
+ 'ml',
64
+ 'pa',
65
+ 'gu',
66
+ 'kn'
67
+ )
68
+
69
+
70
+ class IndicxnliConfig(datasets.BuilderConfig):
71
+ """BuilderConfig for XNLI."""
72
+
73
+ def __init__(self, language: str, **kwargs):
74
+ """BuilderConfig for XNLI.
75
+
76
+ Args:
77
+ language: One of hi, bn, mr, as, ta, te, or, ml, pa, gu, kn
78
+ **kwargs: keyword arguments forwarded to super.
79
+ """
80
+ super(IndicxnliConfig, self).__init__(**kwargs)
81
+ self.language = language
82
+
83
+
84
+ class Indicxnli(datasets.GeneratorBasedBuilder):
85
+ """XNLI: The Cross-Lingual NLI Corpus. Version 1.0."""
86
+
87
+ VERSION = datasets.Version("1.1.0", "")
88
+ BUILDER_CONFIG_CLASS = IndicxnliConfig
89
+ BUILDER_CONFIGS = [
90
+ IndicxnliConfig(
91
+ name=lang,
92
+ language=lang,
93
+ version=datasets.Version("1.1.0", ""),
94
+ description=f"Plain text import of IndicXNLI for the {lang} language",
95
+ )
96
+ for lang in _LANGUAGES
97
+ ]
98
+
99
+ def _info(self):
100
+ features = datasets.Features(
101
+ {
102
+ "premise": datasets.Value("string"),
103
+ "hypothesis": datasets.Value("string"),
104
+ "label": datasets.ClassLabel(names=["entailment", "neutral", "contradiction"]),
105
+ }
106
+ )
107
+ return datasets.DatasetInfo(
108
+ description=_DESCRIPTION,
109
+ features=features,
110
+ # No default supervised_keys (as we have to pass both premise
111
+ # and hypothesis as input).
112
+ supervised_keys=None,
113
+ homepage="https://www.nyu.edu/projects/bowman/xnli/",
114
+ citation=_CITATION,
115
+ )
116
+
117
+ def _split_generators(self, dl_manager):
118
+ train_dir = 'forward/train'
119
+ dev_dir = 'forward/dev'
120
+ test_dir = 'forward/test'
121
+
122
+ return [
123
+ datasets.SplitGenerator(
124
+ name=datasets.Split.TRAIN,
125
+ gen_kwargs={
126
+ "filepaths": [
127
+ os.path.join(train_dir, f"xnli_{lang}.json") for lang in self.config.languages
128
+ ],
129
+ "data_format": "IndicXNLI",
130
+ },
131
+ ),
132
+ datasets.SplitGenerator(
133
+ name=datasets.Split.TEST,
134
+ gen_kwargs={"filepaths": [os.path.join(
135
+ testval_dir, "xnli.test.tsv")], "data_format": "XNLI"},
136
+ ),
137
+ datasets.SplitGenerator(
138
+ name=datasets.Split.VALIDATION,
139
+ gen_kwargs={"filepaths": [os.path.join(
140
+ testval_dir, "xnli.dev.tsv")], "data_format": "XNLI"},
141
+ ),
142
+ ]
143
+
144
+ def _generate_examples(self, data_format, filepaths):
145
+ """This function returns the examples in the raw (text) form."""
146
+
147
+ if self.config.language == "all_languages":
148
+ if data_format == "XNLI-MT":
149
+ with ExitStack() as stack:
150
+ files = [stack.enter_context(
151
+ open(filepath, encoding="utf-8")) for filepath in filepaths]
152
+ readers = [csv.DictReader(
153
+ file, delimiter="\t", quoting=csv.QUOTE_NONE) for file in files]
154
+ for row_idx, rows in enumerate(zip(*readers)):
155
+ yield row_idx, {
156
+ "premise": {lang: row["premise"] for lang, row in zip(self.config.languages, rows)},
157
+ "hypothesis": {lang: row["hypo"] for lang, row in zip(self.config.languages, rows)},
158
+ "label": rows[0]["label"].replace("contradictory", "contradiction"),
159
+ }
160
+ else:
161
+ rows_per_pair_id = collections.defaultdict(list)
162
+ for filepath in filepaths:
163
+ with open(filepath, encoding="utf-8") as f:
164
+ reader = csv.DictReader(
165
+ f, delimiter="\t", quoting=csv.QUOTE_NONE)
166
+ for row in reader:
167
+ rows_per_pair_id[row["pairID"]].append(row)
168
+
169
+ for rows in rows_per_pair_id.values():
170
+ premise = {row["language"]: row["sentence1"]
171
+ for row in rows}
172
+ hypothesis = {row["language"]: row["sentence2"]
173
+ for row in rows}
174
+ yield rows[0]["pairID"], {
175
+ "premise": premise,
176
+ "hypothesis": hypothesis,
177
+ "label": rows[0]["gold_label"],
178
+ }
179
+ else:
180
+ if data_format == "XNLI-MT":
181
+ for file_idx, filepath in enumerate(filepaths):
182
+ file = open(filepath, encoding="utf-8")
183
+ reader = csv.DictReader(
184
+ file, delimiter="\t", quoting=csv.QUOTE_NONE)
185
+ for row_idx, row in enumerate(reader):
186
+ key = str(file_idx) + "_" + str(row_idx)
187
+ yield key, {
188
+ "premise": row["premise"],
189
+ "hypothesis": row["hypo"],
190
+ "label": row["label"].replace("contradictory", "contradiction"),
191
+ }
192
+ else:
193
+ for filepath in filepaths:
194
+ with open(filepath, encoding="utf-8") as f:
195
+ reader = csv.DictReader(
196
+ f, delimiter="\t", quoting=csv.QUOTE_NONE)
197
+ for row in reader:
198
+ if row["language"] == self.config.language:
199
+ yield row["pairID"], {
200
+ "premise": row["sentence1"],
201
+ "hypothesis": row["sentence2"],
202
+ "label": row["gold_label"],
203
+ }
.history/indicxnli_20220823221440.py ADDED
@@ -0,0 +1,203 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ @misc{https://doi.org/10.48550/arxiv.2204.08776,
30
+ doi = {10.48550/ARXIV.2204.08776},
31
+
32
+ url = {https://arxiv.org/abs/2204.08776},
33
+
34
+ author = {Aggarwal, Divyanshu and Gupta, Vivek and Kunchukuttan, Anoop},
35
+
36
+ keywords = {Computation and Language (cs.CL), Artificial Intelligence (cs.AI), FOS: Computer and information sciences, FOS: Computer and information sciences},
37
+
38
+ title = {IndicXNLI: Evaluating Multilingual Inference for Indian Languages},
39
+
40
+ publisher = {arXiv},
41
+
42
+ year = {2022},
43
+
44
+ copyright = {Creative Commons Attribution 4.0 International}
45
+ }
46
+ }"""
47
+
48
+ _DESCRIPTION = """\
49
+ IndicXNLI is a translated version of XNLI to 11 Indic Languages. As with XNLI, the goal is
50
+ to predict textual entailment (does sentence A imply/contradict/neither sentence
51
+ B) and is a classification task (given two sentences, predict one of three
52
+ labels).
53
+ """
54
+
55
+ _LANGUAGES = (
56
+ 'hi',
57
+ 'bn',
58
+ 'mr',
59
+ 'as',
60
+ 'ta',
61
+ 'te',
62
+ 'or',
63
+ 'ml',
64
+ 'pa',
65
+ 'gu',
66
+ 'kn'
67
+ )
68
+
69
+
70
+ class IndicxnliConfig(datasets.BuilderConfig):
71
+ """BuilderConfig for XNLI."""
72
+
73
+ def __init__(self, language: str, **kwargs):
74
+ """BuilderConfig for XNLI.
75
+
76
+ Args:
77
+ language: One of hi, bn, mr, as, ta, te, or, ml, pa, gu, kn
78
+ **kwargs: keyword arguments forwarded to super.
79
+ """
80
+ super(IndicxnliConfig, self).__init__(**kwargs)
81
+ self.language = language
82
+
83
+
84
+ class Indicxnli(datasets.GeneratorBasedBuilder):
85
+ """XNLI: The Cross-Lingual NLI Corpus. Version 1.0."""
86
+
87
+ VERSION = datasets.Version("1.1.0", "")
88
+ BUILDER_CONFIG_CLASS = IndicxnliConfig
89
+ BUILDER_CONFIGS = [
90
+ IndicxnliConfig(
91
+ name=lang,
92
+ language=lang,
93
+ version=datasets.Version("1.1.0", ""),
94
+ description=f"Plain text import of IndicXNLI for the {lang} language",
95
+ )
96
+ for lang in _LANGUAGES
97
+ ]
98
+
99
+ def _info(self):
100
+ features = datasets.Features(
101
+ {
102
+ "premise": datasets.Value("string"),
103
+ "hypothesis": datasets.Value("string"),
104
+ "label": datasets.ClassLabel(names=["entailment", "neutral", "contradiction"]),
105
+ }
106
+ )
107
+ return datasets.DatasetInfo(
108
+ description=_DESCRIPTION,
109
+ features=features,
110
+ # No default supervised_keys (as we have to pass both premise
111
+ # and hypothesis as input).
112
+ supervised_keys=None,
113
+ homepage="https://www.nyu.edu/projects/bowman/xnli/",
114
+ citation=_CITATION,
115
+ )
116
+
117
+ def _split_generators(self, dl_manager):
118
+ train_dir = 'forward/train'
119
+ dev_dir = 'forward/dev'
120
+ test_dir = 'forward/test'
121
+
122
+ return [
123
+ datasets.SplitGenerator(
124
+ name=datasets.Split.TRAIN,
125
+ gen_kwargs={
126
+ "filepaths": [
127
+ os.path.join(train_dir, f"xnli_{lang}.json") for lang in self.config.languages
128
+ ],
129
+ "data_format": "IndicXNLI",
130
+ },
131
+ ),
132
+ datasets.SplitGenerator(
133
+ name=datasets.Split.TEST,
134
+ gen_kwargs={"filepaths": [os.path.join(
135
+ test_dir, "xnli.test.tsv")], "data_format": "XNLI"},
136
+ ),
137
+ datasets.SplitGenerator(
138
+ name=datasets.Split.VALIDATION,
139
+ gen_kwargs={"filepaths": [os.path.join(
140
+ testval_dir, "xnli.dev.tsv")], "data_format": "XNLI"},
141
+ ),
142
+ ]
143
+
144
+ def _generate_examples(self, data_format, filepaths):
145
+ """This function returns the examples in the raw (text) form."""
146
+
147
+ if self.config.language == "all_languages":
148
+ if data_format == "XNLI-MT":
149
+ with ExitStack() as stack:
150
+ files = [stack.enter_context(
151
+ open(filepath, encoding="utf-8")) for filepath in filepaths]
152
+ readers = [csv.DictReader(
153
+ file, delimiter="\t", quoting=csv.QUOTE_NONE) for file in files]
154
+ for row_idx, rows in enumerate(zip(*readers)):
155
+ yield row_idx, {
156
+ "premise": {lang: row["premise"] for lang, row in zip(self.config.languages, rows)},
157
+ "hypothesis": {lang: row["hypo"] for lang, row in zip(self.config.languages, rows)},
158
+ "label": rows[0]["label"].replace("contradictory", "contradiction"),
159
+ }
160
+ else:
161
+ rows_per_pair_id = collections.defaultdict(list)
162
+ for filepath in filepaths:
163
+ with open(filepath, encoding="utf-8") as f:
164
+ reader = csv.DictReader(
165
+ f, delimiter="\t", quoting=csv.QUOTE_NONE)
166
+ for row in reader:
167
+ rows_per_pair_id[row["pairID"]].append(row)
168
+
169
+ for rows in rows_per_pair_id.values():
170
+ premise = {row["language"]: row["sentence1"]
171
+ for row in rows}
172
+ hypothesis = {row["language"]: row["sentence2"]
173
+ for row in rows}
174
+ yield rows[0]["pairID"], {
175
+ "premise": premise,
176
+ "hypothesis": hypothesis,
177
+ "label": rows[0]["gold_label"],
178
+ }
179
+ else:
180
+ if data_format == "XNLI-MT":
181
+ for file_idx, filepath in enumerate(filepaths):
182
+ file = open(filepath, encoding="utf-8")
183
+ reader = csv.DictReader(
184
+ file, delimiter="\t", quoting=csv.QUOTE_NONE)
185
+ for row_idx, row in enumerate(reader):
186
+ key = str(file_idx) + "_" + str(row_idx)
187
+ yield key, {
188
+ "premise": row["premise"],
189
+ "hypothesis": row["hypo"],
190
+ "label": row["label"].replace("contradictory", "contradiction"),
191
+ }
192
+ else:
193
+ for filepath in filepaths:
194
+ with open(filepath, encoding="utf-8") as f:
195
+ reader = csv.DictReader(
196
+ f, delimiter="\t", quoting=csv.QUOTE_NONE)
197
+ for row in reader:
198
+ if row["language"] == self.config.language:
199
+ yield row["pairID"], {
200
+ "premise": row["sentence1"],
201
+ "hypothesis": row["sentence2"],
202
+ "label": row["gold_label"],
203
+ }
.history/indicxnli_20220823221501.py ADDED
@@ -0,0 +1,203 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ @misc{https://doi.org/10.48550/arxiv.2204.08776,
30
+ doi = {10.48550/ARXIV.2204.08776},
31
+
32
+ url = {https://arxiv.org/abs/2204.08776},
33
+
34
+ author = {Aggarwal, Divyanshu and Gupta, Vivek and Kunchukuttan, Anoop},
35
+
36
+ keywords = {Computation and Language (cs.CL), Artificial Intelligence (cs.AI), FOS: Computer and information sciences, FOS: Computer and information sciences},
37
+
38
+ title = {IndicXNLI: Evaluating Multilingual Inference for Indian Languages},
39
+
40
+ publisher = {arXiv},
41
+
42
+ year = {2022},
43
+
44
+ copyright = {Creative Commons Attribution 4.0 International}
45
+ }
46
+ }"""
47
+
48
+ _DESCRIPTION = """\
49
+ IndicXNLI is a translated version of XNLI to 11 Indic Languages. As with XNLI, the goal is
50
+ to predict textual entailment (does sentence A imply/contradict/neither sentence
51
+ B) and is a classification task (given two sentences, predict one of three
52
+ labels).
53
+ """
54
+
55
+ _LANGUAGES = (
56
+ 'hi',
57
+ 'bn',
58
+ 'mr',
59
+ 'as',
60
+ 'ta',
61
+ 'te',
62
+ 'or',
63
+ 'ml',
64
+ 'pa',
65
+ 'gu',
66
+ 'kn'
67
+ )
68
+
69
+
70
+ class IndicxnliConfig(datasets.BuilderConfig):
71
+ """BuilderConfig for XNLI."""
72
+
73
+ def __init__(self, language: str, **kwargs):
74
+ """BuilderConfig for XNLI.
75
+
76
+ Args:
77
+ language: One of hi, bn, mr, as, ta, te, or, ml, pa, gu, kn
78
+ **kwargs: keyword arguments forwarded to super.
79
+ """
80
+ super(IndicxnliConfig, self).__init__(**kwargs)
81
+ self.language = language
82
+
83
+
84
+ class Indicxnli(datasets.GeneratorBasedBuilder):
85
+ """XNLI: The Cross-Lingual NLI Corpus. Version 1.0."""
86
+
87
+ VERSION = datasets.Version("1.1.0", "")
88
+ BUILDER_CONFIG_CLASS = IndicxnliConfig
89
+ BUILDER_CONFIGS = [
90
+ IndicxnliConfig(
91
+ name=lang,
92
+ language=lang,
93
+ version=datasets.Version("1.1.0", ""),
94
+ description=f"Plain text import of IndicXNLI for the {lang} language",
95
+ )
96
+ for lang in _LANGUAGES
97
+ ]
98
+
99
+ def _info(self):
100
+ features = datasets.Features(
101
+ {
102
+ "premise": datasets.Value("string"),
103
+ "hypothesis": datasets.Value("string"),
104
+ "label": datasets.ClassLabel(names=["entailment", "neutral", "contradiction"]),
105
+ }
106
+ )
107
+ return datasets.DatasetInfo(
108
+ description=_DESCRIPTION,
109
+ features=features,
110
+ # No default supervised_keys (as we have to pass both premise
111
+ # and hypothesis as input).
112
+ supervised_keys=None,
113
+ homepage="https://www.nyu.edu/projects/bowman/xnli/",
114
+ citation=_CITATION,
115
+ )
116
+
117
+ def _split_generators(self, dl_manager):
118
+ train_dir = 'forward/train'
119
+ dev_dir = 'forward/dev'
120
+ test_dir = 'forward/test'
121
+
122
+ return [
123
+ datasets.SplitGenerator(
124
+ name=datasets.Split.TRAIN,
125
+ gen_kwargs={
126
+ "filepaths": [
127
+ os.path.join(train_dir, f"xnli_{lang}.json") for lang in self.config.languages
128
+ ],
129
+ "data_format": "IndicXNLI",
130
+ },
131
+ ),
132
+ datasets.SplitGenerator(
133
+ name=datasets.Split.TEST,
134
+ gen_kwargs={"filepaths": [os.path.join(
135
+ test_dir, f"xnli_{lang}.json")], "data_format": "XNLI"},
136
+ ),
137
+ datasets.SplitGenerator(
138
+ name=datasets.Split.VALIDATION,
139
+ gen_kwargs={"filepaths": [os.path.join(
140
+ testval_dir, "xnli.dev.tsv")], "data_format": "XNLI"},
141
+ ),
142
+ ]
143
+
144
+ def _generate_examples(self, data_format, filepaths):
145
+ """This function returns the examples in the raw (text) form."""
146
+
147
+ if self.config.language == "all_languages":
148
+ if data_format == "XNLI-MT":
149
+ with ExitStack() as stack:
150
+ files = [stack.enter_context(
151
+ open(filepath, encoding="utf-8")) for filepath in filepaths]
152
+ readers = [csv.DictReader(
153
+ file, delimiter="\t", quoting=csv.QUOTE_NONE) for file in files]
154
+ for row_idx, rows in enumerate(zip(*readers)):
155
+ yield row_idx, {
156
+ "premise": {lang: row["premise"] for lang, row in zip(self.config.languages, rows)},
157
+ "hypothesis": {lang: row["hypo"] for lang, row in zip(self.config.languages, rows)},
158
+ "label": rows[0]["label"].replace("contradictory", "contradiction"),
159
+ }
160
+ else:
161
+ rows_per_pair_id = collections.defaultdict(list)
162
+ for filepath in filepaths:
163
+ with open(filepath, encoding="utf-8") as f:
164
+ reader = csv.DictReader(
165
+ f, delimiter="\t", quoting=csv.QUOTE_NONE)
166
+ for row in reader:
167
+ rows_per_pair_id[row["pairID"]].append(row)
168
+
169
+ for rows in rows_per_pair_id.values():
170
+ premise = {row["language"]: row["sentence1"]
171
+ for row in rows}
172
+ hypothesis = {row["language"]: row["sentence2"]
173
+ for row in rows}
174
+ yield rows[0]["pairID"], {
175
+ "premise": premise,
176
+ "hypothesis": hypothesis,
177
+ "label": rows[0]["gold_label"],
178
+ }
179
+ else:
180
+ if data_format == "XNLI-MT":
181
+ for file_idx, filepath in enumerate(filepaths):
182
+ file = open(filepath, encoding="utf-8")
183
+ reader = csv.DictReader(
184
+ file, delimiter="\t", quoting=csv.QUOTE_NONE)
185
+ for row_idx, row in enumerate(reader):
186
+ key = str(file_idx) + "_" + str(row_idx)
187
+ yield key, {
188
+ "premise": row["premise"],
189
+ "hypothesis": row["hypo"],
190
+ "label": row["label"].replace("contradictory", "contradiction"),
191
+ }
192
+ else:
193
+ for filepath in filepaths:
194
+ with open(filepath, encoding="utf-8") as f:
195
+ reader = csv.DictReader(
196
+ f, delimiter="\t", quoting=csv.QUOTE_NONE)
197
+ for row in reader:
198
+ if row["language"] == self.config.language:
199
+ yield row["pairID"], {
200
+ "premise": row["sentence1"],
201
+ "hypothesis": row["sentence2"],
202
+ "label": row["gold_label"],
203
+ }
.history/indicxnli_20220823221505.py ADDED
@@ -0,0 +1,203 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ @misc{https://doi.org/10.48550/arxiv.2204.08776,
30
+ doi = {10.48550/ARXIV.2204.08776},
31
+
32
+ url = {https://arxiv.org/abs/2204.08776},
33
+
34
+ author = {Aggarwal, Divyanshu and Gupta, Vivek and Kunchukuttan, Anoop},
35
+
36
+ keywords = {Computation and Language (cs.CL), Artificial Intelligence (cs.AI), FOS: Computer and information sciences, FOS: Computer and information sciences},
37
+
38
+ title = {IndicXNLI: Evaluating Multilingual Inference for Indian Languages},
39
+
40
+ publisher = {arXiv},
41
+
42
+ year = {2022},
43
+
44
+ copyright = {Creative Commons Attribution 4.0 International}
45
+ }
46
+ }"""
47
+
48
+ _DESCRIPTION = """\
49
+ IndicXNLI is a translated version of XNLI to 11 Indic Languages. As with XNLI, the goal is
50
+ to predict textual entailment (does sentence A imply/contradict/neither sentence
51
+ B) and is a classification task (given two sentences, predict one of three
52
+ labels).
53
+ """
54
+
55
+ _LANGUAGES = (
56
+ 'hi',
57
+ 'bn',
58
+ 'mr',
59
+ 'as',
60
+ 'ta',
61
+ 'te',
62
+ 'or',
63
+ 'ml',
64
+ 'pa',
65
+ 'gu',
66
+ 'kn'
67
+ )
68
+
69
+
70
+ class IndicxnliConfig(datasets.BuilderConfig):
71
+ """BuilderConfig for XNLI."""
72
+
73
+ def __init__(self, language: str, **kwargs):
74
+ """BuilderConfig for XNLI.
75
+
76
+ Args:
77
+ language: One of hi, bn, mr, as, ta, te, or, ml, pa, gu, kn
78
+ **kwargs: keyword arguments forwarded to super.
79
+ """
80
+ super(IndicxnliConfig, self).__init__(**kwargs)
81
+ self.language = language
82
+
83
+
84
+ class Indicxnli(datasets.GeneratorBasedBuilder):
85
+ """XNLI: The Cross-Lingual NLI Corpus. Version 1.0."""
86
+
87
+ VERSION = datasets.Version("1.1.0", "")
88
+ BUILDER_CONFIG_CLASS = IndicxnliConfig
89
+ BUILDER_CONFIGS = [
90
+ IndicxnliConfig(
91
+ name=lang,
92
+ language=lang,
93
+ version=datasets.Version("1.1.0", ""),
94
+ description=f"Plain text import of IndicXNLI for the {lang} language",
95
+ )
96
+ for lang in _LANGUAGES
97
+ ]
98
+
99
+ def _info(self):
100
+ features = datasets.Features(
101
+ {
102
+ "premise": datasets.Value("string"),
103
+ "hypothesis": datasets.Value("string"),
104
+ "label": datasets.ClassLabel(names=["entailment", "neutral", "contradiction"]),
105
+ }
106
+ )
107
+ return datasets.DatasetInfo(
108
+ description=_DESCRIPTION,
109
+ features=features,
110
+ # No default supervised_keys (as we have to pass both premise
111
+ # and hypothesis as input).
112
+ supervised_keys=None,
113
+ homepage="https://www.nyu.edu/projects/bowman/xnli/",
114
+ citation=_CITATION,
115
+ )
116
+
117
+ def _split_generators(self, dl_manager):
118
+ train_dir = 'forward/train'
119
+ dev_dir = 'forward/dev'
120
+ test_dir = 'forward/test'
121
+
122
+ return [
123
+ datasets.SplitGenerator(
124
+ name=datasets.Split.TRAIN,
125
+ gen_kwargs={
126
+ "filepaths": [
127
+ os.path.join(train_dir, f"xnli_{lang}.json") for lang in self.config.languages
128
+ ],
129
+ "data_format": "IndicXNLI",
130
+ },
131
+ ),
132
+ datasets.SplitGenerator(
133
+ name=datasets.Split.TEST,
134
+ gen_kwargs={"filepaths": [os.path.join(
135
+ test_dir, f"xnli_{lang}.json")], "data_format": "IndicXNLI"},
136
+ ),
137
+ datasets.SplitGenerator(
138
+ name=datasets.Split.VALIDATION,
139
+ gen_kwargs={"filepaths": [os.path.join(
140
+ testval_dir, "xnli.dev.tsv")], "data_format": "XNLI"},
141
+ ),
142
+ ]
143
+
144
+ def _generate_examples(self, data_format, filepaths):
145
+ """This function returns the examples in the raw (text) form."""
146
+
147
+ if self.config.language == "all_languages":
148
+ if data_format == "XNLI-MT":
149
+ with ExitStack() as stack:
150
+ files = [stack.enter_context(
151
+ open(filepath, encoding="utf-8")) for filepath in filepaths]
152
+ readers = [csv.DictReader(
153
+ file, delimiter="\t", quoting=csv.QUOTE_NONE) for file in files]
154
+ for row_idx, rows in enumerate(zip(*readers)):
155
+ yield row_idx, {
156
+ "premise": {lang: row["premise"] for lang, row in zip(self.config.languages, rows)},
157
+ "hypothesis": {lang: row["hypo"] for lang, row in zip(self.config.languages, rows)},
158
+ "label": rows[0]["label"].replace("contradictory", "contradiction"),
159
+ }
160
+ else:
161
+ rows_per_pair_id = collections.defaultdict(list)
162
+ for filepath in filepaths:
163
+ with open(filepath, encoding="utf-8") as f:
164
+ reader = csv.DictReader(
165
+ f, delimiter="\t", quoting=csv.QUOTE_NONE)
166
+ for row in reader:
167
+ rows_per_pair_id[row["pairID"]].append(row)
168
+
169
+ for rows in rows_per_pair_id.values():
170
+ premise = {row["language"]: row["sentence1"]
171
+ for row in rows}
172
+ hypothesis = {row["language"]: row["sentence2"]
173
+ for row in rows}
174
+ yield rows[0]["pairID"], {
175
+ "premise": premise,
176
+ "hypothesis": hypothesis,
177
+ "label": rows[0]["gold_label"],
178
+ }
179
+ else:
180
+ if data_format == "XNLI-MT":
181
+ for file_idx, filepath in enumerate(filepaths):
182
+ file = open(filepath, encoding="utf-8")
183
+ reader = csv.DictReader(
184
+ file, delimiter="\t", quoting=csv.QUOTE_NONE)
185
+ for row_idx, row in enumerate(reader):
186
+ key = str(file_idx) + "_" + str(row_idx)
187
+ yield key, {
188
+ "premise": row["premise"],
189
+ "hypothesis": row["hypo"],
190
+ "label": row["label"].replace("contradictory", "contradiction"),
191
+ }
192
+ else:
193
+ for filepath in filepaths:
194
+ with open(filepath, encoding="utf-8") as f:
195
+ reader = csv.DictReader(
196
+ f, delimiter="\t", quoting=csv.QUOTE_NONE)
197
+ for row in reader:
198
+ if row["language"] == self.config.language:
199
+ yield row["pairID"], {
200
+ "premise": row["sentence1"],
201
+ "hypothesis": row["sentence2"],
202
+ "label": row["gold_label"],
203
+ }
.history/indicxnli_20220823221601.py ADDED
@@ -0,0 +1,203 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ @misc{https://doi.org/10.48550/arxiv.2204.08776,
30
+ doi = {10.48550/ARXIV.2204.08776},
31
+
32
+ url = {https://arxiv.org/abs/2204.08776},
33
+
34
+ author = {Aggarwal, Divyanshu and Gupta, Vivek and Kunchukuttan, Anoop},
35
+
36
+ keywords = {Computation and Language (cs.CL), Artificial Intelligence (cs.AI), FOS: Computer and information sciences, FOS: Computer and information sciences},
37
+
38
+ title = {IndicXNLI: Evaluating Multilingual Inference for Indian Languages},
39
+
40
+ publisher = {arXiv},
41
+
42
+ year = {2022},
43
+
44
+ copyright = {Creative Commons Attribution 4.0 International}
45
+ }
46
+ }"""
47
+
48
+ _DESCRIPTION = """\
49
+ IndicXNLI is a translated version of XNLI to 11 Indic Languages. As with XNLI, the goal is
50
+ to predict textual entailment (does sentence A imply/contradict/neither sentence
51
+ B) and is a classification task (given two sentences, predict one of three
52
+ labels).
53
+ """
54
+
55
+ _LANGUAGES = (
56
+ 'hi',
57
+ 'bn',
58
+ 'mr',
59
+ 'as',
60
+ 'ta',
61
+ 'te',
62
+ 'or',
63
+ 'ml',
64
+ 'pa',
65
+ 'gu',
66
+ 'kn'
67
+ )
68
+
69
+
70
+ class IndicxnliConfig(datasets.BuilderConfig):
71
+ """BuilderConfig for XNLI."""
72
+
73
+ def __init__(self, language: str, **kwargs):
74
+ """BuilderConfig for XNLI.
75
+
76
+ Args:
77
+ language: One of hi, bn, mr, as, ta, te, or, ml, pa, gu, kn
78
+ **kwargs: keyword arguments forwarded to super.
79
+ """
80
+ super(IndicxnliConfig, self).__init__(**kwargs)
81
+ self.language = language
82
+
83
+
84
+ class Indicxnli(datasets.GeneratorBasedBuilder):
85
+ """XNLI: The Cross-Lingual NLI Corpus. Version 1.0."""
86
+
87
+ VERSION = datasets.Version("1.1.0", "")
88
+ BUILDER_CONFIG_CLASS = IndicxnliConfig
89
+ BUILDER_CONFIGS = [
90
+ IndicxnliConfig(
91
+ name=lang,
92
+ language=lang,
93
+ version=datasets.Version("1.1.0", ""),
94
+ description=f"Plain text import of IndicXNLI for the {lang} language",
95
+ )
96
+ for lang in _LANGUAGES
97
+ ]
98
+
99
+ def _info(self):
100
+ features = datasets.Features(
101
+ {
102
+ "premise": datasets.Value("string"),
103
+ "hypothesis": datasets.Value("string"),
104
+ "label": datasets.ClassLabel(names=["entailment", "neutral", "contradiction"]),
105
+ }
106
+ )
107
+ return datasets.DatasetInfo(
108
+ description=_DESCRIPTION,
109
+ features=features,
110
+ # No default supervised_keys (as we have to pass both premise
111
+ # and hypothesis as input).
112
+ supervised_keys=None,
113
+ homepage="https://www.nyu.edu/projects/bowman/xnli/",
114
+ citation=_CITATION,
115
+ )
116
+
117
+ def _split_generators(self, dl_manager):
118
+ train_dir = 'forward/train'
119
+ dev_dir = 'forward/dev'
120
+ test_dir = 'forward/test'
121
+
122
+ return [
123
+ datasets.SplitGenerator(
124
+ name=datasets.Split.TRAIN,
125
+ gen_kwargs={
126
+ "filepaths": [
127
+ os.path.join(train_dir, f"xnli_{lang}.json") for lang in self.config.languages
128
+ ],
129
+ "data_format": "IndicXNLI",
130
+ },
131
+ ),
132
+ datasets.SplitGenerator(
133
+ name=datasets.Split.TEST,
134
+ gen_kwargs={"filepaths": [os.path.join(
135
+ test_dir, f"xnli_{lang}.json") for lang in self.config.languages], "data_format": "IndicXNLI"},
136
+ ),
137
+ datasets.SplitGenerator(
138
+ name=datasets.Split.VALIDATION,
139
+ gen_kwargs={"filepaths": [os.path.join(
140
+ testval_dir, "xnli.dev.tsv")], "data_format": "XNLI"},
141
+ ),
142
+ ]
143
+
144
+ def _generate_examples(self, data_format, filepaths):
145
+ """This function returns the examples in the raw (text) form."""
146
+
147
+ if self.config.language == "all_languages":
148
+ if data_format == "XNLI-MT":
149
+ with ExitStack() as stack:
150
+ files = [stack.enter_context(
151
+ open(filepath, encoding="utf-8")) for filepath in filepaths]
152
+ readers = [csv.DictReader(
153
+ file, delimiter="\t", quoting=csv.QUOTE_NONE) for file in files]
154
+ for row_idx, rows in enumerate(zip(*readers)):
155
+ yield row_idx, {
156
+ "premise": {lang: row["premise"] for lang, row in zip(self.config.languages, rows)},
157
+ "hypothesis": {lang: row["hypo"] for lang, row in zip(self.config.languages, rows)},
158
+ "label": rows[0]["label"].replace("contradictory", "contradiction"),
159
+ }
160
+ else:
161
+ rows_per_pair_id = collections.defaultdict(list)
162
+ for filepath in filepaths:
163
+ with open(filepath, encoding="utf-8") as f:
164
+ reader = csv.DictReader(
165
+ f, delimiter="\t", quoting=csv.QUOTE_NONE)
166
+ for row in reader:
167
+ rows_per_pair_id[row["pairID"]].append(row)
168
+
169
+ for rows in rows_per_pair_id.values():
170
+ premise = {row["language"]: row["sentence1"]
171
+ for row in rows}
172
+ hypothesis = {row["language"]: row["sentence2"]
173
+ for row in rows}
174
+ yield rows[0]["pairID"], {
175
+ "premise": premise,
176
+ "hypothesis": hypothesis,
177
+ "label": rows[0]["gold_label"],
178
+ }
179
+ else:
180
+ if data_format == "XNLI-MT":
181
+ for file_idx, filepath in enumerate(filepaths):
182
+ file = open(filepath, encoding="utf-8")
183
+ reader = csv.DictReader(
184
+ file, delimiter="\t", quoting=csv.QUOTE_NONE)
185
+ for row_idx, row in enumerate(reader):
186
+ key = str(file_idx) + "_" + str(row_idx)
187
+ yield key, {
188
+ "premise": row["premise"],
189
+ "hypothesis": row["hypo"],
190
+ "label": row["label"].replace("contradictory", "contradiction"),
191
+ }
192
+ else:
193
+ for filepath in filepaths:
194
+ with open(filepath, encoding="utf-8") as f:
195
+ reader = csv.DictReader(
196
+ f, delimiter="\t", quoting=csv.QUOTE_NONE)
197
+ for row in reader:
198
+ if row["language"] == self.config.language:
199
+ yield row["pairID"], {
200
+ "premise": row["sentence1"],
201
+ "hypothesis": row["sentence2"],
202
+ "label": row["gold_label"],
203
+ }
.history/indicxnli_20220823221611.py ADDED
@@ -0,0 +1,203 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ @misc{https://doi.org/10.48550/arxiv.2204.08776,
30
+ doi = {10.48550/ARXIV.2204.08776},
31
+
32
+ url = {https://arxiv.org/abs/2204.08776},
33
+
34
+ author = {Aggarwal, Divyanshu and Gupta, Vivek and Kunchukuttan, Anoop},
35
+
36
+ keywords = {Computation and Language (cs.CL), Artificial Intelligence (cs.AI), FOS: Computer and information sciences, FOS: Computer and information sciences},
37
+
38
+ title = {IndicXNLI: Evaluating Multilingual Inference for Indian Languages},
39
+
40
+ publisher = {arXiv},
41
+
42
+ year = {2022},
43
+
44
+ copyright = {Creative Commons Attribution 4.0 International}
45
+ }
46
+ }"""
47
+
48
+ _DESCRIPTION = """\
49
+ IndicXNLI is a translated version of XNLI to 11 Indic Languages. As with XNLI, the goal is
50
+ to predict textual entailment (does sentence A imply/contradict/neither sentence
51
+ B) and is a classification task (given two sentences, predict one of three
52
+ labels).
53
+ """
54
+
55
+ _LANGUAGES = (
56
+ 'hi',
57
+ 'bn',
58
+ 'mr',
59
+ 'as',
60
+ 'ta',
61
+ 'te',
62
+ 'or',
63
+ 'ml',
64
+ 'pa',
65
+ 'gu',
66
+ 'kn'
67
+ )
68
+
69
+
70
+ class IndicxnliConfig(datasets.BuilderConfig):
71
+ """BuilderConfig for XNLI."""
72
+
73
+ def __init__(self, language: str, **kwargs):
74
+ """BuilderConfig for XNLI.
75
+
76
+ Args:
77
+ language: One of hi, bn, mr, as, ta, te, or, ml, pa, gu, kn
78
+ **kwargs: keyword arguments forwarded to super.
79
+ """
80
+ super(IndicxnliConfig, self).__init__(**kwargs)
81
+ self.language = language
82
+
83
+
84
+ class Indicxnli(datasets.GeneratorBasedBuilder):
85
+ """XNLI: The Cross-Lingual NLI Corpus. Version 1.0."""
86
+
87
+ VERSION = datasets.Version("1.1.0", "")
88
+ BUILDER_CONFIG_CLASS = IndicxnliConfig
89
+ BUILDER_CONFIGS = [
90
+ IndicxnliConfig(
91
+ name=lang,
92
+ language=lang,
93
+ version=datasets.Version("1.1.0", ""),
94
+ description=f"Plain text import of IndicXNLI for the {lang} language",
95
+ )
96
+ for lang in _LANGUAGES
97
+ ]
98
+
99
+ def _info(self):
100
+ features = datasets.Features(
101
+ {
102
+ "premise": datasets.Value("string"),
103
+ "hypothesis": datasets.Value("string"),
104
+ "label": datasets.ClassLabel(names=["entailment", "neutral", "contradiction"]),
105
+ }
106
+ )
107
+ return datasets.DatasetInfo(
108
+ description=_DESCRIPTION,
109
+ features=features,
110
+ # No default supervised_keys (as we have to pass both premise
111
+ # and hypothesis as input).
112
+ supervised_keys=None,
113
+ homepage="https://www.nyu.edu/projects/bowman/xnli/",
114
+ citation=_CITATION,
115
+ )
116
+
117
+ def _split_generators(self, dl_manager):
118
+ train_dir = 'forward/train'
119
+ dev_dir = 'forward/dev'
120
+ test_dir = 'forward/test'
121
+
122
+ return [
123
+ datasets.SplitGenerator(
124
+ name=datasets.Split.TRAIN,
125
+ gen_kwargs={
126
+ "filepaths": [
127
+ os.path.join(train_dir, f"xnli_{lang}.json") for lang in self.config.languages
128
+ ],
129
+ "data_format": "IndicXNLI",
130
+ },
131
+ ),
132
+ datasets.SplitGenerator(
133
+ name=datasets.Split.TEST,
134
+ gen_kwargs={"filepaths": [os.path.join(
135
+ test_dir, f"xnli_{lang}.json") for lang in self.config.languages], "data_format": "IndicXNLI"},
136
+ ),
137
+ datasets.SplitGenerator(
138
+ name=datasets.Split.VALIDATION,
139
+ gen_kwargs={"filepaths": [os.path.join(
140
+ testval_dir, "xnli.dev.tsv") for lang in self.config.languages], "data_format": "XNLI"},
141
+ ),
142
+ ]
143
+
144
+ def _generate_examples(self, data_format, filepaths):
145
+ """This function returns the examples in the raw (text) form."""
146
+
147
+ if self.config.language == "all_languages":
148
+ if data_format == "XNLI-MT":
149
+ with ExitStack() as stack:
150
+ files = [stack.enter_context(
151
+ open(filepath, encoding="utf-8")) for filepath in filepaths]
152
+ readers = [csv.DictReader(
153
+ file, delimiter="\t", quoting=csv.QUOTE_NONE) for file in files]
154
+ for row_idx, rows in enumerate(zip(*readers)):
155
+ yield row_idx, {
156
+ "premise": {lang: row["premise"] for lang, row in zip(self.config.languages, rows)},
157
+ "hypothesis": {lang: row["hypo"] for lang, row in zip(self.config.languages, rows)},
158
+ "label": rows[0]["label"].replace("contradictory", "contradiction"),
159
+ }
160
+ else:
161
+ rows_per_pair_id = collections.defaultdict(list)
162
+ for filepath in filepaths:
163
+ with open(filepath, encoding="utf-8") as f:
164
+ reader = csv.DictReader(
165
+ f, delimiter="\t", quoting=csv.QUOTE_NONE)
166
+ for row in reader:
167
+ rows_per_pair_id[row["pairID"]].append(row)
168
+
169
+ for rows in rows_per_pair_id.values():
170
+ premise = {row["language"]: row["sentence1"]
171
+ for row in rows}
172
+ hypothesis = {row["language"]: row["sentence2"]
173
+ for row in rows}
174
+ yield rows[0]["pairID"], {
175
+ "premise": premise,
176
+ "hypothesis": hypothesis,
177
+ "label": rows[0]["gold_label"],
178
+ }
179
+ else:
180
+ if data_format == "XNLI-MT":
181
+ for file_idx, filepath in enumerate(filepaths):
182
+ file = open(filepath, encoding="utf-8")
183
+ reader = csv.DictReader(
184
+ file, delimiter="\t", quoting=csv.QUOTE_NONE)
185
+ for row_idx, row in enumerate(reader):
186
+ key = str(file_idx) + "_" + str(row_idx)
187
+ yield key, {
188
+ "premise": row["premise"],
189
+ "hypothesis": row["hypo"],
190
+ "label": row["label"].replace("contradictory", "contradiction"),
191
+ }
192
+ else:
193
+ for filepath in filepaths:
194
+ with open(filepath, encoding="utf-8") as f:
195
+ reader = csv.DictReader(
196
+ f, delimiter="\t", quoting=csv.QUOTE_NONE)
197
+ for row in reader:
198
+ if row["language"] == self.config.language:
199
+ yield row["pairID"], {
200
+ "premise": row["sentence1"],
201
+ "hypothesis": row["sentence2"],
202
+ "label": row["gold_label"],
203
+ }
.history/indicxnli_20220823221621.py ADDED
@@ -0,0 +1,203 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ @misc{https://doi.org/10.48550/arxiv.2204.08776,
30
+ doi = {10.48550/ARXIV.2204.08776},
31
+
32
+ url = {https://arxiv.org/abs/2204.08776},
33
+
34
+ author = {Aggarwal, Divyanshu and Gupta, Vivek and Kunchukuttan, Anoop},
35
+
36
+ keywords = {Computation and Language (cs.CL), Artificial Intelligence (cs.AI), FOS: Computer and information sciences, FOS: Computer and information sciences},
37
+
38
+ title = {IndicXNLI: Evaluating Multilingual Inference for Indian Languages},
39
+
40
+ publisher = {arXiv},
41
+
42
+ year = {2022},
43
+
44
+ copyright = {Creative Commons Attribution 4.0 International}
45
+ }
46
+ }"""
47
+
48
+ _DESCRIPTION = """\
49
+ IndicXNLI is a translated version of XNLI to 11 Indic Languages. As with XNLI, the goal is
50
+ to predict textual entailment (does sentence A imply/contradict/neither sentence
51
+ B) and is a classification task (given two sentences, predict one of three
52
+ labels).
53
+ """
54
+
55
+ _LANGUAGES = (
56
+ 'hi',
57
+ 'bn',
58
+ 'mr',
59
+ 'as',
60
+ 'ta',
61
+ 'te',
62
+ 'or',
63
+ 'ml',
64
+ 'pa',
65
+ 'gu',
66
+ 'kn'
67
+ )
68
+
69
+
70
+ class IndicxnliConfig(datasets.BuilderConfig):
71
+ """BuilderConfig for XNLI."""
72
+
73
+ def __init__(self, language: str, **kwargs):
74
+ """BuilderConfig for XNLI.
75
+
76
+ Args:
77
+ language: One of hi, bn, mr, as, ta, te, or, ml, pa, gu, kn
78
+ **kwargs: keyword arguments forwarded to super.
79
+ """
80
+ super(IndicxnliConfig, self).__init__(**kwargs)
81
+ self.language = language
82
+
83
+
84
+ class Indicxnli(datasets.GeneratorBasedBuilder):
85
+ """XNLI: The Cross-Lingual NLI Corpus. Version 1.0."""
86
+
87
+ VERSION = datasets.Version("1.1.0", "")
88
+ BUILDER_CONFIG_CLASS = IndicxnliConfig
89
+ BUILDER_CONFIGS = [
90
+ IndicxnliConfig(
91
+ name=lang,
92
+ language=lang,
93
+ version=datasets.Version("1.1.0", ""),
94
+ description=f"Plain text import of IndicXNLI for the {lang} language",
95
+ )
96
+ for lang in _LANGUAGES
97
+ ]
98
+
99
+ def _info(self):
100
+ features = datasets.Features(
101
+ {
102
+ "premise": datasets.Value("string"),
103
+ "hypothesis": datasets.Value("string"),
104
+ "label": datasets.ClassLabel(names=["entailment", "neutral", "contradiction"]),
105
+ }
106
+ )
107
+ return datasets.DatasetInfo(
108
+ description=_DESCRIPTION,
109
+ features=features,
110
+ # No default supervised_keys (as we have to pass both premise
111
+ # and hypothesis as input).
112
+ supervised_keys=None,
113
+ homepage="https://www.nyu.edu/projects/bowman/xnli/",
114
+ citation=_CITATION,
115
+ )
116
+
117
+ def _split_generators(self, dl_manager):
118
+ train_dir = 'forward/train'
119
+ dev_dir = 'forward/dev'
120
+ test_dir = 'forward/test'
121
+
122
+ return [
123
+ datasets.SplitGenerator(
124
+ name=datasets.Split.TRAIN,
125
+ gen_kwargs={
126
+ "filepaths": [
127
+ os.path.join(train_dir, f"xnli_{lang}.json") for lang in self.config.languages
128
+ ],
129
+ "data_format": "IndicXNLI",
130
+ },
131
+ ),
132
+ datasets.SplitGenerator(
133
+ name=datasets.Split.TEST,
134
+ gen_kwargs={"filepaths": [os.path.join(
135
+ test_dir, f"xnli_{lang}.json") for lang in self.config.languages], "data_format": "IndicXNLI"},
136
+ ),
137
+ datasets.SplitGenerator(
138
+ name=datasets.Split.VALIDATION,
139
+ gen_kwargs={"filepaths": [os.path.join(
140
+ test_dir, f"xnli_{lang}.json") for lang in self.config.languages], "data_format": "XNLI"},
141
+ ),
142
+ ]
143
+
144
+ def _generate_examples(self, data_format, filepaths):
145
+ """This function returns the examples in the raw (text) form."""
146
+
147
+ if self.config.language == "all_languages":
148
+ if data_format == "XNLI-MT":
149
+ with ExitStack() as stack:
150
+ files = [stack.enter_context(
151
+ open(filepath, encoding="utf-8")) for filepath in filepaths]
152
+ readers = [csv.DictReader(
153
+ file, delimiter="\t", quoting=csv.QUOTE_NONE) for file in files]
154
+ for row_idx, rows in enumerate(zip(*readers)):
155
+ yield row_idx, {
156
+ "premise": {lang: row["premise"] for lang, row in zip(self.config.languages, rows)},
157
+ "hypothesis": {lang: row["hypo"] for lang, row in zip(self.config.languages, rows)},
158
+ "label": rows[0]["label"].replace("contradictory", "contradiction"),
159
+ }
160
+ else:
161
+ rows_per_pair_id = collections.defaultdict(list)
162
+ for filepath in filepaths:
163
+ with open(filepath, encoding="utf-8") as f:
164
+ reader = csv.DictReader(
165
+ f, delimiter="\t", quoting=csv.QUOTE_NONE)
166
+ for row in reader:
167
+ rows_per_pair_id[row["pairID"]].append(row)
168
+
169
+ for rows in rows_per_pair_id.values():
170
+ premise = {row["language"]: row["sentence1"]
171
+ for row in rows}
172
+ hypothesis = {row["language"]: row["sentence2"]
173
+ for row in rows}
174
+ yield rows[0]["pairID"], {
175
+ "premise": premise,
176
+ "hypothesis": hypothesis,
177
+ "label": rows[0]["gold_label"],
178
+ }
179
+ else:
180
+ if data_format == "XNLI-MT":
181
+ for file_idx, filepath in enumerate(filepaths):
182
+ file = open(filepath, encoding="utf-8")
183
+ reader = csv.DictReader(
184
+ file, delimiter="\t", quoting=csv.QUOTE_NONE)
185
+ for row_idx, row in enumerate(reader):
186
+ key = str(file_idx) + "_" + str(row_idx)
187
+ yield key, {
188
+ "premise": row["premise"],
189
+ "hypothesis": row["hypo"],
190
+ "label": row["label"].replace("contradictory", "contradiction"),
191
+ }
192
+ else:
193
+ for filepath in filepaths:
194
+ with open(filepath, encoding="utf-8") as f:
195
+ reader = csv.DictReader(
196
+ f, delimiter="\t", quoting=csv.QUOTE_NONE)
197
+ for row in reader:
198
+ if row["language"] == self.config.language:
199
+ yield row["pairID"], {
200
+ "premise": row["sentence1"],
201
+ "hypothesis": row["sentence2"],
202
+ "label": row["gold_label"],
203
+ }
.history/indicxnli_20220823221623.py ADDED
@@ -0,0 +1,203 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ @misc{https://doi.org/10.48550/arxiv.2204.08776,
30
+ doi = {10.48550/ARXIV.2204.08776},
31
+
32
+ url = {https://arxiv.org/abs/2204.08776},
33
+
34
+ author = {Aggarwal, Divyanshu and Gupta, Vivek and Kunchukuttan, Anoop},
35
+
36
+ keywords = {Computation and Language (cs.CL), Artificial Intelligence (cs.AI), FOS: Computer and information sciences, FOS: Computer and information sciences},
37
+
38
+ title = {IndicXNLI: Evaluating Multilingual Inference for Indian Languages},
39
+
40
+ publisher = {arXiv},
41
+
42
+ year = {2022},
43
+
44
+ copyright = {Creative Commons Attribution 4.0 International}
45
+ }
46
+ }"""
47
+
48
+ _DESCRIPTION = """\
49
+ IndicXNLI is a translated version of XNLI to 11 Indic Languages. As with XNLI, the goal is
50
+ to predict textual entailment (does sentence A imply/contradict/neither sentence
51
+ B) and is a classification task (given two sentences, predict one of three
52
+ labels).
53
+ """
54
+
55
+ _LANGUAGES = (
56
+ 'hi',
57
+ 'bn',
58
+ 'mr',
59
+ 'as',
60
+ 'ta',
61
+ 'te',
62
+ 'or',
63
+ 'ml',
64
+ 'pa',
65
+ 'gu',
66
+ 'kn'
67
+ )
68
+
69
+
70
+ class IndicxnliConfig(datasets.BuilderConfig):
71
+ """BuilderConfig for XNLI."""
72
+
73
+ def __init__(self, language: str, **kwargs):
74
+ """BuilderConfig for XNLI.
75
+
76
+ Args:
77
+ language: One of hi, bn, mr, as, ta, te, or, ml, pa, gu, kn
78
+ **kwargs: keyword arguments forwarded to super.
79
+ """
80
+ super(IndicxnliConfig, self).__init__(**kwargs)
81
+ self.language = language
82
+
83
+
84
+ class Indicxnli(datasets.GeneratorBasedBuilder):
85
+ """XNLI: The Cross-Lingual NLI Corpus. Version 1.0."""
86
+
87
+ VERSION = datasets.Version("1.1.0", "")
88
+ BUILDER_CONFIG_CLASS = IndicxnliConfig
89
+ BUILDER_CONFIGS = [
90
+ IndicxnliConfig(
91
+ name=lang,
92
+ language=lang,
93
+ version=datasets.Version("1.1.0", ""),
94
+ description=f"Plain text import of IndicXNLI for the {lang} language",
95
+ )
96
+ for lang in _LANGUAGES
97
+ ]
98
+
99
+ def _info(self):
100
+ features = datasets.Features(
101
+ {
102
+ "premise": datasets.Value("string"),
103
+ "hypothesis": datasets.Value("string"),
104
+ "label": datasets.ClassLabel(names=["entailment", "neutral", "contradiction"]),
105
+ }
106
+ )
107
+ return datasets.DatasetInfo(
108
+ description=_DESCRIPTION,
109
+ features=features,
110
+ # No default supervised_keys (as we have to pass both premise
111
+ # and hypothesis as input).
112
+ supervised_keys=None,
113
+ homepage="https://www.nyu.edu/projects/bowman/xnli/",
114
+ citation=_CITATION,
115
+ )
116
+
117
+ def _split_generators(self, dl_manager):
118
+ train_dir = 'forward/train'
119
+ dev_dir = 'forward/dev'
120
+ test_dir = 'forward/test'
121
+
122
+ return [
123
+ datasets.SplitGenerator(
124
+ name=datasets.Split.TRAIN,
125
+ gen_kwargs={
126
+ "filepaths": [
127
+ os.path.join(train_dir, f"xnli_{lang}.json") for lang in self.config.languages
128
+ ],
129
+ "data_format": "IndicXNLI",
130
+ },
131
+ ),
132
+ datasets.SplitGenerator(
133
+ name=datasets.Split.TEST,
134
+ gen_kwargs={"filepaths": [os.path.join(
135
+ test_dir, f"xnli_{lang}.json") for lang in self.config.languages], "data_format": "IndicXNLI"},
136
+ ),
137
+ datasets.SplitGenerator(
138
+ name=datasets.Split.VALIDATION,
139
+ gen_kwargs={"filepaths": [os.path.join(
140
+ dev_dir, f"xnli_{lang}.json") for lang in self.config.languages], "data_format": "XNLI"},
141
+ ),
142
+ ]
143
+
144
+ def _generate_examples(self, data_format, filepaths):
145
+ """This function returns the examples in the raw (text) form."""
146
+
147
+ if self.config.language == "all_languages":
148
+ if data_format == "XNLI-MT":
149
+ with ExitStack() as stack:
150
+ files = [stack.enter_context(
151
+ open(filepath, encoding="utf-8")) for filepath in filepaths]
152
+ readers = [csv.DictReader(
153
+ file, delimiter="\t", quoting=csv.QUOTE_NONE) for file in files]
154
+ for row_idx, rows in enumerate(zip(*readers)):
155
+ yield row_idx, {
156
+ "premise": {lang: row["premise"] for lang, row in zip(self.config.languages, rows)},
157
+ "hypothesis": {lang: row["hypo"] for lang, row in zip(self.config.languages, rows)},
158
+ "label": rows[0]["label"].replace("contradictory", "contradiction"),
159
+ }
160
+ else:
161
+ rows_per_pair_id = collections.defaultdict(list)
162
+ for filepath in filepaths:
163
+ with open(filepath, encoding="utf-8") as f:
164
+ reader = csv.DictReader(
165
+ f, delimiter="\t", quoting=csv.QUOTE_NONE)
166
+ for row in reader:
167
+ rows_per_pair_id[row["pairID"]].append(row)
168
+
169
+ for rows in rows_per_pair_id.values():
170
+ premise = {row["language"]: row["sentence1"]
171
+ for row in rows}
172
+ hypothesis = {row["language"]: row["sentence2"]
173
+ for row in rows}
174
+ yield rows[0]["pairID"], {
175
+ "premise": premise,
176
+ "hypothesis": hypothesis,
177
+ "label": rows[0]["gold_label"],
178
+ }
179
+ else:
180
+ if data_format == "XNLI-MT":
181
+ for file_idx, filepath in enumerate(filepaths):
182
+ file = open(filepath, encoding="utf-8")
183
+ reader = csv.DictReader(
184
+ file, delimiter="\t", quoting=csv.QUOTE_NONE)
185
+ for row_idx, row in enumerate(reader):
186
+ key = str(file_idx) + "_" + str(row_idx)
187
+ yield key, {
188
+ "premise": row["premise"],
189
+ "hypothesis": row["hypo"],
190
+ "label": row["label"].replace("contradictory", "contradiction"),
191
+ }
192
+ else:
193
+ for filepath in filepaths:
194
+ with open(filepath, encoding="utf-8") as f:
195
+ reader = csv.DictReader(
196
+ f, delimiter="\t", quoting=csv.QUOTE_NONE)
197
+ for row in reader:
198
+ if row["language"] == self.config.language:
199
+ yield row["pairID"], {
200
+ "premise": row["sentence1"],
201
+ "hypothesis": row["sentence2"],
202
+ "label": row["gold_label"],
203
+ }
.history/indicxnli_20220823221950.py ADDED
@@ -0,0 +1,151 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ @misc{https://doi.org/10.48550/arxiv.2204.08776,
30
+ doi = {10.48550/ARXIV.2204.08776},
31
+
32
+ url = {https://arxiv.org/abs/2204.08776},
33
+
34
+ author = {Aggarwal, Divyanshu and Gupta, Vivek and Kunchukuttan, Anoop},
35
+
36
+ keywords = {Computation and Language (cs.CL), Artificial Intelligence (cs.AI), FOS: Computer and information sciences, FOS: Computer and information sciences},
37
+
38
+ title = {IndicXNLI: Evaluating Multilingual Inference for Indian Languages},
39
+
40
+ publisher = {arXiv},
41
+
42
+ year = {2022},
43
+
44
+ copyright = {Creative Commons Attribution 4.0 International}
45
+ }
46
+ }"""
47
+
48
+ _DESCRIPTION = """\
49
+ IndicXNLI is a translated version of XNLI to 11 Indic Languages. As with XNLI, the goal is
50
+ to predict textual entailment (does sentence A imply/contradict/neither sentence
51
+ B) and is a classification task (given two sentences, predict one of three
52
+ labels).
53
+ """
54
+
55
+ _LANGUAGES = (
56
+ 'hi',
57
+ 'bn',
58
+ 'mr',
59
+ 'as',
60
+ 'ta',
61
+ 'te',
62
+ 'or',
63
+ 'ml',
64
+ 'pa',
65
+ 'gu',
66
+ 'kn'
67
+ )
68
+
69
+
70
+ class IndicxnliConfig(datasets.BuilderConfig):
71
+ """BuilderConfig for XNLI."""
72
+
73
+ def __init__(self, language: str, **kwargs):
74
+ """BuilderConfig for XNLI.
75
+
76
+ Args:
77
+ language: One of hi, bn, mr, as, ta, te, or, ml, pa, gu, kn
78
+ **kwargs: keyword arguments forwarded to super.
79
+ """
80
+ super(IndicxnliConfig, self).__init__(**kwargs)
81
+ self.language = language
82
+
83
+
84
+ class Indicxnli(datasets.GeneratorBasedBuilder):
85
+ """XNLI: The Cross-Lingual NLI Corpus. Version 1.0."""
86
+
87
+ VERSION = datasets.Version("1.1.0", "")
88
+ BUILDER_CONFIG_CLASS = IndicxnliConfig
89
+ BUILDER_CONFIGS = [
90
+ IndicxnliConfig(
91
+ name=lang,
92
+ language=lang,
93
+ version=datasets.Version("1.1.0", ""),
94
+ description=f"Plain text import of IndicXNLI for the {lang} language",
95
+ )
96
+ for lang in _LANGUAGES
97
+ ]
98
+
99
+ def _info(self):
100
+ features = datasets.Features(
101
+ {
102
+ "premise": datasets.Value("string"),
103
+ "hypothesis": datasets.Value("string"),
104
+ "label": datasets.ClassLabel(names=["entailment", "neutral", "contradiction"]),
105
+ }
106
+ )
107
+ return datasets.DatasetInfo(
108
+ description=_DESCRIPTION,
109
+ features=features,
110
+ # No default supervised_keys (as we have to pass both premise
111
+ # and hypothesis as input).
112
+ supervised_keys=None,
113
+ homepage="https://www.nyu.edu/projects/bowman/xnli/",
114
+ citation=_CITATION,
115
+ )
116
+
117
+ def _split_generators(self, dl_manager):
118
+ train_dir = 'forward/train'
119
+ dev_dir = 'forward/dev'
120
+ test_dir = 'forward/test'
121
+
122
+ return [
123
+ datasets.SplitGenerator(
124
+ name=datasets.Split.TRAIN,
125
+ gen_kwargs={
126
+ "filepaths": [
127
+ os.path.join(train_dir, f"xnli_{lang}.json") for lang in self.config.languages
128
+ ],
129
+ "data_format": "IndicXNLI",
130
+ },
131
+ ),
132
+ datasets.SplitGenerator(
133
+ name=datasets.Split.TEST,
134
+ gen_kwargs={"filepaths": [os.path.join(
135
+ test_dir, f"xnli_{lang}.json") for lang in self.config.languages], "data_format": "IndicXNLI"},
136
+ ),
137
+ datasets.SplitGenerator(
138
+ name=datasets.Split.VALIDATION,
139
+ gen_kwargs={"filepaths": [os.path.join(
140
+ dev_dir, f"xnli_{lang}.json") for lang in self.config.languages], "data_format": "XNLI"},
141
+ ),
142
+ ]
143
+
144
+ def _generate_examples(self, data_format, filepaths):
145
+ """This function returns the examples in the raw (text) form."""
146
+
147
+ yield row["pairID"], {
148
+ "premise": row["sentence1"],
149
+ "hypothesis": row["sentence2"],
150
+ "label": row["gold_label"],
151
+ }
.history/indicxnli_20220823221952.py ADDED
@@ -0,0 +1,151 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ @misc{https://doi.org/10.48550/arxiv.2204.08776,
30
+ doi = {10.48550/ARXIV.2204.08776},
31
+
32
+ url = {https://arxiv.org/abs/2204.08776},
33
+
34
+ author = {Aggarwal, Divyanshu and Gupta, Vivek and Kunchukuttan, Anoop},
35
+
36
+ keywords = {Computation and Language (cs.CL), Artificial Intelligence (cs.AI), FOS: Computer and information sciences, FOS: Computer and information sciences},
37
+
38
+ title = {IndicXNLI: Evaluating Multilingual Inference for Indian Languages},
39
+
40
+ publisher = {arXiv},
41
+
42
+ year = {2022},
43
+
44
+ copyright = {Creative Commons Attribution 4.0 International}
45
+ }
46
+ }"""
47
+
48
+ _DESCRIPTION = """\
49
+ IndicXNLI is a translated version of XNLI to 11 Indic Languages. As with XNLI, the goal is
50
+ to predict textual entailment (does sentence A imply/contradict/neither sentence
51
+ B) and is a classification task (given two sentences, predict one of three
52
+ labels).
53
+ """
54
+
55
+ _LANGUAGES = (
56
+ 'hi',
57
+ 'bn',
58
+ 'mr',
59
+ 'as',
60
+ 'ta',
61
+ 'te',
62
+ 'or',
63
+ 'ml',
64
+ 'pa',
65
+ 'gu',
66
+ 'kn'
67
+ )
68
+
69
+
70
+ class IndicxnliConfig(datasets.BuilderConfig):
71
+ """BuilderConfig for XNLI."""
72
+
73
+ def __init__(self, language: str, **kwargs):
74
+ """BuilderConfig for XNLI.
75
+
76
+ Args:
77
+ language: One of hi, bn, mr, as, ta, te, or, ml, pa, gu, kn
78
+ **kwargs: keyword arguments forwarded to super.
79
+ """
80
+ super(IndicxnliConfig, self).__init__(**kwargs)
81
+ self.language = language
82
+
83
+
84
+ class Indicxnli(datasets.GeneratorBasedBuilder):
85
+ """XNLI: The Cross-Lingual NLI Corpus. Version 1.0."""
86
+
87
+ VERSION = datasets.Version("1.1.0", "")
88
+ BUILDER_CONFIG_CLASS = IndicxnliConfig
89
+ BUILDER_CONFIGS = [
90
+ IndicxnliConfig(
91
+ name=lang,
92
+ language=lang,
93
+ version=datasets.Version("1.1.0", ""),
94
+ description=f"Plain text import of IndicXNLI for the {lang} language",
95
+ )
96
+ for lang in _LANGUAGES
97
+ ]
98
+
99
+ def _info(self):
100
+ features = datasets.Features(
101
+ {
102
+ "premise": datasets.Value("string"),
103
+ "hypothesis": datasets.Value("string"),
104
+ "label": datasets.ClassLabel(names=["entailment", "neutral", "contradiction"]),
105
+ }
106
+ )
107
+ return datasets.DatasetInfo(
108
+ description=_DESCRIPTION,
109
+ features=features,
110
+ # No default supervised_keys (as we have to pass both premise
111
+ # and hypothesis as input).
112
+ supervised_keys=None,
113
+ homepage="https://www.nyu.edu/projects/bowman/xnli/",
114
+ citation=_CITATION,
115
+ )
116
+
117
+ def _split_generators(self, dl_manager):
118
+ train_dir = 'forward/train'
119
+ dev_dir = 'forward/dev'
120
+ test_dir = 'forward/test'
121
+
122
+ return [
123
+ datasets.SplitGenerator(
124
+ name=datasets.Split.TRAIN,
125
+ gen_kwargs={
126
+ "filepaths": [
127
+ os.path.join(train_dir, f"xnli_{lang}.json") for lang in self.config.languages
128
+ ],
129
+ "data_format": "IndicXNLI",
130
+ },
131
+ ),
132
+ datasets.SplitGenerator(
133
+ name=datasets.Split.TEST,
134
+ gen_kwargs={"filepaths": [os.path.join(
135
+ test_dir, f"xnli_{lang}.json") for lang in self.config.languages], "data_format": "IndicXNLI"},
136
+ ),
137
+ datasets.SplitGenerator(
138
+ name=datasets.Split.VALIDATION,
139
+ gen_kwargs={"filepaths": [os.path.join(
140
+ dev_dir, f"xnli_{lang}.json") for lang in self.config.languages], "data_format": "XNLI"},
141
+ ),
142
+ ]
143
+
144
+ def _generate_examples(self, data_format, filepaths):
145
+ """This function returns the examples in the raw (text) form."""
146
+
147
+ yield row["pairID"], {
148
+ "premise": row["sentence1"],
149
+ "hypothesis": row["sentence2"],
150
+ "label": row["gold_label"],
151
+ }
.history/indicxnli_20220823221954.py ADDED
@@ -0,0 +1,151 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ @misc{https://doi.org/10.48550/arxiv.2204.08776,
30
+ doi = {10.48550/ARXIV.2204.08776},
31
+
32
+ url = {https://arxiv.org/abs/2204.08776},
33
+
34
+ author = {Aggarwal, Divyanshu and Gupta, Vivek and Kunchukuttan, Anoop},
35
+
36
+ keywords = {Computation and Language (cs.CL), Artificial Intelligence (cs.AI), FOS: Computer and information sciences, FOS: Computer and information sciences},
37
+
38
+ title = {IndicXNLI: Evaluating Multilingual Inference for Indian Languages},
39
+
40
+ publisher = {arXiv},
41
+
42
+ year = {2022},
43
+
44
+ copyright = {Creative Commons Attribution 4.0 International}
45
+ }
46
+ }"""
47
+
48
+ _DESCRIPTION = """\
49
+ IndicXNLI is a translated version of XNLI to 11 Indic Languages. As with XNLI, the goal is
50
+ to predict textual entailment (does sentence A imply/contradict/neither sentence
51
+ B) and is a classification task (given two sentences, predict one of three
52
+ labels).
53
+ """
54
+
55
+ _LANGUAGES = (
56
+ 'hi',
57
+ 'bn',
58
+ 'mr',
59
+ 'as',
60
+ 'ta',
61
+ 'te',
62
+ 'or',
63
+ 'ml',
64
+ 'pa',
65
+ 'gu',
66
+ 'kn'
67
+ )
68
+
69
+
70
+ class IndicxnliConfig(datasets.BuilderConfig):
71
+ """BuilderConfig for XNLI."""
72
+
73
+ def __init__(self, language: str, **kwargs):
74
+ """BuilderConfig for XNLI.
75
+
76
+ Args:
77
+ language: One of hi, bn, mr, as, ta, te, or, ml, pa, gu, kn
78
+ **kwargs: keyword arguments forwarded to super.
79
+ """
80
+ super(IndicxnliConfig, self).__init__(**kwargs)
81
+ self.language = language
82
+
83
+
84
+ class Indicxnli(datasets.GeneratorBasedBuilder):
85
+ """XNLI: The Cross-Lingual NLI Corpus. Version 1.0."""
86
+
87
+ VERSION = datasets.Version("1.1.0", "")
88
+ BUILDER_CONFIG_CLASS = IndicxnliConfig
89
+ BUILDER_CONFIGS = [
90
+ IndicxnliConfig(
91
+ name=lang,
92
+ language=lang,
93
+ version=datasets.Version("1.1.0", ""),
94
+ description=f"Plain text import of IndicXNLI for the {lang} language",
95
+ )
96
+ for lang in _LANGUAGES
97
+ ]
98
+
99
+ def _info(self):
100
+ features = datasets.Features(
101
+ {
102
+ "premise": datasets.Value("string"),
103
+ "hypothesis": datasets.Value("string"),
104
+ "label": datasets.ClassLabel(names=["entailment", "neutral", "contradiction"]),
105
+ }
106
+ )
107
+ return datasets.DatasetInfo(
108
+ description=_DESCRIPTION,
109
+ features=features,
110
+ # No default supervised_keys (as we have to pass both premise
111
+ # and hypothesis as input).
112
+ supervised_keys=None,
113
+ homepage="https://www.nyu.edu/projects/bowman/xnli/",
114
+ citation=_CITATION,
115
+ )
116
+
117
+ def _split_generators(self, dl_manager):
118
+ train_dir = 'forward/train'
119
+ dev_dir = 'forward/dev'
120
+ test_dir = 'forward/test'
121
+
122
+ return [
123
+ datasets.SplitGenerator(
124
+ name=datasets.Split.TRAIN,
125
+ gen_kwargs={
126
+ "filepaths": [
127
+ os.path.join(train_dir, f"xnli_{lang}.json") for lang in self.config.languages
128
+ ],
129
+ "data_format": "IndicXNLI",
130
+ },
131
+ ),
132
+ datasets.SplitGenerator(
133
+ name=datasets.Split.TEST,
134
+ gen_kwargs={"filepaths": [os.path.join(
135
+ test_dir, f"xnli_{lang}.json") for lang in self.config.languages], "data_format": "IndicXNLI"},
136
+ ),
137
+ datasets.SplitGenerator(
138
+ name=datasets.Split.VALIDATION,
139
+ gen_kwargs={"filepaths": [os.path.join(
140
+ dev_dir, f"xnli_{lang}.json") for lang in self.config.languages], "data_format": "XNLI"},
141
+ ),
142
+ ]
143
+
144
+ def _generate_examples(self, data_format, filepaths):
145
+ """This function returns the examples in the raw (text) form."""
146
+
147
+ yield row["pairID"], {
148
+ "premise": row["sentence1"],
149
+ "hypothesis": row["sentence2"],
150
+ "label": row["gold_label"],
151
+ }