File size: 10,812 Bytes
449984e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f315d37
449984e
 
 
 
f315d37
449984e
 
 
 
 
 
 
f315d37
 
449984e
 
 
 
 
 
 
 
 
 
 
 
 
f315d37
 
449984e
 
 
 
 
 
 
 
f315d37
 
449984e
 
 
 
 
 
 
 
f315d37
449984e
f315d37
 
 
 
 
449984e
f315d37
 
 
 
 
 
977bf5b
 
 
 
 
 
f315d37
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# coding=utf-8
# Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""EXAMS: a benchmark dataset for multilingual and cross-lingual question answering"""


import json

import datasets


_CITATION = """\
@article{hardalov2020exams,
  title={EXAMS: A Multi-subject High School Examinations Dataset for Cross-lingual and Multilingual Question Answering},
  author={Hardalov, Momchil and Mihaylov, Todor and Dimitrina Zlatkova and Yoan Dinkov and Ivan Koychev and Preslav Nvakov},
  journal={arXiv preprint arXiv:2011.03080},
  year={2020}
}
"""

_DESCRIPTION = """\
EXAMS is a benchmark dataset for multilingual and cross-lingual question answering from high school examinations.
It consists of more than 24,000 high-quality high school exam questions in 16 languages,
covering 8 language families and 24 school subjects from Natural Sciences and Social Sciences, among others.
"""

_HOMEPAGE = "https://github.com/mhardalov/exams-qa"

_LICENSE = "CC-BY-SA-4.0"

_URLS_LIST = [
    ("alignments", "https://github.com/mhardalov/exams-qa/raw/main/data/exams/parallel_questions.jsonl"),
]
_URLS_LIST += [
    (
        "multilingual_train",
        "https://github.com/mhardalov/exams-qa/raw/main/data/exams/multilingual/train.jsonl.tar.gz",
    ),
    ("multilingual_dev", "https://github.com/mhardalov/exams-qa/raw/main/data/exams/multilingual/dev.jsonl.tar.gz"),
    ("multilingual_test", "https://github.com/mhardalov/exams-qa/raw/main/data/exams/multilingual/test.jsonl.tar.gz"),
    (
        "multilingual_with_para_train",
        "https://github.com/mhardalov/exams-qa/raw/main/data/exams/multilingual/with_paragraphs/train_with_para.jsonl.tar.gz",
    ),
    (
        "multilingual_with_para_dev",
        "https://github.com/mhardalov/exams-qa/raw/main/data/exams/multilingual/with_paragraphs/dev_with_para.jsonl.tar.gz",
    ),
    (
        "multilingual_with_para_test",
        "https://github.com/mhardalov/exams-qa/raw/main/data/exams/multilingual/with_paragraphs/test_with_para.jsonl.tar.gz",
    ),
]

_CROSS_LANGUAGES = ["bg", "hr", "hu", "it", "mk", "pl", "pt", "sq", "sr", "tr", "vi"]
_URLS_LIST += [
    ("crosslingual_test", "https://github.com/mhardalov/exams-qa/raw/main/data/exams/cross-lingual/test.jsonl.tar.gz"),
    (
        "crosslingual_with_para_test",
        "https://github.com/mhardalov/exams-qa/raw/main/data/exams/cross-lingual/with_paragraphs/test_with_para.jsonl.tar.gz",
    ),
]
for ln in _CROSS_LANGUAGES:
    _URLS_LIST += [
        (
            f"crosslingual_{ln}_train",
            f"https://github.com/mhardalov/exams-qa/raw/main/data/exams/cross-lingual/train_{ln}.jsonl.tar.gz",
        ),
        (
            f"crosslingual_with_para_{ln}_train",
            f"https://github.com/mhardalov/exams-qa/raw/main/data/exams/cross-lingual/with_paragraphs/train_{ln}_with_para.jsonl.tar.gz",
        ),
        (
            f"crosslingual_{ln}_dev",
            f"https://github.com/mhardalov/exams-qa/raw/main/data/exams/cross-lingual/dev_{ln}.jsonl.tar.gz",
        ),
        (
            f"crosslingual_with_para_{ln}_dev",
            f"https://github.com/mhardalov/exams-qa/raw/main/data/exams/cross-lingual/with_paragraphs/dev_{ln}_with_para.jsonl.tar.gz",
        ),
    ]
_URLs = dict(_URLS_LIST)


class ExamsConfig(datasets.BuilderConfig):
    def __init__(self, lang, with_para, **kwargs):
        super(ExamsConfig, self).__init__(version=datasets.Version("1.0.0", ""), **kwargs)
        self.lang = lang
        self.with_para = "_with_para" if with_para else ""


class Exams(datasets.GeneratorBasedBuilder):
    """Exams dataset"""

    VERSION = datasets.Version("1.0.0")
    BUILDER_CONFIG_CLASS = ExamsConfig
    BUILDER_CONFIGS = [
        ExamsConfig(
            lang="",
            with_para=False,
            name="alignments",
            description="loads the alignment between question IDs across languages",
        ),
        ExamsConfig(
            lang="all",
            with_para=False,
            name="multilingual",
            description="Loads the unified multilingual train/dev/test split",
        ),
        ExamsConfig(
            lang="all",
            with_para=True,
            name="multilingual_with_para",
            description="Loads the unified multilingual train/dev/test split with Wikipedia support paragraphs",
        ),
        ExamsConfig(
            lang="all", with_para=False, name="crosslingual_test", description="Loads crosslingual test set only"
        ),
        ExamsConfig(
            lang="all",
            with_para=True,
            name="crosslingual_with_para_test",
            description="Loads crosslingual test set only with Wikipedia support paragraphs",
        ),
    ]
    for ln in _CROSS_LANGUAGES:
        BUILDER_CONFIGS += [
            ExamsConfig(
                lang=ln,
                with_para=False,
                name=f"crosslingual_{ln}",
                description=f"Loads crosslingual train and dev set for {ln}",
            ),
            ExamsConfig(
                lang=ln,
                with_para=True,
                name=f"crosslingual_with_para_{ln}",
                description=f"Loads crosslingual train and dev set for {ln} with Wikipedia support paragraphs",
            ),
        ]

    DEFAULT_CONFIG_NAME = (
        "multilingual_with_para"  # It's not mandatory to have a default configuration. Just use one if it make sense.
    )

    def _info(self):
        if self.config.name == "alignments":  # This is the name of the configuration selected in BUILDER_CONFIGS above
            features = datasets.Features(
                {
                    "source_id": datasets.Value("string"),
                    "target_id_list": datasets.Sequence(datasets.Value("string")),
                }
            )
        else:  # This is an example to show how to have different features for "first_domain" and "second_domain"
            features = datasets.Features(
                {
                    "id": datasets.Value("string"),
                    "question": {
                        "stem": datasets.Value("string"),
                        "choices": datasets.Sequence(
                            {
                                "text": datasets.Value("string"),
                                "label": datasets.Value("string"),
                                "para": datasets.Value("string"),
                            }
                        ),
                    },
                    "answerKey": datasets.Value("string"),
                    "info": {
                        "grade": datasets.Value("int32"),
                        "subject": datasets.Value("string"),
                        "language": datasets.Value("string"),
                    },
                }
            )
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=features,  # Here we define them above because they are different between the two configurations
            supervised_keys=None,
            homepage=_HOMEPAGE,
            license=_LICENSE,
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager):
        """Returns SplitGenerators."""
        archives = dl_manager.download(_URLs)
        if self.config.name == "alignments":
            return [
                datasets.SplitGenerator(
                    name="full",
                    gen_kwargs={"filepath": archives["alignments"]},
                ),
            ]
        elif self.config.name in ["multilingual", "multilingual_with_para"]:
            return [
                datasets.SplitGenerator(
                    name=spl_enum,
                    gen_kwargs={
                        "filepath": f"{spl}{self.config.with_para}.jsonl",
                        "files": dl_manager.iter_archive(archives[f"{self.config.name}_{spl}"]),
                    },
                )
                for spl, spl_enum in [
                    ("train", datasets.Split.TRAIN),
                    ("dev", datasets.Split.VALIDATION),
                    ("test", datasets.Split.TEST),
                ]
            ]
        elif self.config.name in ["crosslingual_test", "crosslingual_with_para_test"]:
            return [
                datasets.SplitGenerator(
                    name=datasets.Split.TEST,
                    gen_kwargs={
                        "filepath": f"test{self.config.with_para}.jsonl",
                        "files": dl_manager.iter_archive(archives[self.config.name]),
                    },
                ),
            ]
        else:
            return [
                datasets.SplitGenerator(
                    name=spl_enum,
                    gen_kwargs={
                        "filepath": f"{spl}_{self.config.lang}{self.config.with_para}.jsonl",
                        "files": dl_manager.iter_archive(archives[f"{self.config.name}_{spl}"]),
                    },
                )
                for spl, spl_enum in [
                    ("train", datasets.Split.TRAIN),
                    ("dev", datasets.Split.VALIDATION),
                ]
            ]

    def _generate_examples(self, filepath, files=None):
        if self.config.name == "alignments":
            with open(filepath, encoding="utf-8") as f:
                for id_, line in enumerate(f):
                    line_dict = json.loads(line.strip())
                    in_id, out_list = list(line_dict.items())[0]
                    yield id_, {"source_id": in_id, "target_id_list": out_list}
        else:
            for path, f in files:
                if path == filepath:
                    for id_, line in enumerate(f):
                        line_dict = json.loads(line.strip())
                        for choice in line_dict["question"]["choices"]:
                            choice["para"] = choice.get("para", "")
                        yield id_, {
                            "id": line_dict["id"],
                            "question": line_dict["question"],
                            "answerKey": line_dict["answerKey"],
                            "info": line_dict["info"],
                        }
                    break