|
|
|
|
|
import os |
|
import shutil |
|
import uuid |
|
from functools import partial |
|
from pathlib import Path |
|
from typing import Dict, Iterable |
|
|
|
import datasets |
|
from datasets import DatasetDict, DownloadManager, load_dataset |
|
import pandas as pd |
|
|
|
|
|
VERSION = datasets.Version("0.0.1") |
|
|
|
AVAILABLE_DATASETS = { |
|
"job_titles": "https://github.com/jneidel/job-titles" |
|
} |
|
|
|
class JobTitlesDataset(datasets.GeneratorBasedBuilder): |
|
"""JobTitlesDataset 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 = "job_titles") -> DatasetDict: |
|
ds = load_dataset(__file__, data_name_config) |
|
return ds |
|
|
|
def _info(self) -> datasets.DatasetInfo: |
|
return datasets.DatasetInfo( |
|
description="", |
|
features=datasets.Features( |
|
{ |
|
"id": datasets.Value("string"), |
|
"name": datasets.Value("string"), |
|
} |
|
), |
|
supervised_keys=None, |
|
homepage="https://github.com/jneidel/job-titles", |
|
citation="", |
|
) |
|
|
|
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) |
|
os.system(f"cd {temp_dir} && git clone {git_repo}") |
|
shutil.copy(temp_dir / "job-titles" / "job-titles.txt", current_dir) |
|
shutil.rmtree(f"{temp_dir}") |
|
filepath = str(current_dir / "job-titles.txt") |
|
|
|
|
|
return [ |
|
datasets.SplitGenerator( |
|
name=datasets.Split.TRAIN, |
|
gen_kwargs={ |
|
"filepath": filepath |
|
}, |
|
), |
|
] |
|
|
|
def _generate_examples(self, filepath: str) -> Iterable[Dict]: |
|
with open(filepath, "r") as f_in: |
|
for idx, line in enumerate(f_in): |
|
line = line.strip() |
|
yield idx, { |
|
"id": idx, |
|
"name": line |
|
} |
|
|