Create job_titles.py
Browse files- job_titles.py +79 -0
job_titles.py
ADDED
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python3
|
2 |
+
# -*- coding: utf-8 -*-
|
3 |
+
import os
|
4 |
+
import shutil
|
5 |
+
import uuid
|
6 |
+
from functools import partial
|
7 |
+
from pathlib import Path
|
8 |
+
from typing import Dict, Iterable
|
9 |
+
|
10 |
+
import datasets
|
11 |
+
from datasets import DatasetDict, DownloadManager, load_dataset
|
12 |
+
import pandas as pd
|
13 |
+
|
14 |
+
|
15 |
+
VERSION = datasets.Version("0.0.1")
|
16 |
+
|
17 |
+
AVAILABLE_DATASETS = {
|
18 |
+
"job_titles": "https://github.com/jneidel/job-titles"
|
19 |
+
}
|
20 |
+
|
21 |
+
class JobTitlesDataset(datasets.GeneratorBasedBuilder):
|
22 |
+
"""JobTitlesDataset dataset."""
|
23 |
+
|
24 |
+
BUILDER_CONFIGS = [
|
25 |
+
datasets.BuilderConfig(
|
26 |
+
name=data_name, version=VERSION, description=f"{data_name} dataset"
|
27 |
+
)
|
28 |
+
for data_name in AVAILABLE_DATASETS
|
29 |
+
]
|
30 |
+
|
31 |
+
@staticmethod
|
32 |
+
def load(data_name_config: str = "job_titles") -> DatasetDict:
|
33 |
+
ds = load_dataset(__file__, data_name_config)
|
34 |
+
return ds
|
35 |
+
|
36 |
+
def _info(self) -> datasets.DatasetInfo:
|
37 |
+
return datasets.DatasetInfo(
|
38 |
+
description="",
|
39 |
+
features=datasets.Features(
|
40 |
+
{
|
41 |
+
"id": datasets.Value("string"),
|
42 |
+
"name": datasets.Value("string"),
|
43 |
+
}
|
44 |
+
),
|
45 |
+
supervised_keys=None,
|
46 |
+
homepage="https://github.com/jneidel/job-titles",
|
47 |
+
citation="",
|
48 |
+
)
|
49 |
+
|
50 |
+
def _split_generators(
|
51 |
+
self, dl_manager: DownloadManager
|
52 |
+
) -> Iterable[datasets.SplitGenerator]:
|
53 |
+
git_repo = AVAILABLE_DATASETS[self.config.name]
|
54 |
+
current_dir = Path(__file__).resolve().parent
|
55 |
+
temp_dir = current_dir / uuid.uuid4().hex
|
56 |
+
temp_dir.mkdir(exist_ok=True)
|
57 |
+
os.system(f"cd {temp_dir} && git clone {git_repo}")
|
58 |
+
shutil.copy(temp_dir / "job-titles" / "job-titles.txt", current_dir)
|
59 |
+
shutil.rmtree(f"{temp_dir}")
|
60 |
+
filepath = str(current_dir / "job-titles.txt")
|
61 |
+
|
62 |
+
# There is no predefined train/val/test split for this dataset.
|
63 |
+
return [
|
64 |
+
datasets.SplitGenerator(
|
65 |
+
name=datasets.Split.TRAIN,
|
66 |
+
gen_kwargs={
|
67 |
+
"filepath": filepath
|
68 |
+
},
|
69 |
+
),
|
70 |
+
]
|
71 |
+
|
72 |
+
def _generate_examples(self, filepath: str) -> Iterable[Dict]:
|
73 |
+
with open(filepath, "r") as f_in:
|
74 |
+
for idx, line in enumerate(f_in):
|
75 |
+
line = line.strip()
|
76 |
+
yield idx, {
|
77 |
+
"id": idx,
|
78 |
+
"name": line
|
79 |
+
}
|