Mehmet Yıldız commited on
Commit
1ae129a
·
1 Parent(s): 3a000d9
Files changed (1) hide show
  1. toqad-aug.py +134 -0
toqad-aug.py ADDED
@@ -0,0 +1,134 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ """ToQAD: The Turkish 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
+ Turkish Question Answering Dataset - Base
34
+ """
35
+
36
+ _URL = "https://raw.githubusercontent.com/meetyildiz/toqad/main/data/"
37
+ _URLS = {
38
+ "train": _URL + "toqad-aug-train.json",
39
+ "dev": _URL + "toqad-dev.json",
40
+ "test": _URL + "toqad-test.json",
41
+ }
42
+
43
+ class ToqadConfig(datasets.BuilderConfig):
44
+ """BuilderConfig for Toqad."""
45
+
46
+ def __init__(self, **kwargs):
47
+ """BuilderConfig for Toqad.
48
+ Args:
49
+ **kwargs: keyword arguments forwarded to super.
50
+ """
51
+ super(ToqadConfig, self).__init__(**kwargs)
52
+
53
+
54
+ class Toqad(datasets.GeneratorBasedBuilder):
55
+ """Toqad: The Stanford Question Answering Dataset. Version 1.1."""
56
+
57
+ BUILDER_CONFIGS = [
58
+ ToqadConfig(
59
+ name="plain_text",
60
+ version=datasets.Version("1.0.0", ""),
61
+ description="Plain text",
62
+ ),
63
+ ]
64
+
65
+ def _info(self):
66
+ return datasets.DatasetInfo(
67
+ description=_DESCRIPTION,
68
+ features=datasets.Features(
69
+ {
70
+ "id": datasets.Value("string"),
71
+ "title": datasets.Value("string"),
72
+ "context": datasets.Value("string"),
73
+ "question": datasets.Value("string"),
74
+ "answers": datasets.features.Sequence(
75
+ {
76
+ "text": datasets.Value("string"),
77
+ "answer_start": datasets.Value("int32"),
78
+ }
79
+ ),
80
+ }
81
+ ),
82
+ # No default supervised_keys (as we have to pass both question
83
+ # and context as input).
84
+ supervised_keys=None,
85
+ homepage="https://github.com/meetyildiz/toqad",
86
+ citation=_CITATION,
87
+ task_templates=[
88
+ QuestionAnsweringExtractive(
89
+ question_column="question", context_column="context", answers_column="answers"
90
+ )
91
+ ],
92
+ )
93
+
94
+ def _split_generators(self, dl_manager):
95
+ downloaded_files = dl_manager.download_and_extract(_URLS)
96
+
97
+ return [
98
+ datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": downloaded_files["train"]}),
99
+ datasets.SplitGenerator(name=datasets.Split.VALIDATION, gen_kwargs={"filepath": downloaded_files["dev"]}),
100
+ datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={"filepath": downloaded_files["test"]}),
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
+
110
+ for document in squad["data"]:
111
+ for par in document['paragraphs']:
112
+ for qas in par['qas']:
113
+ if len(qas['answers']) == 0: #no answer
114
+ ans_start = -1
115
+ ans_end = -1
116
+ ans_text = ""
117
+ else:
118
+ ans_start = int(qas['answers'][0]['answer_start'])
119
+ ans_end = ans_start + len(qas['answers'][0]['text'])
120
+ ans_text = qas['answers'][0]['text']
121
+
122
+ ex = {
123
+ "id": qas["id"],
124
+ "title": document["title"],
125
+ "context": par['context'],
126
+ "question": qas['question'],
127
+ "answers": {
128
+ "text": [ans_text],
129
+ "answer_start": [ans_start],
130
+ },
131
+ }
132
+
133
+ yield key, ex
134
+ key += 1