Datasets:

Modalities:
Text
Formats:
parquet
ArXiv:
Libraries:
Datasets
pandas
License:
File size: 6,939 Bytes
8f48d18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
306aca3
8f48d18
 
 
 
 
 
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# 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.
"""OPUS-100"""


import os

import datasets


_CITATION = """\
@misc{zhang2020improving,
      title={Improving Massively Multilingual Neural Machine Translation and Zero-Shot Translation},
      author={Biao Zhang and Philip Williams and Ivan Titov and Rico Sennrich},
      year={2020},
      eprint={2004.11867},
      archivePrefix={arXiv},
      primaryClass={cs.CL}
}
"""

_DESCRIPTION = """\
OPUS-100 is English-centric, meaning that all training pairs include English on either the source or target side.
The corpus covers 100 languages (including English).OPUS-100 contains approximately 55M sentence pairs.
Of the 99 language pairs, 44 have 1M sentence pairs of training data, 73 have at least 100k, and 95 have at least 10k.
"""

_URL = {
    "supervised": "https://object.pouta.csc.fi/OPUS-100/v1.0/opus-100-corpus-{}-v1.0.tar.gz",
    "zero-shot": "https://object.pouta.csc.fi/OPUS-100/v1.0/opus-100-corpus-zeroshot-v1.0.tar.gz",
}

_SupervisedLanguagePairs = [
    "af-en",
    "am-en",
    "an-en",
    "ar-en",
    "as-en",
    "az-en",
    "be-en",
    "bg-en",
    "bn-en",
    "br-en",
    "bs-en",
    "ca-en",
    "cs-en",
    "cy-en",
    "da-en",
    "de-en",
    "dz-en",
    "el-en",
    "en-eo",
    "en-es",
    "en-et",
    "en-eu",
    "en-fa",
    "en-fi",
    "en-fr",
    "en-fy",
    "en-ga",
    "en-gd",
    "en-gl",
    "en-gu",
    "en-ha",
    "en-he",
    "en-hi",
    "en-hr",
    "en-hu",
    "en-hy",
    "en-id",
    "en-ig",
    "en-is",
    "en-it",
    "en-ja",
    "en-ka",
    "en-kk",
    "en-km",
    "en-ko",
    "en-kn",
    "en-ku",
    "en-ky",
    "en-li",
    "en-lt",
    "en-lv",
    "en-mg",
    "en-mk",
    "en-ml",
    "en-mn",
    "en-mr",
    "en-ms",
    "en-mt",
    "en-my",
    "en-nb",
    "en-ne",
    "en-nl",
    "en-nn",
    "en-no",
    "en-oc",
    "en-or",
    "en-pa",
    "en-pl",
    "en-ps",
    "en-pt",
    "en-ro",
    "en-ru",
    "en-rw",
    "en-se",
    "en-sh",
    "en-si",
    "en-sk",
    "en-sl",
    "en-sq",
    "en-sr",
    "en-sv",
    "en-ta",
    "en-te",
    "en-tg",
    "en-th",
    "en-tk",
    "en-tr",
    "en-tt",
    "en-ug",
    "en-uk",
    "en-ur",
    "en-uz",
    "en-vi",
    "en-wa",
    "en-xh",
    "en-yi",
    "en-yo",
    "en-zh",
    "en-zu",
]

_0shotLanguagePairs = [
    "ar-de",
    "ar-fr",
    "ar-nl",
    "ar-ru",
    "ar-zh",
    "de-fr",
    "de-nl",
    "de-ru",
    "de-zh",
    "fr-nl",
    "fr-ru",
    "fr-zh",
    "nl-ru",
    "nl-zh",
    "ru-zh",
]


class Opus100Config(datasets.BuilderConfig):
    """BuilderConfig for Opus100"""

    def __init__(self, language_pair, **kwargs):
        super().__init__(**kwargs)
        """

        Args:
            language_pair: language pair, you want to load
            **kwargs: keyword arguments forwarded to super.
        """
        self.language_pair = language_pair


class Opus100(datasets.GeneratorBasedBuilder):
    """OPUS-100 is English-centric, meaning that all training pairs include English on either the source or target side."""

    VERSION = datasets.Version("1.0.0")

    BUILDER_CONFIG_CLASS = Opus100Config
    BUILDER_CONFIGS = [
        Opus100Config(name=pair, description=_DESCRIPTION, language_pair=pair)
        for pair in _SupervisedLanguagePairs + _0shotLanguagePairs
    ]

    def _info(self):
        src_tag, tgt_tag = self.config.language_pair.split("-")
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=datasets.Features({"translation": datasets.features.Translation(languages=(src_tag, tgt_tag))}),
            supervised_keys=(src_tag, tgt_tag),
            homepage="http://opus.nlpl.eu/opus-100.php",
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager):

        lang_pair = self.config.language_pair
        src_tag, tgt_tag = lang_pair.split("-")

        domain = "supervised"
        if lang_pair in _0shotLanguagePairs:
            domain = "zero-shot"

        if domain == "supervised":
            dl_dir = dl_manager.download_and_extract(_URL["supervised"].format(lang_pair))
        elif domain == "zero-shot":
            dl_dir = dl_manager.download_and_extract(_URL["zero-shot"])

        data_dir = os.path.join(dl_dir, os.path.join("opus-100-corpus", "v1.0", domain, lang_pair))
        output = []

        test = datasets.SplitGenerator(
            name=datasets.Split.TEST,
            # These kwargs will be passed to _generate_examples
            gen_kwargs={
                "filepath": os.path.join(data_dir, f"opus.{lang_pair}-test.{src_tag}"),
                "labelpath": os.path.join(data_dir, f"opus.{lang_pair}-test.{tgt_tag}"),
            },
        )

        if f"opus.{lang_pair}-test.{src_tag}" in os.listdir(data_dir):
            output.append(test)

        if domain == "supervised":

            train = datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={
                    "filepath": os.path.join(data_dir, f"opus.{lang_pair}-train.{src_tag}"),
                    "labelpath": os.path.join(data_dir, f"opus.{lang_pair}-train.{tgt_tag}"),
                },
            )

            if f"opus.{lang_pair}-train.{src_tag}" in os.listdir(data_dir):
                output.append(train)

            valid = datasets.SplitGenerator(
                name=datasets.Split.VALIDATION,
                # These kwargs will be passed to _generate_examples
                gen_kwargs={
                    "filepath": os.path.join(data_dir, f"opus.{lang_pair}-dev.{src_tag}"),
                    "labelpath": os.path.join(data_dir, f"opus.{lang_pair}-dev.{tgt_tag}"),
                },
            )

            if f"opus.{lang_pair}-dev.{src_tag}" in os.listdir(data_dir):
                output.append(valid)

        return output

    def _generate_examples(self, filepath, labelpath):
        """Yields examples."""
        src_tag, tgt_tag = self.config.language_pair.split("-")
        with open(filepath, encoding="utf-8") as f1, open(labelpath, encoding="utf-8") as f2:
            src = f1.read().split("\n")[:-1]
            tgt = f2.read().split("\n")[:-1]
            for idx, (s, t) in enumerate(zip(src, tgt)):
                yield idx, {"translation": {src_tag: s, tgt_tag: t}}