forked from OpenNMT/OpenNMT-py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
translate.py
executable file
·54 lines (38 loc) · 1.36 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from datetime import datetime
from onmt.utils.logging import init_logger
from onmt.utils.misc import concate_level
from onmt.translate.translator import build_translator
import onmt.opts as opts
from onmt.utils.parse import ArgumentParser
def main(opt):
ArgumentParser.validate_translate_opts(opt)
logger = init_logger(opt.log_file)
translator = build_translator(opt, report_score=True)
for level in opt.levels:
logger.info("Reading source and target files: %s %s. of level %s" % (opt.src, opt.tgt, level))
src_path = concate_level(opt.src, level)
tgt_path = concate_level(opt.tgt, level)
logger.info("Translating level %d." % level)
translator.translate(
src=src_path,
tgt=tgt_path,
level=level,
src_dir=opt.src_dir,
batch_size=opt.batch_size,
attn_debug=opt.attn_debug
)
def _get_parser():
parser = ArgumentParser(description='translate.py')
opts.config_opts(parser)
opts.general_opts(parser)
opts.model_opts(parser)
opts.translate_opts(parser)
return parser
if __name__ == "__main__":
parser = _get_parser()
opt = parser.parse_args()
setattr(opt, 'datetime', datetime.now().strftime("%b-%d_%H-%M-%S"))
main(opt)