summaryrefslogtreecommitdiff
path: root/realtime/rt/rt.py
diff options
context:
space:
mode:
authorMichael Denkowski <mdenkows@cs.cmu.edu>2013-09-04 12:27:22 -0700
committerMichael Denkowski <mdenkows@cs.cmu.edu>2013-09-04 12:27:22 -0700
commited6325760c6cbd7681330bd9591f66ce5efe60ae (patch)
treec48d9ea649c1bb00d25a228f97dba25cac7d2aed /realtime/rt/rt.py
parente636b458b445f781fcc493797dea3576c869f935 (diff)
Infrastructure for HPYPLM, config file management.
Diffstat (limited to 'realtime/rt/rt.py')
-rw-r--r--realtime/rt/rt.py36
1 files changed, 30 insertions, 6 deletions
diff --git a/realtime/rt/rt.py b/realtime/rt/rt.py
index fc6e3929..b04b4ed5 100644
--- a/realtime/rt/rt.py
+++ b/realtime/rt/rt.py
@@ -10,11 +10,12 @@ import subprocess
import tempfile
import time
-import cdec.configobj
+from cdec.configobj import ConfigObj
import cdec.sa
import aligner
import decoder
+import util
class RealtimeDecoder:
@@ -32,21 +33,38 @@ class RealtimeDecoder:
self.aligner = aligner.ForceAligner(fwd_params, fwd_err, rev_params, rev_err)
# Grammar extractor
- sa_config = os.path.join(configdir, 'sa.ini')
- self.extractor = cdec.sa.GrammarExtractor(sa_config, online=True)
+ sa_config = ConfigObj(os.path.join(configdir, 'sa.ini'), unrepr=True)
+ sa_config.filename = os.path.join(self.tmp, 'sa.ini')
+ util.sa_ini_for_realtime(sa_config, os.path.abspath(configdir))
+ sa_config.write()
+ self.extractor = cdec.sa.GrammarExtractor(sa_config.filename, online=True)
self.grammar_files = collections.deque()
self.grammar_dict = {}
self.cache_size = cache_size
+ # HPYPLM reference stream
+ ref_fifo_file = os.path.join(self.tmp, 'ref.fifo')
+ os.mkfifo(ref_fifo_file)
+ self.ref_fifo = open(ref_fifo_file, 'w+')
+ # Start with empty line (do not learn prior to first input)
+ self.ref_fifo.write('\n')
+ self.ref_fifo.flush()
+
# Decoder
- decoder_config = os.path.join(configdir, 'cdec.ini')
+ decoder_config = [[f.strip() for f in line.split('=')] for line in open(os.path.join(configdir, 'cdec.ini'))]
+ util.cdec_ini_for_realtime(decoder_config, os.path.abspath(configdir), ref_fifo_file)
+ decoder_config_file = os.path.join(self.tmp, 'cdec.ini')
+ with open(decoder_config_file, 'w') as output:
+ for (k, v) in decoder_config:
+ output.write('{}={}\n'.format(k, v))
decoder_weights = os.path.join(configdir, 'weights.final')
- self.decoder = decoder.MIRADecoder(decoder_config, decoder_weights)
+ self.decoder = decoder.MIRADecoder(decoder_config_file, decoder_weights)
def close(self):
logging.info('Closing processes')
self.aligner.close()
self.decoder.close()
+ self.ref_fifo.close()
logging.info('Deleting {}'.format(self.tmp))
shutil.rmtree(self.tmp)
@@ -75,6 +93,9 @@ class RealtimeDecoder:
grammar_file = self.grammar(sentence)
start_time = time.time()
hyp = self.decoder.decode(sentence, grammar_file)
+ # Empty reference: HPYPLM does not learn prior to next translation
+ self.ref_fifo.write('\n')
+ self.ref_fifo.flush()
stop_time = time.time()
logging.info('Translation time: {} seconds'.format(stop_time - start_time))
return hyp
@@ -91,4 +112,7 @@ class RealtimeDecoder:
# Clear (old) cached grammar
rm_grammar = self.grammar_dict.pop(source)
os.remove(rm_grammar)
- # TODO: Add to LM by writing to fifo
+ # Add to HPYPLM by writing to fifo (read on next translation)
+ logging.info('Adding to HPYPLM: {}'.format(target))
+ self.ref_fifo.write('{}\n'.format(target))
+ self.ref_fifo.flush()