summaryrefslogtreecommitdiff
path: root/realtime/rt/decoder.py
blob: f4fea0e2a9f666b10ba7aa313f405993ba61b493 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import os
import subprocess

import util

class Decoder:

    def close(self):
        self.decoder.stdin.close()

class CdecDecoder(Decoder):
    
    def __init__(self, config, weights):
        cdec_root = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
        decoder = os.path.join(cdec_root, 'decoder', 'cdec')
        decoder_cmd = [decoder, '-c', config, '-w', weights]
        self.decoder = util.popen_io(decoder_cmd)

    def decode(self, sentence, grammar):
        input = '<seg grammar="{g}">{s}</seg>\n'.format(i=id, s=sentence, g=grammar)
        self.decoder.stdin.write(input)
        return self.decoder.stdout.readline().strip()