|
import os |
|
import datasets |
|
|
|
_CITATION = """\ |
|
Put your dataset citation here. |
|
""" |
|
|
|
_DESCRIPTION = """\ |
|
Description of your dataset goes here. |
|
""" |
|
|
|
_HOMEPAGE = "https://your-dataset-homepage.com" |
|
|
|
_LICENSE = "License information goes here." |
|
|
|
|
|
class Test(datasets.GeneratorBasedBuilder): |
|
"""Your dataset description""" |
|
|
|
BUILDER_CONFIGS = [ |
|
datasets.BuilderConfig(name="customers", version=datasets.Version("1.0.0"), description="This is subset A"), |
|
datasets.BuilderConfig(name="products", version=datasets.Version("1.0.0"), description="This is subset B"), |
|
] |
|
|
|
def _info(self): |
|
if self.config.name == "customers": |
|
features = datasets.Features( |
|
{ |
|
"customer_id": datasets.Value("string"), |
|
"name": datasets.Value("string"), |
|
"age": datasets.Value("int32"), |
|
} |
|
) |
|
elif self.config.name == "products": |
|
features = datasets.Features( |
|
{ |
|
"product_id": datasets.Value("float32"), |
|
"name": datasets.Value("string"), |
|
"price": datasets.Value("float32"), |
|
} |
|
) |
|
else: |
|
raise ValueError(f"Unknown subset: {self.config.name}") |
|
|
|
return datasets.DatasetInfo( |
|
description=_DESCRIPTION, |
|
features=features, |
|
supervised_keys=None, |
|
homepage=_HOMEPAGE, |
|
license=_LICENSE, |
|
citation=_CITATION, |
|
) |
|
|
|
def _split_generators(self, dl_manager): |
|
data_dir = os.path.join(self.config.data_dir, self.config.name) |
|
return [ |
|
datasets.SplitGenerator( |
|
name=datasets.Split.TRAIN, |
|
gen_kwargs={"filepath": os.path.join(data_dir, "train.csv")}, |
|
), |
|
datasets.SplitGenerator( |
|
name=datasets.Split.VALIDATION, |
|
gen_kwargs={"filepath": os.path.join(data_dir, "val.csv")}, |
|
), |
|
datasets.SplitGenerator( |
|
name=datasets.Split.TEST, |
|
gen_kwargs={"filepath": os.path.join(data_dir, "test.csv")}, |
|
), |
|
] |
|
|
|
def _generate_examples(self, filepath): |
|
with open(filepath, encoding="utf-8") as f: |
|
for id_, line in enumerate(f): |
|
|
|
if self.config.name == "customers": |
|
|
|
feature_a1, feature_a2, label = line.strip().split(",") |
|
yield id_, { |
|
"customer_id": feature_a1, |
|
"name": feature_a2, |
|
"age": int(label), |
|
} |
|
elif self.config.name == "products": |
|
|
|
feature_b1, feature_b2, additional_info = line.strip().split(",") |
|
yield id_, { |
|
"product_id": feature_b1, |
|
"name": feature_b2, |
|
"price": float(additional_info), |
|
} |
|
|