Abinaya Mahendiran
commited on
Commit
•
2ea833c
1
Parent(s):
dab4c48
Added script for SQuAD2.0
Browse files- squad_v2.py +133 -0
squad_v2.py
ADDED
@@ -0,0 +1,133 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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://rajpurkar.github.io/SQuAD-explorer/dataset/"
|
32 |
+
_URLS = {
|
33 |
+
"train": _URL + "train-v2.0.json",
|
34 |
+
"dev": _URL + "dev-v2.0.json",
|
35 |
+
}
|
36 |
+
|
37 |
+
|
38 |
+
class SquadV2Config(datasets.BuilderConfig):
|
39 |
+
"""BuilderConfig for SQUAD."""
|
40 |
+
|
41 |
+
def __init__(self, **kwargs):
|
42 |
+
"""BuilderConfig for SQUADV2.
|
43 |
+
|
44 |
+
Args:
|
45 |
+
**kwargs: keyword arguments forwarded to super.
|
46 |
+
"""
|
47 |
+
super(SquadV2Config, self).__init__(**kwargs)
|
48 |
+
|
49 |
+
|
50 |
+
class SquadV2(datasets.GeneratorBasedBuilder):
|
51 |
+
"""TODO(squad_v2): Short description of my dataset."""
|
52 |
+
|
53 |
+
# TODO(squad_v2): Set up version.
|
54 |
+
BUILDER_CONFIGS = [
|
55 |
+
SquadV2Config(name="squad_v2", version=datasets.Version("2.0.0"), description="SQuAD plaint text version 2"),
|
56 |
+
]
|
57 |
+
|
58 |
+
def _info(self):
|
59 |
+
# TODO(squad_v2): Specifies the datasets.DatasetInfo object
|
60 |
+
return datasets.DatasetInfo(
|
61 |
+
# This is the description that will appear on the datasets page.
|
62 |
+
description=_DESCRIPTION,
|
63 |
+
# datasets.features.FeatureConnectors
|
64 |
+
features=datasets.Features(
|
65 |
+
{
|
66 |
+
"id": datasets.Value("string"),
|
67 |
+
"title": datasets.Value("string"),
|
68 |
+
"context": datasets.Value("string"),
|
69 |
+
"question": datasets.Value("string"),
|
70 |
+
"answers": datasets.features.Sequence(
|
71 |
+
{
|
72 |
+
"text": datasets.Value("string"),
|
73 |
+
"answer_start": datasets.Value("int32"),
|
74 |
+
}
|
75 |
+
),
|
76 |
+
# These are the features of your dataset like images, labels ...
|
77 |
+
}
|
78 |
+
),
|
79 |
+
# If there's a common (input, target) tuple from the features,
|
80 |
+
# specify them here. They'll be used if as_supervised=True in
|
81 |
+
# builder.as_dataset.
|
82 |
+
supervised_keys=None,
|
83 |
+
# Homepage of the dataset for documentation
|
84 |
+
homepage="https://rajpurkar.github.io/SQuAD-explorer/",
|
85 |
+
citation=_CITATION,
|
86 |
+
task_templates=[
|
87 |
+
QuestionAnsweringExtractive(
|
88 |
+
question_column="question", context_column="context", answers_column="answers"
|
89 |
+
)
|
90 |
+
],
|
91 |
+
)
|
92 |
+
|
93 |
+
def _split_generators(self, dl_manager):
|
94 |
+
"""Returns SplitGenerators."""
|
95 |
+
# TODO(squad_v2): Downloads the data and defines the splits
|
96 |
+
# dl_manager is a datasets.download.DownloadManager that can be used to
|
97 |
+
# download and extract URLs
|
98 |
+
urls_to_download = _URLS
|
99 |
+
downloaded_files = dl_manager.download_and_extract(urls_to_download)
|
100 |
+
|
101 |
+
return [
|
102 |
+
datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": downloaded_files["train"]}),
|
103 |
+
datasets.SplitGenerator(name=datasets.Split.VALIDATION, gen_kwargs={"filepath": downloaded_files["dev"]}),
|
104 |
+
]
|
105 |
+
|
106 |
+
def _generate_examples(self, filepath):
|
107 |
+
"""Yields examples."""
|
108 |
+
# TODO(squad_v2): Yields (key, example) tuples from the dataset
|
109 |
+
with open(filepath, encoding="utf-8") as f:
|
110 |
+
squad = json.load(f)
|
111 |
+
for example in squad["data"]:
|
112 |
+
title = example.get("title", "")
|
113 |
+
for paragraph in example["paragraphs"]:
|
114 |
+
context = paragraph["context"] # do not strip leading blank spaces GH-2585
|
115 |
+
for qa in paragraph["qas"]:
|
116 |
+
question = qa["question"]
|
117 |
+
id_ = qa["id"]
|
118 |
+
|
119 |
+
answer_starts = [answer["answer_start"] for answer in qa["answers"]]
|
120 |
+
answers = [answer["text"] for answer in qa["answers"]]
|
121 |
+
|
122 |
+
# Features currently used are "context", "question", and "answers".
|
123 |
+
# Others are extracted here for the ease of future expansions.
|
124 |
+
yield id_, {
|
125 |
+
"title": title,
|
126 |
+
"context": context,
|
127 |
+
"question": question,
|
128 |
+
"id": id_,
|
129 |
+
"answers": {
|
130 |
+
"answer_start": answer_starts,
|
131 |
+
"text": answers,
|
132 |
+
},
|
133 |
+
}
|