jeanpoll commited on
Commit
6364957
1 Parent(s): e554bc1

first commit

Browse files
Files changed (1) hide show
  1. wikiner_fr.py +177 -0
wikiner_fr.py ADDED
@@ -0,0 +1,177 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ """TODO: Add a description here."""
16
+
17
+ from __future__ import absolute_import, division, print_function
18
+
19
+ import csv
20
+ import json
21
+ import os
22
+
23
+ import datasets
24
+
25
+
26
+ # TODO: Add BibTeX citation
27
+ # Find for instance the citation on arxiv or on the dataset repo/website
28
+ _CITATION = """\
29
+ @InProceedings{huggingface:dataset,
30
+ title = {A great new dataset},
31
+ authors={huggingface, Inc.
32
+ },
33
+ year={2020}
34
+ }
35
+ """
36
+
37
+ # TODO: Add description of the dataset here
38
+ # You can copy an official description
39
+ _DESCRIPTION = """\
40
+ This new dataset is designed to solve this great NLP task and is crafted with a lot of care.
41
+ """
42
+
43
+ # TODO: Add a link to an official homepage for the dataset here
44
+ _HOMEPAGE = ""
45
+
46
+ # TODO: Add the licence for the dataset here if you can find it
47
+ _LICENSE = ""
48
+
49
+ # TODO: Add link to the official dataset URLs here
50
+ # The HuggingFace dataset library don't host the datasets but only point to the original files
51
+ # This can be an arbitrary nested dict/list of URLs (see below in `_split_generators` method)
52
+ _URLs = {
53
+ 'first_domain': "https://huggingface.co/great-new-dataset-first_domain.zip",
54
+ 'second_domain': "https://huggingface.co/great-new-dataset-second_domain.zip",
55
+ }
56
+
57
+
58
+ # TODO: Name of the dataset usually match the script name with CamelCase instead of snake_case
59
+ class NewDataset(datasets.GeneratorBasedBuilder):
60
+ """TODO: Short description of my dataset."""
61
+
62
+ VERSION = datasets.Version("1.1.0")
63
+
64
+ # This is an example of a dataset with multiple configurations.
65
+ # If you don't want/need to define several sub-sets in your dataset,
66
+ # just remove the BUILDER_CONFIG_CLASS and the BUILDER_CONFIGS attributes.
67
+
68
+ # If you need to make complex sub-parts in the datasets with configurable options
69
+ # You can create your own builder configuration class to store attribute, inheriting from datasets.BuilderConfig
70
+ # BUILDER_CONFIG_CLASS = MyBuilderConfig
71
+
72
+ # You will be able to load one or the other configurations in the following list with
73
+ # data = datasets.load_dataset('my_dataset', 'first_domain')
74
+ # data = datasets.load_dataset('my_dataset', 'second_domain')
75
+ BUILDER_CONFIGS = [
76
+ datasets.BuilderConfig(name="first_domain", version=VERSION, description="This part of my dataset covers a first domain"),
77
+ datasets.BuilderConfig(name="second_domain", version=VERSION, description="This part of my dataset covers a second domain"),
78
+ ]
79
+
80
+ DEFAULT_CONFIG_NAME = "first_domain" # It's not mandatory to have a default configuration. Just use one if it make sense.
81
+
82
+ def _info(self):
83
+ # TODO: This method specifies the datasets.DatasetInfo object which contains informations and typings for the dataset
84
+ if self.config.name == "first_domain": # This is the name of the configuration selected in BUILDER_CONFIGS above
85
+ features = datasets.Features(
86
+ {
87
+ "sentence": datasets.Value("string"),
88
+ "option1": datasets.Value("string"),
89
+ "answer": datasets.Value("string")
90
+ # These are the features of your dataset like images, labels ...
91
+ }
92
+ )
93
+ else: # This is an example to show how to have different features for "first_domain" and "second_domain"
94
+ features = datasets.Features(
95
+ {
96
+ "sentence": datasets.Value("string"),
97
+ "option2": datasets.Value("string"),
98
+ "second_domain_answer": datasets.Value("string")
99
+ # These are the features of your dataset like images, labels ...
100
+ }
101
+ )
102
+ return datasets.DatasetInfo(
103
+ # This is the description that will appear on the datasets page.
104
+ description=_DESCRIPTION,
105
+ # This defines the different columns of the dataset and their types
106
+ features=features, # Here we define them above because they are different between the two configurations
107
+ # If there's a common (input, target) tuple from the features,
108
+ # specify them here. They'll be used if as_supervised=True in
109
+ # builder.as_dataset.
110
+ supervised_keys=None,
111
+ # Homepage of the dataset for documentation
112
+ homepage=_HOMEPAGE,
113
+ # License for the dataset if available
114
+ license=_LICENSE,
115
+ # Citation for the dataset
116
+ citation=_CITATION,
117
+ )
118
+
119
+ def _split_generators(self, dl_manager):
120
+ """Returns SplitGenerators."""
121
+ # TODO: This method is tasked with downloading/extracting the data and defining the splits depending on the configuration
122
+ # If several configurations are possible (listed in BUILDER_CONFIGS), the configuration selected by the user is in self.config.name
123
+
124
+ # dl_manager is a datasets.download.DownloadManager that can be used to download and extract URLs
125
+ # 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.
126
+ # By default the archives will be extracted and a path to a cached folder where they are extracted is returned instead of the archive
127
+ my_urls = _URLs[self.config.name]
128
+ data_dir = dl_manager.download_and_extract(my_urls)
129
+ return [
130
+ datasets.SplitGenerator(
131
+ name=datasets.Split.TRAIN,
132
+ # These kwargs will be passed to _generate_examples
133
+ gen_kwargs={
134
+ "filepath": os.path.join(data_dir, "train.jsonl"),
135
+ "split": "train",
136
+ },
137
+ ),
138
+ datasets.SplitGenerator(
139
+ name=datasets.Split.TEST,
140
+ # These kwargs will be passed to _generate_examples
141
+ gen_kwargs={
142
+ "filepath": os.path.join(data_dir, "test.jsonl"),
143
+ "split": "test"
144
+ },
145
+ ),
146
+ datasets.SplitGenerator(
147
+ name=datasets.Split.VALIDATION,
148
+ # These kwargs will be passed to _generate_examples
149
+ gen_kwargs={
150
+ "filepath": os.path.join(data_dir, "dev.jsonl"),
151
+ "split": "dev",
152
+ },
153
+ ),
154
+ ]
155
+
156
+ def _generate_examples(
157
+ self, filepath, split # method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
158
+ ):
159
+ """ Yields examples as (key, example) tuples. """
160
+ # This method handles input defined in _split_generators to yield (key, example) tuples from the dataset.
161
+ # The `key` is here for legacy reason (tfds) and is not important in itself.
162
+
163
+ with open(filepath, encoding="utf-8") as f:
164
+ for id_, row in enumerate(f):
165
+ data = json.loads(row)
166
+ if self.config.name == "first_domain":
167
+ yield id_, {
168
+ "sentence": data["sentence"],
169
+ "option1": data["option1"],
170
+ "answer": "" if split == "test" else data["answer"],
171
+ }
172
+ else:
173
+ yield id_, {
174
+ "sentence": data["sentence"],
175
+ "option2": data["option2"],
176
+ "second_domain_answer": "" if split == "test" else data["second_domain_answer"],
177
+ }