-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdata_utils.py
More file actions
195 lines (162 loc) · 7.52 KB
/
Copy pathdata_utils.py
File metadata and controls
195 lines (162 loc) · 7.52 KB
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
"""Utilities for downloading data from WMT, tokenizing, vocabularies."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import gzip
import os
import re
import tarfile
from six.moves import urllib
from tensorflow.python.platform import gfile
import tensorflow as tf
_PAD = b"_PAD"
_GO = b"_GO"
_EOS = b"_EOS"
_UNK = b"_UNK"
_START_VOCAB = [_PAD, _GO, _EOS, _UNK]
PAD_ID = 0
GO_ID = 1
EOS_ID = 2
UNK_ID = 3
_WORD_SPLIT = re.compile(b"([.,!?\"':;)(])")
_DIGIT_RE = re.compile(br"\d")
_WMT_DE_TRAIN_URL = "http://www.statmt.org/wmt14/training-monolingual-europarl-v7/europarl-v7.de.gz"
_WMT_EN_TRAIN_URL = "http://www.statmt.org/wmt14/training-monolingual-europarl-v7/europarl-v7.en.gz"
_WMT_ENDE_DEV_URL = "http://www.statmt.org/wmt15/dev-v2.tgz"
def maybe_download(directory, filename, url):
if not os.path.exists(directory):
print("Creating directory %s" % directory)
os.mkdir(directory)
filepath = os.path.join(directory, filename)
print("Filepath : %s" % filepath)
if not os.path.exists(filepath):
print("Downloading %s to %s" % (url, filepath))
filepath, _ = urllib.request.urlretrieve(url, filepath)
statinfo = os.stat(filepath)
print("Successfully downloaded", filename, statinfo.st_size, "bytes")
return filepath
def gunzip_file(gz_path, new_path):
print("Unpacking %s to %s" % (gz_path, new_path))
with gzip.open(gz_path, "rb") as gz_file:
with open(new_path, "wb") as new_file:
for line in gz_file:
new_file.write(line)
def get_wmt_ende_train_set(directory):
train_path = os.path.join(directory, "commoncrawl.de-en")
if not (gfile.Exists(train_path +".de") and gfile.Exists(train_path +".en")):
corpus_de_file = maybe_download(directory, "europarl-v7.de.gz", _WMT_DE_TRAIN_URL)
corpus_en_file = maybe_download(directory, "europarl-v7.en.gz", _WMT_EN_TRAIN_URL)
print("Downloaded files...")
#print("Extracting tar file %s" % corpus_file)
#with tarfile.open(corpus_file, "r") as corpus_tar:
# corpus_tar.extractall(directory)
gunzip_file(train_path + ".de.gz", train_path + ".de")
gunzip_file(train_path + ".en.gz", train_path + ".en")
return train_path
def get_wmt_ende_dev_set(directory):
dev_name = "newstest2013"
dev_path = os.path.join(directory, dev_name)
if not (gfile.Exists(dev_path + ".de") and gfile.Exists(dev_path + ".en")):
dev_file = maybe_download(directory, "dev-v2.tgz", _WMT_ENDE_DEV_URL)
print("Extracting tgz file %s" % dev_file)
with tarfile.open(dev_file, "r:gz") as dev_tar:
de_dev_file = dev_tar.getmember("dev/" + dev_name + ".de")
en_dev_file = dev_tar.getmember("dev/" + dev_name + ".en")
de_dev_file.name = dev_name + ".de" # Extract without "dev/" prefix.
en_dev_file.name = dev_name + ".en"
dev_tar.extract(de_dev_file, directory)
dev_tar.extract(en_dev_file, directory)
return dev_path
def basic_tokenizer(sentence):
words = []
for space_separated_fragment in sentence.strip().split():
words.extend(_WORD_SPLIT.split(space_separated_fragment))
return [w for w in words if w]
def create_vocabulary(vocabulary_path, data_path, max_vocabulary_size,
tokenizer=None, normalize_digits=True):
if not gfile.Exists(vocabulary_path):
print("Creating vocabulary %s from data %s" % (vocabulary_path, data_path))
vocab = {}
with gfile.GFile(data_path, mode="rb") as f:
counter = 0
for line in f:
counter += 1
if counter % 100000 == 0:
print(" processing line %d" % counter)
line = tf.compat.as_bytes(line)
tokens = tokenizer(line) if tokenizer else basic_tokenizer(line)
for w in tokens:
word = _DIGIT_RE.sub(b"0", w) if normalize_digits else w
if word in vocab:
vocab[word] += 1
else:
vocab[word] = 1
vocab_list = _START_VOCAB + sorted(vocab, key=vocab.get, reverse=True)
if len(vocab_list) > max_vocabulary_size:
vocab_list = vocab_list[:max_vocabulary_size]
with gfile.GFile(vocabulary_path, mode="wb") as vocab_file:
for w in vocab_list:
vocab_file.write(w + b"\n")
def initialize_vocabulary(vocabulary_path):
if gfile.Exists(vocabulary_path):
rev_vocab = []
with gfile.GFile(vocabulary_path, mode="rb") as f:
rev_vocab.extend(f.readlines())
rev_vocab = [tf.compat.as_bytes(line.strip()) for line in rev_vocab]
vocab = dict([(x, y) for (y, x) in enumerate(rev_vocab)])
return vocab, rev_vocab
else:
raise ValueError("Vocabulary file %s not found.", vocabulary_path)
def sentence_to_token_ids(sentence, vocabulary,
tokenizer=None, normalize_digits=True):
if tokenizer:
words = tokenizer(sentence)
else:
words = basic_tokenizer(sentence)
if not normalize_digits:
return [vocabulary.get(w, UNK_ID) for w in words]
# Normalize digits by 0 before looking words up in the vocabulary.
return [vocabulary.get(_DIGIT_RE.sub(b"0", w), UNK_ID) for w in words]
def data_to_token_ids(data_path, target_path, vocabulary_path,
tokenizer=None, normalize_digits=True):
if not gfile.Exists(target_path):
print("Tokenizing data in %s" % data_path)
vocab, _ = initialize_vocabulary(vocabulary_path)
with gfile.GFile(data_path, mode="rb") as data_file:
with gfile.GFile(target_path, mode="w") as tokens_file:
counter = 0
for line in data_file:
counter += 1
if counter % 100000 == 0:
print(" tokenizing line %d" % counter)
token_ids = sentence_to_token_ids(tf.compat.as_bytes(line), vocab,
tokenizer, normalize_digits)
tokens_file.write(" ".join([str(tok) for tok in token_ids]) + "\n")
def prepare_wmt_data(data_dir, en_vocabulary_size, de_vocabulary_size, tokenizer=None):
train_path = get_wmt_ende_train_set(data_dir)
dev_path = get_wmt_ende_dev_set(data_dir)
from_train_path = train_path + ".en"
to_train_path = train_path + ".de"
from_dev_path = dev_path + ".en"
to_dev_path = dev_path + ".de"
return prepare_data(data_dir, from_train_path, to_train_path, from_dev_path, to_dev_path, en_vocabulary_size,
de_vocabulary_size, tokenizer)
def prepare_data(data_dir, from_train_path, to_train_path, from_dev_path, to_dev_path, from_vocabulary_size,
to_vocabulary_size, tokenizer=None):
to_vocab_path = os.path.join(data_dir, "vocab%d.to" % to_vocabulary_size)
from_vocab_path = os.path.join(data_dir, "vocab%d.from" % from_vocabulary_size)
create_vocabulary(to_vocab_path, to_train_path , to_vocabulary_size, tokenizer)
create_vocabulary(from_vocab_path, from_train_path , from_vocabulary_size, tokenizer)
# Create token ids for the training data.
to_train_ids_path = to_train_path + (".ids%d" % to_vocabulary_size)
from_train_ids_path = from_train_path + (".ids%d" % from_vocabulary_size)
data_to_token_ids(to_train_path, to_train_ids_path, to_vocab_path, tokenizer)
data_to_token_ids(from_train_path, from_train_ids_path, from_vocab_path, tokenizer)
# Create token ids for the development data.
to_dev_ids_path = to_dev_path + (".ids%d" % to_vocabulary_size)
from_dev_ids_path = from_dev_path + (".ids%d" % from_vocabulary_size)
data_to_token_ids(to_dev_path, to_dev_ids_path, to_vocab_path, tokenizer)
data_to_token_ids(from_dev_path, from_dev_ids_path, from_vocab_path, tokenizer)
return (from_train_ids_path, to_train_ids_path,
from_dev_ids_path, to_dev_ids_path,
from_vocab_path, to_vocab_path)