File size: 7,873 Bytes
c7edabd 071b155 3ce1aad c7edabd 3ce1aad c7edabd 071b155 3ce1aad c7edabd 071b155 c7edabd |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# basic-sentence-transforms.py: the HF datasets "loading script" for the NC_PAT dataset (defines configurations/tasks, columns, etc.)
import os
import json
import datasets
from datasets import Split, SplitGenerator
no_extra = {
"source": datasets.Value("string"),
"target": datasets.Value("string"),
}
samp_class = {
"source": datasets.Value("string"),
"target": datasets.Value("string"),
"class": datasets.Value("string"),
}
count_class = {
"source": datasets.Value("string"),
"target": datasets.Value("string"),
"count": datasets.Value("string"),
"class": datasets.Value("string"),
}
dir_only = {
"source": datasets.Value("string"),
"target": datasets.Value("string"),
"direction": datasets.Value("string"),
}
configs = [
{"name": "car_cdr_cons",
"desc": "small phrase translation tasks that require only: CAR, CDR, or CAR+CDR+CONS operations",
"features": samp_class},
{"name": "car_cdr_cons_tuc",
"desc": "same task as car_cdr_cons, but requires mapping lowercase fillers to their uppercase tokens",
"features": samp_class},
{"name": "car_cdr_rcons",
"desc": "same task as car_cdr_cons, but the CONS samples have their left/right children swapped",
"features": samp_class},
{"name": "car_cdr_rcons_tuc",
"desc": "same task as car_cdr_rcons, but requires mapping lowercase fillers to their uppercase tokens",
"features": samp_class},
{"name": "car_cdr_seq",
"desc": "each samples requires 1-4 combinations of CAR and CDR, as identified by the root filler token",
"features": count_class},
{"name": "car_cdr_seq_40k",
"desc": "same task as car_cdr_seq, but train samples increased from 10K to 40K",
"features": count_class},
{"name": "car_cdr_seq_tuc",
"desc": "same task as car_cdr_seq, but requires mapping lowercase fillers to their uppercase tokens",
"features": count_class},
{"name": "car_cdr_seq_40k_tuc",
"desc": "same task as car_cdr_seq_tuc, but train samples increased from 10K to 40K",
"features": count_class},
{"name": "car_cdr_seq_path",
"desc": "similiar to car_cdr_seq, but each needed operation in represented as a node in the left child of the root",
"features": count_class},
{"name": "car_cdr_seq_path_40k",
"desc": "same task as car_cdr_seq_path, but train samples increased from 10K to 40K",
"features": count_class},
{"name": "car_cdr_seq_path_40k_tuc",
"desc": "same task as car_cdr_seq_path_40k, but requires mapping lowercase fillers to their uppercase tokens",
"features": count_class},
{"name": "car_cdr_seq_path_tuc",
"desc": "same task as car_cdr_seq_path, but requires mapping lowercase fillers to their uppercase tokens",
"features": count_class},
{"name": "active_active_stb",
"desc": "active sentence translation, from sentence to parenthesized tree form, both directions",
"features": dir_only},
{"name": "active_active_stb_40k",
"desc": "same task as active_active_stb, but train samples increased from 10K to 40K",
"features": dir_only},
{"name": "active_logical_ttb",
"desc": "active to logical tree translation, in both directions",
"features": dir_only},
{"name": "active_logical_ttb_40k",
"desc": "same task as active_logical_ttb, but train samples increased from 10K to 40K",
"features": dir_only},
{"name": "active_passive_ssb",
"desc": "active to passive sentence translation, in both directions",
"features": dir_only},
{"name": "active_passive_ssb_40k",
"desc": "same task as active_passive_ssb, but train samples increased from 10K to 40K",
"features": dir_only},
{"name": "active_passive_ttb",
"desc": "active to passive tree translation, in both directions",
"features": dir_only},
{"name": "active_passive_ttb_40k",
"desc": "same task as active_passive_ttb, but train samples increased from 10K to 40K",
"features": dir_only},
{"name": "actpass_logical_tt",
"desc": "mixture of active to logical and passive to logical tree translations, single direction",
"features": no_extra},
{"name": "actpass_logical_tt_40k",
"desc": "same task as actpass_logical_tt, but train samples increased from 10K to 40K",
"features": no_extra},
{"name": "passive_logical_ttb",
"desc": "passive to logical tree translation, in both directions",
"features": dir_only},
{"name": "passive_logical_ttb_40k",
"desc": "same task as passive_logical_ttb, but train samples increased from 10K to 40K",
"features": dir_only},
{"name": "passive_passive_stb",
"desc": "passive sentence translation, from sentence to parenthesized tree form, both directions",
"features": dir_only},
{"name": "passive_passive_stb_40k",
"desc": "same task as passive_passive_stb, but train samples increased from 10K to 40K",
"features": dir_only},
]
class BasicSentenceTransformsConfig(datasets.BuilderConfig):
"""BuilderConfig for basic_sentence_transforms dataset."""
def __init__(self, features=None, **kwargs):
# Version history:
# 0.0.18: Initial version released to HF datasets
super().__init__(version=datasets.Version("0.0.18"), **kwargs)
self.features = features
self.label_classes = None
self.data_url = "./{}.zip".format(kwargs["name"])
self.citation = None
self.homepage = None
def _info(self):
return datasets.DatasetInfo(
description=self.description,
features=self.features,
# No default supervised_keys (as we have to pass both question
# and context as input).
supervised_keys=None,
homepage=self.homepage,
citation=self.citation,
)
class BasicSentenceTransforms(datasets.GeneratorBasedBuilder):
BUILDER_CONFIGS = [BasicSentenceTransformsConfig(name=c["name"], description=c["desc"], features=c["features"]) for c in configs]
VERSION = datasets.Version("0.0.18")
def _info(self):
return datasets.DatasetInfo(
description="The dataset consists of diagnostic/warm-up tasks and core tasks within this dataset." +
"The core tasks represent the translation of English sentences between the active, passive, and logical forms.",
supervised_keys=None,
homepage=None,
citation=None,
)
def _split_generators(self, dl_manager: datasets.DownloadManager):
url = self.config.data_url
dl_dir = dl_manager.download_and_extract(url)
task = self.config_id
splits = [
SplitGenerator(name=Split.TRAIN, gen_kwargs={"data_file": os.path.join(dl_dir, "train.jsonl")}),
SplitGenerator(name=Split.VALIDATION, gen_kwargs={"data_file": os.path.join(dl_dir, "dev.jsonl")}),
SplitGenerator(name=Split.TEST, gen_kwargs={"data_file": os.path.join(dl_dir, "test.jsonl")}),
]
if not task.startswith("car_cdr_cons") and not task.startswith("car_cdr_rcons"):
splits += [
SplitGenerator(name="ood_new", gen_kwargs={"data_file": os.path.join(dl_dir, "ood_new_adj.jsonl")}),
SplitGenerator(name="ood_long", gen_kwargs={"data_file": os.path.join(dl_dir, "ood_long_adj.jsonl")}),
]
return splits
def _generate_examples(self, data_file):
with open(data_file, encoding="utf-8") as f:
for i, line in enumerate(f):
key = str(i)
row = json.loads(line)
yield key, row
if __name__ == "__main__":
# short test
builder = BasicSentenceTransforms.BUILDER_CONFIGS[0]
print("name: {}, desc: {}".format(builder.name, builder.description))
|