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CRIPTION = """\
|
| 5 |
+
Train, validation and test splits for TED talks as in http://phontron.com/data/ted_talks.tar.gz (detokenized)
|
| 6 |
+
"""
|
| 7 |
+
|
| 8 |
+
_CITATION = """\
|
| 9 |
+
@inproceedings{Ye2018WordEmbeddings,
|
| 10 |
+
author = {Ye, Qi and Devendra, Sachan and Matthieu, Felix and Sarguna, Padmanabhan and Graham, Neubig},
|
| 11 |
+
title = {When and Why are pre-trained word embeddings useful for Neural Machine Translation},
|
| 12 |
+
booktitle = {HLT-NAACL},
|
| 13 |
+
year = {2018},
|
| 14 |
+
}
|
| 15 |
+
"""
|
| 16 |
+
|
| 17 |
+
_DATA_URL = "data/TED.tar"
|
| 18 |
+
|
| 19 |
+
_LANGUAGES = ["ar", "az", "be", "bg", "bn", "bs", "cs", "da", "de", "el", "en", "eo", "es", "et", "eu", "fa", "fi", "fr", "fr-ca", "gl", "he", "hi", "hr", "hu", "hy", "id", "it", "ja", "ka", "kk", "ko", "ku", "lt", "mk", "mn", "mr", "ms", "my", "nb", "nl", "pl", "pt", "pt-br", "ro", "ru", "sk", "sl", "sq", "sr", "sv", "ta", "th", "tr", "uk", "ur", "vi", "zh", "zh-cn", "zh-tw"]
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
class TedTalksConfig(datasets.BuilderConfig):
|
| 23 |
+
"""BuilderConfig for TED talk dataset."""
|
| 24 |
+
|
| 25 |
+
def __init__(self, language_pair=(None, None), **kwargs):
|
| 26 |
+
# sort such that az_ar is same as ar_az
|
| 27 |
+
self.language_pair = sorted(language_pair)
|
| 28 |
+
self.source, self.target = self.language_pair[0], self.language_pair[1]
|
| 29 |
+
|
| 30 |
+
name = f"{self.source}_{self.target}"
|
| 31 |
+
description = f"Parallel sentences in `{self.source}` and `{self.target}`."
|
| 32 |
+
super(TedTalksConfig, self).__init__(name=name, description=description, **kwargs)
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
class TedTalks(datasets.GeneratorBasedBuilder):
|
| 36 |
+
"""TED talk data from http://phontron.com/data/ted_talks.tar.gz."""
|
| 37 |
+
|
| 38 |
+
unique_pairs = sorted(set([
|
| 39 |
+
"_".join(sorted([l1, l2]))
|
| 40 |
+
for l1 in _LANGUAGES
|
| 41 |
+
for l2 in _LANGUAGES
|
| 42 |
+
if l1 != l2
|
| 43 |
+
]))
|
| 44 |
+
|
| 45 |
+
BUILDER_CONFIGS = [
|
| 46 |
+
TedTalksConfig(
|
| 47 |
+
language_pair=(pair.split("_")[0], pair.split("_")[1]),
|
| 48 |
+
version=datasets.Version("1.0.0", ""),
|
| 49 |
+
)
|
| 50 |
+
for pair in unique_pairs
|
| 51 |
+
]
|
| 52 |
+
|
| 53 |
+
def _info(self):
|
| 54 |
+
return datasets.DatasetInfo(
|
| 55 |
+
description=_DESCRIPTION,
|
| 56 |
+
features=datasets.Features(
|
| 57 |
+
{"translation": datasets.features.Translation(languages=self.config.language_pair)}
|
| 58 |
+
),
|
| 59 |
+
homepage="https://github.com/neulab/word-embeddings-for-nmt",
|
| 60 |
+
citation=_CITATION,
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
def _split_generators(self, dl_manager):
|
| 64 |
+
archive = dl_manager.download(_DATA_URL)
|
| 65 |
+
|
| 66 |
+
def _get_overlap(source_file, target_file):
|
| 67 |
+
for path, f in dl_manager.iter_archive(archive):
|
| 68 |
+
if path == source_file:
|
| 69 |
+
source_sentences = f.read().decode("utf-8").split("\n")
|
| 70 |
+
elif path == target_file:
|
| 71 |
+
target_sentences = f.read().decode("utf-8").split("\n")
|
| 72 |
+
|
| 73 |
+
return len([
|
| 74 |
+
(src, tgt)
|
| 75 |
+
for src, tgt
|
| 76 |
+
in zip(source_sentences, target_sentences)
|
| 77 |
+
if src != "" and tgt != ""
|
| 78 |
+
])
|
| 79 |
+
|
| 80 |
+
split2tedsplit = {"train": "train", "validation": "dev", "test": "test"}
|
| 81 |
+
|
| 82 |
+
overlap = {
|
| 83 |
+
split: _get_overlap(
|
| 84 |
+
f"{split}/ted.{split2tedsplit[split]}.{self.config.source}",
|
| 85 |
+
f"{split}/ted.{split2tedsplit[split]}.{self.config.target}"
|
| 86 |
+
) for split in ["train", "validation", "test"]
|
| 87 |
+
}
|
| 88 |
+
|
| 89 |
+
generators = []
|
| 90 |
+
if overlap["train"] > 0:
|
| 91 |
+
generators.append(
|
| 92 |
+
datasets.SplitGenerator(
|
| 93 |
+
name=datasets.Split.TRAIN,
|
| 94 |
+
gen_kwargs={
|
| 95 |
+
"source_file": f"train/ted.train.{self.config.source}",
|
| 96 |
+
"target_file": f"train/ted.train.{self.config.target}",
|
| 97 |
+
"files": dl_manager.iter_archive(archive),
|
| 98 |
+
},
|
| 99 |
+
),
|
| 100 |
+
)
|
| 101 |
+
if overlap["validation"] > 0:
|
| 102 |
+
generators.append(
|
| 103 |
+
datasets.SplitGenerator(
|
| 104 |
+
name=datasets.Split.VALIDATION,
|
| 105 |
+
gen_kwargs={
|
| 106 |
+
"source_file": f"validation/ted.dev.{self.config.source}",
|
| 107 |
+
"target_file": f"validation/ted.dev.{self.config.target}",
|
| 108 |
+
"files": dl_manager.iter_archive(archive),
|
| 109 |
+
},
|
| 110 |
+
),
|
| 111 |
+
)
|
| 112 |
+
if overlap["test"] > 0:
|
| 113 |
+
generators.append(
|
| 114 |
+
datasets.SplitGenerator(
|
| 115 |
+
name=datasets.Split.TEST,
|
| 116 |
+
gen_kwargs={
|
| 117 |
+
"source_file": f"test/ted.test.{self.config.source}",
|
| 118 |
+
"target_file": f"test/ted.test.{self.config.target}",
|
| 119 |
+
"files": dl_manager.iter_archive(archive),
|
| 120 |
+
},
|
| 121 |
+
),
|
| 122 |
+
)
|
| 123 |
+
|
| 124 |
+
return generators
|
| 125 |
+
|
| 126 |
+
|
| 127 |
+
def _generate_examples(self, source_file, target_file, files):
|
| 128 |
+
"""Returns examples as raw text."""
|
| 129 |
+
|
| 130 |
+
source_sentences, target_sentences = None, None
|
| 131 |
+
for path, f in files:
|
| 132 |
+
if path == source_file:
|
| 133 |
+
source_sentences = f.read().decode("utf-8").split("\n")
|
| 134 |
+
elif path == target_file:
|
| 135 |
+
target_sentences = f.read().decode("utf-8").split("\n")
|
| 136 |
+
|
| 137 |
+
assert len(target_sentences) == len(source_sentences), (
|
| 138 |
+
f"Sizes do not match: {len(source_sentences)} vs {len(target_sentences)}."
|
| 139 |
+
)
|
| 140 |
+
|
| 141 |
+
# ignore empty
|
| 142 |
+
source_target_pairs = [
|
| 143 |
+
(src, tgt)
|
| 144 |
+
for src, tgt
|
| 145 |
+
in zip(source_sentences, target_sentences)
|
| 146 |
+
if src != "" and tgt != ""
|
| 147 |
+
]
|
| 148 |
+
|
| 149 |
+
if len(source_target_pairs) > 0:
|
| 150 |
+
source_sentences, target_sentences = zip(*source_target_pairs)
|
| 151 |
+
|
| 152 |
+
for idx, (l1, l2) in enumerate(zip(source_sentences, target_sentences)):
|
| 153 |
+
yield idx, {"translation": {self.config.source: l1, self.config.target: l2}}
|