#!/usr/bin/perl 
use locale;
#open (out,">totalres.dat")|| die "Couldn't open lpmax.dat: $!\n";
$fcount=1;$ts=0;$td=0;
#$evaltype=1;#0 ise wordbased modelin wordbased sonuclari 1 ise wordbased modelin igbased sonuçlari da verilecek. 2 ise igbased model için hem igbased hem wordbased sonuclar verilecek
$crv=1;
$guide="svm";
$numArgs = $#ARGV + 1;
$maltoptions="";
$numArgs--;
$dirname=$ARGV[0];
$i=1;
while($numArgs>0){
    $maltoptions.=$ARGV[$i]." ";
    $i++;
    $numArgs--;
}

$totalf1=0;
	$totalf2=0;
	$totalsnt=0;
	$meansnt=0;
	undef @snt;
	$snt=0;
	$snttotal=0;
	$meansig=0;
	$sig=0;
	undef @sig;
	$meansigwopunc=0;
	$sigwopunc=0;
	undef @sigwopunc[$fcount];
	$meanf1=0;
	$meanigf1=0;
	undef @igf1;
	$stdevsumf1=0;
	$stdevsumigf1=0;
	$stdevsumsnt=0;
	$stdevsumsig=0;
	$stdevsumsigwopunc=0;
system "rm overalltest.conll";

    system "rm overalloutput.conll";
open (out,">>totalres.dat")|| die "Couldn't open lpmax.dat: $!\n";
    printf out "\n\fcount\tsnt_accuracy\tunlabeled\tlabeled\t\tlabeledsnt\tunlabeledWord\n";
open (t,">prec_recall.dat")|| die "Couldn't open lpmax.dat: $!\n";
open (t,">eval.html")|| die "Couldn't open eval.html: $!\n";
close t;

while ($fcount<=$crv){    
    	
    close out;
    open (out,">>totalres.dat")|| die "Couldn't open lpmax.dat: $!\n";    
    

#train with the projectives and test with the entire data
    system "cp ./gulsentest.conll test.conll";
	

    
    print "cv#$fcount: test.conll created";
    
    if($guide eq "svm"){
	
	    print "malt.exe -f svmoption1.txt $maltoptions\n";
	    system "malt.exe -f svmoption1.txt $maltoptions";
    }
    #print "perl verbpostprocessor.pl output.conll output.changed\n";
    #system "perl verbpostprocessor.pl output.conll output.changed";
    
    print "perl evaluate.pl test.conll output.conll\n";
    system "perl evaluate.pl test.conll output.conll";
    
    
    system "cat overalltest.conll test.conll > temp.conll";
    system "cp temp.conll overalltest.conll";
    
    
    system "cat overalloutput.conll output.conll > temp.conll";
    system "cp temp.conll overalloutput.conll";
    
    
#    print "perl eval.pl -g test.conll -s output.conll\n";
#    system "perl eval.pl -g test.conll -s output.conll >>eval.html";
    
    	open (prob,"res.dat")|| die "Couldn't open probability.txt: $!\n";    
	while ($probline =<prob>){
	    if($probline=~/truely connected ig\= (\d+)/){
		$hashkeyig=$1;
		next;
	    }
	    elsif($probline=~/truely connected dependency\= (\d+)/){
		$hashkey=$1;
		next;
	    }
	    elsif($probline=~/truely labeled\= (\d+)/){
		$truelab=$1;
		next;
	    }
	    elsif($probline=~/total count connected dependency\= (\d+)/){
		$hashkey2=$1;
		next;
	    }
	    elsif($probline=~/truely connected dependency1\= (\d+)/){
		$igwopunc=$1;
		next;
	    }
	    elsif($probline=~/total count connected dependency1\= (\d+)/){
		$totaligwopunc=$1;
		next;
	    }
	    elsif($probline=~/truely connected sentences \= (\d+)/){
		$snt=$1;
		next;
	    }
	    elsif($probline=~/truely labeled sentences \= (\d+)/){
		$sntlabels=$1;
		next;
	    }
	    
	    elsif($probline=~/truely connected igs \= (\d+)/){
		$sig=$1;
		next;
	    }
	    elsif($probline=~/truely connected igwopuncs \= (\d+)/){
		$sigwopunc=$1;
		next;
	    }
	    elsif($probline=~/total sentences\= (\d+)/){
		$snttotal=$1;
		printf out "$fcount\t";
		#printf out "fcount=$fcount\t $hashkey\/$hashkey2 \= \%%%2.2f\t",$hashkey*100/$hashkey2;
		#printf out "$hashkey\/$hashkey2 \= \%%%2.2f\t",$hashkey*100/$hashkey2;
		#printf out "$snt\/$snttotal \= \%%%2.2f\t",$snt*100/$snttotal;
		#printf out "$sig\/$snttotal \= \%%%2.2f\t",$sig*100/$snttotal;
		#printf out "$sigwopunc\/$snttotal \= \%%%2.2f\t",$sigwopunc*100/$snttotal;
		printf out " \%%%.1f($snt/$snttotal)\t",$snt*100/$snttotal;		
		#printf out " \%%%2.2f\t",$sig*100/$snttotal;
		#printf out " \%%%2.2f\t",$sigwopunc*100/$snttotal;
		$meansnt+=$snt*100/$snttotal;
		$snt[$fcount]=$snt*100/$snttotal;
		
		$meansntlabels+=$sntlabels*100/$snttotal;
		$sntlabels[$fcount]=$sntlabels*100/$snttotal;
		
		#$meansig+=$sig*100/$snttotal;
		#$sig[$fcount]=$sig*100/$snttotal;
		#$meansigwopunc+=$sigwopunc*100/$snttotal;
		#$sigwopunc[$fcount]=$sigwopunc*100/$snttotal;
		$p=$hashkey/$hashkey2;
		$f1=(2*$p*$p/($p+$p))*100;
		printf out "%.1f($hashkey/$hashkey2)\t",$f1;	    
		$totalf1+=$hashkey;
		$meanf1+=$f1;
		$f1[$fcount]=$f1;
		
		$p=$truelab/$hashkey2;
		$f1=(2*$p*$p/($p+$p))*100;
		printf out "%.1f($truelab/$hashkey2)\t",$f1;	    
		$totalf1+=$truelab;
		$meanlabeled+=$f1;
		$labeled[$fcount]=$f1;
		printf out " \%%%.1f($sntlabels/$snttotal)\t",$sntlabels*100/$snttotal;
		
		#if($evaltype==1){
		    #$p=$hashkeyig/$hashkey2;
		    #$f1=(2*$p*$p/($p+$p))*100;
		    #printf out "%2.2f\t",$f1;
		    #$meanigf1+=$f1;
		    #$igf1[$fcount]=$f1;
		    #$totalf2+=$hashkeyig;
    
		    $p=$igwopunc/$totaligwopunc;
		    $f1=(2*$p*$p/($p+$p))*100;
		    printf out "%2.2f\n",$f1;
		    $meanigwopunc+=$f1;
		    $igwopunc[$fcount]=$f1;
		#}
		
		
		$totalsnt+=$snt;
		
		next;
	    }
        }
    $fcount++;
    

}
	$meanf1/=$crv;$meanlabeled/=$crv;$meanigf1/=$crv;$meansnt/=$crv;$meansntlabels/=$crv;$meansig/=$crv;$meansigwopunc/=$crv;
	
	$meanigwopunc/=$crv;
	for($fcount=1;$fcount<=$crv;$fcount++){
	    $stdevsumf1+=($meanf1-$f1[$fcount])*($meanf1-$f1[$fcount]);
	    $stdevsuml+=($meanlabeled-$labeled[$fcount])*($meanlabeled-$labeled[$fcount]);
	    #$stdevsumigf1+=($meanigf1-$igf1[$fcount])*($meanigf1-$igf1[$fcount]);
	    $stdevsumsnt+=($meansnt-$snt[$fcount])*($meansnt-$snt[$fcount]);
	    $stdevsumsntlabels+=($meansntlabels-$sntlabels[$fcount])*($meansntlabels-$sntlabels[$fcount]);
	    #$stdevsumsig+=($meansig-$sig[$fcount])*($meansig-$sig[$fcount]);
	    #$stdevsumsigwopunc+=($meansigwopunc-$sigwopunc[$fcount])*($meansigwopunc-$sigwopunc[$fcount]);
	    #if($evaltype==1){
		$stdevsumigwopunc+=($meanigwopunc-$igwopunc[$fcount])*($meanigwopunc-$igwopunc[$fcount]);
	    #}
	}
	$stdevsumf1/=9;$stdevsuml/=9;$stdevsumigf1/=9;$stdevsumsnt/=9;$stdevsumsig/=9;$stdevsumsigwopunc/=9;$stdevsumsntlabels/=9;
	$stdevsumf1=(sqrt $stdevsumf1)/(sqrt $crv);
	$stdevsuml=(sqrt $stdevsuml)/(sqrt $crv);
	#$stdevsumigf1=(sqrt $stdevsumigf1)/(sqrt $crv);
	$stdevsumsnt=(sqrt $stdevsumsnt)/(sqrt $crv);
	$stdevsumsntlabels=(sqrt $stdevsumsntlabels)/(sqrt $crv);
	#$stdevsumsig=(sqrt $stdevsumsig)/(sqrt $crv);
	#$stdevsumsigwopunc=(sqrt $stdevsumsigwopunc)/(sqrt $crv);
	#if($evaltype==1){
	    $stdevsumigwopunc=(sqrt $stdevsumigwopunc)/(sqrt $crv);
	#}
#	printf out "unlabeled\t\t=\t\%%%.1f+-%.1f\t",$meanf1,$stdevsumf1;
#	printf out "min=%.1f,max=%.1f\n",$meanf1-$stdevsumf1,$meanf1+$stdevsumf1;
#	printf out "labeled\t\t=\t\%%%.1f+-%.1f\t",$meanlabeled,$stdevsuml;
#	printf out "min=%.1f,max=%.1f\n",$meanlabeled-$stdevsuml,$meanlabeled+$stdevsuml;
#	printf out "sntunlabeled=\t\%%%.1f+-%.1f\t",$meansnt,$stdevsumsnt;
#	printf out "min=%.1f,max=%.1f\n",$meansnt-$stdevsumsnt,$meansnt+$stdevsumsnt;
#	printf out "sntlabeled\t=\t\%%%.1f+-%.1f\t",$meansntlabels,$stdevsumsntlabels;
#	printf out "min=%.1f,max=%.1f\n",$meansntlabels-$stdevsumsntlabels,$meansntlabels+$stdevsumsntlabels;
#	#printf out "igf1\t\t=\t\%%%2.2f+-%2.2f\t",$meanigf1,$stdevsumigf1;
#	#printf out "min=%2.2f,max=%2.2f\n",$meanigf1-$stdevsumigf1,$meanigf1+$stdevsumigf1;
#	#if($evaltype==1){
#	    printf out "word_wopunc\t=\t\%%%2.2f+-%2.2f\t",$meanigwopunc,$stdevsumigwopunc;
#	    printf out "min=%2.2f,max=%2.2f\n",$meanigwopunc-$stdevsumigwopunc,$meanigwopunc+$stdevsumigwopunc;
#	#}
#	#printf out "sig\t\t=\t\%%%2.2f+-%2.2f\t",$meansig,$stdevsumsig;
#	#printf out "min=%2.2f,max=%2.2f\n",$meansig-$stdevsumsig,$meansig+$stdevsumsig;
#	#printf out "sigwopunc\t=\t\%%%2.2f+-%2.2f\t",$meansigwopunc,$stdevsumsigwopunc;
#	#printf out "min=%2.2f,max=%2.2f\n",$meansigwopunc-$stdevsumsigwopunc,$meansigwopunc+$stdevsumsigwopunc;
	
	
	printf out "IG-IG AS_U\t=\t\%%%.1f+-%.1f\n",$meanf1,$stdevsumf1;
	
	printf out "IG-IG AS_L\t=\t\%%%.1f+-%.1f\n",$meanlabeled,$stdevsuml;
	
	printf out "exactmatch U\t=\t\%%%.1f+-%.1f\n",$meansnt,$stdevsumsnt;
	
	printf out "exactmatch L\t=\t\%%%.1f+-%.1f\n",$meansntlabels,$stdevsumsntlabels;
	
	printf out "W-W AS_U\t=\t\%%%2.1f+-%2.1f\n",$meanigwopunc,$stdevsumigwopunc;
	
	
    close out;
    close out2;
