summaryrefslogtreecommitdiff
path: root/realtime/realtime.py
blob: 3c384fa2bb190c6459ae333c59a80947fad9daa8 (plain)
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
#!/usr/bin/env python

import argparse
import logging
import sys

import rt

class Parser(argparse.ArgumentParser):

    def error(self, message):
        self.print_help()
        sys.stderr.write('\n{}\n'.format(message))
        sys.exit(2)

def main():

    parser = Parser(description='Real-time adaptive translation with cdec.  (See README.md)')
    parser.add_argument('-c', '--config', required=True, help='Config directory')
    parser.add_argument('-s', '--state', help='Load state file (saved incremental data)')
    parser.add_argument('-n', '--normalize', help='Normalize text (tokenize, translate, detokenize)', action='store_true')
    parser.add_argument('-T', '--temp', help='Temp directory (default /tmp)', default='/tmp')
    parser.add_argument('-a', '--cache', help='Grammar cache size (default 5)', default='5')
    parser.add_argument('-v', '--verbose', help='Info to stderr', action='store_true')
    args = parser.parse_args()

    if args.verbose:
        logging.basicConfig(level=logging.INFO)

    with rt.RealtimeDecoder(args.config, tmpdir=args.temp, cache_size=int(args.cache), norm=args.normalize) as rtd:

        try:
            # Load state if given
            if args.state:
                with open(args.state) as input:
                    rtd.load_state(input)
            # Read lines and commands
            while True:
                line = sys.stdin.readline()
                if not line:
                    break
                line = line.strip()
                if '|||' in line:
                    rtd.command_line(line)
                else:
                    hyp = rtd.decode(line)
                    sys.stdout.write('{}\n'.format(hyp))
                    sys.stdout.flush()
    
        # Clean exit on ctrl+c
        except KeyboardInterrupt:
            logging.info('Caught KeyboardInterrupt, exiting')

if __name__ == '__main__':
    main()