-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathutils.py
More file actions
32 lines (23 loc) · 893 Bytes
/
Copy pathutils.py
File metadata and controls
32 lines (23 loc) · 893 Bytes
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
import numpy as np
from tensorlayer.nlp import initialize_vocabulary
def seq2seq_onehot2label(data):
data = np.array(data)
seqlen, batch_size, _ = data.shape
# (seqlen, batch_size, embedding_size) -> (batch_size, seqlen, embedding_size)
data = np.transpose(data, (1, 0, 2))
data = np.argmax(data, 2)
data.reshape((batch_size, seqlen))
return data
class Translator(object):
def __init__(self, dict_path):
_, self.my_dict = initialize_vocabulary(dict_path)
def translate(self, data):
r = []
for sentence in data:
r.append(list(map(lambda t: self.my_dict[t].decode('utf-8'), sentence)))
return r
def translate_and_print(self, data,logger = None):
for sentence in self.translate(data):
print ' '.join(sentence)
if(logger):
logger.debug(' '.join(sentence))