-
Notifications
You must be signed in to change notification settings - Fork 16
/
octavius.php
executable file
·378 lines (335 loc) · 12.4 KB
/
octavius.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
<?php /*
ocPortal
Copyright (c) ocProducts, 2004-2011
See text/EN/licence.txt for full licencing information.
*/
#####################################################
# eliza.php- Final Project: Eliza program written in PHP
#
# The purpose of this program is to mimick the
# infamous Eliza program that was created some
# time ago.
#
# Note: I am not especially good with regular
# expressions at the moment, thus much of this code
# could most likely have been slimmed down. This is
# also the reason I don't have parts of strings the
# user types stored in a variable and print them out
# when responding to the user (for ex: User: "I have
# a cat".... Eliza: "Oh you have a cat? That's nice.")
# I will be getting a regular expression book for
# X-Mas, however =)
#
# ©2001 Wayne Eggert
#-----------------------------------------------------
# DISCLAIMER: THIS CODE MAY NOT BE MODIFIED IN ANY MANNER
# WITHOUT MY CONSENT. IT SHOULD BE USED FOR EDUCATIONAL
# PURPOSES ONLY. LEARN, BUT DO NOT STEAL -- MEANING
# DO NOT COPY CODE VERBATIM JUST TO PASS A CLASS IF YOU
# EVER HAVE A SIMILAR PROJECT, ETC. IT WILL BE WORTH
# YOUR WHILE TO PUT SOME THOUGHT INTO THE TASK AT HAND
# AND COME UP WITH YOUR OWN SOLUTION. THAT'S ALL I'M
# GOING TO SAY -- OTHERWISE GOD HELP YOU WHEN YOU ENTER
# INTO THE REAL WORLD WITH THOSE HABITS.
######################################################
class Hook_chat_bot_octavius
{
/**
* Give ability to reply to any communication.
*
* @param AUTO_LINK The ID of the chat room
* @param string The message used.
* @return ?string Bot reply (NULL: bot does not handle the command)
*/
function reply_to_any_communication($room_id,$string)
{
$people=get_chatters_in_room($room_id);
if (count($people)>2) return NULL; // Don't spam if noone is here
if (running_script('shoutbox')) return NULL; // Messes up shoutbox
$is_im=$GLOBALS['SITE_DB']->query_value('chat_rooms','is_im',array('id'=>$room_id));
if ($is_im==1) return NULL;
$message=preg_replace('#\[[^\[\]]*\]#U','',$string);
if ($message=='') return NULL;
return $this->handle_commands($room_id,$message);
}
/**
* Handle hooks supported bot commands. Note multiple bots may support the same commands, and all respond. It is recommended all bots support the command 'help'.
*
* @param AUTO_LINK The ID of the chat room
* @param string The command used. This is just the chat message, so you can encode and recognise your own parameter scheme if you like.
* @return ?string Bot reply (NULL: bot does not handle the command)
*/
function handle_commands($room_id,$string)
{
require_code('developer_tools');
destrictify();
if ($string=='((SHAKE))') return NULL;
if (file_exists(get_custom_file_base().'/sources_custom/programe')) // AliceBot, much better
{
if (get_value('octavius_installed')!=='1')
{
disable_php_memory_limit();
if (function_exists('set_time_limit')) @set_time_limit(600);
$GLOBALS['SITE_DB']->query("DROP TABLE bot",NULL,NULL,true);
$GLOBALS['SITE_DB']->query("DROP TABLE bots",NULL,NULL,true);
$GLOBALS['SITE_DB']->query("DROP TABLE conversationlog",NULL,NULL,true);
$GLOBALS['SITE_DB']->query("DROP TABLE dstore",NULL,NULL,true);
$GLOBALS['SITE_DB']->query("DROP TABLE gmcache",NULL,NULL,true);
$GLOBALS['SITE_DB']->query("DROP TABLE gossip",NULL,NULL,true);
$GLOBALS['SITE_DB']->query("DROP TABLE patterns",NULL,NULL,true);
$GLOBALS['SITE_DB']->query("DROP TABLE templates",NULL,NULL,true);
$GLOBALS['SITE_DB']->query("DROP TABLE thatindex",NULL,NULL,true);
$GLOBALS['SITE_DB']->query("DROP TABLE thatstack",NULL,NULL,true);
$GLOBALS['SITE_DB']->query("CREATE TABLE bot (
id int(11) NOT NULL auto_increment,
bot tinyint(4) NOT NULL default '0',
name varchar(255) NOT NULL default '',
value text NOT NULL,
PRIMARY KEY (id),
KEY botname (bot,name)
) TYPE=MyISAM",NULL,NULL,true);
$GLOBALS['SITE_DB']->query("CREATE TABLE bots (
id tinyint(3) unsigned NOT NULL auto_increment,
botname varchar(255) NOT NULL default '',
PRIMARY KEY (botname),
KEY id (id)
) TYPE=MyISAM",NULL,NULL,true);
$GLOBALS['SITE_DB']->query("CREATE TABLE conversationlog (
bot tinyint(3) unsigned NOT NULL default '0',
id int(11) NOT NULL auto_increment,
input text,
response text,
uid varchar(255) default NULL,
enteredtime timestamp(14) NOT NULL,
PRIMARY KEY (id),
KEY botid (bot)
) TYPE=MyISAM",NULL,NULL,true);
$GLOBALS['SITE_DB']->query("CREATE TABLE dstore (
uid varchar(255) default NULL,
name text,
value text,
enteredtime timestamp(14) NOT NULL,
id int(11) NOT NULL auto_increment,
PRIMARY KEY (id),
KEY nameidx (name(40))
) TYPE=MyISAM",NULL,NULL,true);
$GLOBALS['SITE_DB']->query("CREATE TABLE gmcache (
id int(11) NOT NULL auto_increment,
bot tinyint(3) unsigned NOT NULL default '0',
template int(11) NOT NULL default '0',
inputstarvals text,
thatstarvals text,
topicstarvals text,
patternmatched text,
inputmatched text,
combined text NOT NULL,
PRIMARY KEY (id),
KEY combined (bot,combined(255))
) TYPE=MyISAM",NULL,NULL,true);
$GLOBALS['SITE_DB']->query("CREATE TABLE gossip (
bot tinyint(3) unsigned NOT NULL default '0',
gossip text,
id int(11) NOT NULL auto_increment,
PRIMARY KEY (id),
KEY botidx (bot)
) TYPE=MyISAM",NULL,NULL,true);
$GLOBALS['SITE_DB']->query("CREATE TABLE patterns (
bot tinyint(3) unsigned NOT NULL default '0',
id int(11) NOT NULL auto_increment,
word varchar(255) default NULL,
ordera tinyint(4) NOT NULL default '0',
parent int(11) NOT NULL default '0',
isend tinyint(4) NOT NULL default '0',
PRIMARY KEY (id),
KEY wordparent (parent,word),
KEY botid (bot)
) TYPE=MyISAM",NULL,NULL,true);
$GLOBALS['SITE_DB']->query("CREATE TABLE templates (
bot tinyint(3) unsigned NOT NULL default '0',
id int(11) NOT NULL default '0',
template text NOT NULL,
pattern varchar(255) default NULL,
that varchar(255) default NULL,
topic varchar(255) default NULL,
PRIMARY KEY (id),
KEY bot (id)
) TYPE=MyISAM",NULL,NULL,true);
$GLOBALS['SITE_DB']->query("CREATE TABLE thatindex (
uid varchar(255) default NULL,
enteredtime timestamp(14) NOT NULL,
id int(11) NOT NULL auto_increment,
PRIMARY KEY (id)
) TYPE=MyISAM",NULL,NULL,true);
$GLOBALS['SITE_DB']->query("CREATE TABLE thatstack (
thatid int(11) NOT NULL default '0',
id int(11) NOT NULL auto_increment,
value varchar(255) default NULL,
enteredtime timestamp(14) NOT NULL,
PRIMARY KEY (id)
) TYPE=MyISAM",NULL,NULL,true);
$fp = "";
$templatesinserted=0;
$depth = array();
$whaton = "";
$pattern = "";
$topic = "";
$that = "";
$template = "";
$startupwhich = "";
$splitterarray = array();
$inputarray = array();
$genderarray = array();
$personarray = array();
$person2array = array();
require_code('programe/botloaderfuncs');
loadstartup();
makesubscode();
set_value('octavius_installed','1');
}
require_code('programe/respond');
$response=replybotname(str_replace('?','.',$string),get_session_id(),'octavius');
restrictify();
if (is_null($response) || $response->response=='') return NULL;
return '[html]'.$response->response.'[/html]';
}
// Eliza...
// setup initial variables and values
$kwarray = array();
$vararray = array();
$resparray = array();
$priarray = array();
$wordarray = array();
$kwcount=0; $varcount=0; $respcount=0; $syncount=0;
mt_srand ((double) microtime() * 1000000);
// load knowledge file
$lines_array = file(get_custom_file_base()."/sources_custom/hooks/modules/chat_bots/knowledge.txt");
$count = count($lines_array);
// This for loop goes through the entire knowledge file and places
// the elements into arrays. This later allows us to pull the information
// (ie. key words, variances on the keywords, and responses) out of the
// arrays.
for ($x=0;$x<$count;$x++){
$lines_array[$x] = trim($lines_array[$x]);
$lines_array[$x] = ereg_replace("[\]","",$lines_array[$x]);
if (strstr($lines_array[$x],"key:")){
eregi("key: (.*)",$lines_array[$x],$kw);
$kwarray[$kwcount] = strtoupper($kw[1]);
$currentkw = $kwcount;
$kwcount++;
$varcount=0; // reset varcount to null
$respcount=0; // reset respcount to null
$pricount=0; // reset pricount to null
}else if (strstr($lines_array[$x],"var:")){
eregi("var: (.*)",$lines_array[$x],$variance);
$vararray[$currentkw][$varcount] = strtoupper($variance[1]);
$varcurrent=$varcount;
$varcount++;
$respcount=0;
}else if (strstr($lines_array[$x],"pri:")){
eregi("pri: (.*)",$lines_array[$x],$priority);
$priarray[$currentkw] = $priority[1];
}else if (strstr($lines_array[$x],"resp:")){
eregi("resp: (.*)",$lines_array[$x],$response);
$resparray[$currentkw][$varcurrent][$respcount] = $response[1];
$respcount++;
}else if (strstr($lines_array[$x],"syn:")){
eregi("syn: (.*)",$lines_array[$x],$synonym);
$synonymarray[$syncount] = strtoupper($synonym[1]);
$syncount++;
}else if (strstr($lines_array[$x],"goto:")){
eregi("goto: (.*)",$lines_array[$x],$goto);
$goto = strtoupper($goto[1]);
// find the keyword
for ($zcount=0;$zcount<count($kwarray);$zcount++){
// if the keyword already exists
if (eregi($goto,$kwarray[$zcount])){
// then we assign properties of the keyword
$vararray[$currentkw][0] = $kwarray[$currentkw];
$resparray[$currentkw] = $resparray[$zcount];
}
}
}
}
$y=0;
$z=0;
$v=0;
$bestpriority=-2;
$originalstring = $string;
if (!$string){
$string = "hello";
}
$string = strtoupper($string);
// Figures out what word in the string has the most priority.
// It can then check words to the left/right of this word depending
// upon settings in the knowledge.txt file.
while ($y < count($kwarray)){
// remove beginning and trailing white space, breaks, etc
$string = trim($string);
// remove puncuation from string
$string = ereg_replace('[!?,.]','',$string);
// split the string up into seperate words
$wordarray = explode(" ",$string);
while ($v < count($wordarray)){
if(eregi($wordarray[$v]."$",$kwarray[$y])){
// find which word holds the most weight in the sentance
if($bestpriority==-2){
$bestpriority=$y;
}else if ($priarray[$bestpriority] < $priarray[$y]){
$bestpriority = $y;
}
}
$v++;
}
$v = 0;
$y++;
}
// find the variance with the most matching words
$vcount=0;
while ($vcount < count($vararray[$bestpriority])){
if (strstr($vararray[$bestpriority][$vcount],"@")){
eregi("@(.*)",$vararray[$bestpriority][$vcount],$syn); // fix this
$syn = $syn[1];
for($x=0;$x<count($synonymarray);$x++){
if (eregi($syn,strtoupper($synonymarray[$x]))){
$sarray = explode(" ",$synonymarray[$x]);
for($f=0;$f<count($sarray);$f++){
$newstring = ereg_replace("@(.*)$",$sarray[$f],$vararray[$bestpriority][$vcount]);
// works to this point
if(eregi($newstring."$",$string)){
$varray = explode(" ",$vararray[$bestpriority][$vcount]);
if(count($varray) > $pvarray){
$bestvariance = $vcount;
$pvarray = count($varray);
}
}
}
}
}
}else{
if(ereg($vararray[$bestpriority][$vcount],$string)){
$varray = explode(" ",$vararray[$bestpriority][$vcount]);
if(count($varray) > $pvarray){
$bestvariance = $vcount;
$pvarray = count($varray);
}
}
}
$vcount++;
}
// Using the bestpriority (aka the keyword (key:) with the most weight in the sentence)
// and the bestvariance (aka, the variance (var:) phrase that most fits the context of
// the original sentence, we form a response.
if (count($resparray[$bestpriority][$bestvariance]) > 1){
$random = mt_rand(0,count($resparray[$bestpriority][$bestvariance])-1);
}else{
$random = 0;
}
$response = $resparray[$bestpriority][$bestvariance][$random];
if ($response==""){
$response = "Sorry, I don't understand what you're trying to say.";
}
$originalstring = ereg_replace("[\]","",$originalstring);
restrictify();
return $response;
}
}