summaryrefslogtreecommitdiff
path: root/src/moses.py
blob: 857ddbff3e1f4ba2c5f1ca117e295fb5d7214fb4 (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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import logging
import os
import subprocess
import gzip

class Moses:

  def __init__(self, config):
    self.config = config

  def run_train(self):
    args = [self.config.moses_train,
            '--root-dir', self.config.experiment_dir,
            '--corpus', '%s/%s' % (self.config.experiment_dir,
                                   self.config.train_name),
            '--f', self.config.src,
            '--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]
    if self.config.model == 'hier':
      args += ['-hierarchical', '-glue-grammar']

    logging.info(' '.join(args))

    log = open('%s/train.log' % self.config.experiment_dir, 'w')
    p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=log)
    p.wait()
    log.close()

  def run_retrain(self):
    old_train_nl = '%s/%s.nl' % (self.config.experiment_dir,
        self.config.train_name)
    old_train_mrl = '%s/%s.mrl' % (self.config.experiment_dir,
        self.config.train_name)
    moved_train_nl = '%s.notune' % old_train_nl
    moved_train_mrl = '%s.notune' % old_train_mrl
    tune_nl = '%s/tune.nl' % self.config.experiment_dir
    tune_mrl = '%s/tune.mrl' % self.config.experiment_dir
    os.rename(old_train_nl, moved_train_nl)
    os.rename(old_train_mrl, moved_train_mrl)
    with open(old_train_nl, 'w') as rt_train_nl:
      subprocess.call(['cat', moved_train_nl, tune_nl], stdout=rt_train_nl)
    with open(old_train_mrl, 'w') as rt_train_mrl:
      subprocess.call(['cat', moved_train_mrl, tune_mrl], stdout=rt_train_mrl)

    os.remove('%s/model/extract.inv.gz' % self.config.experiment_dir)
    os.remove('%s/model/extract.gz' % self.config.experiment_dir)
    if self.config.model == 'hier':
      os.remove('%s/model/rule-table.gz' % self.config.experiment_dir)
    else:
      os.remove('%s/model/phrase-table.gz' % self.config.experiment_dir)

    self.run_train()

  def parens_ok(self, line):
    mrl_part = line.split(' ||| ')[1]
    tokens = [t[-1] for t in mrl_part.split() if t[-2] == '@']
    tokens.reverse()
    stack = []
    while tokens:
      t = tokens.pop()
      assert t != '*'
      if t == 's':
        t = 0
      t = int(t)
      if t > 0:
        stack.append(t)
      else:
        while stack:
          top = stack.pop()
          if top > 1:
            stack.append(top - 1)
            break
        if tokens and not stack:
          return False
    return True

  def filter_phrase_table(self):
    table_name = 'phrase' if self.config.model == 'phrase' else 'rule'
    oldname = '%s/model/%s-table.gz' % (self.config.experiment_dir, table_name)
    newname = '%s/model/%s-table.old.gz' % (self.config.experiment_dir, table_name)
    os.rename(oldname, newname)

    with gzip.open(oldname, 'w') as filtered_table_f:
      with gzip.open(newname, 'r') as old_table_f:
        for line in old_table_f:
          if self.parens_ok(line):
            print >>filtered_table_f, line,

  def run_tune(self):
    wd = os.getcwd()
    os.chdir(self.config.experiment_dir)
    args = [self.config.moses_tune,
            '%s/tune.%s' % (self.config.experiment_dir, self.config.src),
            '%s/tune.%s' % (self.config.experiment_dir, self.config.tgt)]
    if self.config.model == 'hier':
      args += [self.config.moses_decode_hier]
    else:
      args += [self.config.moses_decode_phrase]
    args += ['%s/model/moses.ini' % self.config.experiment_dir,
             '--mertdir', '%s/dist/bin' % self.config.moses]
    if self.config.model == 'hier':
      args += ['--filtercmd', 
               '%s/scripts/training/filter-model-given-input.pl --Hierarchical'\
                   % self.config.moses]

    log = open('%s/tune.log' % self.config.experiment_dir, 'w')
    p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=log)
    p.wait()
    log.close()
    os.chdir(wd)

  def run_decode(self):
    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':
      args += ['-f', '%s/mert-work/moses.ini' % self.config.experiment_dir]
    else:
      args += ['-f', '%s/model/moses.ini' % self.config.experiment_dir]
    #args += ['-f', '%s/model/moses.ini' % self.config.experiment_dir]

    args += ['-drop-unknown',
             '-n-best-list', '%s/hyp.%s.nbest' % (self.config.experiment_dir, self.config.tgt),
                             str(self.config.nbest), 'distinct',
             '-threads', '3']

    #nullfile = open(os.devnull, 'w')
    infile = open('%s/test.%s' % (self.config.experiment_dir, self.config.src))
    outfile = open('%s/hyp.%s' % (self.config.experiment_dir, self.config.tgt), 'w')
    log = open('%s/decode.log' % self.config.experiment_dir, 'w')
    p = subprocess.Popen(args, stdin=infile, stdout=outfile, stderr=log)
    p.wait()
    infile.close()
    log.close()
    outfile.close()