<?php

	ini_set('memory_limit',"32M");

	include_once("common.php");

	load_global_task_info();
	
	
	
	head("Statistics");
	//ini_set('display_errors', '0');

	$line = load_judgements();

	$test_stat_types = array();

	for($i=0;$i<count($line);$i++) {
		list($t,$j,$assignment_id,$type,$judgement,$time) = split(" \|\|\| ",$line[$i]);
		$score_list = split(" ",$judgement);
		$score_total = 0;
		$system_score_list=array();
		list($ignore,$sentence_id) = split("-",$assignment_id);
		
		//load file for this type
		$include_file = get_global_task_property('class_file',$type,true,$t);
		include_once($include_file);	

		foreach($score_list as $system_score) {
			list($system,$score) = split(":",$system_score);
			
			#for normalization
			$system_score_list[$system]=$score;

			if(call_user_func(array($type,'is_numerical_score'))) {
				$score_total += $score;
			}
		}
				
		//process each score according to test type
		$processed_scores = call_user_func(array($type,'process_statistics'),$system_score_list,$score_total);
		
		foreach ($processed_scores as $system => $test_scores) {
			foreach($test_scores as $type=>$score) {
				$COUNT_TS [$t][$system][$type] += $score;
				$TOTAL_TS [$t][$system][$type]++;
				$TOTAL_TSJ[$t][$system][$j][$type]++;
				if ($UNIQUE_ASSIGNMENTS[$t][$j][$sentence_id] == 0) {
					#only unique judges
					$COUNT_TSA [$t][$system][$type][$sentence_id] += $score;
					$TOTAL_TSA [$t][$system][$type][$sentence_id]++;
				}
				$COUNT_TType[$t][$type]++;
			}
			$test_stat_types[$type] = true;
		}
		
		//add global stats
		$COUNT_TJ [$t][$j]++;
		$COUNT_J [$j]++;
		$TIME_J [$j]+= (($time > 300) ? 300 : $time);
		$COUNT_T [$t]++;


		$UNIQUE_ASSIGNMENTS [$t][$j][$sentence_id]++;
	}
	?>
	<H2>Annotators</H2>
	<TABLE BORDER=1>
	<TR><TH>Annotator</TH><TH>Judged</TH><TH>Time<br>(hhh:mm:ss)</TH><TH>Sec / Judgement</TH></TR>
	<?php
        $all_items=0;
        $all_time=0;
		arsort($COUNT_J);
		foreach($COUNT_J as $j => $count) {
		$all_time+=$TIME_J[$j];
		$all_count+=$count;
		$sec=$TIME_J[$j] % 60;
		$min=floor($TIME_J[$j] / 60) % 60;
		$hour=floor($TIME_J[$j] / 3600);
		print("<TR><TD>".$j."</TD><TD>".$count."</TD><TD align=\"right\">".sprintf("%02d:%02d:%02d",$hour,$min,$sec)."</TD><TD align=\"right\">".sprintf("%01.2f",$TIME_J[$j]/$count)."</TD></TR>");
	}
	print("<tr><td><B>TOTAL</B></td><td><B>".$all_count."</b></td><td align=\"right\"><b>".sprintf("%03d:%02d:%02d",floor($all_time / 3600),floor($all_time / 60) % 60,$all_time % 60)."</b></td><td align=\"right\"><b>".sprintf("%01.2f",$all_time/$all_count)."</b></td></tr>");
	?>
	</TABLE>
	
	<BR><HR>
	
	
	<H2>Tasks Summary</H2>
	<TABLE BORDER=1>
	<TR><TH>Task</TH>
	<?php
	//TODO declare this automagically
	//$test_type = array("NIST_ADEQUACY","NIST_FLUENCY","RANK_NORMALIZED","CONSTITUENT_NORMALIZED");
	$test_type = array_keys($test_stat_types);
	sort($test_type);
	foreach($test_type as $test) {
       		print("<TH>$test</TH>");
	}
	print("</TR>");
	ksort($COUNT_TType);
	foreach(array_keys($COUNT_TType) as $t) {
		print("<TR><TD><B>".$t."</B></TD>");
		foreach($test_type as $test) {
			print("<TD align=right>".($COUNT_TType[$t][$test]?$COUNT_TType[$t][$test]:"0")."</TD>");
		}
		print("</TR>");
	}


?></TABLE>

	<BR>
	<HR>
	<H2>Tasks Breakdown</H2>
	<?php	
	#$test_type = array("NIST_ADEQUACY","NIST_FLUENCY","RANK","RANK_NORMALIZED","CONSTITUENT","CONSTITUENT_NORMALIZED");

	ksort($COUNT_TS);
	foreach (array_keys($COUNT_TS) as $t) {
		$AVG_SCORE = array();
		?>
		<H3><?php print($t);?></H3>
		<TABLE BORDER=1>
		<TR><TH>System</TH>
		<?php
		foreach($test_type as $test) {
			print("<TH>$test</TH>");
			$test_total[$test] = 0;

		}
		print("<TH>TOTAL</TH></TR>");
		$test_total = array();

		#pre-compute best score
      		foreach (array_keys($COUNT_TS[$t]) as $system) {
			foreach($test_type as $test) {
				if ($COUNT_TS[$t][$system][$test]) {
					$AVG_SCORE[$test][$system] = $COUNT_TS[$t][$system][$test]/$TOTAL_TS[$t][$system][$test];
				}
			}
		}

		foreach ($test_type as $test) {
			if ($AVG_SCORE[$test]) asort($AVG_SCORE[$test]);
		}

		foreach (array_keys($COUNT_TS[$t]) as $system) {
			$sys_total = 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]) {
					$avg_score = $AVG_SCORE[$test][$system];
					$best_system = array_pop(array_keys($AVG_SCORE[$test]));
					if ($best_system == $system) print("<font color=red>");
					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];
					if ($best_system == $system) print("</font>");
				} else {
					print"0.0 (0)";
				}
				
				print"</TD>";
			}
			print "<TD><b>$sys_total</b></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>
	       <?php
	       }
	?>
	<BR><HR>
	<H2>Most Repeated Sentences (Unique Users)</H2>
	<?php	
	foreach (array_keys($COUNT_TS) as $t) {
		?>
		<H3><?php print($t);?></H3>
		<TABLE BORDER=1>
		<TR><TH>System</TH>
		<?php
		foreach($test_type as $test) {
			print("<TH>$test</TH>");
		}
		print("</TR>");
	
		foreach (array_keys($COUNT_TS[$t]) as $system) {
	
			print "<TR><TD valign='top'>$system</TD>";
			foreach($test_type as $test) {
				print"<TD valign='top'>";
				if (sizeof($TOTAL_TSA[$t][$system][$test])>0) {
					#arsort($TOTAL_TSA[$t][$system][$test]);
					#$ordered_sentence_ids = array_keys($TOTAL_TSA[$t][$system][$test]);
					#for($i=0;$i<sizeof($ordered_sentence_ids) && $i<5;$i++) {
					#  $sentence_id = $ordered_sentence_ids[$i];
					#  print($sentence_id." (".$TOTAL_TSA[$t][$system][$test][$sentence_id].")<BR>");
					
					#}
					$histogram = array_count_values($TOTAL_TSA[$t][$system][$test]);
					krsort($histogram);
					
					foreach($histogram as $times => $count) {
						print("".$times."x: ".$count." items<BR>");
					}
				} else {
					print"&nbsp;";
				}
			
				print"</TD>";
			}
			print "</TR>\n";      
		}
				?></TABLE>
		      <?php
	}
	tail();

