fedryanto commited on
Commit
1502cae
·
1 Parent(s): 0c2c914

Create squad5.py

Browse files
Files changed (1) hide show
  1. squad5.py +141 -0
squad5.py ADDED
@@ -0,0 +1,141 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf-8
2
+ # Copyright 2020 The TensorFlow Datasets Authors and the HuggingFace Datasets Authors.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+
16
+ # Lint as: python3
17
+ """SQUAD: The Stanford Question Answering Dataset."""
18
+
19
+
20
+ import json
21
+
22
+ import datasets
23
+ from datasets.tasks import QuestionAnsweringExtractive
24
+
25
+
26
+ logger = datasets.logging.get_logger(__name__)
27
+
28
+
29
+ _CITATION = """\
30
+ @article{2016arXiv160605250R,
31
+ author = {{Rajpurkar}, Pranav and {Zhang}, Jian and {Lopyrev},
32
+ Konstantin and {Liang}, Percy},
33
+ title = "{SQuAD: 100,000+ Questions for Machine Comprehension of Text}",
34
+ journal = {arXiv e-prints},
35
+ year = 2016,
36
+ eid = {arXiv:1606.05250},
37
+ pages = {arXiv:1606.05250},
38
+ archivePrefix = {arXiv},
39
+ eprint = {1606.05250},
40
+ }
41
+ """
42
+
43
+ _DESCRIPTION = """\
44
+ Stanford Question Answering Dataset (SQuAD) is a reading comprehension \
45
+ dataset, consisting of questions posed by crowdworkers on a set of Wikipedia \
46
+ articles, where the answer to every question is a segment of text, or span, \
47
+ from the corresponding reading passage, or the question might be unanswerable.
48
+ """
49
+
50
+ _URL = "https://rajpurkar.github.io/SQuAD-explorer/dataset/"
51
+ _URLS = {
52
+ "train": _URL + "train-v1.1.json",
53
+ "dev": _URL + "dev-v1.1.json",
54
+ }
55
+
56
+
57
+ class SquadConfig(datasets.BuilderConfig):
58
+ """BuilderConfig for SQUAD."""
59
+
60
+ def __init__(self, **kwargs):
61
+ """BuilderConfig for SQUAD.
62
+ Args:
63
+ **kwargs: keyword arguments forwarded to super.
64
+ """
65
+ super(SquadConfig, self).__init__(**kwargs)
66
+
67
+
68
+ class Squad(datasets.GeneratorBasedBuilder):
69
+ """SQUAD: The Stanford Question Answering Dataset. Version 1.1."""
70
+
71
+ BUILDER_CONFIGS = [
72
+ SquadConfig(
73
+ name="plain_text",
74
+ version=datasets.Version("1.0.0", ""),
75
+ description="Plain text",
76
+ ),
77
+ ]
78
+
79
+ def _info(self):
80
+ return datasets.DatasetInfo(
81
+ description=_DESCRIPTION,
82
+ features=datasets.Features(
83
+ {
84
+ "id": datasets.Value("string"),
85
+ "title": datasets.Value("string"),
86
+ "context": datasets.Value("string"),
87
+ "question": datasets.Value("string"),
88
+ "answers": datasets.features.Sequence(
89
+ {
90
+ "text": datasets.Value("string"),
91
+ "answer_start": datasets.Value("int32"),
92
+ }
93
+ ),
94
+ }
95
+ ),
96
+ # No default supervised_keys (as we have to pass both question
97
+ # and context as input).
98
+ supervised_keys=None,
99
+ homepage="https://rajpurkar.github.io/SQuAD-explorer/",
100
+ citation=_CITATION,
101
+ task_templates=[
102
+ QuestionAnsweringExtractive(
103
+ question_column="question", context_column="context", answers_column="answers"
104
+ )
105
+ ],
106
+ )
107
+
108
+ def _split_generators(self, dl_manager):
109
+ downloaded_files = dl_manager.download_and_extract(_URLS)
110
+
111
+ return [
112
+ datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": downloaded_files["train"]}),
113
+ datasets.SplitGenerator(name=datasets.Split.VALIDATION, gen_kwargs={"filepath": downloaded_files["dev"]}),
114
+ ]
115
+
116
+ def _generate_examples(self, filepath):
117
+ """This function returns the examples in the raw (text) form."""
118
+ logger.info("generating examples from = %s", filepath)
119
+ key = 0
120
+ with open(filepath, encoding="utf-8") as f:
121
+ squad5 = json.load(f)
122
+ for article in squad5["data"]:
123
+ title = article.get("title", "")
124
+ for paragraph in article["paragraphs"]:
125
+ context = paragraph["context"] # do not strip leading blank spaces GH-2585
126
+ for qa in paragraph["qas"]:
127
+ answer_starts = [answer["answer_start"] for answer in qa["answers"]]
128
+ answers = [answer["text"] for answer in qa["answers"]]
129
+ # Features currently used are "context", "question", and "answers".
130
+ # Others are extracted here for the ease of future expansions.
131
+ yield key, {
132
+ "title": title,
133
+ "context": context,
134
+ "question": qa["question"],
135
+ "id": qa["id"],
136
+ "answers": {
137
+ "answer_start": answer_starts,
138
+ "text": answers,
139
+ },
140
+ }
141
+ key += 1