From def48bde959fa932cbe87228dc84afc8e635b49b Mon Sep 17 00:00:00 2001 From: Michael Denkowski Date: Tue, 3 Sep 2013 12:13:03 -0700 Subject: MIRA updates in realtime.py --- realtime/rt/aligner.py | 5 ++++- realtime/rt/decoder.py | 7 ++++++- realtime/rt/util.py | 6 ++++++ 3 files changed, 16 insertions(+), 2 deletions(-) (limited to 'realtime/rt') diff --git a/realtime/rt/aligner.py b/realtime/rt/aligner.py index d94dbda0..4a0ace48 100644 --- a/realtime/rt/aligner.py +++ b/realtime/rt/aligner.py @@ -23,7 +23,10 @@ class ForceAligner: self.rev_align = util.popen_io(rev_cmd) self.tools = util.popen_io(tools_cmd) - def align(self, line): + def align(self, source, target): + return self.align_formatted('{} ||| {}'.format(source, target)) + + def align_formatted(self, line): self.fwd_align.stdin.write('{}\n'.format(line)) self.rev_align.stdin.write('{}\n'.format(line)) # f words ||| e words ||| links ||| score diff --git a/realtime/rt/decoder.py b/realtime/rt/decoder.py index 786bc07a..6bbef6f2 100644 --- a/realtime/rt/decoder.py +++ b/realtime/rt/decoder.py @@ -9,7 +9,7 @@ class Decoder: self.decoder.stdin.close() def decode(self, sentence, grammar): - input = '{s}\n'.format(i=id, s=sentence, g=grammar) + input = '{s}\n'.format(s=sentence, g=grammar) self.decoder.stdin.write(input) return self.decoder.stdout.readline().strip() @@ -29,3 +29,8 @@ class MIRADecoder(Decoder): # optimizer=2 step=0.001 best=500, k=500, uniq, stream mira_cmd = [mira, '-c', config, '-w', weights, '-o', '2', '-C', '0.001', '-b', '500', '-k', '500', '-u', '-t'] self.decoder = util.popen_io(mira_cmd) + + def update(self, sentence, grammar, reference): + input = '{s} ||| {r}\n'.format(s=sentence, g=grammar, r=reference) + self.decoder.stdin.write(input) + return self.decoder.stdout.readline().strip() diff --git a/realtime/rt/util.py b/realtime/rt/util.py index 7f877161..263e33fb 100644 --- a/realtime/rt/util.py +++ b/realtime/rt/util.py @@ -1,4 +1,5 @@ import subprocess +import sys import threading def popen_io(cmd): @@ -6,6 +7,11 @@ def popen_io(cmd): consume_stream(p.stderr) return p +def popen_io_v(cmd): + sys.stderr.write('{}\n'.format(' '.join(cmd))) + p = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE) + return p + def consume_stream(stream): def consume(s): for _ in s: -- cgit v1.2.3