Datasets:
GEM
/

Tasks:
Other
Modalities:
Text
Languages:
English
ArXiv:
Libraries:
Datasets
License:
WorkInTheDark commited on
Commit
d7ebb3a
1 Parent(s): b2f0b10
Files changed (1) hide show
  1. FairytaleQA.py +199 -0
FairytaleQA.py ADDED
@@ -0,0 +1,199 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ # Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
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
+ # TODO: Address all TODOs and remove all explanatory comments
16
+ """FairytaleQA: An Authentic Dataset for children's Narrative Comprehension """
17
+
18
+
19
+ import csv
20
+ import json
21
+ import os
22
+
23
+ import datasets
24
+
25
+ logger = datasets.logging.get_logger(__name__)
26
+
27
+
28
+
29
+ # TODO: Add BibTeX citation
30
+ # Find for instance the citation on arxiv or on the dataset repo/website
31
+ _CITATION = """\
32
+ @inproceedings{xu2022fairytaleqa,
33
+ author={Xu, Ying and Wang, Dakuo and Yu, Mo and Ritchie, Daniel and Yao, Bingsheng and Wu, Tongshuang and Zhang, Zheng and Li, Toby Jia-Jun and Bradford, Nora and Sun, Branda and Hoang, Tran Bao and Sang, Yisi and Hou, Yufang and Ma, Xiaojuan and Yang, Diyi and Peng, Nanyun and Yu, Zhou and Warschauer, Mark},
34
+ title = {Fantastic Questions and Where to Find Them: Fairytale{QA} -- An Authentic Dataset for Narrative Comprehension},
35
+ publisher = {Association for Computational Linguistics},
36
+ year = {2022}
37
+ }
38
+ """
39
+
40
+ # TODO: Add description of the dataset here
41
+ # You can copy an official description
42
+ _DESCRIPTION = """\
43
+ The FairytaleQA dataset focusing on narrative comprehension of kindergarten to eighth-grade students. Generated by educational experts based on an evidence-based theoretical framework, FairytaleQA consists of 10,580 explicit and implicit questions derived from 278 children-friendly stories, covering seven types of narrative elements or relations. This is for the Question Generation Task of FairytaleQA.
44
+ """
45
+
46
+ # TODO: Add a link to an official homepage for the dataset here
47
+ _HOMEPAGE = "https://github.com/uci-soe/FairytaleQAData"
48
+
49
+ # TODO: Add the licence for the dataset here if you can find it
50
+ _LICENSE = ""
51
+
52
+ # TODO: Add link to the official dataset URLs here
53
+ # The HuggingFace Datasets library doesn't host the datasets but only points to the original files.
54
+ # This can be an arbitrary nested dict/list of URLs (see below in `_split_generators` method)
55
+
56
+ _URLS = {
57
+ "train": "train.json",
58
+ "validation": "valid.json",
59
+ "test": "test.json"
60
+ }
61
+
62
+
63
+ # TODO: Name of the dataset usually match the script name with CamelCase instead of snake_case
64
+ class FairytaleQA(datasets.GeneratorBasedBuilder):
65
+ """TODO: Short description of my dataset."""
66
+
67
+ # VERSION = datasets.Version("1.1.0")
68
+
69
+ # This is an example of a dataset with multiple configurations.
70
+ # If you don't want/need to define several sub-sets in your dataset,
71
+ # just remove the BUILDER_CONFIG_CLASS and the BUILDER_CONFIGS attributes.
72
+
73
+ # If you need to make complex sub-parts in the datasets with configurable options
74
+ # You can create your own builder configuration class to store attribute, inheriting from datasets.BuilderConfig
75
+ # BUILDER_CONFIG_CLASS = MyBuilderConfig
76
+
77
+ # You will be able to load one or the other configurations in the following list with
78
+ # data = datasets.load_dataset('my_dataset', 'first_domain')
79
+ # data = datasets.load_dataset('my_dataset', 'second_domain')
80
+
81
+ VERSION = datasets.Version("1.0.0")
82
+
83
+ # DEFAULT_CONFIG_NAME = "fairytaleqa"
84
+
85
+ # BUILDER_CONFIGS = [
86
+ # FairytaleQAConfig(
87
+ # name="plain_text",
88
+ # description="Plain Text")
89
+ # ]
90
+
91
+ # DEFAULT_CONFIG_NAME = "train" # It's not mandatory to have a default configuration. Just use one if it make sense.
92
+
93
+ def _info(self):
94
+ # TODO: This method specifies the datasets.DatasetInfo object which contains informations and typings for the dataset
95
+
96
+ features = datasets.Features(
97
+ {
98
+ "story_name": datasets.Value("string"),
99
+ "content": datasets.Value("string"),
100
+ "answer": datasets.Value("string"),
101
+ "question": datasets.Value("string"),
102
+ "gem_id": datasets.Value("string"),
103
+ "target": datasets.Value("string"),
104
+ "references":[datasets.Value("string")],
105
+ "local_or_sum": datasets.Value("string"),
106
+ "attribute": datasets.Value("string"),
107
+ "ex_or_im": datasets.Value("string")
108
+ # These are the features of your dataset like images, labels ...
109
+ }
110
+ )
111
+
112
+
113
+ return datasets.DatasetInfo(
114
+ # This is the description that will appear on the datasets page.
115
+ description=_DESCRIPTION,
116
+ # This defines the different columns of the dataset and their types
117
+ features=features, # Here we define them above because they are different between the two configurations
118
+ # If there's a common (input, target) tuple from the features, uncomment supervised_keys line below and
119
+ # specify them. They'll be used if as_supervised=True in builder.as_dataset.
120
+ # supervised_keys=("sentence", "label"),
121
+ supervised_keys=None,
122
+ # Homepage of the dataset for documentation
123
+ homepage=_HOMEPAGE,
124
+ # License for the dataset if available
125
+ license=_LICENSE,
126
+ # Citation for the dataset
127
+ citation=_CITATION,
128
+ )
129
+
130
+ def _split_generators(self, dl_manager):
131
+ # TODO: This method is tasked with downloading/extracting the data and defining the splits depending on the configuration
132
+ # If several configurations are possible (listed in BUILDER_CONFIGS), the configuration selected by the user is in self.config.name
133
+
134
+ # dl_manager is a datasets.download.DownloadManager that can be used to download and extract URLS
135
+ # It can accept any type or nested list/dict and will give back the same structure with the url replaced with path to local files.
136
+ # By default the archives will be extracted and a path to a cached folder where they are extracted is returned instead of the archive
137
+ # urls = _URLS[self.config.name]
138
+ downloaded_files = dl_manager.download_and_extract(_URLS)
139
+ return [
140
+ datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={
141
+ "filepath": downloaded_files["train"],
142
+ "split": "train"}),
143
+ datasets.SplitGenerator(name=datasets.Split.VALIDATION, gen_kwargs={
144
+ "filepath": downloaded_files["validation"],
145
+ "split": "validation"}),
146
+ datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={
147
+ "filepath": downloaded_files["test"],
148
+ "split": "test"})
149
+ ]
150
+
151
+ # method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
152
+ def _generate_examples(self, filepath, split, filepaths=None, lang=None):
153
+ # TODO: This method handles input defined in _split_generators to yield (key, example) tuples from the dataset.
154
+ # The `key` is for legacy reasons (tfds) and is not important in itself, but must be unique for each example.
155
+ logger.info("generating examples from = %s", filepath)
156
+
157
+ with open(filepath, encoding="utf-8") as f:
158
+
159
+ # json_data = json.load(f)
160
+
161
+
162
+ for id_, row in enumerate(f):
163
+ data = json.loads(row)
164
+ story_name = data['story_name']
165
+ content = data['content']
166
+ answer = data['answer']
167
+ question = data['question']
168
+ local_or_sum = data['local_or_sum']
169
+ attribute = data['attribute']
170
+ ex_or_im = data['ex_or_im']
171
+ # data = json.loads(row)
172
+
173
+ yield id_, {
174
+ "story_name": story_name,
175
+ "content": content,
176
+ "answer": answer,
177
+ "question": question,
178
+ "gem_id": f"GEM-FairytaleQA-{split}-{id_}",
179
+ "target": question,
180
+ "references": [] if split == "train" else [ question ],
181
+ "local_or_sum": local_or_sum,
182
+ "attribute": attribute,
183
+ "ex_or_im": ex_or_im
184
+ }
185
+ # yield id_, {
186
+ # "story_name": data['story_name'],
187
+ # "content": data['content'],
188
+ # "answer": data['answer'],
189
+ # "gem_id": f"GEM-FairytaleQA-{split}-{id_}",
190
+ # "target": data['question'],
191
+ # "references": [] if split == "train" else [ data['question'] ],
192
+ # "local_or_sum": data['local_or_sum'],
193
+ # "attribute": data['attribute'],
194
+ # "ex_or_im": data['ex_or_im']
195
+ # }
196
+
197
+
198
+
199
+