File size: 4,934 Bytes
158b61b |
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 |
#!/usr/bin/env perl
#
# This file is part of moses. Its use is licensed under the GNU Lesser General
# Public License version 2.1 or, at your option, any later version.
# given a moses.ini, filter the phrase tables to contain
# only ttable-limit options per source phrase
#
# outputs new phrase tables and updated moses.ini into targetdir
#
# usage: reduce-topt-count.pl moses.ini targetdir
use strict;
use warnings;
use File::Basename;
use File::Path;
use POSIX;
use List::Util qw( min sum );
my ($ini_file, $targetdir) = @ARGV;
if (! defined $targetdir) {
die "usage: reduce-topt-count.pl moses.ini targetdir\n"
}
my %ttables;
my $ini_hdl = my_open($ini_file);
my $outini_hdl = my_save("$targetdir/moses.ini");
my $section = "";
my %section_handlers = (
'ttable-file' => read_ttable_file(),
'ttable-limit' => read_ttable_limit(),
'weight-t' => read_weight_t()
);
# print header for updated moses.ini
my $timestamp = POSIX::strftime("%m/%d/%Y %H:%M:%S", localtime);
print $outini_hdl <<"END";
# Generated by reduce-topt-count.pl at $timestamp
# Original file: $ini_file
END
# load original moses.ini & generate new moses.ini
while (<$ini_hdl>) {
chomp(my $line = $_);
my $do_print = 1;
if ($line =~ m/^\s*#/ || $line =~ m/^\s*$/) {
#ignore empty and commented lines
} elsif ($line =~ m/^\[(.*)\]/) {
$section = $1; # start of a new section
} else {
if (defined $section_handlers{$section}) {
# call appropriate section handler;
# handlers are also responsible for printing out
# (possibly modified) line into new moses.ini
$do_print = 0;
$section_handlers{$section}->($line, $outini_hdl);
}
}
if ($do_print) {
print $outini_hdl "$line\n";
}
}
close $outini_hdl;
# write filtered phrase tables
for my $ttable (keys %ttables) {
filter_table($ttables{$ttable});
}
# filter phrase tables
## subroutines
sub read_ttable_file
{
my $ttable_id = 0;
return sub {
my ($line, $outhdl) = @_;
if ($line !~ m/^(\d+) ([\d\,\-]+) ([\d\,\-]+) (\d+) (\S+)$/) {
die "Format not recognized: $line";
}
my ($type, $srcfacts, $tgtfacts, $numscores, $file) = ($1, $2, $3, $4, $5);
if ($type != 0) {
die "Cannot work with ttables of type $type";
}
$ttables{$ttable_id} = {
file => $file,
scores => $numscores
};
print $outhdl
"$type $srcfacts $tgtfacts $numscores $targetdir/", basename($file), "\n";
$ttable_id++;
}
}
sub read_ttable_limit
{
my $ttable_id = 0;
return sub {
my ($line, $outhdl) = @_;
$ttables{$ttable_id}->{limit} = $line;
print $outhdl "$line\n";
$ttable_id++;
}
}
sub read_weight_t
{
my $weight_idx = 0;
my $ttable_id = 0;
return sub {
my ($line, $outhdl) = @_;
if ($ttables{$ttable_id}->{scores} == $weight_idx) {
$weight_idx = 0;
$ttable_id++;
}
push @{ $ttables{$ttable_id}->{weights} }, $line;
print $outhdl "$line\n";
$weight_idx++;
}
}
sub filter_table
{
my $ttable = shift;
my $in = my_open($ttable->{file});
my $out = my_save($targetdir . "/" . basename($ttable->{file}));
my $limit = $ttable->{limit};
my @weights = @{ $ttable->{weights} };
print STDERR "Filtering ", $ttable->{file}, ", using limit $limit\n";
my $kept = 0;
my $total = 0;
my $src_phrase = "";
my @tgt_phrases;
while (<$in>) {
chomp(my $line = $_);
$total++;
print STDERR '.' if $total % 1000 == 0;
my @cols = split / \|\|\| /, $line;
if ($cols[0] ne $src_phrase) {
my @sorted = sort { $b->{score} <=> $a->{score} } @tgt_phrases;
for my $phrase (@sorted[0 .. min($#sorted, $limit - 1)]) {
$kept++;
print $out $phrase->{str}, "\n";
}
$src_phrase = $cols[0];
@tgt_phrases = ();
}
my @scores = split ' ', $cols[2];
push @tgt_phrases, {
str => $line,
score => sum(map { $weights[$_] * log $scores[$_] } (0 .. $#weights))
};
}
printf STDERR "Finished, kept %d%% of phrases\n", $kept / $total * 100;
close $in;
close $out;
}
sub my_open {
my $f = shift;
die "Not found: $f" if ! -e $f;
my $opn;
my $hdl;
my $ft = `file $f`;
# file might not recognize some files!
if ($f =~ /\.gz$/ || $ft =~ /gzip compressed data/) {
$opn = "zcat $f |";
} elsif ($f =~ /\.bz2$/ || $ft =~ /bzip2 compressed data/) {
$opn = "bzcat $f |";
} else {
$opn = "$f";
}
open $hdl, $opn or die "Can't open '$opn': $!";
binmode $hdl, ":utf8";
return $hdl;
}
sub my_save {
my $f = shift;
if ($f eq "-") {
binmode(STDOUT, ":utf8");
return *STDOUT;
}
my $opn;
my $hdl;
# file might not recognize some files!
if ($f =~ /\.gz$/) {
$opn = "| gzip -c > '$f'";
} elsif ($f =~ /\.bz2$/) {
$opn = "| bzip2 > '$f'";
} else {
$opn = ">$f";
}
mkpath( dirname($f) );
open $hdl, $opn or die "Can't write to '$opn': $!";
binmode $hdl, ":utf8";
return $hdl;
}
|