#!/usr/bin/perl 

# Score each of a list of KIS runs

$closeVideoSearchTopicResult = "</videoSearchTopicResult";
$closeVideoSearchRunResult = "</videoSearchRunResult";
$knownItemAnswerFile = "/home/barney1/trecvid/10/known.item.search/A.queries.truth";
$minTopic = 1;
$maxTopicF = 300;
$maxTopicI = 24;
$maxTopic = 0;
$bigInt = 9999999999999999999999999999999999999;


# topics to be ignored
$dropped[8] = 1;
$dropped[11] = 1;
$ignoredCount = 2;


# Read in the known-item answers

open TRUTH, "$knownItemAnswerFile" or die "Can't open $knownItemAnswerFile: $!\n";
while ($line = <TRUTH>)
{
    ($topic,$fileNum) = split /\s+/,$line;
    $truth[$topic] = $fileNum;
}
close TRUTH;

# Read in each submission file

for my $infile (@ARGV) 
{
    open(IN,$infile) or die "Cannot open $infile: $!\n";
    while ($line = <IN>)
    {

	if ($line =~ /<videoSearchRunResult/)
	{
	    # Initialize run-level data
	    $sum_etime = 0;
            $sum_score = 0;
	    $topicCount = 0;
	    @topics =  ();
	    
	}

	($attr1,$val1,$attr2,$val2,$rest) = split /[\"\']/,$line;

	if ($attr1 =~ /sysId/)
	{
	    $sysid =  $val1;
	}

	elsif ($attr1 =~ /priority/)
	{
	    $priority =  $val1;
	}

	elsif ($attr1 =~ /pType/)
	{
	    $ptype =  $val1;

	    if ($ptype eq "F")
	    {
		$maxTopic = $maxTopicF;
	    }
	    elsif ($ptype eq "I")
	    {
		$maxTopic = $maxTopicI;
	    }
	    else
	    {
		die "Invalid processing type \"$ptype\"\n";
	    }
	}

	elsif ($attr1 =~ /trType/)
	{
	    $trtype =  $val1;
	}

	elsif ($attr1 =~ /userSat/)
	{
	    $usersat =  $val1; 


	}

	elsif ($attr1 =~ /condition/)
	{
            $us="_";
	    $condition =  $val1;
            $runid = "$ptype$us$trtype$us$condition$us$sysid$us$priority";
            $scorefile = "../../scores/$runid.scores";
            open SCORES, ">$scorefile" or die "Can't open $scorefile: $!\n";
	    printf SCORES " Run ID                                 Topic  1/Rank   Time  KI:rank file#\n";
	}

        # Start a new result set for topic tNum
	if ($line =~ /<videoSearchTopicResult/)
	{
            $itemCount = 0;
            @result = ();
            $minSeqNum = $bigInt;
            $maxSeqNum = -1;
	    $tnum = $val1+0;
     
	    if ($dropped[$tnum] == 1)
	    {
		next;
	    }

            $topicCount++;
            $tnum = $val1;
            $etime = $val2;
	    if ($etime <= 0)
	    {
		print SCORES "ERROR: Invalid elapsedTime: \"$etime\"\n";
	    }
            $sum_etime += $etime;
	    if ($tnum > $maxTopic or $tnum < $minTopic)
	    {
		print SCORES "ERROR: Invalid topic: \"$tnum\"\n";
	    }
	    $topics[$tnum]++;
	    $rank = 0;
	    $scoreRank = 0;
	    $scoreFile = "--";
 	}

	if ($line =~ /<item seqNum/)
	{
            $itemCount++;
            $result[$val1] = $val2;

            # Adjust min and/or max sequence number
            if ($val1>$maxSeqNum)
            {
               $maxSeqNum = $val1;
            }
            if ($val1<$minSeqNum)
            {
               $minSeqNum = $val1;
            }
	}

        # End of result for topic; calculate score
        if ($line =~ /$closeVideoSearchTopicResult/)
	{
	    if ($dropped[$tnum] == 1)
	    {
		printf SCORES "%-40s  %3d\n",$runid,$tnum;
		next;
	    }

            $score = 0;

            # Override the default 0 score if result non-empty 
            if ($itemCount > 0)
            {

               for ($i=$minSeqNum; $i<=$maxSeqNum; $i++)
               {
                   if ($result[$i] != 0) 
                   {
                       $rank++;
                       # If the answer in the result matches the true answer
                       if ($result[$i] == $truth[$tnum])
                       {
                          $score = 1/$rank;
			  $scoreRank = $rank;
			  $scoreFile = $result[$i];
                          last;
                       }
                    }
                    else # Gap in sequence numbers
                    {
                         # Ignore gap
                    }
	       }
            }

            $sum_score += $score;

            printf SCORES "%-40s  %3d   %1.3f %6.3f    %5d  %s\n",$runid,$tnum,$score,$etime,$scoreRank,$scoreFile;

	}


	if ($line =~ /$closeVideoSearchRunResult/)
	{
	    if ($topicCount + $ignoredCount < $maxTopic)
	    {
		$diff = $maxTopic - $topicCount;
		print SCORES "ERROR: Missing topics: $diff\n";
	    }

	    $score = $sum_score/$topicCount;
	    $etime = $sum_etime/$topicCount;
          
            printf SCORES "----------------------------------------------------------------------------\n";  
	    printf SCORES "%-40s  all    meanInvertedRank      %1.3f\n",$runid,$score;
	    printf SCORES "%-40s  all    meanElapsedTime (min)%6.3f\n",$runid,$etime;
	    printf SCORES "%-40s  all    UserSat              %6.3f\n",$runid,$usersat;

	    for ($i=$minTopic;$i<=$maxTopic;$i++)
	    {
		if ($dropped[$i] == 1)
		{
		    next;
		}
		
		if ($topics[$i] != 1)
		{
		    $c = $topics[$i];
		    if ($c eq "")
		    {
			$c = 0;
		    }
		    print SCORES " ERROR $c topic $i\n";
		}
	    }

	    close SCORES or die "Can't close $scorefile: $!\n";

	}
    }
	       
    close IN;
}
