-
Notifications
You must be signed in to change notification settings - Fork 22
/
util.py
executable file
·244 lines (200 loc) · 6.81 KB
/
util.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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import time, os, sys, shutil, io, subprocess, re
import tensorflow as tf
import numpy as np
import zipfile
# Progress bar
TOTAL_BAR_LENGTH = 100.
last_time = time.time()
begin_time = last_time
print(os.popen('stty size', 'r').read())
_, term_width = os.popen('stty size', 'r').read().split()
term_width = int(term_width)
def bleu_score(labels_file, predictions_path):
bleu_script = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'multi-bleu.perl')
try:
with io.open(predictions_path, encoding="utf-8", mode="r") as predictions_file:
bleu_out = subprocess.check_output(
[bleu_script, labels_file],
stdin=predictions_file,
stderr=subprocess.STDOUT)
bleu_out = bleu_out.decode("utf-8")
bleu_score = re.search(r"BLEU = (.+?),", bleu_out).group(1)
print(bleu_score)
return float(bleu_score)
except subprocess.CalledProcessError as error:
if error.output is not None:
msg = error.output.strip()
tf.logging.warning(
"{} script returned non-zero exit code: {}".format(bleu_script, msg))
return None
def read_word2vec_zip(word2vec_file):
wordvec_map = {}
num_words = 0
dimension = 0
zfile = zipfile.ZipFile(word2vec_file)
for finfo in zfile.infolist():
ifile = zfile.open(finfo)
for line in ifile:
line = line.strip()
#print line
entries = line.split(' ')
if len(entries) == 2:
continue
word = entries[0].strip()
vec = map(float, entries[1:])
if word in wordvec_map:
print ("Invalid word in embedding. Does not matter.")
continue
assert dimension == 0 or dimension == len(vec)
wordvec_map[word] = np.array(vec)
num_words += 1
dimension = len(vec)
return wordvec_map, num_words, dimension
def read_word2vec(word2vec_file):
wordvec_map = {}
num_words = 0
dimension = 0
with open(word2vec_file, "r") as f:
for line in f:
line = line.strip()
#print line
entries = line.split(' ')
if len(entries) == 2:
continue
word = entries[0].strip()
vec = map(float, entries[1:])
if word in wordvec_map:
print ("Invalid word in embedding. Does not matter.")
continue
# assert word not in wordvec_map
assert dimension == 0 or dimension == len(vec)
wordvec_map[word] = np.array(vec)
num_words += 1
dimension = len(vec)
return wordvec_map, num_words, dimension
def load_vocab(vocab_file):
vocab = {}
vocab['<_PAD>'] = 0
vocab['<_START_TOKEN>'] = 1
vocab['<_END_TOKEN>'] = 2
vocab['<_UNK_TOKEN>'] = 3
cnt = 4
with open(vocab_file, "r") as v:
for line in v:
if len(line.strip().split()) > 1:
word = line.strip().split()[0]
ori_id = int(line.strip().split()[1])
if word not in vocab:
vocab[word] = (cnt + ori_id)
return vocab
def create_init_embedding(vocab_file, extend_vocab_size, word2vec_file, emblen):
'''
create initial embedding for text relation words.
words not in word2vec file initialized to random.
key_map['PAD'] = 0
key_map['START_TOKEN'] = 1
key_map['END_TOKEN'] = 2
key_map['UNK_TOKEN'] = 3
'''
vocab = load_vocab(vocab_file)
print("vocab len: ", len(vocab))
init_embedding = np.random.uniform(-np.sqrt(3), np.sqrt(3), size = (len(vocab) + extend_vocab_size, emblen))
if word2vec_file.endswith('.gz'):
word2vec_map = KeyedVectors.load_word2vec_format(word2vec_file, binary=True)
elif word2vec_file.endswith('.zip'):
word2vec_map, num_words, dimension = read_word2vec_zip(word2vec_file)
else:
word2vec_map, num_words, dimension = read_word2vec(word2vec_file)
num_covered = 0
for word in vocab:
if word in word2vec_map:
vec = word2vec_map[word]
if len(vec) != emblen:
raise ValueError("word2vec dimension doesn't match.")
init_embedding[vocab[word], :] = vec
num_covered += 1
unk_vec = init_embedding[3, :]
for ind in range(len(vocab), len(init_embedding)):
init_embedding[ind, :] = unk_vec
## embedding for pad
# init_embedding[0][:] = np.zeros(emblen)
print ("word2vec covered: %d" % num_covered)
return init_embedding
def progress_bar(current, total, msg=None):
global last_time, begin_time
if current == 0:
begin_time = time.time() # Reset for new bar.
cur_len = int(TOTAL_BAR_LENGTH*current/total)
rest_len = int(TOTAL_BAR_LENGTH - cur_len) - 1
sys.stdout.write(' [')
for i in range(cur_len):
sys.stdout.write('=')
sys.stdout.write('>')
for i in range(rest_len):
sys.stdout.write('.')
sys.stdout.write(']')
cur_time = time.time()
step_time = cur_time - last_time
last_time = cur_time
tot_time = cur_time - begin_time
L = []
L.append(' Step: %s' % format_time(step_time))
L.append(' | Tot: %s' % format_time(tot_time))
if msg:
L.append(' | ' + msg)
msg = ''.join(L)
sys.stdout.write(msg)
for i in range(term_width-int(TOTAL_BAR_LENGTH)-len(msg)-3):
sys.stdout.write(' ')
# Go back to the center of the bar.
for i in range(term_width-int(TOTAL_BAR_LENGTH/2)+2):
sys.stdout.write('\b')
sys.stdout.write(' %d/%d ' % (current+1, total))
if current < total-1:
sys.stdout.write('\r')
else:
sys.stdout.write('\n')
sys.stdout.flush()
def format_time(seconds):
days = int(seconds / 3600/24)
seconds = seconds - days*3600*24
hours = int(seconds / 3600)
seconds = seconds - hours*3600
minutes = int(seconds / 60)
seconds = seconds - minutes*60
secondsf = int(seconds)
seconds = seconds - secondsf
millis = int(seconds*1000)
f = ''
i = 1
if days > 0:
f += str(days) + 'D'
i += 1
if hours > 0 and i <= 2:
f += str(hours) + 'h'
i += 1
if minutes > 0 and i <= 2:
f += str(minutes) + 'm'
i += 1
if secondsf > 0 and i <= 2:
f += str(secondsf) + 's'
i += 1
if millis > 0 and i <= 2:
f += str(millis) + 'ms'
i += 1
if f == '':
f = '0ms'
return f
def write_word(pred_list, save_dir, name):
ss = open(save_dir + name, "w+")
for item in pred_list:
ss.write(" ".join(item) + '\n')
def get_current_git_version():
import git
repo = git.Repo(search_parent_directories=True)
sha = repo.head.object.hexsha
return sha
def write_log(log_file, s):
print(s)
with open(log_file, 'a') as f:
f.write(s+'\n')