holylovenia
commited on
Commit
•
e91656b
1
Parent(s):
913e312
Upload xquad.py with huggingface_hub
Browse files
xquad.py
ADDED
@@ -0,0 +1,124 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
from pathlib import Path
|
3 |
+
from typing import Dict, List, Tuple
|
4 |
+
|
5 |
+
import datasets
|
6 |
+
|
7 |
+
from seacrowd.utils import schemas
|
8 |
+
from seacrowd.utils.configs import SEACrowdConfig
|
9 |
+
from seacrowd.utils.constants import Licenses, Tasks
|
10 |
+
|
11 |
+
_CITATION = """\
|
12 |
+
@article{Artetxe:etal:2019,
|
13 |
+
author = {Mikel Artetxe and Sebastian Ruder and Dani Yogatama},
|
14 |
+
title = {On the cross-lingual transferability of monolingual representations},
|
15 |
+
journal = {CoRR},
|
16 |
+
volume = {abs/1910.11856},
|
17 |
+
year = {2019},
|
18 |
+
archivePrefix = {arXiv},
|
19 |
+
eprint = {1910.11856}
|
20 |
+
}
|
21 |
+
"""
|
22 |
+
|
23 |
+
_DATASETNAME = "xquad"
|
24 |
+
|
25 |
+
_DESCRIPTION = """\
|
26 |
+
XQuAD (Cross-lingual Question Answering Dataset) is a benchmark dataset for evaluating cross-lingual question answering performance.
|
27 |
+
The dataset consists of a subset of 240 paragraphs and 1190 question-answer pairs from the development set of SQuAD v1.1 together (Rajpurkar et al., 2016)
|
28 |
+
with their professional translations into ten languages in their original implementation: Spanish, German, Greek, Russian, Turkish, Arabic, Vietnamese, Thai, Chinese, and Hindi and two in this dataloader: Vietnamese & Thai
|
29 |
+
"""
|
30 |
+
|
31 |
+
_HOMEPAGE = "https://github.com/google-deepmind/xquad"
|
32 |
+
|
33 |
+
_LICENSE = Licenses.CC_BY_SA_4_0.value
|
34 |
+
|
35 |
+
_LOCAL = False
|
36 |
+
_LANGUAGES = ["tha", "vie"]
|
37 |
+
|
38 |
+
_SUPPORTED_TASKS = [Tasks.QUESTION_ANSWERING]
|
39 |
+
|
40 |
+
_SOURCE_VERSION = "1.0.0"
|
41 |
+
_SEACROWD_VERSION = "2024.06.20"
|
42 |
+
|
43 |
+
|
44 |
+
class XQuADDataset(datasets.GeneratorBasedBuilder):
|
45 |
+
"""
|
46 |
+
XQuAD (Cross-lingual Question Answering Dataset) is a benchmark dataset for evaluating cross-lingual question answering performance.
|
47 |
+
The dataset consists of a subset of 240 paragraphs and 1190 question-answer pairs from the development set of SQuAD v1.1 together
|
48 |
+
with their professional translations into ten languages: Spanish, German, Greek, Russian, Turkish, Arabic, Vietnamese, Thai, Chinese, and Hindi.
|
49 |
+
"""
|
50 |
+
|
51 |
+
subsets = ["xquad.vi", "xquad.th"]
|
52 |
+
|
53 |
+
BUILDER_CONFIGS = [
|
54 |
+
SEACrowdConfig(
|
55 |
+
name="{sub}_source".format(sub=subset),
|
56 |
+
version=datasets.Version(_SOURCE_VERSION),
|
57 |
+
description="{sub} source schema".format(sub=subset),
|
58 |
+
schema="source",
|
59 |
+
subset_id="{sub}".format(sub=subset),
|
60 |
+
)
|
61 |
+
for subset in subsets
|
62 |
+
] + [
|
63 |
+
SEACrowdConfig(
|
64 |
+
name="{sub}_seacrowd_qa".format(sub=subset),
|
65 |
+
version=datasets.Version(_SEACROWD_VERSION),
|
66 |
+
description="{sub} SEACrowd schema".format(sub=subset),
|
67 |
+
schema="seacrowd_qa",
|
68 |
+
subset_id="{sub}".format(sub=subset),
|
69 |
+
)
|
70 |
+
for subset in subsets
|
71 |
+
]
|
72 |
+
|
73 |
+
DEFAULT_CONFIG_NAME = "xquad.vi_source"
|
74 |
+
|
75 |
+
def _info(self) -> datasets.DatasetInfo:
|
76 |
+
if self.config.schema == "source":
|
77 |
+
features = datasets.Features(
|
78 |
+
{"context": datasets.Value("string"), "question": datasets.Value("string"), "answers": datasets.Features({"answer_start": [datasets.Value("int64")], "text": [datasets.Value("string")]}), "id": datasets.Value("string")}
|
79 |
+
)
|
80 |
+
elif self.config.schema == "seacrowd_qa":
|
81 |
+
features = schemas.qa_features
|
82 |
+
|
83 |
+
return datasets.DatasetInfo(
|
84 |
+
description=_DESCRIPTION,
|
85 |
+
features=features,
|
86 |
+
homepage=_HOMEPAGE,
|
87 |
+
license=_LICENSE,
|
88 |
+
citation=_CITATION,
|
89 |
+
)
|
90 |
+
|
91 |
+
def _split_generators(self, dl_manager: datasets.DownloadManager) -> List[datasets.SplitGenerator]:
|
92 |
+
return [
|
93 |
+
datasets.SplitGenerator(
|
94 |
+
name=datasets.Split.TRAIN
|
95 |
+
)
|
96 |
+
]
|
97 |
+
|
98 |
+
def _generate_examples(self) -> Tuple[int, Dict]:
|
99 |
+
name_split = self.config.name.split("_")
|
100 |
+
subset_name = name_split[0]
|
101 |
+
dataset = datasets.load_dataset(_DATASETNAME, subset_name)
|
102 |
+
|
103 |
+
# Validation is the only subset name available for this dataset
|
104 |
+
for data in dataset['validation']:
|
105 |
+
if self.config.schema == "source":
|
106 |
+
yield data['id'], {
|
107 |
+
"context": data['context'],
|
108 |
+
"question": data['question'],
|
109 |
+
"answers": {"answer_start": str(data['answers']['answer_start'][0]), "text": data['answers']['text'][0]},
|
110 |
+
"id": data['id'],
|
111 |
+
}
|
112 |
+
|
113 |
+
elif self.config.schema == "seacrowd_qa":
|
114 |
+
yield data['id'], {
|
115 |
+
"question_id": data['id'],
|
116 |
+
"context": data['context'],
|
117 |
+
"question": data['question'],
|
118 |
+
"answer": {"answer_start": data['answers']['answer_start'][0], "text": data['answers']['text'][0]},
|
119 |
+
"id": data['id'],
|
120 |
+
"choices": [],
|
121 |
+
"type": "",
|
122 |
+
"document_id": data['id'],
|
123 |
+
"meta": {},
|
124 |
+
}
|