File size: 5,298 Bytes
513c770
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# coding=utf-8
# Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Cleaned Indonesian split of the KoPI corpus."""
import json
import glob
import gzip
import textwrap
import datasets
import zstandard as zstd
logger = datasets.logging.get_logger(__name__)

_CITATION = """

"""
_DESCRIPTION = """\

"""
_HOMEPAGE = "https://huggingface.co/datasets/munggok/KoPI"
_LICENSE = "CC0"
_BASE_URL = {
    "train":"https://huggingface.co/datasets/munggok/KoPI/resolve/main/data/kopi-{index:012d}.json.zst",
    "val":"https://huggingface.co/datasets/munggok/KoPI/resolve/main/data/kopi-val-{index:012d}.json.zst"

}
_CONFIGS = {
    "tiny": {"train": 10, "validation": 1},
    "small": {"train": 30, "validation": 2},
    "medium": {"train": 55, "validation": 2},
    "large": {"train": 75, "validation": 3},
    "full": {"train": 107, "validation": 4}
}
class KoPIConfig(datasets.BuilderConfig):
    """BuilderConfig for the Clean KoPI corpus."""
    def __init__(self, **kwargs):
        """BuilderConfig for Clean KoPI corpus.
        Args:
            **kwargs: keyword arguments forwarded to super.
        """
        super().__init__(**kwargs)
class KoPI(datasets.GeneratorBasedBuilder):
    """KoPI corpus."""
    BUILDER_CONFIGS = [
        KoPIConfig(
            name="tiny",
            version=datasets.Version("1.0.0"),
            description=textwrap.dedent(
                f"""\
                Tiny version only using 10 shard 
                """
            )
        ),
        KoPIConfig(
            name="small",
            version=datasets.Version("1.0.0"),
            description=textwrap.dedent(
                f"""\
                small version only using 30 shard 
                """
            )
        ),
        KoPIConfig(
            name="medium",
            version=datasets.Version("1.0.0"),
            description=textwrap.dedent(
                f"""\
                medion version only using 50 shard 
                """
            )
        ),
        KoPIConfig(
            name="large",
            version=datasets.Version("1.0.0"),
            description=textwrap.dedent(
                f"""\
                large version only using 75 shard 
                """
            )
        ),
        KoPIConfig(
            name="full",
            version=datasets.Version("1.0.0"),
            description=textwrap.dedent(
                f"""\
                The full cleaned version of KoPI corpus.
                Estimated size of compressed files: 53GB
                """
            )
        )
    ]
    def _info(self):
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=datasets.Features(
                {
                    "text": datasets.Value("string"),
                    "url": datasets.Value("string"),
                    "timestamp": datasets.Value("string"),
                    "meta": datasets.Value("string"),
                }
            ),
            supervised_keys=None,
            homepage=_HOMEPAGE,
            license=_LICENSE,
            citation=_CITATION,
        )
    def _split_generators(self, dl_manager):
        train = [_BASE_URL["train"].format(index=k + 1) for k in range(107)][0:_CONFIGS[self.config.name]['train']]
        validation =  [_BASE_URL["val"].format(index=k + 108) for k in range(4)][0:_CONFIGS[self.config.name]['validation']]
        train_downloaded_files = dl_manager.download(train)
        validation_downloaded_files = dl_manager.download(validation)
        return [
            datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepaths": train_downloaded_files}),
            datasets.SplitGenerator(
                name=datasets.Split.VALIDATION, gen_kwargs={"filepaths": validation_downloaded_files}
            ),
        ]
    def _generate_examples(self, filepaths):
        """This function returns the examples in the raw (text) form by iterating on all the files."""
        id_ = 0
        for filepath in filepaths:
            logger.info(f"Generating examples from {filepath}")
            with zstd.open(open(filepath, "rb"), "rt", encoding="utf-8") as f:
                for line in f:
                    if line:
                        example = json.loads(line)
                        if example.get('meta') is not None:
                            yield id_, {'text':example['text'],'url':example['url'],'timestamp':example['timestamp'],'meta': example['meta']}
                            id_ += 1
                        else:
                            yield id_, {'text':example['text'],'url':example['url'],'timestamp':example['timestamp'],'meta': "None"}
                            id_ += 1