mathiascreutz commited on
Commit
586e978
1 Parent(s): 575cdeb

Some initial version of a data loader

Browse files
Files changed (1) hide show
  1. opusparcus.py +155 -0
opusparcus.py ADDED
@@ -0,0 +1,155 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf-8
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
+
16
+ """TODO: Add a description here."""
17
+
18
+ import csv
19
+ import json
20
+ import os
21
+ import datasets
22
+
23
+ # Add BibTeX citation
24
+
25
+ _CITATION = """\
26
+
27
+ @InProceedings{huggingface:dataset,
28
+ title = {A great new dataset},
29
+ author={huggingface, Inc.
30
+ },
31
+ year={2020}
32
+ }
33
+ """
34
+
35
+ _DESCRIPTION = """\
36
+ Test adding a dataset with challenge set to GEM benchmark .
37
+ """
38
+
39
+ _HOMEPAGE = ""
40
+
41
+ _LICENSE = ""
42
+
43
+ # The HuggingFace dataset library don't host the datasets but only point to the original files
44
+ # This can be an arbitrary nested dict/list of URLs (see below in `_split_generators` method)
45
+
46
+ _URLs = {
47
+
48
+ #"train": "train.jsonl",
49
+ "validation": "validation.jsonl",
50
+ "test": "test.jsonl"
51
+
52
+ }
53
+
54
+ class Opusparcus(datasets.GeneratorBasedBuilder):
55
+
56
+ """TODO: Short description of my dataset."""
57
+
58
+ VERSION = datasets.Version("1.1.0")
59
+
60
+ # This is an example of a dataset with multiple configurations.
61
+ # If you don't want/need to define several sub-sets in your dataset,
62
+ # just remove the BUILDER_CONFIG_CLASS and the BUILDER_CONFIGS attributes.
63
+ # If you need to make complex sub-parts in the datasets with configurable options
64
+ # You can create your own builder configuration class to store attribute, inheriting from datasets.BuilderConfig
65
+ # BUILDER_CONFIG_CLASS = MyBuilderConfig
66
+ # You will be able to load one or the other configurations in the following list with
67
+ # data = datasets.load_dataset('my_dataset', 'first_domain')
68
+ # data = datasets.load_dataset('my_dataset', 'second_domain')
69
+ #BUILDER_CONFIGS = [
70
+ # datasets.BuilderConfig(name="test", version=VERSION, description="This part of my dataset covers a first domain"),
71
+ #]
72
+
73
+ #DEFAULT_CONFIG_NAME = "test" # It's not mandatory to have a default configuration. Just use one if it make sense.
74
+
75
+ def _info(self):
76
+ # TODO: This method specifies the datasets.DatasetInfo object which contains informations and typings for the dataset
77
+ #if self.config.name == "test": # This is the name of the configuration selected in BUILDER_CONFIGS above
78
+ features = datasets.Features(
79
+ {
80
+ "sentence": datasets.Value("string"),
81
+ "label": datasets.Value("float"),
82
+ "gem_id": datasets.Value("string")
83
+ }
84
+ )
85
+
86
+ return datasets.DatasetInfo(
87
+ # This is the description that will appear on the datasets page.
88
+ description=_DESCRIPTION,
89
+ # This defines the different columns of the dataset and their types
90
+ features=features, # Here we define them above because they are different between the two configurations
91
+ # If there's a common (input, target) tuple from the features,
92
+ # specify them here. They'll be used if as_supervised=True in
93
+ # builder.as_dataset.
94
+ supervised_keys=None,
95
+ # Homepage of the dataset for documentation
96
+ homepage=_HOMEPAGE,
97
+ # License for the dataset if available
98
+ license=_LICENSE,
99
+
100
+ # Citation for the dataset
101
+ citation=_CITATION,
102
+ )
103
+
104
+ def _split_generators(self, dl_manager):
105
+ """Returns SplitGenerators."""
106
+ # TODO: This method is tasked with downloading/extracting the data and defining the splits depending on the configuration
107
+ # If several configurations are possible (listed in BUILDER_CONFIGS), the configuration selected by the user is in self.config.name
108
+ # dl_manager is a datasets.download.DownloadManager that can be used to download and extract URLs
109
+ # 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.
110
+ # By default the archives will be extracted and a path to a cached folder where they are extracted is returned instead of the archive
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": data_dir["train"],
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": data_dir["test.jsonl"],
126
+ "split": "test"
127
+ },
128
+ ),
129
+ datasets.SplitGenerator(
130
+ name=datasets.Split.VALIDATION,
131
+ # These kwargs will be passed to _generate_examples
132
+ gen_kwargs={
133
+ "filepath": data_dir["validation.jsonl"],
134
+ "split": "validation",
135
+ },
136
+ ),
137
+ ]
138
+
139
+ def _generate_examples(
140
+ self, filepath, split # method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
141
+ ):
142
+
143
+ """ Yields examples as (key, example) tuples. """
144
+ # This method handles input defined in _split_generators to yield (key, example) tuples from the dataset.
145
+ # The `key` is here for legacy reason (tfds) and is not important in itself.
146
+ with open(filepath, encoding="utf-8") as f:
147
+ for id_, row in enumerate(f):
148
+ data = json.loads(row)
149
+ yield id_, {
150
+ "sent1": data["sent1"],
151
+ "sent2": data["sent2"],
152
+ "annot_score": data["annot_score"],
153
+ "gem_id": data["gem_id"]
154
+ }
155
+