<?php

	include_once("common.php");
	
	head("Statistics");
	//ini_set('display_errors', '0');

	$line = load_judgements();
	for($i=0;$i<count($line);$i++) {
		list($t,$j,$assignment_id,$type,$judgement,$time) = split(" \|\|\| ",$line[$i]);
		$score_list = split(" ",$judgement);
		foreach($score_list as $system_score) {
			list($system,$score) = split(":",$system_score);
			if($type == "NIST") {
				list($adequacy,$fluency) = split(",",$score);
				if ($adequacy != "-") {
					$COUNT_TS [$t][$system][$type."_ADEQUACY"] += $adequacy;
					$TOTAL_TS [$t][$system][$type."_ADEQUACY"]++;
					$TOTAL_TSJ[$t][$system][$j][$type."_ADEQUACY"]++;
				}
				if ($fluency != "-") {
					$COUNT_TS [$t][$system][$type."_FLUENCY"] += $fluency;
					$TOTAL_TS [$t][$system][$type."_FLUENCY"]++;
					$TOTAL_TSJ[$t][$system][$j][$type."_FLUENCY"]++;
				}
				


			} else {
				$COUNT_TS [$t][$system][$type] += $score;
				$COUNT_TSJ[$t][$system][$j][$type] += $score;
				$TOTAL_TS [$t][$system][$type]++;
				$TOTAL_TSJ[$t][$system][$j][$type]++;
			}
		}
		$COUNT_TJ [$t][$j]++;
		$COUNT_J [$j]++;
		$COUNT_T [$t]++;
	}
	?>
	<H2>Annotators</H2>
	<TABLE BORDER=1>
	<TR><TH>Annotator</TH><TH>Judged</TH></TR>
	<?php
	foreach($COUNT_J as $j => $count) {
		print("<TR><TD>".$j."</TD><TD>".$count."</TD></TR>");
	}
	?>
	</TABLE>
	<H2>Tasks</H2>
	<?php	
	$test_type = array("NIST_ADEQUACY","NIST_FLUENCY","RANK","CONSTITUENT");

	while (list($t, $dummy) = each($COUNT_TS)) {
		?>
		<H3><?php print($t);?></H3>
		<TABLE BORDER=1>
		<TR><TH>System</TH>
		<?php
		foreach($test_type as $test) {
			print("<TH>$test</TH>");
		}
		print("<TH>TOTAL</TH></TR>");
		$test_total = array();
		while (list($system, $dummy) = each($COUNT_TS[$t])) {
			$sys_total = 0;
			$test_total[$test] = 0;
			$sys = preg_replace("/(.+\/)([^\/]+)/","<FONT SIZE=-3 COLOR=#808080>$1</FONT>$2",$system);
			print "<TR><TD>$sys</TD>";
			foreach($test_type as $test) {
				print"<TD>";
				if ($COUNT_TS[$t][$system][$test]) {
					print number_format($COUNT_TS[$t][$system][$test]/$TOTAL_TS[$t][$system][$test],2)
					." (".($TOTAL_TS[$t][$system][$test]).")";
					$sys_total+= $TOTAL_TS[$t][$system][$test];
					$test_total[$test] += $TOTAL_TS[$t][$system][$test];
				} else {
					print"0.0 (0)";
				}
				
				print"</TD>";
			}
			print "<TD>$sys_total</TD></TR>\n";      
		}
		print("<TR><TD><B>TOTAL</B></TD>");
		foreach($test_type as $test) {
			print("<TD><B>".number_format($test_total[$test],0)."<?B></TD>");
		}
		?></TR></TABLE>
		<!--<P>Per annotator: <TABLE BORDER=1>-->
		<?php /*
		while (list($system, $dummy) = each($COUNT_TSJ[$t])) {
			$sys = preg_replace("/(.+\/)([^\/]+)/","<FONT SIZE=-3 COLOR=#808080>$1</FONT>$2",$system);
			while (list($annotator, $dummy) = each($COUNT_TSJ[$t][$system])) {
				print "<TR><TD>$sys</TD><TD>$annotator</TD><TD>Adq: ".
				number_format($COUNT_TSJ[$t][$system][$annotator]["ADEQUACY"]/$TOTAL_TSJ[$t][$system][$annotator]["ADEQUACY"],2)
				." (".($TOTAL_TSJ[$t][$system][$annotator]["ADEQUACY"]).")"
				."</TD><TD>Flcy: ".
				number_format($COUNT_TSJ[$t][$system][$annotator]["FLUENCY"]/$TOTAL_TSJ[$t][$system][$annotator]["FLUENCY"],2)
				." (".($TOTAL_TSJ[$t][$system][$annotator]["FLUENCY"]).")"
				."</TD></TR>\n";      
				$sys = "&nbsp;";
			}
		}*/
		?><!--</TABLE>--><?php
	}
	
	tail();

