Mehmet Yıldız commited on
Commit
3b0c39b
·
1 Parent(s): c807d88

add json files

Browse files
Files changed (1) hide show
  1. toqad.py +131 -0
toqad.py ADDED
@@ -0,0 +1,131 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ """
31
+
32
+ _DESCRIPTION = """\
33
+ """
34
+
35
+ _URL = "https://raw.githubusercontent.com/meetyildiz/toqad/main/"
36
+ _URLS = {
37
+ "train": _URL + "train.json",
38
+ "dev": _URL + "dev.json",
39
+ }
40
+
41
+ class SquadConfig(datasets.BuilderConfig):
42
+ """BuilderConfig for SQUAD."""
43
+
44
+ def __init__(self, **kwargs):
45
+ """BuilderConfig for SQUAD.
46
+ Args:
47
+ **kwargs: keyword arguments forwarded to super.
48
+ """
49
+ super(SquadConfig, self).__init__(**kwargs)
50
+
51
+
52
+ class Squad(datasets.GeneratorBasedBuilder):
53
+ """SQUAD: The Stanford Question Answering Dataset. Version 1.1."""
54
+
55
+ BUILDER_CONFIGS = [
56
+ SquadConfig(
57
+ name="plain_text",
58
+ version=datasets.Version("1.0.0", ""),
59
+ description="Plain text",
60
+ ),
61
+ ]
62
+
63
+ def _info(self):
64
+ return datasets.DatasetInfo(
65
+ description=_DESCRIPTION,
66
+ features=datasets.Features(
67
+ {
68
+ "id": datasets.Value("string"),
69
+ "title": datasets.Value("string"),
70
+ "context": datasets.Value("string"),
71
+ "question": datasets.Value("string"),
72
+ "answers": datasets.features.Sequence(
73
+ {
74
+ "text": datasets.Value("string"),
75
+ "answer_start": datasets.Value("int32"),
76
+ }
77
+ ),
78
+ }
79
+ ),
80
+ # No default supervised_keys (as we have to pass both question
81
+ # and context as input).
82
+ supervised_keys=None,
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
+ downloaded_files = dl_manager.download_and_extract(_URLS)
94
+
95
+ return [
96
+ datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": downloaded_files["train"]}),
97
+ datasets.SplitGenerator(name=datasets.Split.VALIDATION, gen_kwargs={"filepath": downloaded_files["dev"]}),
98
+ ]
99
+
100
+ def _generate_examples(self, filepath):
101
+ """This function returns the examples in the raw (text) form."""
102
+ logger.info("generating examples from = %s", filepath)
103
+ key = 0
104
+ with open(filepath, encoding="utf-8") as f:
105
+ squad = json.load(f)
106
+
107
+ for document in squad["data"]:
108
+ for par in document['paragraphs']:
109
+ for qas in par['qas']:
110
+ if len(qas['answers']) == 0: #no answer
111
+ ans_start = -1
112
+ ans_end = -1
113
+ ans_text = ""
114
+ else:
115
+ ans_start = int(qas['answers'][0]['answer_start'])
116
+ ans_end = ans_start + len(qas['answers'][0]['text'])
117
+ ans_text = qas['answers'][0]['text']
118
+
119
+ ex = {
120
+ "id": qas["id"],
121
+ "title": document["title"],
122
+ "context": par['context'],
123
+ "question": qas['question'],
124
+ "answers": {
125
+ "text": [ans_text],
126
+ "answer_start": [ans_start],
127
+ },
128
+ }
129
+
130
+ yield key, ex
131
+ key += 1