File size: 5,051 Bytes
e1c470c 2890d00 e1c470c 0f3636f e1c470c e45ebf9 f7a8f5c e45ebf9 f7a8f5c e1c470c |
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 |
# 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.
""" WMT16 English-Romanian Translation Data with further preprocessing """
from __future__ import absolute_import, division, print_function
import csv
import json
import os
import datasets
_CITATION = """\
@InProceedings{huggingface:dataset,
title = {WMT14 English-German Translation Data with further preprocessing},
authors={},
year={2016}
}
"""
_DESCRIPTION = "WMT14 English-German Translation Data with further preprocessing"
_HOMEPAGE = "http://www.statmt.org/wmt16/"
_LICENSE = ""
_DATA_URL = "https://cdn-datasets.huggingface.co/translation/wmt_en_de.tgz"
class Wmt14EnDePreProcessedConfig(datasets.BuilderConfig):
"""BuilderConfig for wmt16."""
def __init__(self, language_pair=(None, None), **kwargs):
"""BuilderConfig for wmt16
Args:
for the `datasets.features.text.TextEncoder` used for the features feature.
language_pair: pair of languages that will be used for translation. Should
contain 2-letter coded strings. First will be used at source and second
as target in supervised mode. For example: ("se", "en").
**kwargs: keyword arguments forwarded to super.
"""
name = "%s%s" % (language_pair[0], language_pair[1])
description = ("Translation dataset from %s to %s") % (language_pair[0], language_pair[1])
super(Wmt14EnDePreProcessedConfig, self).__init__(
name=name,
description=description,
version=datasets.Version("1.1.0", ""),
**kwargs,
)
# Validate language pair.
assert "en" in language_pair, ("Config language pair must contain `en`, got: %s", language_pair)
source, target = language_pair
non_en = source if target == "en" else target
assert non_en in ["de"], ("Invalid non-en language in pair: %s", non_en)
self.language_pair = language_pair
# TODO: Name of the dataset usually match the script name with CamelCase instead of snake_case
class Wmt14EnDePreProcessed(datasets.GeneratorBasedBuilder):
BUILDER_CONFIGS = [
Wmt14EnDePreProcessedConfig(
language_pair=("en", "de"),
),
]
def _info(self):
source, target = self.config.language_pair
return datasets.DatasetInfo(
description=_DESCRIPTION,
features=datasets.Features(
{"translation": datasets.features.Translation(languages=self.config.language_pair)}
),
supervised_keys=(source, target),
homepage=_HOMEPAGE,
citation=_CITATION,
)
def _split_generators(self, dl_manager):
dl_dir = dl_manager.download_and_extract(_DATA_URL)
source, target = self.config.language_pair
non_en = source if target == "en" else target
path_tmpl = "{dl_dir}/wmt_en_de/{split}.{type}"
files = {}
for split in ("train", "val", "test"):
files[split] = {
"source_file": path_tmpl.format(dl_dir=dl_dir, split=split, type="source"),
"target_file": path_tmpl.format(dl_dir=dl_dir, split=split, type="target"),
}
return [
datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs=files["train"]),
datasets.SplitGenerator(name=datasets.Split.VALIDATION, gen_kwargs=files["val"]),
datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs=files["test"]),
]
def _generate_examples(self, source_file, target_file):
"""This function returns the examples in the raw (text) form."""
with open(source_file, mode="rb") as f:
source_sentences = f.read().decode("utf8").split("\n")
with open(target_file, mode="rb") as f:
target_sentences = f.read().decode("utf8").split("\n")
assert len(target_sentences) == len(source_sentences), "Sizes do not match: %d vs %d for %s vs %s." % (
len(source_sentences),
len(target_sentences),
source_file,
target_file,
)
source, target = self.config.language_pair
for idx, (l1, l2) in enumerate(zip(source_sentences, target_sentences)):
result = {"translation": {source: l1, target: l2}}
# Make sure that both translations are non-empty.
if all(result.values()):
yield idx, result
|