File size: 7,193 Bytes
4b8ca92
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89922d6
4b8ca92
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89922d6
 
4b8ca92
 
 
 
 
89922d6
 
 
4b8ca92
 
 
 
 
 
 
 
 
89922d6
4b8ca92
 
 
 
 
89922d6
4b8ca92
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# coding=utf-8

"""Mintaka: A Complex, Natural, and Multilingual Dataset for End-to-End Question Answering"""

import json
import datasets

logger = datasets.logging.get_logger(__name__)

_DESCRIPTION = """\
         Mintaka is a complex, natural, and multilingual dataset designed for experimenting with end-to-end
         question-answering models. Mintaka is composed of 20,000 question-answer pairs collected in English,
         annotated with Wikidata entities, and translated into Arabic, French, German, Hindi, Italian,
         Japanese, Portuguese, and Spanish for a total of 180,000 samples.
         Mintaka includes 8 types of complex questions, including superlative, intersection, and multi-hop questions, 
         which were naturally elicited from crowd workers. 
"""

_CITATION = """\
        @inproceedings{sen-etal-2022-mintaka,
            title = "Mintaka: A Complex, Natural, and Multilingual Dataset for End-to-End Question Answering",
            author = "Sen, Priyanka and Aji, Alham Fikri and Saffari, Amir",
            booktitle = "Proceedings of the 29th International Conference on Computational Linguistics",
            month = oct,
            year = "2022",
            address = "Gyeongju, Republic of Korea",
            publisher = "International Committee on Computational Linguistics",
            url = "https://aclanthology.org/2022.coling-1.138",
            pages = "1604--1619"
        }
"""

_LICENSE = """\
Copyright Amazon.com Inc. or its affiliates.
Attribution 4.0 International
"""

_TRAIN_URL = "https://raw.githubusercontent.com/amazon-science/mintaka/main/data/mintaka_train.json"
_DEV_URL = "https://raw.githubusercontent.com/amazon-science/mintaka/main/data/mintaka_dev.json"
_TEST_URL = "https://raw.githubusercontent.com/amazon-science/mintaka/main/data/mintaka_test.json"


_LANGUAGES = ['en', 'ar', 'de', 'ja', 'hi', 'pt', 'es', 'it', 'fr']

_ALL = "all"

class Mintaka(datasets.GeneratorBasedBuilder):
    """Mintaka: A Complex, Natural, and Multilingual Dataset for End-to-End Question Answering"""

    BUILDER_CONFIGS = [
        datasets.BuilderConfig(
            name = name,
            version = datasets.Version("1.0.0"),
            description = f"Mintaka: A Complex, Natural, and Multilingual Dataset for End-to-End Question Answering for {name}",
        ) for name in _LANGUAGES
    ]

    BUILDER_CONFIGS.append(datasets.BuilderConfig(
        name = _ALL,
        version = datasets.Version("1.0.0"),
        description = f"Mintaka: A Complex, Natural, and Multilingual Dataset for End-to-End Question Answering",
    ))

    DEFAULT_CONFIG_NAME = 'en'

    def _info(self):
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=datasets.Features(
                {
                    "id": datasets.Value("string"),
                    "lang": datasets.Value("string"),
                    "question": datasets.Value("string"),
                    "answerText": datasets.Value("string"),
                    "category": datasets.Value("string"),
                    "complexityType": datasets.Value("string"),
                    "questionEntity": [{
                        "name": datasets.Value("string"),
                        "entityType": datasets.Value("string"),
                        "label": datasets.Value("string"),
                        "mention": datasets.Value("string"),
                        "span": [datasets.Value("int32")],
                    }],
                    "answerEntity": [{
                        "name": datasets.Value("string"),
                        "label": datasets.Value("string"),
                    }]
                },
            ),
            supervised_keys=None,
            citation=_CITATION,
            license=_LICENSE,
        )

    def _split_generators(self, dl_manager):
        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={
                    "file": dl_manager.download_and_extract(_TRAIN_URL),
                    "lang": self.config.name,
                }
            ),
            datasets.SplitGenerator(
                name=datasets.Split.VALIDATION,
                gen_kwargs={
                    "file": dl_manager.download_and_extract(_DEV_URL),
                    "lang": self.config.name,
                }
            ),
            datasets.SplitGenerator(
                name=datasets.Split.TEST,
                gen_kwargs={
                    "file": dl_manager.download_and_extract(_TEST_URL),
                    "lang": self.config.name,
                }
            ),
        ]

    def _generate_examples(self, file, lang):
        if lang == _ALL:
            langs = _LANGUAGES
        else:
            langs = [lang]

        key_ = 0

        logger.info("⏳ Generating examples from = %s", ", ".join(lang))

        with open(file, encoding='utf-8') as json_file:
            data = json.load(json_file)
            for lang in langs:
                for sample in data:
                    questionEntity = [
                        {
                            "name": str(qe["name"]),
                            "entityType": qe["entityType"],
                            "label": qe["label"] if "label" in qe else "",
                            # span only applies for English question
                            "mention": qe["mention"] if lang == "en" else None,
                            "span": qe["span"] if lang == "en" else [],
                        } for qe in sample["questionEntity"]
                    ]

                    answers = []
                    if sample['answer']["answerType"] == "entity" and sample['answer']['answer'] is not None:
                        answers = sample['answer']['answer']
                    elif sample['answer']["answerType"] == "numerical" and "supportingEnt" in sample["answer"]:
                        answers = sample['answer']['supportingEnt']

                    # helper to get language for the corresponding language
                    def get_label(labels, lang):
                        if lang in labels:
                            return labels[lang]
                        if 'en' in labels:
                            return labels['en']
                        return None

                    answerEntity = [
                        {
                            "name": str(ae["name"]),
                            "label": get_label(ae["label"], lang),
                        } for ae in answers
                    ]

                    yield key_, {
                        "id": sample["id"],
                        "lang": lang,
                        "question": sample["question"] if lang == 'en' else sample['translations'][lang],
                        "answerText": sample["answer"]["mention"],
                        "category": sample["category"],
                        "complexityType": sample["complexityType"],
                        "questionEntity": questionEntity,
                        "answerEntity": answerEntity,
                    }

                    key_ += 1