-
Notifications
You must be signed in to change notification settings - Fork 30
/
translate.py
67 lines (55 loc) · 2 KB
/
translate.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
import numpy as np
import argparse
word2idx = np.load("./data/w2i.npy")
word2idx = word2idx.item()
id2word = {k: v for v, k in zip(word2idx.keys(), word2idx.values())}
def translate(word_indexs):
words = []
for idx in word_indexs:
idx = int(idx)
word = id2word.get(idx)
# print(word)
if word:
words.append(id2word.get(idx))
# else:
# words.append("<UNK>")
return "".join(words)
def parse(flag):
if flag.lower() in ('yes', 'true', 't', 'y', '1'):
return True
else:
return False
def translate_file(file_name, reverse):
translated = []
with open(file_name, "r") as f:
for l in f:
l = l.split()
if parse(reverse):
l = l[::-1]
line = [int(x) for x in l]
# trans = translate(line)
translated.append(translate(line))
# print(line)
return translated
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('index_file', type=str, help='index file')
parser.add_argument('--refer_file', default=None, type=str, help='reference file')
parser.add_argument('--input_file', default=None, type=str, help='input file')
parser.add_argument('--reverse', default="False", type=str, help='whether the token is reversed')
args = parser.parse_args()
if args.refer_file and args.input_file:
trans = translate_file(args.index_file, args.reverse)
refer = translate_file(args.refer_file, args.reverse)
input = translate_file(args.input_file, args.reverse)
for t, r, i in zip(trans, refer, input):
print("input: ", i)
print("translate: ", t)
print("refer: ", r)
else:
for i in translate_file(args.index_file, args.reverse):
print(i)
# print(translate([58,59, 3,60, 17 ,5 ,8 ,5 ,1 ,61, 62, 3, 63, 7 ,9 ,64 ,18, 3 ,19, 3]))
# with open(translate_file, "r") as f:
# for l in f:
# print(translate(l))