Upload tquad2.py
#1
by
BugraHamza
- opened
tquad2.py
ADDED
@@ -0,0 +1,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
|
3 |
+
import datasets
|
4 |
+
from datasets.tasks import QuestionAnsweringExtractive
|
5 |
+
|
6 |
+
_CITATION = ""
|
7 |
+
|
8 |
+
_DESCRIPTION = ""
|
9 |
+
|
10 |
+
_URL = "https://huggingface.co/datasets/husnu/tquad2/raw/main/"
|
11 |
+
_URLS = {
|
12 |
+
"train": _URL + "tquad_train_data_v2.json",
|
13 |
+
"dev": _URL + "tquad_dev_data_v2.json",
|
14 |
+
}
|
15 |
+
|
16 |
+
|
17 |
+
class TQuAD2Config(datasets.BuilderConfig):
|
18 |
+
"""BuilderConfig for TQuAD2."""
|
19 |
+
|
20 |
+
def __init__(self, **kwargs):
|
21 |
+
"""BuilderConfig for TQuAD2.
|
22 |
+
Args:
|
23 |
+
**kwargs: keyword arguments forwarded to super.
|
24 |
+
"""
|
25 |
+
super(TQuAD2Config, self).__init__(**kwargs)
|
26 |
+
|
27 |
+
|
28 |
+
class TQuAD2(datasets.GeneratorBasedBuilder):
|
29 |
+
BUILDER_CONFIGS = [
|
30 |
+
TQuAD2Config(name="tquad2", version=datasets.Version("2.0.0"), description="TQuAD2 dataset"),
|
31 |
+
]
|
32 |
+
|
33 |
+
IDS_ = []
|
34 |
+
|
35 |
+
def _info(self):
|
36 |
+
return datasets.DatasetInfo(
|
37 |
+
# This is the description that will appear on the datasets page.
|
38 |
+
description=_DESCRIPTION,
|
39 |
+
# datasets.features.FeatureConnectors
|
40 |
+
features=datasets.Features(
|
41 |
+
{
|
42 |
+
"id": datasets.Value("string"),
|
43 |
+
"title": datasets.Value("string"),
|
44 |
+
"context": datasets.Value("string"),
|
45 |
+
"question": datasets.Value("string"),
|
46 |
+
"answers": datasets.features.Sequence(
|
47 |
+
{
|
48 |
+
"text": datasets.Value("string"),
|
49 |
+
"answer_start": datasets.Value("int32"),
|
50 |
+
}
|
51 |
+
),
|
52 |
+
# These are the features of your dataset like images, labels ...
|
53 |
+
}
|
54 |
+
),
|
55 |
+
# If there's a common (input, target) tuple from the features,
|
56 |
+
# specify them here. They'll be used if as_supervised=True in
|
57 |
+
# builder.as_dataset.
|
58 |
+
supervised_keys=None,
|
59 |
+
# Homepage of the dataset for documentation
|
60 |
+
homepage="https://huggingface.co/datasets/husnu/tquad2",
|
61 |
+
citation=_CITATION,
|
62 |
+
task_templates=[
|
63 |
+
QuestionAnsweringExtractive(
|
64 |
+
question_column="question", context_column="context", answers_column="answers"
|
65 |
+
)
|
66 |
+
],
|
67 |
+
)
|
68 |
+
|
69 |
+
def _split_generators(self, dl_manager):
|
70 |
+
"""Returns SplitGenerators."""
|
71 |
+
# dl_manager is a datasets.download.DownloadManager that can be used to
|
72 |
+
# download and extract URLs
|
73 |
+
urls_to_download = _URLS
|
74 |
+
downloaded_files = dl_manager.download_and_extract(urls_to_download)
|
75 |
+
|
76 |
+
return [
|
77 |
+
datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": downloaded_files["train"]}),
|
78 |
+
datasets.SplitGenerator(name=datasets.Split.VALIDATION, gen_kwargs={"filepath": downloaded_files["dev"]}),
|
79 |
+
]
|
80 |
+
|
81 |
+
def _generate_examples(self, filepath):
|
82 |
+
"""Yields examples."""
|
83 |
+
with open(filepath, encoding="utf-8") as f:
|
84 |
+
squad = json.load(f)
|
85 |
+
for example in squad["data"]:
|
86 |
+
title = example.get("title", "")
|
87 |
+
for paragraph in example["paragraphs"]:
|
88 |
+
context = paragraph["context"] # do not strip leading blank spaces GH-2585
|
89 |
+
for qa in paragraph["qas"]:
|
90 |
+
question = qa["question"]
|
91 |
+
id_ = qa["id"]
|
92 |
+
|
93 |
+
answer_starts = [answer["answer_start"] for answer in qa["answers"]]
|
94 |
+
answers = [answer["text"] for answer in qa["answers"]]
|
95 |
+
|
96 |
+
# if id_ is already in the dataset, we skip it
|
97 |
+
while id_ in self.IDS_:
|
98 |
+
if isinstance(id_, int):
|
99 |
+
id_ = id_ + 1
|
100 |
+
else:
|
101 |
+
id_ = id_ + "_duplicate"
|
102 |
+
|
103 |
+
self.IDS_.append(id_)
|
104 |
+
|
105 |
+
# Features currently used are "context", "question", and "answers".
|
106 |
+
# Others are extracted here for the ease of future expansions.
|
107 |
+
yield id_, {
|
108 |
+
"title": title,
|
109 |
+
"context": context,
|
110 |
+
"question": question,
|
111 |
+
"id": id_,
|
112 |
+
"answers": {
|
113 |
+
"answer_start": answer_starts,
|
114 |
+
"text": answers,
|
115 |
+
},
|
116 |
+
}
|