#!/usr/local/bin/perl

#if (@ARGV == 0) {
#    die " No arguments.  Exiting...
#Usage: scorexfer.pl -r reference_file
#                    -s sysid
#                    -c src_sgml_file
#                    hypothesis_file\n";
#} 

$srcset = "/afs/cs.cmu.edu/project/avenue-1/Avenue/Mapu-MT/Corpora/harmelinkTestSet.sgm";
$reference = "/afs/cs.cmu.edu/project/avenue-1/Avenue/Mapu-MT/Corpora/harmelinkTestSet.ref.sgm";
$sysid = "ANICESYSID";

print "FILE\tMETEOR\tMBLEU\tBLEU\n";

for ($i = 0; $i < @ARGV; $i++) {

    if ($ARGV[$i] eq "-r") {
	if ($i+1 < @ARGV) {
	    $reference = $ARGV[$i+1];
	    $i++;
	}
	next;

    } elsif ($ARGV[$i] eq "-s") {
	if ($i+1 < @ARGV) {
	    $sysid = $ARGV[$i+1];
	    $i++;
	}
	next;

    } elsif ($ARGV[$i] eq "-c") {
	if ($i+1 < @ARGV) {
	    $srcset = $ARGV[$i+1];
	    $i++;
	}
	next;

    }

    $arg = $ARGV[$i];
    if (!-e $arg) {
	print "Hypothesis file $arg does not exist.\n";
	exit;
    }

    if ($arg =~ m/sgm$/i) {
	$hypfile = $arg;
    } else {
	$hypfile = "/tmp/xfer-hypfile.sgm";
	`perl /afs/cs.cmu.edu/project/avenue-1/Avenue/Transfer/mktstset.pl $arg $srcset $sysid > $hypfile`;
    }
    
    # METEOR
    $meteorcommand = "perl -I/avenue/usr2/shared/Evaluation/MEMT /avenue/usr2/shared/Evaluation/MEMT/meteor.pl -d 10 -s $sysid -r $reference -t $hypfile 2> /dev/null |";
#print "METEOR: $meteorcommand\n";
    open(SCORE, $meteorcommand) or die $!;
    while ($line = <SCORE>) {
	#print "$line";
	if ($line =~ m/^Score,(.*)$/) {
	    $meteor = $1;
	}
    }
    close(SCORE);
    
    # Bleu
    
    $evalRoot = "/afs/cs.cmu.edu/usr/joy/Eval/evaluationScripts";
    # "$evalRoot/nist-v11/mteval-v11a-cmufix_b.pl"
    #print "Command: perl $evalRoot/bleuv09/bleu-v09e.pl -s $sysid -t $hypfile -r $reference";
    open(SCORE, "perl $evalRoot/bleuv09/bleu-v09e.pl -s $sysid -t $hypfile -r $reference |") or die $!;
    while ($line = <SCORE>) {
	if ($line =~ m/^BLEU,/) {
	    $line =~ m/^BLEU,(.+)$/;
	    $bleu = $1;
	    #print $line;
	    
	} elsif ($line =~ m/^Modified/) {
	    $line =~ m/^Modified BLEU,(.+)$/;
	    $modified = $1;
	    #print $line;
	}
    }
    close(SCORE);
    print "$arg\t$meteor\t$modified\t$bleu\n\n";
}
