-
Notifications
You must be signed in to change notification settings - Fork 0
/
willbot.php
109 lines (92 loc) · 2.6 KB
/
willbot.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
header('Content-type: application/json');
$input = urldecode($_POST["i"]);
//$input = "i think i";
error_reporting(E_ALL);
ini_set('display_errors', 'on');
$WORD_LIMIT = 30;
$ORDER = 2;
$m = new MongoClient();
$db = $m->selectDB("willbot");
$chains = $db->selectCollection('chains');
$collectionCount = $chains->count();
function getwords($key){
global $m, $db, $chains;
$query = array('key' => $key);
$result = $chains->findOne($query);
//if no key matches, return a random entry
if($result == null){
return null;
}
//pick a random word from the word list to return
$wordcount = count($result['words']);
$randomnum = rand(0, $wordcount-1);
return $result['words'][$randomnum];
}
function keyexists($key){
global $m, $db, $chains;
$query = array('key' => $key);
$result = $chains->find($query);
return $result->count() > 0;
}
function logwords($key, $words){
global $m, $db, $chains;
$criteria = array("key" => $key);
if(keyexists($key)){
$addtoset = array('$addToSet' => array("words" => $words[0]));
$chains->update($criteria, $addtoset);
}
else{
$set = array('$set' => array("key" => $key, "words" => $words));
$chains->update($criteria, $set, array("upsert" => true));
}
}
function augmentbrain($input){
global $ORDER;
$inputwords = explode(" ", $input);
$wordcount = count($inputwords);
$keyarray = array();
for($i = 0; $i< $wordcount - $ORDER; $i++){
$key = array_slice($inputwords, $i, 2);
$words = array_slice($inputwords, $i + $ORDER, 1);
logwords($key, $words);
array_push($keyarray, $key);
}
return $keyarray;
}
//sanitize our input a bit
$input = strtolower($input);
$input = preg_replace("#[[:punct:]]#", "", $input);
//add these words to the lexicon
$keyarray = augmentbrain($input);
//split the input into individual words
$input_words = explode(' ', $input);
$input_count = count($input_words);
$bestphrase = "";
foreach($keyarray as $key){
$nextkey = $key;
$phrase = array($key[0], $key[1]);
for($i = $WORD_LIMIT; $i > 0; $i--){
$next_word = getwords($nextkey);
if($next_word != null){
array_push($phrase, $next_word);
$nextkey = array_slice($phrase,-2,2,false);
}
else{
//if we didn't find a next word, use what we got and save it off
//if it's longer than the best phrase so far
$newphrase = join(" ", $phrase);
if(strlen($newphrase) > strlen($bestphrase)){
$bestphrase = $newphrase;
}
break;
}
}
//this assumes we found enough words up the to the word limit
$newphrase = join(" ", $phrase);
if(strlen($newphrase) > strlen($bestphrase)){
$bestphrase = $newphrase;
}
}
echo json_encode(array('response' => $bestphrase));
?>