Welcome to mirror list, hosted at ThFree Co, Russian Federation.

exodus.perl « training « scripts - github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 32d2e24a29c1dde7cf304fb129473e878f534307 (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
#!/usr/bin/perl -w

# $Id: exodus.perl 1308 2007-03-14 22:30:25Z hieuhoang1972 $

use strict;

my @LINE = <STDIN>;

print "# Moses configuration file\n";
print "# automatic exodus from pharaoh.ini ".`date`;
print "\n";

# replicate the old header
my $header = 0;
while($LINE[$header] =~ /^$/ || $LINE[$header] =~ /^\#/) {
    $header++;
}
for(my $i=0;$i<$header;$i++) { print $LINE[$i]; }

# read Pharaoh parameters that will be absorbed
my(@LMODEL_TYPE, @DISTORTION_TYPE);
for(my $i=$header;$i<=$#LINE;$i++) {
    # language model specification
    if ($LINE[$i] =~ /^\[lmodel-type\]/) {
	$i = &read(\@LMODEL_TYPE,$i);
	foreach (@LMODEL_TYPE) {
	    $_ = "3gram" if $_ eq "normal";
	    $_ = "3gram-".$_ unless /\d/;
            $_ =~ s/gram//;
	}
    }
    # distortion model specification
    elsif ($LINE[$i] =~ /^\[distortion-type\]/) {
        my @DT;
	$i = &read(\@DT,$i);
        foreach (@DT) {
	    next if /distance/;
	    s/orientation/msd/;
            s/monotonicity/monotone/;
            push @DISTORTION_TYPE,$_;
        }
    }
}
# adapt/replicate Pharaoh parameters
for(my $i=$header;$i<=$#LINE;$i++) {
    # parameters to be dropped
    if ($LINE[$i] =~ /^\[lmodel-type\]/) {
	my @DUMMY;
	$i = &read(\@DUMMY,$i);
    }
    # 
		elsif ($LINE[$i] =~ /^\[distortion-type\]/) {
				my @DISTORTION_TYPE;
				$i = &read(\@DISTORTION_TYPE,$i);
        foreach (@DISTORTION_TYPE) {
						next if /distance/;
						s/orientation/msd/;
                                                s/monotonicity/monotone/;
                                                s/unidirectional/backward/;
				} 
    }
    # parameters to be changed
    elsif ($LINE[$i] =~ /^\[lmodel-file\]/) {
	print $LINE[$i];	
	# add language model type, factors
	my @LMODEL_FILE;
	$i = &read(\@LMODEL_FILE,$i);
	for(my $j=0;$j<=$#LMODEL_FILE;$j++) {
	    print "0 0 ";
	    if (defined($LMODEL_TYPE[$j])) {
		print $LMODEL_TYPE[$j];
	    }
	    else {
		print "3";
	    }
	    print " $LMODEL_FILE[$j]\n";
	}
        print "\n";
    }
    elsif ($LINE[$i] =~ /^\[ttable-file\]/) {
	print $LINE[$i];
	# add factors
	my @TTABLE_FILE;
	$i = &read(\@TTABLE_FILE,$i);
	my $first_line;
	if (-e $TTABLE_FILE[0]) {
	    if ($TTABLE_FILE[0] =~ /\.gz$/) { 
		$first_line = `zcat $TTABLE_FILE[0] | head -1`;
	    }
	    else {
		$first_line = `head -1 $TTABLE_FILE[0]`;
	    }
	}
	elsif (-e $TTABLE_FILE[0].".gz") {
	    $first_line = `zcat $TTABLE_FILE[0] | head -1`;
	}
	else {
	    print STDERR "ERROR: Thou shalt have a translation table in '$TTABLE_FILE[0]'\n";
	    exit;
	}
	chop($first_line);
	my ($f,$e,$p) = split(/ \|\|\| /,$first_line);
	$p =~ s/ +/ /g; $p =~ s/^ //; $p =~ s/ $//;
	my @P = split(/ /,$p);
	my $p_count = scalar @P;
	print "0 0 $p_count $TTABLE_FILE[0]\n\n";
    }
    elsif ($LINE[$i] =~ /^\[distortion-file\]/) {
	print $LINE[$i];
	my @DISTORTION_FILE;
	$i = &read(\@DISTORTION_FILE,$i);
	for(my $j=0;$j<=$#DISTORTION_FILE;$j++) {
	    if (!defined($DISTORTION_TYPE[$j])) {
                die("ERROR: no distortion type specified for distortion file $DISTORTION_FILE[$j]\n");
            }
            my $weight_count = 2;
	    $weight_count++  if $DISTORTION_TYPE[$j] =~ /msd/;
	    $weight_count*=2 if $DISTORTION_TYPE[$j] =~ /fe/;
	    print "0-0 $DISTORTION_TYPE[$j] $weight_count $DISTORTION_FILE[$j]\n";
	}
        print "\n";
    }
    elsif ($LINE[$i] =~ /\# distortion \(reordering\) type/) {}
    else {
	# keep unchanged
	print $LINE[$i];
    }
}

# add Moses-specific configuration

print "\n[input-factors]\n0\n\n";
print "[mapping]\nT 0\n\n";

# sub: read values for one parameter
sub read {
    my ($VALUE,$i) = @_;
    $i++;
    while($i<=$#LINE && $LINE[$i] !~ /^\[/) {
	if ($LINE[$i] !~ /^\s*$/ && # ignore comments and empty lines
	    $LINE[$i] !~ /^\#/) { 
	    # store value
	    my $line = $LINE[$i];
	    chop($line);
	    push @{$VALUE},$line;
	}
	$i++;
    }
    $i--;

    # leave comments above next parameter
    while($LINE[$i] =~ /^\#/) { $i--; }

    return $i;
}