#!/usr/local/bin/perl

use IPC::Open2;

#$command = "/shared/code/Lexicon/src/GetProbs";
$command = "/shared/code/Lexicon/src/TestLexiconSALM";
$probfile = "/barrow/usr1/shared/data/Chinese/GIZA.fge";
#$probfile = "/shared/data/Chinese/GIZA.egf";
$fgepid = open2($fgeread, $fgewrite, $command, $probfile);

binmode($fgewrite, ":encoding(gbk)");
#binmode($fgeread, ":encoding(gbk)");


$line = <$fgeread>; print $line;
$line = <$fgeread>; print $line;
$line = <$fgeread>; print $line;
$line = <$fgeread>; print $line;
$line = <$fgeread>; print $line;

$command = "/shared/code/Lexicon/src/TestLexiconWords";
$probfile = "/barrow/usr1/shared/data/Chinese/GIZA.egf";
#$probfile = "/shared/data/Chinese/GIZA.egf";
$egfpid = open2($egfread, $egfwrite, $command, $probfile);

binmode($egfwrite, ":encoding(gbk)");
#binmode($fgeread, ":encoding(gbk)");


#$line = <$egfread>; print $line;
#$line = <$egfread>; print $line;
$line = <$egfread>; print $line;
$line = <$egfread>; print $line;
$line = <$egfread>; print $line;


#require "lexProbs.pl";
#&loadProbs($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-trans2.txt";

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

#$nbest = "/shared/data/Chinese/mt03_chinese_evlset_v0-xfer3-070519-2.txt";
#$plustrans = "/shared/data/Chinese/mt03_chinese_evlset_v0-xfer3-070519-2-trans3.txt";

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

$nbest = "/shared/data/Chinese/GALE_DEV07/raw/GALE_DEV07_shadow-070530-1.txt";
$plustrans = "/shared/data/Chinese/GALE_DEV07/raw/GALE_DEV07_shadow-070530-trans.txt";


open(NBEST, "<:encoding(gbk)", $nbest) or die $!;
open(PTRANS, ">:encoding(gbk)", $plustrans) or die $!;
#open(OUT, ">:encoding(gbk)", "ceall.txt") or die $!;
while ($line = <NBEST>) {
    #print $line;
    if ($line =~ /SrcSent/) {
	print STDERR "$sentcount ";
	$sentcount++;
	print PTRANS $line;

	#print STDERR "BEST $minval\n$minsent\n";
	$minsent = "";
	$minval = 1000;
    } 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 .= lc(join(" ", @tgtwords)) . " ";
	}
	$src =~ s/^\s*//; $src =~ s/\s*$//; $src =~ s/\s+/ /g;
	$tgt =~ s/^\s*//; $tgt =~ s/\s*$//; $tgt =~ s/\s+/ /g;
	#print "$src\n$tgt\n";
	print $fgewrite "$src\n"; # Chinese
	print $fgewrite "$tgt\n"; # English
	$tprob = <$fgeread>;
	chomp($tprob);
	if ($tprob < $minval) {
	    $minval = $tprob;
	    $minsent = "$src\n$tgt\n";
	}
	#print STDERR "tprob $tprob\n";
	$avgprob = <$fgeread>;
	chomp($avgprob);
	$tprob = -$tprob;
	$overall =~ s/TransSGT: ([^,]+)/TransSGT: $tprob/;


	print $egfwrite "$tgt\n"; # English
	print $egfwrite "$src\n"; # Chinese
	$tprob = <$egfread>;
	chomp($tprob);
	if ($tprob < $minval) {
	    $minval = $tprob;
	    $minsent = "$src\n$tgt\n";
	}
	#print STDERR "tprob $tprob\n";
	$avgprob = <$egfread>;
	chomp($avgprob);

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

	#print "avg $avgprob\n";
	#print "$tprob\t$avgprob\n\n";

    } else {
	print PTRANS $line;
    }
}
close(NBEST);
close(PTRANS);
#close(OUT);

close($fgepid);close($egfpid);
