summaryrefslogtreecommitdiff
path: root/training/utils/decode-and-evaluate.pl
blob: 1a332c08e0fc30be0876bdce08e11f10d5a4efd1 (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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#!/usr/bin/env perl
use strict;
my @ORIG_ARGV=@ARGV;
use Cwd qw(getcwd);
my $SCRIPT_DIR; BEGIN { use Cwd qw/ abs_path /; use File::Basename; $SCRIPT_DIR = dirname(abs_path($0)); push @INC, $SCRIPT_DIR, "$SCRIPT_DIR/../../environment"; }

# Skip local config (used for distributing jobs) if we're running in local-only mode
use LocalConfig;
use Getopt::Long;
use File::Basename qw(basename);
my $QSUB_CMD = qsub_args(mert_memory());

require "libcall.pl";

# Default settings
my $default_jobs = env_default_jobs();
my $bin_dir = $SCRIPT_DIR;
die "Bin directory $bin_dir missing/inaccessible" unless -d $bin_dir;
my $FAST_SCORE="$bin_dir/../../mteval/fast_score";
die "Can't execute $FAST_SCORE" unless -x $FAST_SCORE;
my $parallelize = "$bin_dir/parallelize.pl";
my $libcall = "$bin_dir/libcall.pl";
my $sentserver = "$bin_dir/sentserver";
my $sentclient = "$bin_dir/sentclient";
my $LocalConfig = "$SCRIPT_DIR/../../environment/LocalConfig.pm";

my $SCORER = $FAST_SCORE;
my $cdec = "$bin_dir/../../decoder/cdec";
die "Can't find decoder in $cdec" unless -x $cdec;
die "Can't find $parallelize" unless -x $parallelize;
die "Can't find $libcall" unless -e $libcall;
my $decoder = $cdec;
my $jobs = $default_jobs;   # number of decode nodes
my $pmem = "9g";
my $help = 0;
my $config;
my $test_set;
my $weights;
my $use_make = 1;
my $useqsub;
my $cpbin=1;
# Process command-line options
if (GetOptions(
	"jobs=i" => \$jobs,
	"help" => \$help,
	"qsub" => \$useqsub,
	"input=s" => \$test_set,
        "config=s" => \$config,
	"weights=s" => \$weights,
) == 0 || @ARGV!=0 || $help) {
	print_help();
	exit;
}

if ($useqsub) {
  $use_make = 0;
  die "LocalEnvironment.pm does not have qsub configuration for this host. Cannot run with --qsub!\n" unless has_qsub();
}

my @missing_args = ();

if (!defined $test_set) { push @missing_args, "--input"; }
if (!defined $config) { push @missing_args, "--config"; }
if (!defined $weights) { push @missing_args, "--weights"; }
die "Please specify missing arguments: " . join (', ', @missing_args) . "\nUse --help for more information.\n" if (@missing_args);

my @tf = localtime(time);
my $tname = basename($test_set);
$tname =~ s/\.(sgm|sgml|xml)$//i;
my $dir = "eval.$tname." . sprintf('%d%02d%02d-%02d%02d%02d', 1900+$tf[5], $tf[4], $tf[3], $tf[2], $tf[1], $tf[0]);

my $time = unchecked_output("date");

check_call("mkdir -p $dir");

split_devset($test_set, "$dir/test.input.raw", "$dir/test.refs");
my $refs = "-r $dir/test.refs";
my $newsrc = "$dir/test.input";
enseg("$dir/test.input.raw", $newsrc);
my $src_file = $newsrc;
open F, "<$src_file" or die "Can't read $src_file: $!"; close F;

my $test_trans="$dir/test.trans";
my $logdir="$dir/logs";
my $decoderLog="$logdir/decoder.sentserver.log";
check_call("mkdir -p $logdir");

#decode
print STDERR "RUNNING DECODER AT ";
print STDERR unchecked_output("date");
my $decoder_cmd = "$decoder -c $config --weights $weights";
my $pcmd;
if ($use_make) {
	$pcmd = "cat $src_file | $parallelize --workdir $dir --use-fork -p $pmem -e $logdir -j $jobs --";
} else {
	$pcmd = "cat $src_file | $parallelize --workdir $dir -p $pmem -e $logdir -j $jobs --";
}
my $cmd = "$pcmd $decoder_cmd 2> $decoderLog 1> $test_trans";
check_bash_call($cmd);
print STDERR "DECODER COMPLETED AT ";
print STDERR unchecked_output("date");
print STDERR "\nOUTPUT: $test_trans\n\n";
my $bleu = check_output("cat $test_trans | $SCORER $refs -m ibm_bleu");
chomp $bleu;
print STDERR "BLEU: $bleu\n";
my $ter = check_output("cat $test_trans | $SCORER $refs -m ter");
chomp $ter;
print STDERR " TER: $ter\n";
open TR, ">$dir/test.scores" or die "Can't write $dir/test.scores: $!";
print TR <<EOT;
### SCORE REPORT #############################################################
        OUTPUT=$test_trans
  SCRIPT INPUT=$test_set
 DECODER INPUT=$src_file
    REFERENCES=$dir/test.refs
------------------------------------------------------------------------------
          BLEU=$bleu
           TER=$ter
##############################################################################
EOT
close TR;
my $sr = unchecked_output("cat $dir/test.scores");
print STDERR "\n\n$sr\n(A copy of this report can be found in $dir/test.scores)\n\n";
exit 0;

sub enseg {
	my $src = shift;
	my $newsrc = shift;
	open(SRC, $src);
	open(NEWSRC, ">$newsrc");
	my $i=0;
	while (my $line=<SRC>){
		chomp $line;
		if ($line =~ /^\s*<seg/i) {
		    if($line =~ /id="[0-9]+"/) {
			print NEWSRC "$line\n";
		    } else {
			die "When using segments with pre-generated <seg> tags, you must include a zero-based id attribute";
		    }
		} else {
			print NEWSRC "<seg id=\"$i\">$line</seg>\n";
		}
		$i++;
	}
	close SRC;
	close NEWSRC;
}

sub print_help {
	my $executable = basename($0); chomp $executable;
	print << "Help";

Usage: $executable [options] <ini file>

	$executable --config cdec.ini --weights weights.txt [--jobs N] [--qsub] <testset.in-ref>

Options:

	--help
		Print this message and exit.

	--config <file>
		A path to the cdec.ini file.

	--weights <file>
		A file specifying feature weights.

	--dir <dir>
		Directory for intermediate and output files.

Job control options:

	--jobs <I>
		Number of decoder processes to run in parallel. [default=$default_jobs]

	--qsub
		Use qsub to run jobs in parallel (qsub must be configured in
		environment/LocalEnvironment.pm)

	--pmem <N>
		Amount of physical memory requested for parallel decoding jobs
		(used with qsub requests only)

Help
}

sub convert {
  my ($str) = @_;
  my @ps = split /;/, $str;
  my %dict = ();
  for my $p (@ps) {
    my ($k, $v) = split /=/, $p;
    $dict{$k} = $v;
  }
  return %dict;
}



sub cmdline {
    return join ' ',($0,@ORIG_ARGV);
}

#buggy: last arg gets quoted sometimes?
my $is_shell_special=qr{[ \t\n\\><|&;"'`~*?{}$!()]};
my $shell_escape_in_quote=qr{[\\"\$`!]};

sub escape_shell {
    my ($arg)=@_;
    return undef unless defined $arg;
    if ($arg =~ /$is_shell_special/) {
        $arg =~ s/($shell_escape_in_quote)/\\$1/g;
        return "\"$arg\"";
    }
    return $arg;
}

sub escaped_shell_args {
    return map {local $_=$_;chomp;escape_shell($_)} @_;
}

sub escaped_shell_args_str {
    return join ' ',&escaped_shell_args(@_);
}

sub escaped_cmdline {
    return "$0 ".&escaped_shell_args_str(@ORIG_ARGV);
}

sub split_devset {
  my ($infile, $outsrc, $outref) = @_;
  open F, "<$infile" or die "Can't read $infile: $!";
  open S, ">$outsrc" or die "Can't write $outsrc: $!";
  open R, ">$outref" or die "Can't write $outref: $!";
  while(<F>) {
    chomp;
    my ($src, @refs) = split /\s*\|\|\|\s*/;
    die "Malformed devset line: $_\n" unless scalar @refs > 0;
    print S "$src\n";
    print R join(' ||| ', @refs) . "\n";
  }
  close R;
  close S;
  close F;
}