File size: 6,388 Bytes
1fc7f0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b426581
1fc7f0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0e61b7b
1fc7f0a
 
 
 
 
 
 
 
 
0e61b7b
1fc7f0a
 
 
 
 
 
 
 
 
0e61b7b
1fc7f0a
 
 
 
 
 
 
 
 
0e61b7b
1fc7f0a
 
 
 
 
 
 
 
 
0e61b7b
1fc7f0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
523fcd6
1fc7f0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# 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 Italian split of the mC4 corpus."""


import json
import gzip
import textwrap
import datasets

logger = datasets.logging.get_logger(__name__)

_CITATION = """
@article{JMLR:v21:20-074,
  author  = {Colin Raffel and Noam Shazeer and Adam Roberts and Katherine Lee and Sharan Narang and Michael Matena and Yanqi Zhou and Wei Li and Peter J. Liu},
  title   = {Exploring the Limits of Transfer Learning with a Unified Text-to-Text Transformer},
  journal = {Journal of Machine Learning Research},
  year    = {2020},
  volume  = {21},
  number  = {140},
  pages   = {1-67},
  url     = {http://jmlr.org/papers/v21/20-074.html}
}
"""

_DESCRIPTION = """\
A thoroughly cleaned version of the Italian portion of the multilingual 
colossal, cleaned version of Common Crawl's web crawl corpus (mC4) by AllenAI.

Based on Common Crawl dataset: "https://commoncrawl.org".

This is the processed version of Google's mC4 dataset by AllenAI, with further cleaning
detailed in the repository README file.
"""

_HOMEPAGE = "https://github.com/allenai/allennlp/discussions/5056"

_LICENSE = "Open Data Commons Attribution License (ODC-By) v1.0"

_BASE_URL = "https://huggingface.co/datasets/gsarti/clean_mc4_it/resolve/main/clean-mc4-it/c4-it{split_suffix}.tfrecord-{index:05d}-of-{n_shards:05d}.json.gz"

_CONFIGS = {
    "tiny": {"train": 100, "validation": 1},
    "small": {"train": 250, "validation": 2},
    "medium": {"train": 500, "validation": 4},
    "large": {"train": 750, "validation": 6},
    "full": {"train": 1024, "validation": 8}
}


class CleanMc4ItConfig(datasets.BuilderConfig):
    """BuilderConfig for the Clean mC4 Italian."""

    def __init__(self, **kwargs):
        """BuilderConfig for Clean mC4 Italian.
        Args:
            **kwargs: keyword arguments forwarded to super.
        """
        super().__init__(**kwargs)


class Mc4(datasets.GeneratorBasedBuilder):
    """mC4, a colossal, cleaned version of Common Crawl's web crawl corpus."""

    BUILDER_CONFIGS = [
        CleanMc4ItConfig(
            name="tiny",
            version=datasets.Version("1.0.0"),
            description=textwrap.dedent(
                f"""\
                A tiny cleaned version of the Italian portion of the multilingual C4 corpus.
                Estimated size of compressed files: 10GB
                """
            )
        ),
        CleanMc4ItConfig(
            name="small",
            version=datasets.Version("1.0.0"),
            description=textwrap.dedent(
                f"""\
                A small cleaned version of the Italian portion of the multilingual C4 corpus.
                Estimated size of compressed files: 25GB
                """
            )
        ),
        CleanMc4ItConfig(
            name="medium",
            version=datasets.Version("1.0.0"),
            description=textwrap.dedent(
                f"""\
                A medium cleaned version of the Italian portion of the multilingual C4 corpus.
                Estimated size of compressed files: 50GB
                """
            )
        ),
        CleanMc4ItConfig(
            name="large",
            version=datasets.Version("1.0.0"),
            description=textwrap.dedent(
                f"""\
                A large cleaned version of the Italian portion of the multilingual C4 corpus.
                Estimated size of compressed files: 75GB
                """
            )
        ),
        CleanMc4ItConfig(
            name="full",
            version=datasets.Version("1.0.0"),
            description=textwrap.dedent(
                f"""\
                The full cleaned version of the Italian portion of the multilingual C4 corpus.
                Estimated size of compressed files: 103GB
                """
            )
        )
    ]

    def _info(self):
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=datasets.Features(
                {
                    "text": datasets.Value("string"),
                    "timestamp": datasets.Value("string"),
                    "url": datasets.Value("string"),
                }
            ),
            supervised_keys=None,
            homepage=_HOMEPAGE,
            license=_LICENSE,
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager):
        data_urls = {}
        for split in ["train", "validation"]:
            data_urls[split] = [
                _BASE_URL.format(
                    split_suffix="-validation" if split == "validation" else "",
                    index=index,
                    n_shards=8 if split == "validation" else 1024,
                )
                for index in range(_CONFIGS[self.config.name][split])
            ]
        train_downloaded_files = dl_manager.download(data_urls["train"])
        validation_downloaded_files = dl_manager.download(data_urls["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 gzip.open(open(filepath, "rb"), "rt", encoding="utf-8") as f:
                for line in f:
                    if line:
                        example = json.loads(line)
                        yield id_, example
                        id_ += 1