-
Notifications
You must be signed in to change notification settings - Fork 15
/
index.php
90 lines (73 loc) · 2.17 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?php
require 'Gibberish.class.php';
$trainingDir = 'phrases';
$dir = dirname(__FILE__).'/'.$trainingDir.'/';
$big_text_file = $dir.'big.txt';
$good_text_file = $dir.'good.txt';
$bad_text_file = $dir.'bad.txt';
$matrix_file = $dir.'matrix.txt';
$test_file = $dir.'test.txt';
$trainingRequested = isset($_GET['train']);
$alreadyTrained = is_file($matrix_file);
if ( $trainingRequested ) {
$trainingSuccessful = Gibberish::train(
$big_text_file,
$good_text_file,
$bad_text_file,
$matrix_file
);
if ($trainingSuccessful) {
runGibberishTest($matrix_file, $test_file);
} else {
echo 'Training failed.';
}
} else {
if ( !$alreadyTrained ) {
echo 'Needs training. <a href="?train">Click to Train</a>';
} else {
runGibberishTest($matrix_file, $test_file);
}
}
?><?php
function runGibberishTest($matrix_file, $test_file)
{
$matrix = unserialize(file_get_contents($matrix_file));
echo '<h1>Gibberish Detector</h1>';
echo '<p>'.nl2br(file_get_contents('../ABOUT')).'</p>';
echo '<p>Gibberish Threshold = '.$matrix['threshold'].'<br />
Anything above this value is classed as text, below or equal to this value, classed as gibberish.</p>';
echo '<div><a href="?train" class="button">Re-train</a></div>';
$handle = fopen($test_file, "r");
if ($handle) {
while (($text = fgets($handle)) !== false) {
$htmlText = htmlentities($text, ENT_QUOTES, 'UTF-8');
$isGibberish = Gibberish::test($text, $matrix_file) === true;
$odds = Gibberish::test($text, $matrix_file, true);
echo $htmlText;
if ($isGibberish) {
echo ' = <strong style="color:gray">Gibberish';
echo ' ('.$odds.')';
echo '</strong><br><br>';
} else {
echo ' = <strong style="color:green">Looks Good';
echo ' ('.$odds.')';
echo '</strong><br><br>';
}
}
fclose($handle);
} else {
// error opening the file.
}
} // runTest
?>
<style type="text/css">
.button {
display: inline-block;
margin: 10px;
background: blue;
color: white;
font-weight: bold;
text-decoration: none;
padding: 10px;
}
</style>