Upload testsq.py
Browse files
testsq.py
ADDED
@@ -0,0 +1,132 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""TODO(squad_v2): Add a description here."""
|
2 |
+
|
3 |
+
|
4 |
+
import json
|
5 |
+
|
6 |
+
import datasets
|
7 |
+
# from datasets.tasks import QuestionAnsweringExtractive
|
8 |
+
|
9 |
+
|
10 |
+
# TODO(squad_v2): BibTeX citation
|
11 |
+
_CITATION = """\
|
12 |
+
@article{2016arXiv160605250R,
|
13 |
+
author = {{Rajpurkar}, Pranav and {Zhang}, Jian and {Lopyrev},
|
14 |
+
Konstantin and {Liang}, Percy},
|
15 |
+
title = "{SQuAD: 100,000+ Questions for Machine Comprehension of Text}",
|
16 |
+
journal = {arXiv e-prints},
|
17 |
+
year = 2016,
|
18 |
+
eid = {arXiv:1606.05250},
|
19 |
+
pages = {arXiv:1606.05250},
|
20 |
+
archivePrefix = {arXiv},
|
21 |
+
eprint = {1606.05250},
|
22 |
+
}
|
23 |
+
"""
|
24 |
+
|
25 |
+
_DESCRIPTION = """\
|
26 |
+
combines the 100,000 questions in SQuAD1.1 with over 50,000 unanswerable questions written adversarially by crowdworkers
|
27 |
+
to look similar to answerable ones. To do well on SQuAD2.0, systems must not only answer questions when possible, but
|
28 |
+
also determine when no answer is supported by the paragraph and abstain from answering.
|
29 |
+
"""
|
30 |
+
|
31 |
+
_URL = "https://huggingface.co/datasets/prk/testsq/blob/main/"
|
32 |
+
_URLS = {
|
33 |
+
"train": _URL + "answers.json",
|
34 |
+
"dev": _URL + "answers_test.json",
|
35 |
+
}
|
36 |
+
|
37 |
+
|
38 |
+
class SquadV2Config(datasets.BuilderConfig):
|
39 |
+
"""BuilderConfig for SQUAD."""
|
40 |
+
|
41 |
+
def __init__(self, **kwargs):
|
42 |
+
"""BuilderConfig for SQUADV2.
|
43 |
+
Args:
|
44 |
+
**kwargs: keyword arguments forwarded to super.
|
45 |
+
"""
|
46 |
+
super(SquadV2Config, self).__init__(**kwargs)
|
47 |
+
|
48 |
+
|
49 |
+
class SquadV2(datasets.GeneratorBasedBuilder):
|
50 |
+
"""TODO(squad_v2): Short description of my dataset."""
|
51 |
+
|
52 |
+
# TODO(squad_v2): Set up version.
|
53 |
+
BUILDER_CONFIGS = [
|
54 |
+
SquadV2Config(name="squad_v2", version=datasets.Version("2.0.0"), description="SQuAD plaint text version 2"),
|
55 |
+
]
|
56 |
+
|
57 |
+
def _info(self):
|
58 |
+
# TODO(squad_v2): Specifies the datasets.DatasetInfo object
|
59 |
+
return datasets.DatasetInfo(
|
60 |
+
# This is the description that will appear on the datasets page.
|
61 |
+
description=_DESCRIPTION,
|
62 |
+
# datasets.features.FeatureConnectors
|
63 |
+
features=datasets.Features(
|
64 |
+
{
|
65 |
+
"id": datasets.Value("string"),
|
66 |
+
# "title": datasets.Value("string"),
|
67 |
+
"context": datasets.Value("string"),
|
68 |
+
"question": datasets.Value("string"),
|
69 |
+
"answers": datasets.features.Sequence(
|
70 |
+
{
|
71 |
+
"text": datasets.Value("string"),
|
72 |
+
"answer_start": datasets.Value("int32"),
|
73 |
+
}
|
74 |
+
),
|
75 |
+
# These are the features of your dataset like images, labels ...
|
76 |
+
}
|
77 |
+
),
|
78 |
+
# If there's a common (input, target) tuple from the features,
|
79 |
+
# specify them here. They'll be used if as_supervised=True in
|
80 |
+
# builder.as_dataset.
|
81 |
+
supervised_keys=None,
|
82 |
+
# Homepage of the dataset for documentation
|
83 |
+
homepage="https://rajpurkar.github.io/SQuAD-explorer/",
|
84 |
+
citation=_CITATION,
|
85 |
+
# task_templates=[
|
86 |
+
# QuestionAnsweringExtractive(
|
87 |
+
# question_column="question", context_column="context", answers_column="answers"
|
88 |
+
# )
|
89 |
+
# ],
|
90 |
+
)
|
91 |
+
|
92 |
+
def _split_generators(self, dl_manager):
|
93 |
+
"""Returns SplitGenerators."""
|
94 |
+
# TODO(squad_v2): Downloads the data and defines the splits
|
95 |
+
# dl_manager is a datasets.download.DownloadManager that can be used to
|
96 |
+
# download and extract URLs
|
97 |
+
urls_to_download = _URLS
|
98 |
+
downloaded_files = dl_manager.download_and_extract(urls_to_download)
|
99 |
+
|
100 |
+
return [
|
101 |
+
datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": downloaded_files["train"]}),
|
102 |
+
datasets.SplitGenerator(name=datasets.Split.VALIDATION, gen_kwargs={"filepath": downloaded_files["dev"]}),
|
103 |
+
]
|
104 |
+
|
105 |
+
def _generate_examples(self, filepath):
|
106 |
+
"""Yields examples."""
|
107 |
+
# TODO(squad_v2): Yields (key, example) tuples from the dataset
|
108 |
+
with open(filepath, encoding="utf-8") as f:
|
109 |
+
squad = json.load(f)
|
110 |
+
for example in squad["data"]:
|
111 |
+
# title = example.get("title", "")
|
112 |
+
for paragraph in example["paragraphs"]:
|
113 |
+
context = paragraph["context"] # do not strip leading blank spaces GH-2585
|
114 |
+
for qa in paragraph["qas"]:
|
115 |
+
question = qa["question"]
|
116 |
+
id_ = qa["id"]
|
117 |
+
|
118 |
+
answer_starts = [answer["answer_start"] for answer in qa["answers"]]
|
119 |
+
answers = [answer["text"] for answer in qa["answers"]]
|
120 |
+
|
121 |
+
# Features currently used are "context", "question", and "answers".
|
122 |
+
# Others are extracted here for the ease of future expansions.
|
123 |
+
yield id_, {
|
124 |
+
# "title": title,
|
125 |
+
"context": context,
|
126 |
+
"question": question,
|
127 |
+
"id": id_,
|
128 |
+
"answers": {
|
129 |
+
"answer_start": answer_starts,
|
130 |
+
"text": answers,
|
131 |
+
},
|
132 |
+
}
|