use strict;
use warnings;

my $f = $ARGV[0];

open (L, "$f.logon");
open (SMT, "$f.smt");
open (OA, "$f.oa");

$/="\n\n";


my %smt;
my %oa;

while (<SMT>) {

    my $input;
    my $output;
    my @l = split(/\n/);
    foreach my $l (@l) {
	
	if ($l =~ s/^\|< \|//) {
	    $l =~ s/\|.*//;
	    $input = $l;
	}
	elsif ($l =~ s/^\|> \|//) {
	    $l =~ s/\|.*//;
	    $output = $l;
	}
    }

    $smt{$input}=$output;

}

while (<OA>) {

    my $input;
    my $output;
    my @l = split(/\n/);
    foreach my $l (@l) {
	
	if ($l =~ s/^\|< \|//) {
	    $l =~ s/\|.*//;
	    $input = $l;
	}
	elsif ($l =~ s/^\|> \|//) {
	    $l =~ s/\|.*//;
	    $output = $l;
	}
    }

    $oa{$input}=$output;


}


while (<L>) {

    #    the |< (source sentence) and |@ (reference translations) lines have the exact
    #    same interpretation as in the original fan-out log file.  the |= output is the 
    #    one from the oracle annotations; the |> output is the one ranked highest by the
    #    LOGON demonstrator, i.e. the one that was output at the top; finally, the |?
    #    line shows the output with the highest BLEU score (in case there are ties, the
    #    first such output is used, according to the LOGON ranking).

#|< |Moslifjell|
#|@ |Moslifjell|
#|@ |Moslifjell|
#|@ |Mt. Moslifjell|
#|= |Moslifjell.|
#|> |Moslifjell.|
#|? |Moslifjell.|

    my $input;
    my $oracle;
    my $bleu;
    my $top;

    my @l = split(/\n/);
    foreach my $l (@l) {
	
	if ($l =~ s/^\|< \|//) {
	    $l =~ s/\|.*//;
	    $input = $l;
	}
	elsif ($l =~ s/^\|> \|//) {
	    $l =~ s/\|.*//;
	    $top = $l;
	}
	elsif ($l =~ s/^\|= \|//) {
	    $l =~ s/\|.*//;
	    $oracle = $l;
	}
	elsif ($l =~ s/^\|\? \|//) {
	    $l =~ s/\|.*//;
	    $bleu = $l;
	}
    }

    my $oa = $oa{$input};
    my $smt = $smt{$input};

    print "INPUT:\t$input\n";
    print "ORACLE:\t$oracle\n";
    print "TOP:\t$top\n";
    print "BLEU:\t$bleu\n";
    print "OA:\t$oa\n";
    print "SMT:\t$smt\n\n";


}

