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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# coding=utf-8
|
| 2 |
+
# Copyright 2020 The HuggingFace Datasets Authors and the Semeru Lab and SEART research group.
|
| 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 |
+
|
| 18 |
+
import csv
|
| 19 |
+
import glob
|
| 20 |
+
import os
|
| 21 |
+
|
| 22 |
+
import datasets
|
| 23 |
+
|
| 24 |
+
import numpy as np
|
| 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 |
+
author={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 |
+
_DATA_URLs = {
|
| 53 |
+
"long": {
|
| 54 |
+
"train": "https://huggingface.co/datasets/semeru/completeformer/resolve/main/long/training.csv",
|
| 55 |
+
"valid": "https://huggingface.co/datasets/semeru/completeformer/resolve/main/long/validation.csv",
|
| 56 |
+
"test": "https://huggingface.co/datasets/semeru/completeformer/resolve/main/long/test.csv",
|
| 57 |
+
},
|
| 58 |
+
"medium": {
|
| 59 |
+
"train": "https://huggingface.co/datasets/semeru/completeformer/resolve/main/medium/training.csv",
|
| 60 |
+
"valid": "https://huggingface.co/datasets/semeru/completeformer/resolve/main/medium/validation.csv",
|
| 61 |
+
"test": "https://huggingface.co/datasets/semeru/completeformer/resolve/main/medium/test.csv",
|
| 62 |
+
},
|
| 63 |
+
"short": {
|
| 64 |
+
"train": "https://huggingface.co/datasets/semeru/completeformer/resolve/main/short/training.csv",
|
| 65 |
+
"valid": "https://huggingface.co/datasets/semeru/completeformer/resolve/main/short/validation.csv",
|
| 66 |
+
"test": "https://huggingface.co/datasets/semeru/completeformer/resolve/main/short/test.csv",
|
| 67 |
+
},
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
# TODO: Name of the dataset usually match the script name with CamelCase instead of snake_case
|
| 72 |
+
class CSNCHumanJudgementDataset(datasets.GeneratorBasedBuilder):
|
| 73 |
+
"""TODO: Short description of my dataset."""
|
| 74 |
+
|
| 75 |
+
VERSION = datasets.Version("1.1.0")
|
| 76 |
+
|
| 77 |
+
# This is an example of a dataset with multiple configurations.
|
| 78 |
+
# If you don't want/need to define several sub-sets in your dataset,
|
| 79 |
+
# just remove the BUILDER_CONFIG_CLASS and the BUILDER_CONFIGS attributes.
|
| 80 |
+
|
| 81 |
+
# If you need to make complex sub-parts in the datasets with configurable options
|
| 82 |
+
# You can create your own builder configuration class to store attribute, inheriting from datasets.BuilderConfig
|
| 83 |
+
# BUILDER_CONFIG_CLASS = MyBuilderConfig
|
| 84 |
+
|
| 85 |
+
# You will be able to load one or the other configurations in the following list with
|
| 86 |
+
# data = datasets.load_dataset('my_dataset', 'first_domain')
|
| 87 |
+
# data = datasets.load_dataset('my_dataset', 'second_domain')
|
| 88 |
+
BUILDER_CONFIGS = [
|
| 89 |
+
datasets.BuilderConfig(
|
| 90 |
+
name="all",
|
| 91 |
+
version=VERSION,
|
| 92 |
+
description="",
|
| 93 |
+
),
|
| 94 |
+
datasets.BuilderConfig(
|
| 95 |
+
name="mix",
|
| 96 |
+
version=VERSION,
|
| 97 |
+
description="",
|
| 98 |
+
),
|
| 99 |
+
datasets.BuilderConfig(
|
| 100 |
+
name="length_short",
|
| 101 |
+
version=VERSION,
|
| 102 |
+
description="",
|
| 103 |
+
),
|
| 104 |
+
datasets.BuilderConfig(
|
| 105 |
+
name="length_medium",
|
| 106 |
+
version=VERSION,
|
| 107 |
+
description="",
|
| 108 |
+
),
|
| 109 |
+
datasets.BuilderConfig(
|
| 110 |
+
name="length_long",
|
| 111 |
+
version=VERSION,
|
| 112 |
+
description="",
|
| 113 |
+
),
|
| 114 |
+
datasets.BuilderConfig(
|
| 115 |
+
name="length_mix",
|
| 116 |
+
version=VERSION,
|
| 117 |
+
description="",
|
| 118 |
+
),
|
| 119 |
+
datasets.BuilderConfig(
|
| 120 |
+
name="tokenizer",
|
| 121 |
+
version=VERSION,
|
| 122 |
+
description="",
|
| 123 |
+
),
|
| 124 |
+
]
|
| 125 |
+
|
| 126 |
+
DEFAULT_CONFIG_NAME = "all"
|
| 127 |
+
|
| 128 |
+
def _info(self):
|
| 129 |
+
if self.config.name == "tokenizer":
|
| 130 |
+
features = datasets.Features(
|
| 131 |
+
{
|
| 132 |
+
"function": datasets.Value("string"),
|
| 133 |
+
}
|
| 134 |
+
)
|
| 135 |
+
elif self.config.name == "all":
|
| 136 |
+
features = datasets.Features(
|
| 137 |
+
{
|
| 138 |
+
"method": datasets.Value("string"),
|
| 139 |
+
"block": datasets.Value("string"),
|
| 140 |
+
"complex_masked_block": datasets.Value("string"),
|
| 141 |
+
"complex_input": datasets.Value("string"),
|
| 142 |
+
"complex_target": datasets.Value("string"),
|
| 143 |
+
"medium_masked_block": datasets.Value("string"),
|
| 144 |
+
"medium_input": datasets.Value("string"),
|
| 145 |
+
"medium_target": datasets.Value("string"),
|
| 146 |
+
"simple_masked_block": datasets.Value("string"),
|
| 147 |
+
"simple_input": datasets.Value("string"),
|
| 148 |
+
"simple_target": datasets.Value("string"),
|
| 149 |
+
}
|
| 150 |
+
)
|
| 151 |
+
elif self.config.name == "mix":
|
| 152 |
+
features = datasets.Features(
|
| 153 |
+
{
|
| 154 |
+
"input": datasets.Value("string"),
|
| 155 |
+
"target": datasets.Value("string"),
|
| 156 |
+
}
|
| 157 |
+
)
|
| 158 |
+
elif self.config.name.startswith("length_"):
|
| 159 |
+
features = datasets.Features(
|
| 160 |
+
{
|
| 161 |
+
"input": datasets.Value("string"),
|
| 162 |
+
"target": datasets.Value("string"),
|
| 163 |
+
"size": datasets.Value("int64"),
|
| 164 |
+
}
|
| 165 |
+
)
|
| 166 |
+
|
| 167 |
+
return datasets.DatasetInfo(
|
| 168 |
+
description=_DESCRIPTION,
|
| 169 |
+
features=features,
|
| 170 |
+
supervised_keys=None,
|
| 171 |
+
homepage=_HOMEPAGE,
|
| 172 |
+
license=_LICENSE,
|
| 173 |
+
citation=_CITATION,
|
| 174 |
+
)
|
| 175 |
+
|
| 176 |
+
def _split_generators(self, dl_manager):
|
| 177 |
+
"""Returns SplitGenerators."""
|
| 178 |
+
# TODO: This method is tasked with downloading/extracting the data and defining the splits depending on the configuration
|
| 179 |
+
# If several configurations are possible (listed in BUILDER_CONFIGS), the configuration selected by the user is in self.config.name
|
| 180 |
+
|
| 181 |
+
# dl_manager is a datasets.download.DownloadManager that can be used to download and extract URLs
|
| 182 |
+
# 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.
|
| 183 |
+
# By default the archives will be extracted and a path to a cached folder where they are extracted is returned instead of the archive
|
| 184 |
+
my_urls = _DATA_URLs[self.config.name]
|
| 185 |
+
if self.config.name == "tokenizer":
|
| 186 |
+
data_dir = dl_manager.download_and_extract(my_urls)
|
| 187 |
+
return [
|
| 188 |
+
datasets.SplitGenerator(
|
| 189 |
+
name=datasets.Split.TRAIN,
|
| 190 |
+
gen_kwargs={"filepath": data_dir},
|
| 191 |
+
),
|
| 192 |
+
]
|
| 193 |
+
else:
|
| 194 |
+
data_dirs = {}
|
| 195 |
+
for k, v in my_urls.items():
|
| 196 |
+
data_dirs[k] = dl_manager.download_and_extract(v)
|
| 197 |
+
return [
|
| 198 |
+
datasets.SplitGenerator(
|
| 199 |
+
name=datasets.Split.TRAIN,
|
| 200 |
+
# These kwargs will be passed to _generate_examples
|
| 201 |
+
gen_kwargs={
|
| 202 |
+
"file_path": data_dirs["train"],
|
| 203 |
+
},
|
| 204 |
+
),
|
| 205 |
+
datasets.SplitGenerator(
|
| 206 |
+
name=datasets.Split.VALIDATION,
|
| 207 |
+
# These kwargs will be passed to _generate_examples
|
| 208 |
+
gen_kwargs={
|
| 209 |
+
"file_path": data_dirs["valid"],
|
| 210 |
+
},
|
| 211 |
+
),
|
| 212 |
+
datasets.SplitGenerator(
|
| 213 |
+
name=datasets.Split.TEST,
|
| 214 |
+
# These kwargs will be passed to _generate_examples
|
| 215 |
+
gen_kwargs={
|
| 216 |
+
"file_path": data_dirs["test"],
|
| 217 |
+
},
|
| 218 |
+
),
|
| 219 |
+
]
|
| 220 |
+
|
| 221 |
+
def _generate_examples(
|
| 222 |
+
self,
|
| 223 |
+
file_path,
|
| 224 |
+
):
|
| 225 |
+
"""Yields examples as (key, example) tuples."""
|
| 226 |
+
# This method handles input defined in _split_generators to yield (key, example) tuples from the dataset.
|
| 227 |
+
# The `key` is here for legacy reason (tfds) and is not important in itself.
|
| 228 |
+
|
| 229 |
+
with open(file_path, encoding="utf-8") as f:
|
| 230 |
+
csv_reader = csv.reader(f, quotechar='"', delimiter=",", quoting=csv.QUOTE_ALL, skipinitialspace=True)
|
| 231 |
+
next(csv_reader, None) # skip header
|
| 232 |
+
|
| 233 |
+
for row_id, row in enumerate(csv_reader):
|
| 234 |
+
if self.config.name == "tokenizer":
|
| 235 |
+
yield row_id, {
|
| 236 |
+
"function": row[1],
|
| 237 |
+
}
|
| 238 |
+
elif self.config.name == "all":
|
| 239 |
+
_, method, block, complex_masked_block, complex_input, complex_target, medium_masked_block, medium_input, medium_target, simple_masked_block, simple_input, simple_target = row
|
| 240 |
+
yield row_id, {
|
| 241 |
+
"method": method,
|
| 242 |
+
"block": block,
|
| 243 |
+
"complex_masked_block": complex_masked_block,
|
| 244 |
+
"complex_input": complex_input,
|
| 245 |
+
"complex_target": complex_target,
|
| 246 |
+
"medium_masked_block": medium_masked_block,
|
| 247 |
+
"medium_input": medium_input,
|
| 248 |
+
"medium_target": medium_target,
|
| 249 |
+
"simple_masked_block": simple_masked_block,
|
| 250 |
+
"simple_input": simple_input,
|
| 251 |
+
"simple_target": simple_target,
|
| 252 |
+
}
|
| 253 |
+
elif self.config.name == "mix":
|
| 254 |
+
_, input, target = row
|
| 255 |
+
yield row_id, {
|
| 256 |
+
"input": input,
|
| 257 |
+
"target": target,
|
| 258 |
+
}
|
| 259 |
+
elif self.config.name.startswith("length_"):
|
| 260 |
+
_, input, target, size = row
|
| 261 |
+
yield row_id, {
|
| 262 |
+
"input": input,
|
| 263 |
+
"target": target,
|
| 264 |
+
"size": int(size),
|
| 265 |
+
}
|