<?php
	require_once('functions.php');
	
	# Generate the text, by file - or by input if specified
		$t[0] =	$t1 		= (isset($_REQUEST['text1'])) 	? $_REQUEST['text1'] 	: file_get_contents('Text1 - LanguageAndMind.txt');
		$t[1] =	$t2 		= (isset($_REQUEST['text2'])) 	? $_REQUEST['text2'] 	: file_get_contents('Text2 - ParisMenelausAndHelen.txt');
		$t[2] =	$sample 	= (isset($_REQUEST['sample'])) 	? $_REQUEST['sample'] 	: file_get_contents('sample1.txt');
		
	$stats = array(array(),array(),array());
	
	$num_funcs = 11; 
	for($i = 0; $i < 3; $i++)
	{
		$stats[$i][0] = "Text ". ($i+1);
		$stats[$i][1] = word_count ($t[$i]);
		$stats[$i][2] = count_names($t[$i]);
		$stats[$i][3] = avg_word_length($t[$i]);
		$stats[$i][4] = avg_sentence_length($t[$i]);
		$stats[$i][5] = count_commas($t[$i]);
		$stats[$i][6] = count_newlines($t[$i]);
		$stats[$i][7] = similar_text($t[$i],$sample);
		$stats[$i][8] = semicolon_frequency($t[$i]);
		$stats[$i][9] = word_per_line($t[$i]);
		$stats[$i][10] = quote_lengths($t[$i]);
		$stats[$i][11] = count_colon_quotes($t[$i]);
		
		if($i==2)
		{
			$stats[$i][$num_funcs +1] = word_freq($t1,$sample);
			$stats[$i][$num_funcs +2] = word_freq($t2,$sample);
		}
	}
	$analyser2 = $analyser  = array(0,0);	
	word_freq($t1,$sample) > word_freq($t2,$sample) ? $analyser[0]++  : $analyser[1]++;	
	word_freq($t1,$sample) > word_freq($t2,$sample) ? $analyser2[0]++ : $analyser2[1]++;	
	
	for($i = 2; $i <= $num_funcs; $i++){
		calculate_point ($stats[0][$i],	$stats[1][$i],	$stats[2][$i],	$analyser);	
		calculate_point2($stats[0][$i],	$stats[1][$i],	$stats[2][$i],	$analyser2);
	}
?>
<html>
	<head>
		<title>TEXT ANALYSER - Turing</title>
		<link href="http://wingerlang.se/Jeneral.css" rel="stylesheet" type="text/css">
		 <style type="text/css">	
			td{padding:5px;	border:1px solid #AAA;	text-align:center;}
			th{font-weight;bold;color:darkred;font-size:.8em;}
			.tl300 p{width:60em;max-width:700px;} 
			span {font-size: 1.2em; color: darkred;
			}
		 </style>	
	</head>	
	<body>
	<header class=tl300><h1>TURING - <span>T</span>ext <span>U</span>niversal <span>R</span>ecognition <span>I</span>n <span>N</span>atural <span>G</span>ateways</h1>
		<p>This was done as a school assignment (<a href="http://edu.bth.se/utbildning/utb_kurstillfalle.asp?KtTermin=20132&KtAnmKod=COM0806&lang=en&parentPtKod=DVAIS12h1">Applied Artificial Intelligence</a> at
		<a href="http://bth.se">Blekinge Institute of Technology</a>).</p>
		<p> The idea is to load two texts and save the characteristics. You then provide a sample from <em>one</em> of the texts. <em>I</em> then should be able to decide from which one of the two text your sample comes from.</p>
	</header>	
		<div id=wrap>
			<form method=POST>
				<h2>Training set 1 | Training set 2 | Sample</h2>
				<textarea rows=15 cols=40 name=text1><?php echo $t1 ?></textarea>
				<textarea rows=15 cols=40 name=text2><?php echo $t2 ?></textarea>
				<textarea rows=10 cols=20 name=sample><?php echo $sample ?></textarea>
				<input type=submit value="Determine" name=x />
			</form>
			<details open><summary>Behind the scenes (Technical details)</summary>
				<table>
					<tr>
						<th>Text</th>
						<th>Word count</th>
						<th title="Number of times a name occour per sentence">Name</th>
						<th title="Simply the average word length">Word length</th>
						<th title="Simply the average sentence length">Sentence length</th>
						<th title="Number of commas per sentence">Commas</th>
						<th title="Number of newlines per sentence">Newlines</th> 
						<th title="A magic function built in to PHP">Similiarity</th> 
						<th title="Number of semicolons per sentence">Semicolons</th> 
						<th title="Number of words per each line">words/ line</th> 
						<th title="Average quote length">Quote length</th> 
						<th title="Number of colons followed by a newline or two spaces">Colons</th> 
						<th title="Number of common most frequent words with text 1">Words t1</th>
						<th title="Number of common most frequent words with text 2">Words t2</th>
					</tr>	
						<?php
							for($i=0; $i<3; $i++) {
								echo "<tr>";
								foreach($stats[$i] as $value) echo "<td>$value</td>"; 
								echo "</tr>";
							}?>
				</table>
			</details>
			<details open><summary> CLICK TO VIEW THE RESULT</summary>
				<p>Algorithm 1: One point for the closest match.<br>Algorithm 2: The assigned score is the ratio between the difference of the closest and not.</p>
				<?php for($i=0; $i < 2; $i++) echo "<h2>In favor of text " . ($i+1) . ": " . rounding($analyser[$i]) . " -------- " . rounding($analyser2[$i]) . "</h2>"; ?>
			</details>
		</div>
	<footer>
		<p>Lines of code: <br>Functions: <strong><?php echo count(file("functions.php"))?></strong><br>
		This file: <strong><?php echo count(file("index.php"))?> </strong><br>Total: <strong><u><?php echo count(file("index.php")) + count(file("functions.php")) ?></u></strong></p>
		<p>Source code: <?php echo "<a href='index.phps'>Index</a>" . " <a href='functions.phps'>Functions</a>" ?>
	</footer>
	</body>
</html>