File size: 3,657 Bytes
fba14e0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import csv
import os
import shutil
import uuid
from pathlib import Path
from typing import Dict, Iterable

import datasets
from datasets import DatasetDict, DownloadManager, load_dataset
from huggingface_hub.repository import Repository


VERSION = datasets.Version("0.0.1")

AVAILABLE_DATASETS = {
    'age_dataset': "https://github.com/Moradnejad/AgeDataset"
}

try:
    import rarfile
except ImportError:
    raise ImportError("Please install rarfile: `pip install rarfile`, to use this dataset")

class AgeDataset(datasets.GeneratorBasedBuilder):
    """AgeDataset dataset."""

    BUILDER_CONFIGS = [
        datasets.BuilderConfig(
            name=data_name, version=VERSION, description=f"{data_name} dataset"
        )
        for data_name in AVAILABLE_DATASETS
    ]

    @staticmethod
    def load(data_name_config: str = "age_dataset") -> DatasetDict:
        ds = load_dataset(__file__, data_name_config)
        return ds

    def _info(self) -> datasets.DatasetInfo:
        return datasets.DatasetInfo(
            description="",
            features=datasets.Features(
                {
                    "entity_id": datasets.Value("int32"),
                    "name": datasets.Value("string"),
                    "short_description": datasets.Value("string"),
                    "gender": datasets.Value("string"),
                    "country": datasets.Value("string"),
                    "occupation": datasets.Value("string"),
                    "birth_year": datasets.Value("string"),
                    "manner_of_death": datasets.Value("string"),
                    "age_of_death": datasets.Value("string"),
                }
            ),
            supervised_keys=None,
            homepage="https://github.com/Moradnejad/AgeDataset",
            citation="https://workshop-proceedings.icwsm.org/pdf/2022_82.pdf",
        )

    def _split_generators(
        self, dl_manager: DownloadManager
    ) -> Iterable[datasets.SplitGenerator]:
        git_repo = AVAILABLE_DATASETS[self.config.name]
        current_dir = Path(__file__).resolve().parent
        temp_dir = current_dir / uuid.uuid4().hex
        temp_dir.mkdir(exist_ok=True)
        # Repository(local_dir=str(temp_dir), clone_from=git_repo)
        os.system(f"cd {temp_dir} && git clone {git_repo}")
        shutil.copy(temp_dir / "AgeDataset" / "AgeDataset.rar", current_dir)
        shutil.rmtree(f"{temp_dir}")
        filepath = str(current_dir / "AgeDataset.rar")
        filepath = os.path.join(dl_manager.download_and_extract(filepath), "AgeDataset.csv")

        # There is no predefined train/val/test split for this dataset.
        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={
                    "filepath": filepath,
                },
            ),
        ]

    def _generate_examples(self, filepath: str) -> Iterable[Dict]:
        with open(filepath, encoding="utf-8") as f_in:
            csv_reader = csv.reader(
                f_in,
                delimiter=",",
            )
            for idx, row in enumerate(csv_reader):
                if idx == 0:
                    continue
                yield idx, {
                    "entity_id": idx,
                    "name": row[0],
                    "short_description": row[1],
                    "gender": row[2],
                    "country": row[3],
                    "occupation": row[4],
                    "birth_year": row[5],
                    "manner_of_death": row[6],
                    "age_of_death": row[7],
                }