| import os |
| import re |
| import datasets |
| from huggingface_hub import HfFileSystem |
| from typing import List |
|
|
| logger = datasets.logging.get_logger(name=__name__) |
| fs = HfFileSystem() |
|
|
| _CITATION = """ |
| """ |
|
|
| _DESCRIPTION = """This dataset contain file about datetime date. |
| It's created with purpose is practice loading dataset from hugging face hub.""" |
|
|
|
|
| _HOMEPAGE = """ |
| """ |
|
|
| _REPO = "datasets/nguyenminh4099/date-data" |
| _BRANCH = "main" |
| _REPO_BRANCH = f"{_REPO}@{_BRANCH}" |
|
|
| _REPO_URL = f"https://huggingface.co/{_REPO}/resolve/{_BRANCH}" |
| _URL = os.path.join(_REPO_URL, "data", "{filename}.zip") |
|
|
| _CONFIGS = ['all'] |
| _CONFIGS.extend( |
| os.path.basename(file)[:-4] |
| for file in fs.listdir(_REPO_BRANCH + "/data/", detail=False) |
| if file.endswith('.zip') |
| ) |
|
|
| class DateDataConfig(datasets.BuilderConfig): |
| def __init__( |
| self, |
| name: str, |
| **kwargs, |
| ): |
| super(DateDataConfig, self).__init__( |
| name=name, |
| version=datasets.Version("1.0.0"), |
| description=_DESCRIPTION, |
| ) |
|
|
| class DateDate(datasets.GeneratorBasedBuilder): |
| BUILDER_CONFIGS = [DateDataConfig(name=name) for name in _CONFIGS] |
| DEFAULT_CONFIG_NAME = 'all' |
|
|
| def _info(self) -> datasets.DatasetInfo: |
| features = datasets.Features({ |
| "id": datasets.Value('string'), |
| "dow": datasets.Value('string'), |
| "month": datasets.Value('string'), |
| "dom": datasets.Value('string'), |
| "hour": datasets.Value('string'), |
| "min": datasets.Value('string'), |
| "second": datasets.Value('string'), |
| "timezone": datasets.Value('string'), |
| "year": datasets.Value('string'), |
| "file_path": datasets.Value('string'), |
| }) |
| return datasets.DatasetInfo( |
| features=features, |
| description=_DESCRIPTION, |
| citation=_CITATION, |
| homepage=_HOMEPAGE, |
| ) |
|
|
| def _split_generators( |
| self, |
| dl_manager: datasets.DownloadManager, |
| ) -> List[datasets.SplitGenerator]: |
| config_names = _CONFIGS[1:] if self.config.name == 'all' else [self.config.name] |
| print("_split_generators ", config_names[:3]) |
|
|
| data_dirs = dl_manager.download_and_extract( |
| [_URL.format(filename=zipfile) for zipfile in config_names] |
| ) |
| |
| return [ |
| datasets.SplitGenerator( |
| name=datasets.Split.TRAIN, |
| gen_kwargs={ |
| "data_dirs": data_dirs, |
| } |
| ) |
| ] |
|
|
| def _generate_examples( |
| self, |
| data_dirs: List[str], |
| ) -> dict: |
| for i, data_dir in enumerate(data_dirs): |
| sample = self._extract_datetime("Wed Oct 16 11:08:00 +07 2024") |
| sample.update((('id','123'),('file_path',"null"))) |
| yield i, sample |
|
|
| def _read_txt( |
| self, |
| file_name: str, |
| ) -> str: |
| with open(file=file_name, mode='r') as f: |
| return self._extract_datetime(f.read()) |
| |
| def _extract_datetime( |
| self, |
| datetime_string: str, |
| ) -> dict: |
| datetime_string = datetime_string.strip("./ ") |
| components = re.split(pattern=r'[\s\:]+', string=datetime_string) |
|
|
| return { |
| "dow": components[0], |
| "month": components[1], |
| "dom": components[2], |
| "hour": components[3], |
| "min": components[4], |
| "second": components[5], |
| "timezone": components[6], |
| "year": components[7], |
| } |
|
|