-
Notifications
You must be signed in to change notification settings - Fork 2
/
translate.py
49 lines (37 loc) · 1.68 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
import sys
import argparse
import codecs
from nmmt import NMTDecoder
import logging
logging.basicConfig(stream=sys.stderr, level=logging.DEBUG)
# Base models and Decoder definitions
# ======================================================================================================================
class Suggestion:
def __init__(self, source, target, score):
self.source = source
self.target = target
self.score = score
def addone(f):
for line in f:
yield line
yield None
def run_main():
# Args parse
# ------------------------------------------------------------------------------------------------------------------
parser = argparse.ArgumentParser(description='Run a forever-loop serving translation requests')
parser.add_argument('model', metavar='MODEL', help='the path to the decoder model')
parser.add_argument('-l', '--log-level', dest='log_level', metavar='LEVEL', help='select the log level',
choices=['critical', 'error', 'warning', 'info', 'debug'], default='info')
parser.add_argument('-g', '--gpu', type=int, dest='gpu', metavar='GPU', help='the index of the GPU to use',
default=None)
parser.add_argument('-src', required=True,
help='Source sequence to decode (one line per sequence)')
args = parser.parse_args()
decoder = NMTDecoder(args.model, gpu_id=args.gpu, random_seed=3435)
suggestions = None
for line in addone(codecs.open(args.src, 'r', 'utf-8')):
if line is not None:
translation = decoder.translate(line, suggestions)
print "%s" % translation
if __name__ == '__main__':
run_main()