Datasets:
initial version
Browse files
skquad.py
ADDED
@@ -0,0 +1,132 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
TBD
|
31 |
+
"""
|
32 |
+
|
33 |
+
_DESCRIPTION = """\
|
34 |
+
Slovak Question Answering Dataset
|
35 |
+
"""
|
36 |
+
|
37 |
+
_URL = "https://files.kemt.fei.tuke.sk/corpora/sk-quad/sk-quad-220614.tar.gz"
|
38 |
+
|
39 |
+
_FILES = {
|
40 |
+
"dev": "sk-quad-220614/sk-quad-220614-dev.json",
|
41 |
+
"train": "sk-quad-220614/sk-quad-220614-train.json",
|
42 |
+
}
|
43 |
+
|
44 |
+
class SkQuadConfig(datasets.BuilderConfig):
|
45 |
+
"""BuilderConfig for SQUAD."""
|
46 |
+
|
47 |
+
def __init__(self, **kwargs):
|
48 |
+
"""BuilderConfig for SQUAD.
|
49 |
+
Args:
|
50 |
+
**kwargs: keyword arguments forwarded to super.
|
51 |
+
"""
|
52 |
+
super(SkQuadConfig, self).__init__(**kwargs)
|
53 |
+
|
54 |
+
|
55 |
+
class SkQuad(datasets.GeneratorBasedBuilder):
|
56 |
+
"""SQUAD: The Stanford Question Answering Dataset. Version 1.1."""
|
57 |
+
|
58 |
+
BUILDER_CONFIGS = [
|
59 |
+
SkQuadConfig(
|
60 |
+
name="plain_text",
|
61 |
+
version=datasets.Version("1.1.1", ""),
|
62 |
+
description="Plain text",
|
63 |
+
),
|
64 |
+
]
|
65 |
+
|
66 |
+
def _info(self):
|
67 |
+
return datasets.DatasetInfo(
|
68 |
+
description=_DESCRIPTION,
|
69 |
+
features=datasets.Features(
|
70 |
+
{
|
71 |
+
"id": datasets.Value("string"),
|
72 |
+
"title": datasets.Value("string"),
|
73 |
+
"context": datasets.Value("string"),
|
74 |
+
"question": datasets.Value("string"),
|
75 |
+
"answers": datasets.features.Sequence(
|
76 |
+
{
|
77 |
+
"text": datasets.Value("string"),
|
78 |
+
"answer_start": datasets.Value("int32"),
|
79 |
+
}
|
80 |
+
),
|
81 |
+
}
|
82 |
+
),
|
83 |
+
# No default supervised_keys (as we have to pass both question
|
84 |
+
# and context as input).
|
85 |
+
supervised_keys=None,
|
86 |
+
homepage="https://rajpurkar.github.io/SQuAD-explorer/",
|
87 |
+
citation=_CITATION,
|
88 |
+
task_templates=[
|
89 |
+
QuestionAnsweringExtractive(
|
90 |
+
question_column="question", context_column="context", answers_column="answers"
|
91 |
+
)
|
92 |
+
],
|
93 |
+
)
|
94 |
+
|
95 |
+
def _split_generators(self, dl_manager):
|
96 |
+
downloaded_dir = dl_manager.download_and_extract(_URL)
|
97 |
+
print(downloaded_dir)
|
98 |
+
return [
|
99 |
+
datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": downloaded_dir + "/" + _FILES["train"]}),
|
100 |
+
datasets.SplitGenerator(name=datasets.Split.VALIDATION, gen_kwargs={"filepath": downloaded_dir+ "/" + _FILES["dev"]}),
|
101 |
+
]
|
102 |
+
|
103 |
+
def _generate_examples(self, filepath):
|
104 |
+
"""This function returns the examples in the raw (text) form."""
|
105 |
+
logger.info("generating examples from = %s", filepath)
|
106 |
+
key = 0
|
107 |
+
with open(filepath, encoding="utf-8") as f:
|
108 |
+
squad = json.load(f)
|
109 |
+
for article in squad["data"]:
|
110 |
+
title = article.get("title", "")
|
111 |
+
for paragraph in article["paragraphs"]:
|
112 |
+
context = paragraph["context"] # do not strip leading blank spaces GH-2585
|
113 |
+
for qa in paragraph["qas"]:
|
114 |
+
answer_starts = [answer["answer_start"] for answer in qa["answers"]]
|
115 |
+
assert len(qa["question"]) > 0
|
116 |
+
#if len(answer_starts) == 0:
|
117 |
+
# continue
|
118 |
+
answers = [answer["text"] for answer in qa["answers"]]
|
119 |
+
assert len(answer_starts) == len(answers)
|
120 |
+
# Features currently used are "context", "question", and "answers".
|
121 |
+
# Others are extracted here for the ease of future expansions.
|
122 |
+
yield key, {
|
123 |
+
"title": title,
|
124 |
+
"context": context,
|
125 |
+
"question": qa["question"],
|
126 |
+
"id": qa["id"],
|
127 |
+
"answers": {
|
128 |
+
"answer_start": answer_starts,
|
129 |
+
"text": answers,
|
130 |
+
},
|
131 |
+
}
|
132 |
+
key += 1
|