#!/usr/local/bin/perl

require "lexProbs.pl";

#$probfile = "/shared/data/Chinese/GIZA.egf";
$probfile = "/shared/data/Chinese/GIZA.fge";

&loadProbsGIZA($probfile);

binmode(STDOUT, ":utf8");

#$nbest = "/shared/data/Chinese/mt03_chinese_devset_v0-xfer3-070419a.txt";
#$plustrans = "/shared/data/Chinese/mt03_chinese_devset_v0-xfer3-070419a-trans.txt";

#$nbest = "/shared/data/Chinese/mt03_chinese_evlset_v0-xfer3-070425.txt";
#$plustrans = "/shared/data/Chinese/mt03_chinese_evlset_v0-xfer3-070425-trans.txt";

$nbest = "/shared/data/Chinese/mt03_chinese_evlset_v0-xfer3-070504-top1.txt";
$plustrans = "/shared/data/Chinese/mt03_chinese_evlset_v0-xfer3-070504-top1-trans.txt";

$sentcount = 0;
open(NBEST, "<:encoding(gbk)", $nbest) or die $!;
open(PTRANS, ">:encoding(gbk)", $plustrans) or die $!;
while ($line = <NBEST>) {
    #print $line;
    if ($line =~ m/^SrcSent/i) {
	print STDERR "$sentcount ";
	$sentcount++;
	print PTRANS $line;
	#print "Best:\n$maxsent\n$max\n\n" if $max != 0;
	$max = -100;
	$maxsent = "";
    } elsif ($line =~ m/^Overall:/) {
	$overall = $line;
	@traces = ();

	$src = ""; $tgt = "";
	while ($line !~ m/^\s*$/) {
	    #print $line;
	    $line = <NBEST>;
	    push @traces, $line;
	    last if $line =~ m/^\s*$/;
	    $line =~ m/:\s*([^\(]+)/;
	    $src .= $1;
	    (@tgtwords) = ($line =~ m/ \'([^\']+)\'\)/g);
	    $tgt .= join(" ", @tgtwords) . " ";
	}
	#print "$src\n$tgt\n";
	$prob = &transProb($src, $tgt);
	if ($prob != 0) {
	    $logprob = log($prob)/log(10);
	} else {
	    $logprob = -99.99;
	}
	@tgtwords = split(/\s+/, $tgt);
	$perword = $logprob/scalar(@tgtwords);

	if ($logprob > $max) {
	    $max = $logprob;
	    $maxsent = "$src\n$tgt";
	}

	$overall =~ s/Trans: ([^,]+)/Trans: $logprob/;
	print PTRANS $overall, join("", @traces);

	#print "$prob\t$logprob\t$perword\n\n";

    } else {
	print PTRANS $line;
    }

}
#print "Best:\n$maxsent\n$max\n\n";

close(NBEST);
