File size: 3,422 Bytes
85a38c8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import os
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_PATH}/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('int'),
            "min": datasets.Value('int'),
            "second": datasets.Value('int'),
            "timezone": datasets.Value('string'),
            "year": datasets.Value('int'),
            "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]

        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 data_dir in data_dirs:
            print(data_dir)
            yield self._extract_datetime("Wed Oct 16 11:08:00 +07 2024").update((('id','123'),('file_path',"null")))
            

    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 = datetime_string.split(' ')

        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],
        }