basic_sentence_transforms / basic_sentence_transforms.py
rfernand's picture
Upload 3 files
66e2146
# basic-sentence-transforms.py: the HF datasets "loading script" for the internal NC_PAT dataset (defines configurations/tasks, columns, etc.)
import os
import json
import datasets
from datasets import Split, SplitGenerator
# relative path was not working here, so switched to absolute path
DATASET_URL = "https://huggingface.co/datasets/rfernand/basic_sentence_transforms/resolve/main"
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"),
}
warmup_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},
]
core_configs = [
{"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_ssb",
"desc": "active to logical sentence translation, in both directions",
"features": dir_only},
{"name": "active_logical_ssb_40k",
"desc": "same task as active_logical_ssb, 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_ss",
"desc": "mixture of active to logical and passive to logical sentence translations, single direction",
"features": no_extra},
{"name": "actpass_logical_ss_40k",
"desc": "same task as actpass_logical_ss, but train samples increased from 10K to 40K",
"features": no_extra},
{"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": "logical_logical_stb",
"desc": "logical form sentence translation, from sentence to parenthesized tree form, both directions",
"features": dir_only},
{"name": "alogical_logical_stb_40k",
"desc": "same task as logical_logical_stb, but train samples increased from 10K to 40K",
"features": dir_only},
{"name": "passive_logical_ssb",
"desc": "passive to logical sentence translation, in both directions",
"features": dir_only},
{"name": "passive_logical_ssb_40k",
"desc": "same task as passive_logical_ssb, but train samples increased from 10K to 40K",
"features": dir_only},
{"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},
]
configs = warmup_configs + core_configs
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
# 0.0.21: release V21 of NC_PAT dataset
super().__init__(version=datasets.Version("0.0.21"), **kwargs)
self.features = features
self.label_classes = None
#self.data_url = "./{}.zip".format(kwargs["name"])
self.data_url ="{}/{}.zip".format(DATASET_URL, 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.21")
def _info(self):
# features are now required here, so get them from the current CONFIG (following code example from super_glue.py)
features = {feature: datasets.Value("string") for feature in self.config.features}
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.",
features=datasets.Features(features),
supervised_keys=None,
homepage=None,
citation=None,
)
def _split_generators(self, dl_manager: datasets.DownloadManager):
url = self.config.data_url
#print("_split_generator: url={}".format(url))
dl_dir = dl_manager.download_and_extract(url)
task = self.config_id
#print("task: {}, dl_dir: {}".format(task, dl_dir))
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.jsonl")}),
SplitGenerator(name="ood_long", gen_kwargs={"data_file": os.path.join(dl_dir, "ood_long.jsonl")}),
SplitGenerator(name="ood_all", gen_kwargs={"data_file": os.path.join(dl_dir, "ood_all.jsonl")}),
]
return splits
def _generate_examples(self, data_file):
#print("_generate_examples: data_file: {}".format(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))