diff options
Diffstat (limited to 'src/moses.py')
-rw-r--r-- | src/moses.py | 38 |
1 files changed, 35 insertions, 3 deletions
diff --git a/src/moses.py b/src/moses.py index 857ddbf..9a159c3 100644 --- a/src/moses.py +++ b/src/moses.py @@ -3,6 +3,8 @@ import os import subprocess import gzip +from subprocess import Popen, PIPE, STDOUT + class Moses: def __init__(self, config): @@ -17,7 +19,8 @@ class Moses: '--e', self.config.tgt, '--lm', '0:3:%s/%s.arpa' % (self.config.experiment_dir, self.config.tgt), #'-score-options', "'--OnlyDirect --NoPhraseCount'" - '--alignment', self.config.symm] + '--alignment', self.config.symm, + '-external-bin-dir', self.config.giza] if self.config.model == 'hier': args += ['-hierarchical', '-glue-grammar'] @@ -99,7 +102,7 @@ class Moses: else: args += [self.config.moses_decode_phrase] args += ['%s/model/moses.ini' % self.config.experiment_dir, - '--mertdir', '%s/dist/bin' % self.config.moses] + '--mertdir', '%s/bin' % self.config.moses] if self.config.model == 'hier': args += ['--filtercmd', '%s/scripts/training/filter-model-given-input.pl --Hierarchical'\ @@ -119,7 +122,7 @@ class Moses: else: assert False - if self.config.run == 'test': + if self.config.run == 'test' or self.config.run == 'all': args += ['-f', '%s/mert-work/moses.ini' % self.config.experiment_dir] else: args += ['-f', '%s/model/moses.ini' % self.config.experiment_dir] @@ -139,3 +142,32 @@ class Moses: infile.close() log.close() outfile.close() + + def decode_sentence(self, experiment_dir, sentence, temp_dir): + if self.config.model == 'phrase': + args = [self.config.moses_decode_phrase] + elif self.config.model == 'hier': + args = [self.config.moses_decode_hier] + else: + assert False + + if self.config.run == 'test' or self.config.run == 'all': + args += ['-f', '%s/mert-work/moses.ini' % experiment_dir] + else: + args += ['-f', '%s/model/moses.ini' % experiment_dir] + + args += ['-drop-unknown', + '-n-best-list', '%s/nbest.tmp' % temp_dir, + str(self.config.nbest), 'distinct', + '-threads', '1'] + + infile = open('%s/sent.tmp' % temp_dir, 'w') + print >>infile, sentence + infile.close + infile = open('%s/sent.tmp' % temp_dir, 'r') + nullfile = open(os.devnull, 'w') + p = subprocess.Popen(args, stdin=infile, stdout=nullfile, stderr=nullfile) + p.wait() + infile.close() + return + |