File size: 5,218 Bytes
a34b765
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ae64f08
a34b765
 
 
ae64f08
a34b765
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ce19054
a34b765
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8fb2a3d
 
 
a34b765
 
10b7722
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
# 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/duckaiml/Polylingual_Id"
_LICENSE = "CC0"
_BASE_URL = {
    "train":"https://huggingface.co/datasets/duckaiml/Polylingual_Id/resolve/main/{folder}/part-{index:012d}.json.zst",
}
_CONFIGS = {
    "hplt": {"part": 10, "url": "https://huggingface.co/datasets/duckaiml/Polylingual_Id/resolve/main/hplt/part_{index:d}.jsonl.zst"},
    "mc4_und": {"part": 24, "url": "https://huggingface.co/datasets/duckaiml/Polylingual_Id/resolve/main/mc4_und/part_{index:d}.jsonl.zst"},
    "indonesia_crawl": {"part": 10, "url": "https://huggingface.co/datasets/duckaiml/Polylingual_Id/resolve/main/indonesia_crawl/part_{index:d}.jsonl.zst"},
}
class Polylingual_IdConfig(datasets.BuilderConfig):
    """BuilderConfig for the Clean Polylingual_Id corpus."""
    def __init__(self, **kwargs):
        """BuilderConfig for Clean Polylingual_Id corpus.
        Args:
            **kwargs: keyword arguments forwarded to super.
        """
        super().__init__(**kwargs)
class Polylingual_Id(datasets.GeneratorBasedBuilder):
    """KoPI corpus."""
    BUILDER_CONFIGS = [
        Polylingual_IdConfig(
            name="hplt",
            version=datasets.Version("1.0.0"),
            description=textwrap.dedent(
                f"""\
                hplt
                """
            )
        ),
        Polylingual_IdConfig(
            name="mc4_und",
            version=datasets.Version("1.0.0"),
            description=textwrap.dedent(
                f"""\
                mc4_und
                """
            )
        ),
        Polylingual_IdConfig(
            name="indonesia_crawl",
            version=datasets.Version("1.0.0"),
            description=textwrap.dedent(
                f"""\
                indonesia_crawl
                """
            )
        ),
        Polylingual_IdConfig(
            name="full",
            version=datasets.Version("1.0.0"),
            description=textwrap.dedent(
                f"""\
                indonesia-crawl
                """
            )
        )
    ]
    def _info(self):
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=datasets.Features(
                {
                    "text": datasets.Value("string"),
                    "url": datasets.Value("string"),
                }
            ),
            supervised_keys=None,
            homepage=_HOMEPAGE,
            license=_LICENSE,
            citation=_CITATION,
        )
    def _split_generators(self, dl_manager):
        if self.config.name == "full":
            data = ['hplt','mc4_und','indonesia_crawl']
            train = []
            for d in data:
                url = [_CONFIGS[d]['url'].format(index=k + 1) for k in range(_CONFIGS[d]['part'])]
                train.extend(url)
            train_downloaded_files = dl_manager.download(train)
        else:
            train = [_CONFIGS[self.config.name]['url'].format(index=k + 1) for k in range(_CONFIGS[self.config.name]['part'])]
            #train = [_BASE_URL["train"].format(index=k + 1) for k in range(107)][0:_CONFIGS[self.config.name]['train']]
            train_downloaded_files = dl_manager.download(train)
        return [
            datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepaths": train_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('url') is not None:
                            yield id_, {'text':example['text'],'url':example['url']}
                            id_ += 1
                        elif example.get('metadata') is not None:
                            yield id_, {'text':example['text'],'url':example['metadata']['url']}
                            id_ += 1
                        else:
                            yield id_, {'text':example['text'],'url':None}
                            id_ += 1