Datasets:
File size: 6,595 Bytes
10937c7 |
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 |
# 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.
"""United nations general assembly resolutions: A six-language parallel corpus"""
import os
import datasets
_CITATION = """\
@inproceedings{title = "United Nations General Assembly Resolutions: a six-language parallel corpus",
abstract = "In this paper we describe a six-ways parallel public-domain corpus consisting of 2100 United Nations General Assembly Resolutions with translations in the six official languages of the United Nations, with an average of around 3 million tokens per language. The corpus is available in a preprocessed, formatting-normalized TMX format with paragraphs aligned across multiple languages. We describe the background to the corpus and its content, the process of its construction, and some of its interesting properties.",
author = "Alexandre Rafalovitch and Robert Dale",
year = "2009",
language = "English",
booktitle = "MT Summit XII proceedings",
publisher = "International Association of Machine Translation",
}"""
_HOMEPAGE = "http://opus.nlpl.eu/UN.php"
_LICENSE = ""
_VALID_LANGUAGE_PAIRS = {
("ar", "en"): "http://opus.nlpl.eu/download.php?f=UN/v20090831/moses/ar-en.txt.zip",
("ar", "es"): "http://opus.nlpl.eu/download.php?f=UN/v20090831/moses/ar-es.txt.zip",
("ar", "fr"): "http://opus.nlpl.eu/download.php?f=UN/v20090831/moses/ar-fr.txt.zip",
("ar", "ru"): "http://opus.nlpl.eu/download.php?f=UN/v20090831/moses/ar-ru.txt.zip",
("ar", "zh"): "http://opus.nlpl.eu/download.php?f=UN/v20090831/moses/ar-zh.txt.zip",
("en", "es"): "http://opus.nlpl.eu/download.php?f=UN/v20090831/moses/en-es.txt.zip",
("en", "fr"): "http://opus.nlpl.eu/download.php?f=UN/v20090831/moses/en-fr.txt.zip",
("en", "ru"): "http://opus.nlpl.eu/download.php?f=UN/v20090831/moses/en-ru.txt.zip",
("en", "zh"): "http://opus.nlpl.eu/download.php?f=UN/v20090831/moses/en-zh.txt.zip",
("es", "fr"): "http://opus.nlpl.eu/download.php?f=UN/v20090831/moses/es-fr.txt.zip",
("es", "ru"): "http://opus.nlpl.eu/download.php?f=UN/v20090831/moses/es-ru.txt.zip",
("es", "zh"): "http://opus.nlpl.eu/download.php?f=UN/v20090831/moses/es-zh.txt.zip",
("fr", "ru"): "http://opus.nlpl.eu/download.php?f=UN/v20090831/moses/fr-ru.txt.zip",
("fr", "zh"): "http://opus.nlpl.eu/download.php?f=UN/v20090831/moses/fr-zh.txt.zip",
("ru", "zh"): "http://opus.nlpl.eu/download.php?f=UN/v20090831/moses/ru-zh.txt.zip",
}
_VERSION = "2.0.0"
_DESCRIPTION = """\
United nations general assembly resolutions: A six-language parallel corpus.
This is a collection of translated documents from the United Nations originally compiled into a translation memory by Alexandre Rafalovitch, Robert Dale (see http://uncorpora.org).
6 languages, 15 bitexts
total number of files: 6
total number of tokens: 18.87M
total number of sentence fragments: 0.44M
"""
_BASE_NAME = "UN.{}-{}.{}"
class UnGaConfig(datasets.BuilderConfig):
"""BuilderConfig for United nations general assembly resolutions: A six-language parallel corpus"""
def __init__(self, language_pair=(None, None), **kwargs):
"""BuilderConfig for United nations general assembly resolutions: A six-language parallel corpus.
The first language in `language_pair` should consist of two strings joined by
an underscore (e.g. "en-tr").
Args:
language_pair: pair of languages that will be used for translation.
**kwargs: keyword arguments forwarded to super.
"""
name = "%s_to_%s" % (language_pair[0], language_pair[1])
description = ("Translation dataset from %s to %s or %s to %s.") % (
language_pair[0],
language_pair[1],
language_pair[1],
language_pair[0],
)
super(UnGaConfig, self).__init__(
name=name, description=description, version=datasets.Version(_VERSION, ""), **kwargs
)
# Validate language pair.
assert language_pair in _VALID_LANGUAGE_PAIRS, (
"Config language pair (%s, " "%s) not supported"
) % language_pair
self.language_pair = language_pair
class UnGa(datasets.GeneratorBasedBuilder):
BUILDER_CONFIGS = [
UnGaConfig(
language_pair=pair,
)
for pair in _VALID_LANGUAGE_PAIRS.keys()
]
BUILDER_CONFIG_CLASS = UnGaConfig
def _info(self):
return datasets.DatasetInfo(
description=_DESCRIPTION,
features=datasets.Features(
{
"id": datasets.Value("string"),
"translation": datasets.Translation(languages=tuple(self.config.language_pair)),
},
),
supervised_keys=None,
homepage=_HOMEPAGE,
citation=_CITATION,
)
def _split_generators(self, dl_manager):
download_url = _VALID_LANGUAGE_PAIRS.get(tuple(self.config.language_pair))
path = dl_manager.download_and_extract(download_url)
return [
datasets.SplitGenerator(
name=datasets.Split.TRAIN,
gen_kwargs={"datapath": path},
)
]
def _generate_examples(self, datapath):
lang1, lang2 = self.config.language_pair
lang1_file = _BASE_NAME.format(lang1, lang2, lang1)
lang2_file = _BASE_NAME.format(lang1, lang2, lang2)
lang1_path = os.path.join(datapath, lang1_file)
lang2_path = os.path.join(datapath, lang2_file)
with open(lang1_path, encoding="utf-8") as f1, open(lang2_path, encoding="utf-8") as f2:
for sentence_counter, (x, y) in enumerate(zip(f1, f2)):
x = x.strip()
y = y.strip()
result = (
sentence_counter,
{
"id": str(sentence_counter),
"translation": {lang1: x, lang2: y},
},
)
yield result
|