Upload date-data.py with huggingface_hub
#2
by
nguyenminh4099
- opened
- date-data.py +128 -0
date-data.py
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import datasets
|
| 3 |
+
from huggingface_hub import HfFileSystem
|
| 4 |
+
from typing import List
|
| 5 |
+
|
| 6 |
+
logger = datasets.logging.get_logger(name=__name__)
|
| 7 |
+
fs = HfFileSystem()
|
| 8 |
+
|
| 9 |
+
_CITATION = """
|
| 10 |
+
"""
|
| 11 |
+
|
| 12 |
+
_DESCRIPTION = """
|
| 13 |
+
This dataset contain file about datetime date.
|
| 14 |
+
It's created with purpose is practice loading dataset from hugging face hub.
|
| 15 |
+
"""
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
_HOMEPAGE = """
|
| 19 |
+
"""
|
| 20 |
+
|
| 21 |
+
_REPO = "datasets/nguyenminh4099/date-data"
|
| 22 |
+
_BRANCH = "main"
|
| 23 |
+
_REPO_BRANCH = f"{_REPO}@{_BRANCH}"
|
| 24 |
+
|
| 25 |
+
_REPO_URL = f"https://huggingface.co/{_REPO_PATH}/resolve/{_BRANCH}"
|
| 26 |
+
_URL = os.path.join(_REPO_URL, "data", "{filename}.zip")
|
| 27 |
+
|
| 28 |
+
_CONFIGS = ['all']
|
| 29 |
+
_CONFIGS.extend(
|
| 30 |
+
os.path.basename(file)[:-4]
|
| 31 |
+
for file in fs.listdir(_REPO_BRANCH + "data", detail=False)
|
| 32 |
+
if file.endswith('.zip')
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
class DateDataConfig(datasets.BuilderConfig):
|
| 36 |
+
def __init__(
|
| 37 |
+
self,
|
| 38 |
+
name: str,
|
| 39 |
+
**kwargs,
|
| 40 |
+
):
|
| 41 |
+
super(DateDataConfig, self).__init__(
|
| 42 |
+
name=name,
|
| 43 |
+
version=datasets.Version("1.0.0"),
|
| 44 |
+
description=_DESCRIPTION,
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
class DateDate(datasets.GeneratorBasedBuilder):
|
| 48 |
+
BUILDER_CONFIGS = [DateDataConfig(name=name) for name in _CONFIGS]
|
| 49 |
+
DEFAULT_CONFIG_NAME = 'all'
|
| 50 |
+
|
| 51 |
+
def info(self) -> datasets.DatasetInfo:
|
| 52 |
+
features = datasets.Features({
|
| 53 |
+
"id": datasets.Value('string'),
|
| 54 |
+
"dow": datasets.Value('string'),
|
| 55 |
+
"month": datasets.Value('string'),
|
| 56 |
+
"dom": datasets.Value('string'),
|
| 57 |
+
"hour": datasets.Value('int'),
|
| 58 |
+
"min": datasets.Value('int'),
|
| 59 |
+
"second": datasets.Value('int'),
|
| 60 |
+
"timezone": datasets.Value('string'),
|
| 61 |
+
"year": datasets.Value('int'),
|
| 62 |
+
"file_path": datasets.Value('string'),
|
| 63 |
+
})
|
| 64 |
+
|
| 65 |
+
return datasets.DatasetInfo(
|
| 66 |
+
features=features,
|
| 67 |
+
description=_DESCRIPTION,
|
| 68 |
+
citation=_CITATION,
|
| 69 |
+
homepage=_HOMEPAGE,
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
def _split_generators(
|
| 73 |
+
self,
|
| 74 |
+
dl_manager: datasets.DownloadManager,
|
| 75 |
+
) -> List[datasets.SplitGenerator]:
|
| 76 |
+
config_names = _CONFIGS[1:] if self.config.name == 'all' else [self.config.name]
|
| 77 |
+
|
| 78 |
+
data_dirs = dl_manager.download_and_extract(
|
| 79 |
+
[_URL.format(filename=zipfile) for zipfile in config_names]
|
| 80 |
+
)
|
| 81 |
+
|
| 82 |
+
return datasets.SplitGenerator(
|
| 83 |
+
name=datasets.Split.TRAIN,
|
| 84 |
+
gen_kwargs={
|
| 85 |
+
"data_dirs": data_dirs,
|
| 86 |
+
}
|
| 87 |
+
)
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
def _generate_examples(
|
| 91 |
+
self,
|
| 92 |
+
data_dirs: List[str],
|
| 93 |
+
) -> dict:
|
| 94 |
+
for data_dir in data_dirs:
|
| 95 |
+
print(data_dir)
|
| 96 |
+
yield self._extract_datetime("Wed Oct 16 11:08:00 +07 2024").update((('id','123'),('file_path',"null")))
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
def _read_txt(
|
| 100 |
+
self,
|
| 101 |
+
file_name: str,
|
| 102 |
+
) -> str:
|
| 103 |
+
with open(file=file_name, mode='r') as f:
|
| 104 |
+
return self._extract_datetime(f.read())
|
| 105 |
+
|
| 106 |
+
|
| 107 |
+
def _extract_datetime(
|
| 108 |
+
self,
|
| 109 |
+
datetime_string: str,
|
| 110 |
+
) -> dict:
|
| 111 |
+
datetime_string = datetime_string.strip("./ ")
|
| 112 |
+
components = datetime_string.split(' ')
|
| 113 |
+
|
| 114 |
+
return {
|
| 115 |
+
"dow": components[0],
|
| 116 |
+
"month": components[1],
|
| 117 |
+
"dom": components[2],
|
| 118 |
+
"hour": components[3],
|
| 119 |
+
"min": components[4],
|
| 120 |
+
"second": components[5],
|
| 121 |
+
"timezone": components[6],
|
| 122 |
+
"year": components[7],
|
| 123 |
+
}
|
| 124 |
+
|
| 125 |
+
|
| 126 |
+
|
| 127 |
+
|
| 128 |
+
|