File size: 3,267 Bytes
c812221
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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):
                # Example: Parsing CSV file based on the subset name
                if self.config.name == "customers":
                    # Subset A expects three columns: feature_a1, feature_a2, label
                    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":
                    # Subset B expects four columns: feature_b1, feature_b2, additional_info, label
                    feature_b1, feature_b2, additional_info = line.strip().split(",")
                    yield id_, {
                        "product_id": feature_b1,
                        "name": feature_b2,
                        "price": float(additional_info),
                    }