JM - Here's an Eliza routine I just found, you might find it interesting. On quick review it looks somewhat clever, but could use some efficienciafying. Also, the text method it's using is bad - if this was production, that should be parsed into an array and kept in an APC cache so that lookups would be really fast.
Have fun!
/p
eliza.php<?
#####################################################
# 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.
######################################################
?>
<HTML>
<HEAD><TITLE>PHPLiza</TITLE></HEAD>
<BODY>
<?
// 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("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);
echo "<h3>Chat with PHPliza!</h3>";
echo "<b>You: </b>".$originalstring."<br>";
echo "<b>PHPliza: </b>".$response;
?>
<FORM ACTION="eliza.php">
<INPUT TYPE="text" size="20" name="string"><INPUT TYPE="SUBMIT" value="Submit">
</FORM>
</BODY>
</HTML>
knowledge.txtsyn: Hello Hi Hey Goodday Hola Yo Hiya
syn: Friend Comrad Mate
syn: you man dood
key: Hello
pri: 3
var: Hello
resp: How's it going?
resp: Yo what's up?
resp: Yo Yo Yo
var: Hello @you
resp: Howdy
var: Hello @you there
resp: G'day.
var: Hello @friend
resp: Hi Friend!
key: Goodbye
pri: 2
var: Goodbye
resp: See ya later!
resp: Good day, sir.
resp: Later mang
var: Goodbye friend
resp: Goodbye, friend.
key: How
pri: 3
var: How
resp: You got me!
var: How are you
resp: I'm not sure
resp: I'm doing fine
resp: Swell, thanks for asking.
var: How do
resp: I don't know
resp: I cannot answer that question
var: How is
resp: I don't know how that is.
key: You
pri: 2
var: You
resp: Does that interest you?
var: You are
resp: Let's not talk about me
key: Name
pri: 4
var: name
resp: I'm not good with names.
var: your name
resp: Why is my name important?
resp: Do you like my name?
key: Yes
pri: 2
var: yes
resp: Ok, cool!
resp: Fine with me.
resp: Ok.
key: No
pri: 3
var: no
resp: Oh, ok.
resp: Why not?
resp: Oh really?
key: Ok
pri: 3
var: ok
resp: If you say so.
resp: Whatever you say =)
resp: Fine with me.
key: Have
pri: 3
var: have
resp: Have what?
var: you have
resp: I'm not sure if I have that.
var: have you
resp: I'm not sure if I've done that.
key: Doctor
pri: 4
var: doctor
resp: I'm no doctor.
resp: What's this about doctors?
key: Hey
pri: 2
goto: Hello
key: Hola
pri: 2
goto: Hello
key: Hi
pri: 2
goto: Hello
key: Aight
pri 2:
goto: Ok
key: Thanks
pri: 3
var: thanks
resp: You're welcome!
key: Why
pri: 3
var: why
resp: I'm not really sure.
var: know why
resp: I don't know why.
key: Maybe
pri: 3
var: maybe
resp: Ok.
resp: Whatever you say.
resp: If you say so.
key: Probably
pri: 3
goto: maybe
key: Possible
pri: 3
goto: maybe
key: yep
pri: 3
goto: yes
key: yeah
pri: 3
goto: yes
key: nope
pri: 3
goto: no
key: Where
pri: 4
var: Where
resp: I really couldn't tell ya.
var: you live
resp: Why do you want to know where I live?
var: you from
resp: Why do you want to know where I'm from?
key: What
pri: 3
var: What
resp: WHAT!!!
key: god
pri: 3
var: god
resp: Sorry, I don't know him.
key: Watt
pri: 3
goto: What
key: Like
pri: 3
var: like
resp: I'm not sure.
var: do you like
resp: I don't know if I like that.
resp: Do you think I like that?
key: Because
pri: 3
goto: Ok
key: Problem
pri: 5
var: you have
resp: I don't have any problems.. I'm here to answer your problems =)
var: I have
resp: Please tell me your problem.
resp: I'd like to hear more about your problem.
key: I
pri: 2
var: I
resp: Oh do you?
resp: I see.
var: I am
resp: Are you really?
resp: Interesting.