Create tydiqa-goldp.py
Browse files- tydiqa-goldp.py +131 -0
    	
        tydiqa-goldp.py
    ADDED
    
    | @@ -0,0 +1,131 @@ | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | 
|  | |
| 1 | 
            +
            import json
         | 
| 2 | 
            +
            import textwrap
         | 
| 3 | 
            +
             | 
| 4 | 
            +
            import datasets
         | 
| 5 | 
            +
            from datasets.tasks import QuestionAnsweringExtractive
         | 
| 6 | 
            +
             | 
| 7 | 
            +
            # TODO(tydiqa): BibTeX citation
         | 
| 8 | 
            +
            _CITATION = """\
         | 
| 9 | 
            +
            @article{tydiqa,
         | 
| 10 | 
            +
            title   = {TyDi QA: A Benchmark for Information-Seeking Question Answering in Typologically Diverse Languages},
         | 
| 11 | 
            +
            author  = {Jonathan H. Clark and Eunsol Choi and Michael Collins and Dan Garrette and Tom Kwiatkowski and Vitaly Nikolaev and Jennimaria Palomaki}
         | 
| 12 | 
            +
            year    = {2020},
         | 
| 13 | 
            +
            journal = {Transactions of the Association for Computational Linguistics}
         | 
| 14 | 
            +
            }
         | 
| 15 | 
            +
            """
         | 
| 16 | 
            +
             | 
| 17 | 
            +
            # TODO(tydiqa):
         | 
| 18 | 
            +
            _DESCRIPTION = """\
         | 
| 19 | 
            +
            TyDi QA is a question answering dataset covering 11 typologically diverse languages with 204K question-answer pairs.
         | 
| 20 | 
            +
            The languages of TyDi QA are diverse with regard to their typology -- the set of linguistic features that each language
         | 
| 21 | 
            +
            expresses -- such that we expect models performing well on this set to generalize across a large number of the languages
         | 
| 22 | 
            +
            in the world. It contains language phenomena that would not be found in English-only corpora. To provide a realistic
         | 
| 23 | 
            +
            information-seeking task and avoid priming effects, questions are written by people who want to know the answer, but
         | 
| 24 | 
            +
            don’t know the answer yet, (unlike SQuAD and its descendents) and the data is collected directly in each language without
         | 
| 25 | 
            +
            the use of translation (unlike MLQA and XQuAD).
         | 
| 26 | 
            +
            """
         | 
| 27 | 
            +
             | 
| 28 | 
            +
             | 
| 29 | 
            +
            _LANG = ["arabic", "bengali", "english", "finnish", "indonesian", "japanese", "korean", "russoam", "swahili", "telugu", "thai"]
         | 
| 30 | 
            +
            #_URL = "https://raw.githubusercontent.com/cambridgeltl/xcopa/master/{subdir}/{language}/{split}.{language}.jsonl"
         | 
| 31 | 
            +
            _URL = "https://huggingface.co/datasets/khalidalt/tydiqa-goldp/resolve/main/{split}/{language}-{split}.jsonl"
         | 
| 32 | 
            +
            _VERSION = datasets.Version("1.1.0", "")
         | 
| 33 | 
            +
             | 
| 34 | 
            +
             | 
| 35 | 
            +
            class tydiqa_GoldP(datasets.GeneratorBasedBuilder):
         | 
| 36 | 
            +
                BUILDER_CONFIGS = [
         | 
| 37 | 
            +
                    datasets.BuilderConfig(
         | 
| 38 | 
            +
                        name=lang,
         | 
| 39 | 
            +
                        description=f"tydiqa-GoldP language {lang}",
         | 
| 40 | 
            +
                        version=_VERSION,
         | 
| 41 | 
            +
                    )
         | 
| 42 | 
            +
                    for lang in _LANG
         | 
| 43 | 
            +
                ]
         | 
| 44 | 
            +
                BUILDER_CONFIGS += [
         | 
| 45 | 
            +
                    datasets.BuilderConfig(
         | 
| 46 | 
            +
                        name=f"translation-{lang}",
         | 
| 47 | 
            +
                        description=f"tydiqa-GoldP English translation for language {lang}",
         | 
| 48 | 
            +
                        version=_VERSION,
         | 
| 49 | 
            +
                    ) ]
         | 
| 50 | 
            +
             | 
| 51 | 
            +
                def _info(self):
         | 
| 52 | 
            +
                    # TODO(tydiqa): Specifies the datasets.DatasetInfo object
         | 
| 53 | 
            +
             | 
| 54 | 
            +
                    return datasets.DatasetInfo(
         | 
| 55 | 
            +
                        description=_DESCRIPTION,
         | 
| 56 | 
            +
                        features=datasets.Features(
         | 
| 57 | 
            +
                            {
         | 
| 58 | 
            +
                                "id": datasets.Value("string"),
         | 
| 59 | 
            +
                                "title": datasets.Value("string"),
         | 
| 60 | 
            +
                                "context": datasets.Value("string"),
         | 
| 61 | 
            +
                                "question": datasets.Value("string"),
         | 
| 62 | 
            +
                                "answers": datasets.features.Sequence(
         | 
| 63 | 
            +
                                    {
         | 
| 64 | 
            +
                                        "text": datasets.Value("string"),
         | 
| 65 | 
            +
                                        "answer_start": datasets.Value("int32"),
         | 
| 66 | 
            +
                                    }
         | 
| 67 | 
            +
                                ),
         | 
| 68 | 
            +
                            }
         | 
| 69 | 
            +
                        ),
         | 
| 70 | 
            +
                        # No default supervised_keys (as we have to pass both question
         | 
| 71 | 
            +
                        # and context as input).
         | 
| 72 | 
            +
                        supervised_keys=None,
         | 
| 73 | 
            +
                        homepage="https://github.com/google-research-datasets/tydiqa",
         | 
| 74 | 
            +
                        citation=_CITATION,
         | 
| 75 | 
            +
                        task_templates=[
         | 
| 76 | 
            +
                            QuestionAnsweringExtractive(
         | 
| 77 | 
            +
                                question_column="question", context_column="context", answers_column="answers"
         | 
| 78 | 
            +
                            )
         | 
| 79 | 
            +
                        ],
         | 
| 80 | 
            +
                    )
         | 
| 81 | 
            +
                    
         | 
| 82 | 
            +
                def _split_generators(self, dl_manager):
         | 
| 83 | 
            +
                    """Returns SplitGenerators."""
         | 
| 84 | 
            +
                    # TODO(tydiqa): Downloads the data and defines the splits
         | 
| 85 | 
            +
                    # dl_manager is a datasets.download.DownloadManager that can be used to
         | 
| 86 | 
            +
                    # download and extract URLs
         | 
| 87 | 
            +
                    language = self.config.name
         | 
| 88 | 
            +
                    splits = {datasets.Split.TRAIN: "train", datasets.Split.VALIDATION: "dev"}
         | 
| 89 | 
            +
                    
         | 
| 90 | 
            +
                    data_urls = {
         | 
| 91 | 
            +
                        split: _URL.format(language=language, split=splits[split]) for split in splits
         | 
| 92 | 
            +
                    }
         | 
| 93 | 
            +
                    
         | 
| 94 | 
            +
                    dl_paths = dl_manager.download(data_urls)
         | 
| 95 | 
            +
                    return [
         | 
| 96 | 
            +
                        datasets.SplitGenerator(
         | 
| 97 | 
            +
                            name=split,
         | 
| 98 | 
            +
                            gen_kwargs={"filepath": dl_paths[split]},
         | 
| 99 | 
            +
                        )
         | 
| 100 | 
            +
                        for split in splits
         | 
| 101 | 
            +
                    ]
         | 
| 102 | 
            +
                    
         | 
| 103 | 
            +
                def _generate_examples(self, filepath):
         | 
| 104 | 
            +
                    """Yields examples."""
         | 
| 105 | 
            +
                    # TODO(tydiqa): Yields (key, example) tuples from the dataset
         | 
| 106 | 
            +
                    
         | 
| 107 | 
            +
                    with open(filepath, encoding="utf-8") as f:
         | 
| 108 | 
            +
                        data = json.load(f)
         | 
| 109 | 
            +
                        for article in data["data"]:
         | 
| 110 | 
            +
                            title = article.get("title", "").strip()
         | 
| 111 | 
            +
                            for paragraph in article["paragraphs"]:
         | 
| 112 | 
            +
                                context = paragraph["context"].strip()
         | 
| 113 | 
            +
                                for qa in paragraph["qas"]:
         | 
| 114 | 
            +
                                    question = qa["question"].strip()
         | 
| 115 | 
            +
                                    id_ = qa["id"]
         | 
| 116 | 
            +
             | 
| 117 | 
            +
                                    answer_starts = [answer["answer_start"] for answer in qa["answers"]]
         | 
| 118 | 
            +
                                    answers = [answer["text"].strip() for answer in qa["answers"]]
         | 
| 119 | 
            +
             | 
| 120 | 
            +
                                    # Features currently used are "context", "question", and "answers".
         | 
| 121 | 
            +
                                    # Others are extracted here for the ease of future expansions.
         | 
| 122 | 
            +
                                    yield id_, {
         | 
| 123 | 
            +
                                        "title": title,
         | 
| 124 | 
            +
                                        "context": context,
         | 
| 125 | 
            +
                                        "question": question,
         | 
| 126 | 
            +
                                        "id": id_,
         | 
| 127 | 
            +
                                        "answers": {
         | 
| 128 | 
            +
                                            "answer_start": answer_starts,
         | 
| 129 | 
            +
                                            "text": answers,
         | 
| 130 | 
            +
                                        },
         | 
| 131 | 
            +
                                    }
         |