#!/usr/local/bin/perl -w 
use utf8;

#---------------------------------------------------------------------------#
# Team (Stage 1) : CruzAzul ---->  Anand,  Zhuo,  Kristy.
# Reference: http://www.d.umn.edu/~tpederse/Courses/CS8995/Assign/stage1.html
#----------------------------------------------------------------------------#

$file1 = shift;
$file2 = shift;
$GOLD = 0; $MYOUT = 1; $FILE1_SIZE = 0; $FILE2_SIZE = 0;

#------------------------------ MAIN BEGIN ----------------------------------#
$CF = 1; create_set($file1);
$CF = 0; create_set($file2);
evaluate_bitexts();
#------------------------------ END OF MAIN ---------------------------------#

sub
evaluate_bitexts {
    $recall = 0; $precision = 0;
    # Get the cardinality of the sets A and A'.
    for($i=0;$i<$FILE1_SIZE;$i++) { $recall = $recall + $sentence_map1[$i][0]-1; }
    for($i=0;$i<$FILE2_SIZE;$i++) { $precision = $precision + $sentence_map2[$i][0]-1; }
    # Find the alignment matchings considering the minimum cardinality.
    $min=$recall if($recall < $precision);
    $min=$precision if($precision <= $recall); 
    $intersection = 0;
    for($i=0;$i<$min;$i++) {
	$n1 = $sentence_map1[$i][0];
	$n2 = $sentence_map2[$i][0];
	for($z1=2;$z1<=$n1;$z1++) {
	    for($z2=2;$z2<=$n2;$z2++) {
		$intersection++ if($sentence_map1[$i][$z1] == $sentence_map2[$i][$z2]);
	    }
	}
    }
    # Calculate the F-measure based on recall and precision. Standard tech.!!
    $F = 2*$intersection/($recall + $precision);
    print "Evaluation based on F-measure(out of 1) is = $F\n";
}
#----------------------------------------------------------------------------#
sub 
create_set {
    $file = shift;
    open(FILE,$file) or die "ERROR : Unable to open $file.\n Try again .....\n";
    # If the file exists , form the 2-d map representing the sentence # maps.
    while(<FILE>) {
	$line = $_;
	return if($line =~ m/<\/article>/);
	if($line =~ m/<alignment ([0-9|=| ]+)>/) {
	    $pattern = $1;
	    $pattern =~ s/^\s+//;
	    $pattern =~ s/\s+$//;
	    @maps = split(/ |=/,$pattern);
	    $sentence_map1[$FILE1_SIZE][0] = @maps if($CF == $GOLD);
	    $sentence_map2[$FILE2_SIZE][0] = @maps if($CF == $MYOUT);
	    $k1 = 1;
	    foreach $k ( @maps ){
		$sentence_map1[$FILE1_SIZE][$k1++] = $k if($CF == $GOLD);
		$sentence_map2[$FILE2_SIZE][$k1++] = $k if($CF == $MYOUT);
	    }
	    $FILE1_SIZE++ if($CF == $GOLD);
	    $FILE2_SIZE++ if($CF == $MYOUT);
	}
    }
    close(FILE);
}
#---------------------------------------------------------------------------#
