This repository has been archived by the owner on Jul 7, 2023. It is now read-only.
/
translate_ende.py
153 lines (122 loc) · 4.75 KB
/
translate_ende.py
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
# coding=utf-8
# Copyright 2018 The Tensor2Tensor Authors.
#
# 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.
"""Data generators for translation data-sets."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import os
import tarfile
from tensor2tensor.data_generators import generator_utils
from tensor2tensor.data_generators import problem
from tensor2tensor.data_generators import text_encoder
from tensor2tensor.data_generators import text_problems
from tensor2tensor.data_generators import translate
from tensor2tensor.utils import registry
import tensorflow as tf
_ENDE_TRAIN_DATASETS = [
[
"http://data.statmt.org/wmt18/translation-task/training-parallel-nc-v13.tgz", # pylint: disable=line-too-long
("training-parallel-nc-v13/news-commentary-v13.de-en.en",
"training-parallel-nc-v13/news-commentary-v13.de-en.de")
],
[
"http://www.statmt.org/wmt13/training-parallel-commoncrawl.tgz",
("commoncrawl.de-en.en", "commoncrawl.de-en.de")
],
[
"http://www.statmt.org/wmt13/training-parallel-europarl-v7.tgz",
("training/europarl-v7.de-en.en", "training/europarl-v7.de-en.de")
],
]
_ENDE_TEST_DATASETS = [
[
"http://data.statmt.org/wmt17/translation-task/dev.tgz",
("dev/newstest2013.en", "dev/newstest2013.de")
],
]
def _get_wmt_ende_bpe_dataset(directory, filename):
"""Extract the WMT en-de corpus `filename` to directory unless it's there."""
train_path = os.path.join(directory, filename)
if not (tf.gfile.Exists(train_path + ".de") and
tf.gfile.Exists(train_path + ".en")):
url = ("https://drive.google.com/uc?export=download&id="
"0B_bZck-ksdkpM25jRUN2X2UxMm8")
corpus_file = generator_utils.maybe_download_from_drive(
directory, "wmt16_en_de.tar.gz", url)
with tarfile.open(corpus_file, "r:gz") as corpus_tar:
corpus_tar.extractall(directory)
return train_path
@registry.register_problem
class TranslateEndeWmtBpe32k(translate.TranslateProblem):
"""Problem spec for WMT En-De translation, BPE version."""
@property
def vocab_type(self):
return text_problems.VocabType.TOKEN
@property
def oov_token(self):
return "UNK"
def generate_samples(self, data_dir, tmp_dir, dataset_split):
"""Instance of token generator for the WMT en->de task, training set."""
train = dataset_split == problem.DatasetSplit.TRAIN
dataset_path = ("train.tok.clean.bpe.32000"
if train else "newstest2013.tok.bpe.32000")
train_path = _get_wmt_ende_bpe_dataset(tmp_dir, dataset_path)
# Vocab
vocab_path = os.path.join(data_dir, self.vocab_filename)
if not tf.gfile.Exists(vocab_path):
bpe_vocab = os.path.join(tmp_dir, "vocab.bpe.32000")
with tf.gfile.Open(bpe_vocab) as f:
vocab_list = f.read().split("\n")
vocab_list.append(self.oov_token)
text_encoder.TokenTextEncoder(
None, vocab_list=vocab_list).store_to_file(vocab_path)
return text_problems.text2text_txt_iterator(train_path + ".en",
train_path + ".de")
@registry.register_problem
class TranslateEndeWmt8k(translate.TranslateProblem):
"""Problem spec for WMT En-De translation."""
@property
def approx_vocab_size(self):
return 2**13 # 8192
def source_data_files(self, dataset_split):
train = dataset_split == problem.DatasetSplit.TRAIN
return _ENDE_TRAIN_DATASETS if train else _ENDE_TEST_DATASETS
@registry.register_problem
class TranslateEndeWmt32k(TranslateEndeWmt8k):
@property
def approx_vocab_size(self):
return 2**15 # 32768
@registry.register_problem
class TranslateEndeWmt32kPacked(TranslateEndeWmt32k):
@property
def packed_length(self):
return 256
@property
def vocab_filename(self):
return TranslateEndeWmt32k().vocab_filename
@registry.register_problem
class TranslateEndeWmt8kPacked(TranslateEndeWmt8k):
@property
def packed_length(self):
return 256
@property
def vocab_filename(self):
return TranslateEndeWmt8k().vocab_filename
@registry.register_problem
class TranslateEndeWmtCharacters(TranslateEndeWmt8k):
"""Problem spec for WMT En-De translation."""
@property
def vocab_type(self):
return text_problems.VocabType.CHARACTER