<?php
	include 'classifier.php';

	// train or load an NGram profile, note a model will not be able to deal with languages it has not seen during training and
	// will gladly misclassify any such language as one it has seen during training.
	$classifier = new NGramProfiles('etc/classifiers/full.dat');
	$classifier->load();

	function predict($classifier, $text) {
		return $classifier->predict($text);
	}

	// handle subitted sentence
	$is_submitted = (isset($_GET['sentence']) && is_string($_GET['sentence']) && $_GET['sentence'] != '');
	$sentence = isset($_GET['sentence']) ? $_GET['sentence'] : '';
	if ($is_submitted) {
		$language = predict($classifier, $sentence);
	}

	// handy demo phrases
	$phrases = array(
		'Dit is een nederlandse tekst' => 'nl',
		'This is an english text' => 'en',
		'Ceci n\'est pas une pipe' => 'fr',
		'Appliquer un nom, une dénomination, un mot , une phrase à une personne, à une chose' => 'fr',
		'dies ist ein Satz auf Deutsch' => 'de',
		'A frase que, além de ser falsa, está longe de apontar para o verdadeiro problema da Lava Jato' => 'pt',
		'El juez pronunció la sentencia a la corte' => 'es',
		'Baik-baik saja, terima kasih' => 'id'
	);
?>
<!doctype html>
<html lang="en">
	<head>
		<meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <meta http-equiv="cache-control" content="max-age=0" />
    <meta http-equiv="cache-control" content="no-cache" />
    <meta http-equiv="expires" content="0" />
    <meta http-equiv="expires" content="Tue, 01 Jan 1980 1:00:00 GMT" />
    <meta http-equiv="pragma" content="no-cache" />

    <script>
      (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
      (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
      m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
      })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
      ga('create', 'UA-96493973-1', 'auto');
      ga('send', 'pageview');
    </script>

    <title>Jillis ter Hove - PHP language Detection</title>

    <link href="https://fonts.googleapis.com/css?family=Montserrat|Rubik:300,400" rel="stylesheet">

    <link href="/site/css/vendors.css" rel="stylesheet">
    <link href="/site/css/app.css" rel="stylesheet">
    <script src="/site/js/vendors.js"></script>
	  <style>
			.form-group {
				width: 100%;
			}
			input#sentence {
				width: 90%;
			}
	  </style>
	  <script>
			// simple function to make sentences submit on click
			function submitWith(str) {
				document.getElementById('sentence').value = str;
				document.getElementById('sentenceForm').submit();
	  	}
	  </script>
	</head>
	<body>
		<div class="container page article">
      <div class="row header">
        <div class="col-md-2">
          <a class="back" href="/site">&larr; Back</a>
        </div>
      </div>

      <div class="row content">
        <div class="col-md-12">
					<form action="index.php" method="GET" id="sentenceForm" class="form-inline">
						<div class="row"><div class="col-md-12">&nbsp;</div></div>

						<div class="row">
			        <div class="col-md-12">
								<h3>PHP Language Detection</h3>
								<p>A pure PHP implementation of language detection (of arbitrary text) based on n-gram profiles. The algorithm is taken from the following <a href="etc/NGram%20Language%20Detection.pdf">paper</a>. <strong>This demo classifier is trained on Dutch, English, French, Portugese, German, Indonesian and Spanish.</strong></p>
								<p>I revisited this problem later on using python and a much better understanding of machine learning: <a href="https://github.com/BruceJillis/LanguageIdentification">LanguageIdentification</a> in python.</p>
							</div>
						</div>

						<div class="row"><div class="col-md-12">&nbsp;</div></div>

						<div class="row">
			        <div class="col-md-12">
								<div class="form-group">
									<input type="text" name="sentence" id="sentence" value="<?php echo $sentence; ?>" placeholder="Enter some text here." class="form-control" />
									<button class="btn btn-default">Submit</button>
								</div>
							</div>
						</div>

						<?php if ($is_submitted) { ?>
							<div class="row">
				        <div class="col-md-12">
									<br/>
									<div class="result" style="font-size: 18px;">
										Found <strong style="font-size: 20px;"><?php echo $language; ?></strong> for <em style="font-size: 14px;">"<?php echo $_GET['sentence']; ?>"</em>
									</div>
								</div>
							</div>
						<?php } ?>

						<div class="row"><div class="col-md-12">&nbsp;</div></div>

						<div class="row">
							<div class="col-md-12">
								<h3>Some example sentences</h3>
								<ul>
									<?php foreach($phrases as $phrase => $language) { ?>
										<li>
											<a href="#" onclick="submitWith('<?php echo addslashes($phrase); ?>');"><?php echo $phrase; ?> (<?php echo $language; ?>)</a>
										</li>
									<?php } ?>
								</ul>
							</div>
						</div>
					</form>
				</div>
			</div>
	</body>
</html>
