Kevin Zhao commited on
Commit
468405d
·
1 Parent(s): 15c49f7

First dataset loading script

Browse files
Files changed (1) hide show
  1. oLMpics.py +142 -0
oLMpics.py ADDED
@@ -0,0 +1,142 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ # TODO: Address all TODOs and remove all explanatory comments
15
+ """TODO: Add a description here."""
16
+
17
+
18
+ import csv
19
+ import json
20
+ import os
21
+
22
+ import datasets
23
+
24
+
25
+ # Find for instance the citation on arxiv or on the dataset repo/website
26
+ _CITATION = """\
27
+ @article{talmor2020olmpics,
28
+ title={oLMpics-on what language model pre-training captures},
29
+ author={Talmor, Alon and Elazar, Yanai and Goldberg, Yoav and Berant, Jonathan},
30
+ journal={Transactions of the Association for Computational Linguistics},
31
+ volume={8},
32
+ pages={743--758},
33
+ year={2020},
34
+ publisher={MIT Press}
35
+ }
36
+ """
37
+
38
+ _DESCRIPTION = """\
39
+ This is a set a eight datasets from the paper "oLMpics - On what Language Model Pre-training Captures"
40
+ by Alon Talmor et al.
41
+ """
42
+
43
+ _HOMEPAGE = "https://github.com/alontalmor/oLMpics"
44
+
45
+ _LICENSE = "Apache 2.0"
46
+
47
+ # TODO: Add link to the official dataset URLs here
48
+ # The HuggingFace Datasets library doesn't host the datasets but only points to the original files.
49
+ # This can be an arbitrary nested dict/list of URLs (see below in `_split_generators` method)
50
+ _URL = "https://huggingface.co/datasets/KevinZ/oLMpics/resolve/main/"
51
+ TASKS = ["Age_Comparison", "Always_Never", "Antonym_Negation", "Encyclopedic_Composition",
52
+ "Multihop_Composition", "Object_Comparison", "Property_Conjunction", "Taxonomy_Conjunction"]
53
+ _URLS = {task: [f"{_URL}{task}/train.jsonl", f"{_URL}{task}/test.jsonl"] for task in TASKS}
54
+
55
+
56
+ class OLMpicsDataset(datasets.GeneratorBasedBuilder):
57
+ """TODO: Short description of my dataset."""
58
+
59
+ VERSION = datasets.Version("1.0.0")
60
+
61
+ # This is an example of a dataset with multiple configurations.
62
+ # If you don't want/need to define several sub-sets in your dataset,
63
+ # just remove the BUILDER_CONFIG_CLASS and the BUILDER_CONFIGS attributes.
64
+
65
+ # If you need to make complex sub-parts in the datasets with configurable options
66
+ # You can create your own builder configuration class to store attribute, inheriting from datasets.BuilderConfig
67
+ # BUILDER_CONFIG_CLASS = MyBuilderConfig
68
+
69
+ # You will be able to load one or the other configurations in the following list with
70
+ # data = datasets.load_dataset('my_dataset', 'first_domain')
71
+ # data = datasets.load_dataset('my_dataset', 'second_domain')
72
+ BUILDER_CONFIGS = [
73
+ datasets.BuilderConfig(name=task, version=VERSION, description=f"{task} description") for task in TASKS
74
+ ]
75
+
76
+ def _info(self):
77
+ # TODO: This method specifies the datasets.DatasetInfo object which contains informations and typings for the dataset
78
+ features = datasets.Features(
79
+ {
80
+ "stem": datasets.Value("string"),
81
+ "choices": datasets.Sequence("string"),
82
+ "answerKey": datasets.Value("string")
83
+ # These are the features of your dataset like images, labels ...
84
+ }
85
+ )
86
+
87
+ return datasets.DatasetInfo(
88
+ # This is the description that will appear on the datasets page.
89
+ description=_DESCRIPTION,
90
+ # This defines the different columns of the dataset and their types
91
+ features=features, # Here we define them above because they are different between the two configurations
92
+ # If there's a common (input, target) tuple from the features, uncomment supervised_keys line below and
93
+ # specify them. They'll be used if as_supervised=True in builder.as_dataset.
94
+ # supervised_keys=("sentence", "label"),
95
+ # Homepage of the dataset for documentation
96
+ homepage=_HOMEPAGE,
97
+ # License for the dataset if available
98
+ license=_LICENSE,
99
+ # Citation for the dataset
100
+ citation=_CITATION,
101
+ )
102
+
103
+ def _split_generators(self, dl_manager):
104
+ # TODO: This method is tasked with downloading/extracting the data and defining the splits depending on the configuration
105
+ # If several configurations are possible (listed in BUILDER_CONFIGS), the configuration selected by the user is in self.config.name
106
+
107
+ # dl_manager is a datasets.download.DownloadManager that can be used to download and extract URLS
108
+ # 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.
109
+ # By default the archives will be extracted and a path to a cached folder where they are extracted is returned instead of the archive
110
+ urls = _URLS[self.config.name]
111
+ data_dir = dl_manager.download_and_extract(urls)
112
+ return [
113
+ datasets.SplitGenerator(
114
+ name=datasets.Split.TRAIN,
115
+ # These kwargs will be passed to _generate_examples
116
+ gen_kwargs={
117
+ "filepath": os.path.join(data_dir, "train.jsonl"),
118
+ "split": "train",
119
+ },
120
+ ),
121
+ datasets.SplitGenerator(
122
+ name=datasets.Split.TEST,
123
+ # These kwargs will be passed to _generate_examples
124
+ gen_kwargs={
125
+ "filepath": os.path.join(data_dir, "test.jsonl"),
126
+ "split": "test"
127
+ },
128
+ ),
129
+ ]
130
+
131
+ # method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
132
+ def _generate_examples(self, filepath, split):
133
+ # TODO: This method handles input defined in _split_generators to yield (key, example) tuples from the dataset.
134
+ # The `key` is for legacy reasons (tfds) and is not important in itself, but must be unique for each example.
135
+ with open(filepath, encoding="utf-8") as f:
136
+ for key, row in enumerate(f):
137
+ data = json.loads(row)
138
+ yield key, {
139
+ "stem": data["stem"],
140
+ "choices": data["choices"],
141
+ "answerKey": data["answerKey"],
142
+ }