age_dataset / age_dataset_generate.py
bstds's picture
Rename age_dataset.py to age_dataset_generate.py
bb0d401
raw
history blame contribute delete
No virus
3.66 kB
#!/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],
}