#!/usr/local/bin/perl

## Process transfer n-best file

use strict;
use warnings;

my $fileName = shift;

open(IFILE, $fileName) or die("Couldn't open the file $fileName\n");
my %data = ();
my $segCount = -1;
my $nbestCount = -1;
print "<DOC docid=\"document\" sysid=\"xfer-urdu\">\n";
while(<IFILE>){
	chomp;
	next if(/^\(/ or $_ eq "");
	if(/^SrcSent (\d+)/){
		print "</seg>\n" unless($segCount == -1);
		$segCount = $1+1;
		print "<seg id=$segCount>\n";
	}
	elsif(/^Overall:/){
		my @tokens = split /,/;
		for(my $i = 1; $i <= 6;$i++){
			$tokens[$i] =~ s/^\s+//;
			(my $name, my $val) = split /:\s+/, $tokens[$i];
			$val = $val/$tokens[$#tokens] if($i == 1 or $i == 3 or $i == 4);
			push @{${$data{"$segCount-$nbestCount"}}{'features'}}, $val;
		}
	}
	else{
		/^(\d+) (\d+)\s+(.+)$/;
		$nbestCount = $2+1;
		print "<nbest rank=\"$2\"> $3 </nbest>\n";
		${$data{"$segCount-$nbestCount"}}{'sent'} = $3;
	}
}
print "</seg>\n</DOC>\n";
close IFILE;

open(IFILE, "nbest.out") or die("Couldn't open the file nbest.out\n");
my $max = 0;
my $maxi = -1;
my $currSeg = 1;
my $currNBest = 1;
while(<IFILE>){
	chomp;
	(my $key , my $score) = split /\s/;
	(my $a, my $seg, my $nbest) = split /::/,$key;
	if($seg != $currSeg){
		my @fm = @{${$data{"$currSeg-$maxi"}}{'features'}};
		for(my $n = 1; $n <= 10; $n++){
			my @fl = @{${$data{"$currSeg-$n"}}{'features'}};
			my @diff = map {$fl[$_] - $fm[$_]} 0..$#fl;
			my @sign = grep {$_ > 0} @diff;
# 			print STDERR "Throw --> " if($#sign == -1 or $#sign == 5);
# 			print STDERR "@diff \n" unless($#sign == -1 or $#sign == 5);
		}
		$currSeg = $seg;
		$max = $score;
		$maxi = 1;
		$currNBest = 1;
	}
	elsif($score > $max){
		$max = $score;
		$maxi = $nbest;
	}
	$currNBest = $nbest;
	${$data{"$seg-$nbest"}}{'meteor'} = $score;
}
close IFILE;

open(IFILE, "svm.out") or die("Couldn't open the file svm.out\n");
my $max = 0;
my $cqid = 1;
my %bnb = ();
while (<IFILE>){
	chomp;
	my @tokens = split /\s+/;
	$tokens[2] =~ /^qid:(.+)$/;
	if($1 != $cqid){
# 		print STDERR "$cqid $bnb{$cqid} ${$data{\"$cqid-$bnb{$cqid}\"}}{'meteor'}\n${$data{\"$cqid-$bnb{$cqid}\"}}{'sent'}\n\n";
		print STDERR "${$data{\"$cqid-$bnb{$cqid}\"}}{'sent'}\n";
		$cqid = $1;
		$max = $tokens[0];
		$bnb{$1} = $tokens[$#tokens];
		next;
	}
	if($tokens[0] > $max){
		$max = $tokens[0];
		$bnb{$cqid} = $tokens[$#tokens];
	}
}
close IFILE;
# my @sorted = sort{
# 		(my $sa, my $na) = split /-/,$a;
# 		(my $sb, my $nb) = split /-/,$b;
# 		if($sa eq $sb){
# 			return ${$data{"$b"}}{'meteor'} <=> ${$data{"$a"}}{'meteor'};
# 		}
# 		else{
# 			return $sa <=> $sb;
# 		}
# 	} keys %data;
# 
# $currSeg = 1;
# my $count = 1000;
# foreach (@sorted){
# 	(my $seg, my $nbest) = split /-/;
# 	if($seg != $currSeg){
# 		$count = 1000;
# 		$currSeg = $seg;
# 	}
# # 	next if($count < 80);
# 	print STDERR "$count qid:$seg ";
# 	my $c = 1;
# 	map {print STDERR "$c:$_ ";$c++} @{${$data{$_}}{'features'}};
# 	print STDERR "# $nbest\n";
# 	$count--;
# }
