-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_dui.php
68 lines (55 loc) · 2.25 KB
/
test_dui.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
<?php
// Takes a character like 'a'
// And an alphabet array('A', 'B', 'C',...,'Z')
// And a direction -1 = backward & 1 = forward
// returns an array like this:
// (Forward from A): array(1, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1);
// (Backward from Z) array(-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1);
function BuildInputArray($char, $alphabet, $direction){
$char = array_search(strtoupper($char), $alphabet); // find char in array
$input = '';
// Prepend -1's
if($char > 0){
$input = str_repeat('-1 ', $char); // -1's
}
$input .= '1 '; // Add 1 at char position
// Append -1's
if($char < 25){
$input .= str_repeat('-1 ', (26 - $char) - 1);
}
// Convert string to array
$input = array_filter(explode(' ', $input));
// Add direction to the beginning of the array
array_unshift($input, $direction);
return $input;
}
// DON'T TOUCH!!! /////////////////
/**/ $alphabet = range(chr(65), chr(90)); // A - Z
////////////////////////////////////
////////////////////////////////////
//// Okay to touch but don't get it greasy!
/**/
/**/ $direction = '1'; // -1 = backward & 1 = forward
/**/ $char = strtoupper('a'); // A
/**/ $stop_char = strtoupper('z'); // Z
///////////////////////////////////
// Load ANN
$train_file = (dirname(__FILE__) . "/dui.net");
if (!is_file($train_file))
die("The file dui.net has not been created! Please run train_dui.php to generate it" . PHP_EOL);
$ann = fann_create_from_file($train_file);
if ($ann) {
echo $char . PHP_EOL;
// While the bot hasn't output the expected last char
while($char != $stop_char){
$calc_out = fann_run($ann, BuildInputArray($char, $alphabet, $direction)); // Run ANN
$max = max($calc_out); // Figure out which value was the largest
$NextChar = array_search($max, $calc_out); // determine which output neuron returned $max
$char = $alphabet[$NextChar]; // determine which char that neuron is correlated with
echo $char . PHP_EOL; // echo the value
}
// Destroy ANN
fann_destroy($ann);
} else {
die("Invalid file format" . PHP_EOL);
}