Create age_dataset_generators.py
Browse files- age_dataset_generators.py +105 -0
age_dataset_generators.py
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
# -*- coding: utf-8 -*-
|
| 3 |
+
import csv
|
| 4 |
+
import os
|
| 5 |
+
import shutil
|
| 6 |
+
import uuid
|
| 7 |
+
from pathlib import Path
|
| 8 |
+
from typing import Dict, Iterable
|
| 9 |
+
|
| 10 |
+
import datasets
|
| 11 |
+
from datasets import DatasetDict, DownloadManager, load_dataset
|
| 12 |
+
from huggingface_hub.repository import Repository
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
VERSION = datasets.Version("0.0.1")
|
| 16 |
+
|
| 17 |
+
AVAILABLE_DATASETS = {
|
| 18 |
+
'age_dataset': "https://github.com/Moradnejad/AgeDataset"
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
try:
|
| 22 |
+
import rarfile
|
| 23 |
+
except ImportError:
|
| 24 |
+
raise ImportError("Please install rarfile: `pip install rarfile`, to use this dataset")
|
| 25 |
+
|
| 26 |
+
class AgeDataset(datasets.GeneratorBasedBuilder):
|
| 27 |
+
"""AgeDataset dataset."""
|
| 28 |
+
|
| 29 |
+
BUILDER_CONFIGS = [
|
| 30 |
+
datasets.BuilderConfig(
|
| 31 |
+
name=data_name, version=VERSION, description=f"{data_name} dataset"
|
| 32 |
+
)
|
| 33 |
+
for data_name in AVAILABLE_DATASETS
|
| 34 |
+
]
|
| 35 |
+
|
| 36 |
+
@staticmethod
|
| 37 |
+
def load(data_name_config: str = "age_dataset") -> DatasetDict:
|
| 38 |
+
ds = load_dataset(__file__, data_name_config)
|
| 39 |
+
return ds
|
| 40 |
+
|
| 41 |
+
def _info(self) -> datasets.DatasetInfo:
|
| 42 |
+
return datasets.DatasetInfo(
|
| 43 |
+
description="",
|
| 44 |
+
features=datasets.Features(
|
| 45 |
+
{
|
| 46 |
+
"entity_id": datasets.Value("int32"),
|
| 47 |
+
"name": datasets.Value("string"),
|
| 48 |
+
"short_description": datasets.Value("string"),
|
| 49 |
+
"gender": datasets.Value("string"),
|
| 50 |
+
"country": datasets.Value("string"),
|
| 51 |
+
"occupation": datasets.Value("string"),
|
| 52 |
+
"birth_year": datasets.Value("string"),
|
| 53 |
+
"manner_of_death": datasets.Value("string"),
|
| 54 |
+
"age_of_death": datasets.Value("string"),
|
| 55 |
+
}
|
| 56 |
+
),
|
| 57 |
+
supervised_keys=None,
|
| 58 |
+
homepage="https://github.com/Moradnejad/AgeDataset",
|
| 59 |
+
citation="https://workshop-proceedings.icwsm.org/pdf/2022_82.pdf",
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
def _split_generators(
|
| 63 |
+
self, dl_manager: DownloadManager
|
| 64 |
+
) -> Iterable[datasets.SplitGenerator]:
|
| 65 |
+
git_repo = AVAILABLE_DATASETS[self.config.name]
|
| 66 |
+
current_dir = Path(__file__).resolve().parent
|
| 67 |
+
temp_dir = current_dir / uuid.uuid4().hex
|
| 68 |
+
temp_dir.mkdir(exist_ok=True)
|
| 69 |
+
# Repository(local_dir=str(temp_dir), clone_from=git_repo)
|
| 70 |
+
os.system(f"cd {temp_dir} && git clone {git_repo}")
|
| 71 |
+
shutil.copy(temp_dir / "AgeDataset" / "AgeDataset.rar", current_dir)
|
| 72 |
+
shutil.rmtree(f"{temp_dir}")
|
| 73 |
+
filepath = str(current_dir / "AgeDataset.rar")
|
| 74 |
+
filepath = os.path.join(dl_manager.download_and_extract(filepath), "AgeDataset.csv")
|
| 75 |
+
|
| 76 |
+
# There is no predefined train/val/test split for this dataset.
|
| 77 |
+
return [
|
| 78 |
+
datasets.SplitGenerator(
|
| 79 |
+
name=datasets.Split.TRAIN,
|
| 80 |
+
gen_kwargs={
|
| 81 |
+
"filepath": filepath,
|
| 82 |
+
},
|
| 83 |
+
),
|
| 84 |
+
]
|
| 85 |
+
|
| 86 |
+
def _generate_examples(self, filepath: str) -> Iterable[Dict]:
|
| 87 |
+
with open(filepath, encoding="utf-8") as f_in:
|
| 88 |
+
csv_reader = csv.reader(
|
| 89 |
+
f_in,
|
| 90 |
+
delimiter=",",
|
| 91 |
+
)
|
| 92 |
+
for idx, row in enumerate(csv_reader):
|
| 93 |
+
if idx == 0:
|
| 94 |
+
continue
|
| 95 |
+
yield idx, {
|
| 96 |
+
"entity_id": idx,
|
| 97 |
+
"name": row[0],
|
| 98 |
+
"short_description": row[1],
|
| 99 |
+
"gender": row[2],
|
| 100 |
+
"country": row[3],
|
| 101 |
+
"occupation": row[4],
|
| 102 |
+
"birth_year": row[5],
|
| 103 |
+
"manner_of_death": row[6],
|
| 104 |
+
"age_of_death": row[7],
|
| 105 |
+
}
|