File size: 4,836 Bytes
00a88ea
 
 
 
754e346
 
00a88ea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fa83233
00a88ea
 
 
 
 
 
c81df53
 
00a88ea
 
 
63c9fac
00a88ea
 
 
63c9fac
 
7b169a8
63c9fac
 
 
 
 
 
 
00a88ea
 
 
 
 
 
333a47c
 
 
 
 
 
00a88ea
 
 
 
 
 
 
 
 
c81df53
00a88ea
 
 
 
 
 
 
 
 
 
63c9fac
00a88ea
63c9fac
 
 
 
 
 
 
 
 
 
 
 
00a88ea
63c9fac
 
 
 
 
 
 
 
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
"""
Modified from https://huggingface.co/datasets/khalidalt/tydiqa-goldp/blob/main/tydiqa-goldp.py
"""

from typing import List

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).
"""


_URL = "https://huggingface.co/datasets/chompk/tydiqa-goldp-th/resolve/main/xtreme/tydiqa.goldp.th.{split}.json"
_VERSION = datasets.Version("1.1.0", "")


class tydiqa_GoldP_th(datasets.GeneratorBasedBuilder):
    BUILDER_CONFIGS = [
        datasets.BuilderConfig(
            name="th",
            description=f"tydiqa-GoldP TH",
            version=_VERSION,
        )
    ]
    
    def _info(self):
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=datasets.Features(
                {
                    "id": datasets.Value("string"),
                    "context": datasets.Value("string"),
                    "question": datasets.Value("string"),
                    "answers": datasets.features.Sequence(
                        {"text": datasets.Value("string"), "answer_start": datasets.Value("int32"),}
                    ),
                }
            ),
            # No default supervised_keys (as we have to pass both question
            # and context as input).
            supervised_keys=None,
            homepage="https://github.com/google-research-datasets/tydiqa",
            citation=_CITATION,
        )
    
    def _split_generators(self, dl_manager: datasets.DownloadManager) -> List[datasets.SplitGenerator]:
        downloaded_files = dl_manager.download([f"data/shard_{i}.jsonl" for i in range(1024)])
        return [
            datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepaths": downloaded_files}),
        ]
        
    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
        splits = {datasets.Split.TRAIN: "train", datasets.Split.VALIDATION: "dev"}
        
        data_urls = {
            split: _URL.format(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):
        """This function returns the examples in the raw (text) form."""
        with open(filepath) as f:
            squad = json.load(f)
            for article in squad["data"]:
                for paragraph in article["paragraphs"]:
                    context = paragraph["context"]
                    for qa in paragraph["qas"]:
                        question = qa["question"]
                        id_ = qa["id"]

                        answer_starts = [answer["answer_start"] for answer in qa["answers"]]
                        answers = [answer["text"].strip() for answer in qa["answers"]]

                        # Features currently used are "context", "question", and "answers".
                        # Others are extracted here for the ease of future expansions.
                        yield id_, {
                            "context": context,
                            "question": question,
                            "id": id_,
                            "answers": {"answer_start": answer_starts, "text": answers,},
                        }