File size: 7,167 Bytes
c2ce206
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
deb5bb3
c2ce206
 
 
 
 
 
 
 
 
 
 
 
 
 
7f1af59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f352ef2
7f1af59
 
 
 
 
 
 
f352ef2
7f1af59
 
 
 
 
 
 
 
 
 
 
 
c2ce206
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7f1af59
c2ce206
7f1af59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json
import textwrap

import datasets
from datasets.tasks import QuestionAnsweringExtractive

# TODO(tydiqa): BibTeX citation
_CITATION = """\
@article{tydiqa,
title   = {TyDi QA: A Benchmark for Information-Seeking Question Answering in Typologically Diverse Languages},
author  = {Jonathan H. Clark and Eunsol Choi and Michael Collins and Dan Garrette and Tom Kwiatkowski and Vitaly Nikolaev and Jennimaria Palomaki}
year    = {2020},
journal = {Transactions of the Association for Computational Linguistics}
}
"""

# TODO(tydiqa):
_DESCRIPTION = """\
TyDi QA is a question answering dataset covering 11 typologically diverse languages with 204K question-answer pairs.
The languages of TyDi QA are diverse with regard to their typology -- the set of linguistic features that each language
expresses -- such that we expect models performing well on this set to generalize across a large number of the languages
in the world. It contains language phenomena that would not be found in English-only corpora. To provide a realistic
information-seeking task and avoid priming effects, questions are written by people who want to know the answer, but
don’t know the answer yet, (unlike SQuAD and its descendents) and the data is collected directly in each language without
the use of translation (unlike MLQA and XQuAD).
"""


_LANG = ["arabic", "bengali", "english", "finnish", "indonesian", "japanese", "korean", "russian", "swahili", "telugu", "thai"]

_URL = "https://huggingface.co/datasets/khalidalt/tydiqa-goldp/resolve/main/primary_tasks/{split}/{language}-{split}.jsonl"
_VERSION = datasets.Version("1.1.0", "")


class tydiqa_Primary(datasets.GeneratorBasedBuilder):
    BUILDER_CONFIGS = [
        datasets.BuilderConfig(
            name=lang,
            description=f"tydiqa-primary language {lang}",
            version=_VERSION,
        )
        for lang in _LANG
    ]


    def _info(self):
        # TODO(tydiqa): Specifies the datasets.DatasetInfo object

        return datasets.DatasetInfo(
            # This is the description that will appear on the datasets page.
            description=_DESCRIPTION,
            # datasets.features.FeatureConnectors
            features=datasets.Features(
                {
                    "passage_answer_candidates": datasets.features.Sequence(
                        {
                            "plaintext_start_byte": datasets.Value("int32"),
                            "plaintext_end_byte": datasets.Value("int32"),
                        }
                    ),
                    "question_text": datasets.Value("string"),
                    "document_title": datasets.Value("string"),
                    "language": datasets.Value("string"),
                    "annotations": datasets.features.Sequence(
                        {
                            #'annotation_id': datasets.Value('int32'),
                            "passage_answer_candidate_index": datasets.Value("int32"),
                            "minimal_answers_start_byte": datasets.Value("int32"),
                            "minimal_answers_end_byte": datasets.Value("int32"),
                            "yes_no_answer": datasets.Value("string"),
                        }
                    ),
                    "document_plaintext": datasets.Value("string"),
                    #'example_id': datasets.Value('int32'),
                    "document_url": datasets.Value("string")
                    # These are the features of your dataset like images, labels ...
                }
            ),
            # If there's a common (input, target) tuple from the features,
            # specify them here. They'll be used if as_supervised=True in
            # builder.as_dataset.
            supervised_keys=None,
            # Homepage of the dataset for documentation
            homepage="https://github.com/google-research-datasets/tydiqa",
            citation=_CITATION,
        )
                    
    def _split_generators(self, dl_manager):
        """Returns SplitGenerators."""
        # TODO(tydiqa): Downloads the data and defines the splits
        # dl_manager is a datasets.download.DownloadManager that can be used to
        # download and extract URLs
        language = self.config.name
        splits = {datasets.Split.TRAIN: "train", datasets.Split.VALIDATION: "dev"}
        
        data_urls = {
            split: _URL.format(language=language, split=splits[split]) for split in splits
        }
        
        dl_paths = dl_manager.download(data_urls)
        return [
            datasets.SplitGenerator(
                name=split,
                gen_kwargs={"filepath": dl_paths[split]},
            )
            for split in splits
        ]
        
    def _generate_examples(self, filepath):
        """Yields examples."""
        # TODO(tydiqa): Yields (key, example) tuples from the dataset

        with open(filepath, encoding="utf-8") as f:
            for id_, row in enumerate(f):
                data = json.loads(row)
                passages = data["passage_answer_candidates"]
                end_byte = [passage["plaintext_end_byte"] for passage in passages]
                start_byte = [passage["plaintext_start_byte"] for passage in passages]
                title = data["document_title"]
                lang = data["language"]
                question = data["question_text"]
                annotations = data["annotations"]
                # annot_ids = [annotation["annotation_id"] for annotation in annotations]
                yes_no_answers = [annotation["yes_no_answer"] for annotation in annotations]
                min_answers_end_byte = [
                    annotation["minimal_answer"]["plaintext_end_byte"] for annotation in annotations
                ]
                min_answers_start_byte = [
                    annotation["minimal_answer"]["plaintext_start_byte"] for annotation in annotations
                ]
                passage_cand_answers = [
                    annotation["passage_answer"]["candidate_index"] for annotation in annotations
                ]
                doc = data["document_plaintext"]
                # example_id = data["example_id"]
                url = data["document_url"]
                yield id_, {
                    "passage_answer_candidates": {
                        "plaintext_start_byte": start_byte,
                        "plaintext_end_byte": end_byte,
                    },
                    "question_text": question,
                    "document_title": title,
                    "language": lang,
                    "annotations": {
                        # 'annotation_id': annot_ids,
                        "passage_answer_candidate_index": passage_cand_answers,
                        "minimal_answers_start_byte": min_answers_start_byte,
                        "minimal_answers_end_byte": min_answers_end_byte,
                        "yes_no_answer": yes_no_answers,
                    },
                    "document_plaintext": doc,
                    # 'example_id': example_id,
                    "document_url": url,
                }