__How to Build Eliza Chatterbot - A Program that can Chat with Humans
This article teaches you how you can create your own Chatterbot, a program that talks with human beings, just as we do. As you read this program, you will explore that this program, which falls under the domain of Artificial Intelligence, is nothing but manipulation of String and File Handling. This article aims at giving you a direction. The outcome is not a perfect program, but a minimal working skeleton. Rest you can do easily. There is practically NO LIMIT to how much input you can give to this program, because once you say that my Eliza is complete, I will find at least ONE question, that your Eliza will not be able to answer ! Hope you will enjoy this. And, Please Vote ;-)
Shrnutí AI: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.
<hr size="1" color="#008000"> <p align="center"><font face="Verdana"><b><font size="5"><font color="#FF6600">How to Build Eliza Chatterbot<br> </font><font color="#008000"> A Program that can Chat with Humans</font><br> <br> </font></b><font size="3"><b>Written by <a href="mailto:[email protected]">Amit Mathur</a> on 10<sup>th</sup> to 12<sup>th</sup> Dec, 2002</b></font></font></p> <hr size="1" color="#008000"> <p class="MsoNormal"><b><span style="font-size: 16.0pt; mso-bidi-font-size: 12.0pt"><font face="Verdana"> What is Eliza ?</font></span><font face="Verdana"><span style="font-size: 16.0pt; mso-bidi-font-size: 12.0pt"> </span><span style="mso-bidi-font-size: 12.0pt"><font size="2"><o:p> </o:p> </font></span></font></b></p> <p class="MsoNormal"><font face="Verdana" size="2">Eliza is an AI Program that simulates the behavior of a therapist. The first program of this sort was developed in 1967 in MIT. Such programs, which interact with user in simple English language and can simulate a conversation are known as <b>Chatterbot.</b></font><b><font face="Verdana" size="2"> <o:p> </o:p> </font></b></p> <p class="MsoNormal"><font face="Verdana" size="2">A program like Eliza requires knowledge of three domains:</font></p> <blockquote> <p class="MsoNormal"><font face="Verdana" size="2">1.<span style="font-style: normal; font-variant: normal; font-weight: normal"> </span>Artificial Intelligence<br> 2.<span style="font-style: normal; font-variant: normal; font-weight: normal"> </span>Expert System<br> 3.<span style="font-style: normal; font-variant: normal; font-weight: normal"> </span>Natural Language Processing</font></p> </blockquote> <p class="MsoNormal"><font face="Verdana" size="2">Even though last two are sub-parts of the first one, they are emerging as<span style="mso-spacerun: yes"> </span>science in themselves.<o:p> </o:p> </font></p> <p class="MsoNormal"><font face="Verdana" size="2">Eliza can not, of course, think on its own. It has a repository or database of facts and rules, which are searched to give the best possible response. </font><font face="Verdana" size="2"><o:p> </o:p> </font></p> <p class="MsoNormal"><font face="Verdana" size="2">Eliza works by matching process.<span style="mso-spacerun: yes"> </span>Very rarely an entire sentence is matched to give the response.</font><font face="Verdana" size="2"> <o:p> </o:p> </font></p> <p class="MsoNormal"><font face="Verdana" size="2">The rules are indexed by keywords. Some rules require no keyword.<o:p> </o:p> </font></p> <p class="MsoNormal"><b><span style="font-size:16.0pt;mso-bidi-font-size:12.0pt; font-family:Verdana">How does it work ?</span></b><b><span style="font-size:16.0pt;mso-bidi-font-size:12.0pt; font-family:Verdana"> <o:p> </o:p> </span></b></p> <p class="MsoNormal"><span style="font-family:Verdana"><font size="2">Ninety percent of what Eliza says is found in the associated Data File. This file acts as <b>Knowledge Base </b>for the complete system.</font><font size="2"> <o:p> </o:p> </font></span></p> <p class="MsoNormal"><span style="font-family:Verdana"><font size="2">The in-built responses comprise the <b>Static Database </b>of the system. These are the responses for the following cases:</font><font size="2"> <o:p> </o:p> </font></span></p> <p class="MsoNormal" style="margin-left:.5in;text-indent:-.25in;mso-list:l2 level1 lfo1; tab-stops:list .5in"><span style="font-family:Verdana"><font size="2">1.<span style="font-style: normal; font-variant: normal; font-weight: normal; font-family: Times New Roman"> </span>When Eliza does not understand what the user is talking about.<o:p> </o:p> </font></span></p> <p class="MsoNormal" style="margin-left:.5in;text-indent:-.25in;mso-list:l2 level1 lfo1; tab-stops:list .5in"><span style="font-family:Verdana"><font size="2">2.<span style="font-style: normal; font-variant: normal; font-weight: normal; font-family: Times New Roman"> </span>When the user repeats himself.<o:p> </o:p> </font></span></p> <p class="MsoNormal" style="margin-left:.5in;text-indent:-.25in;mso-list:l2 level1 lfo1; tab-stops:list .5in"><span style="font-family:Verdana"><font size="2">3.<span style="font-style: normal; font-variant: normal; font-weight: normal; font-family: Times New Roman"> </span>When the user does not type anything and just keeps on pressing Enter.<o:p> </o:p> </font></span></p> <p class="MsoNormal" style="margin-left:.5in;text-indent:-.25in;mso-list:l2 level1 lfo1; tab-stops:list .5in"><span style="font-family:Verdana"><font size="2">4.<span style="font-style: normal; font-variant: normal; font-weight: normal; font-family: Times New Roman"> </span>For the greeting statements.</font><font size="2"> <o:p> </o:p> </font></span></p> <p class="MsoNormal"><span style="font-family:Verdana"><font size="2">The following strategy is used to respond to a request:</font><font size="2"> <o:p> </o:p> </font></span></p> <p class="MsoNormal"><b><span style="font-family:Verdana"><font size="2">Step 1: </font></span></b><font size="2"><span style="font-family:Verdana">Eliza finds out if the user has given any null input. If so, it takes the fact from the static database to respond.</span></font><span style="font-family:Verdana"><font size="2"> <o:p> </o:p> </font></span></p> <p class="MsoNormal"><b><span style="font-family:Verdana"><font size="2">Step 2: </font></span></b><font size="2"><span style="font-family:Verdana">There are some in built responses that Eliza can recognize readily. It finds the presence of any such sentence after fragmenting the user’s input and remembers the associated keyword. This keyword defines the <b>Context </b>of the talk.</span></font><span style="font-family:Verdana"><font size="2"> <o:p> </o:p> </font></span></p> <p class="MsoNormal"><b><span style="font-family:Verdana"><font size="2">Step 3: </font></span></b><font size="2"><span style="font-family:Verdana">If no in-built sentence frame work is found, then the Eliza searches for the specific keyword to define the context. <b>If no context is found, it deliberately motivates the user to speak about a specific topic.</b> </span></font><span style="font-family:Verdana"><font size="2"> <o:p> </o:p> </font></span></p> <p class="MsoNormal"><b><span style="font-family:Verdana"><font size="2">Step 4: </font></span></b><font size="2"><span style="font-family:Verdana">A response is chosen (at this time, randomly) from the database of available responses.</span></font><span style="font-family:Verdana"><font size="2"> <o:p> </o:p> </font></span></p> <p class="MsoNormal"><b><span style="font-family:Verdana"><font size="2">Step 5: </font></span></b><font size="2"><span style="font-family:Verdana">Any necessary <b>transpositions</b> are done. For example, consider the following conversation:</span></font><span style="font-family:Verdana"><font size="2"> <o:p> </o:p> </font></span></p> <div style="border:solid windowtext .5pt;padding:1.0pt 4.0pt 1.0pt 4.0pt"> <p class="MsoNormal" style="text-align:justify;border:none;mso-border-alt:solid windowtext .5pt; padding:0in;mso-padding-alt:1.0pt 4.0pt 1.0pt 4.0pt"><b><span style="font-family: Verdana"><font size="2">SAM></font></span></b><font size="2"><span style="font-family:Verdana"> I PLAN TO GO TO JAIPUR TOMORROW WITH <i>MY </i>WIFE.<o:p> </o:p> </span></font></p> <h5 style="text-align:justify;border:none;mso-border-alt:solid windowtext .5pt; padding:0in;mso-padding-alt:1.0pt 4.0pt 1.0pt 4.0pt"><font size="2">ELIZA> <span style="font-weight:normal">AND WHAT HAPPENS IF YOU WON'T GO TO JAIPUR WITH <i>YOUR </i>WIFE ?</span></font><span style="font-weight:normal"><font size="2"><br> <o:p> </o:p> </font></span></h5> </div> <p class="MsoNormal"><span style="font-family:Verdana"><font size="2">Here, the word <b><i>My</i> </b>has to be transposed to <i><b>YOUR</b></i>.<o:p> </o:p> </font></span></p> <p class="MsoNormal"><b><span style="font-family:Verdana"><font size="2">Step 6: </font></span></b><font size="2"><span style="font-family:Verdana">To simulate the human conversationalists, Eliza simulates Typing and does so slowly with making <b>spelling mistakes and correcting </b>them.<br> </span></font><font size="2"> <o:p> </o:p> </font></p> <p class="MsoNormal"><b><span style="font-size: 16.0pt; mso-bidi-font-size: 12.0pt; font-family: Verdana">What about Coding ?</span></b></p> <p class="MsoNormal"><font size="2"><span style="font-family: Verdana">Now let us start the real coding portion. I am using <b>Turbo C IDE 3.0 </b>as this is the IDE that most Indian Students use. </span></font></p> <p class="MsoNormal"><font size="2"><span style="font-family: Verdana"><b>Note that the complete source code is in the Zip file that accompanies this file. But my main stress is on approach and not on coding. The code, which is written in just 90 minutes, is good as a working skeleton. </b></span></font></p> <p class="MsoNormal"><font size="2"><span style="font-family: Verdana">Before going into the detailed coding aspect, let us first see the structure of a sample Data File. Eliza recognizes certain keywords. If these keywords are found in the user input, then corresponding to that, from a predefined set of responses, one is chosen and displayed.</span></font></p> <p class="MsoNormal"><font size="2"><span style="font-family: Verdana">A keyword is separated in the data file (called Dictionary) from the responses by <b>@KWD@</b> token. This token indicates that the next line that follows is actually a keyword, not a response.</span></font></p> <center> <table border="1" cellpadding="0" cellspacing="0" width="80%" bordercolor="#008000" bgcolor="#CCFFCC"> <tr> <td width="100%"> <blockquote> <p><code><br> <b>@KWD@<br> </b>HELLO<br> HI, HOW ARE YOU<br> HELLO DEAR !<br> <b>@KWD@<br> </b>I WILL<br> YOU WILL DO SO. I BELIEVE IT TOO...<br> WILL YOU BE ABLE TO DO SO ?<br> <b>@KWD@<br> </b>YES<br> ARE YOU SURE ?<br> HOW CAN YOU BE SO SURE ?<br> YOU SEEM TO BE VERY OPTIMISTIC.<br> <b>@KWD@<br> </b>NO<br> YOU SEEM TO BE VERY PESSIMISTIC.<br> NEVER SAY NO...<br> <b>@KWD@<br> </b>COMPUTER<br> I KNOW HOW TO WORK ON COMPUTER.<br> YOU ARE CURRENTLY USING A COMPUTER. RIGHT ?</code></p> <p><code><b><< Add whatever you want in above format >></b></code></p> <p align="center"><b><font face="Verdana" size="2" color="#FF0000">Contents of file : Eliza.dat</font></b></p> </blockquote> </td> </tr> </table> </center> <p class="MsoNormal"><font face="Verdana" size="2">For example, in response to 'Hello', from the above dictionary, Eliza will give one of the following responses:</font></p> <ul> <li> <p class="MsoNormal"><code>HI, HOW ARE YOU</code></li> <li> <p class="MsoNormal"><code>HELLO DEAR !</code></li> </ul> <p class="MsoNormal"><code><font face="Verdana" size="2">Once this thing is clear, let us now define the Data Structures that we will be using. We create two classes : </font></code></p> <ul> <li> <p class="MsoNormal"><code><font face="Verdana" size="2">progstr - This is used to store the user's input related information.</font></code></li> <li> <p class="MsoNormal"><code><font face="Verdana" size="2">resp - This is used to store the information about the various responses.</font></code></li> </ul> <p class="MsoNormal"><code><font face="Verdana" size="2">Let me give the code first and then I will explain it.</font></code></p> <center> <table border="1" cellpadding="0" cellspacing="0" width="80%" bordercolor="#008000" bgcolor="#CCFFCC"> <tr> <td width="100%"> <blockquote> <p><code><br> <b>class progstr</b><br> {<br> public:<br> char userip[MAX_USER_INPUT];<br> char keyword[30];<br> int keyfound;<br> int keyno;<br> int nullip;<br> // constructor<br> <b>progstr()</b><br> {<br> keyno=-1;<br> nullip=0;<br> keyfound=0;<br> }<br> }ip;<br> <br> <b>class resp</b><br> {<br> int tot_resp;<br> int last_resp;<br> char replys[MAX_RESP_NO][MAX_RESP_LEN];<br> char word[MAX_KWD_LEN];<br> public:<br> // constructor<br> <b>resp()</b><br> {<br> tot_resp=0;<br> last_resp=-1;<br> }<br> <br> <b>int getcount()</b><br> {<br> return last_resp;<br> }<br> <br> <b>void addword(char str[MAX_KWD_LEN])</b><br> {<br> strcpy(word,str);<br> }<br> <br> <b> char * getword()</b><br> {<br> return word;<br> }<br> <br> <b> void addresp(char str[MAX_RESP_LEN])</b><br> {<br> strcpy(replys[++last_resp],str);<br> }<br> <br> // defined later<br> <b> void display_resp(int num);<br> void quit_display_resp(int num);<br> </b><br> };<br> </code></p> <p align="center"><b><font face="Verdana" size="2" color="#FF0000">Basic Data Structures Involved</font></b></p> </blockquote> </td> </tr> </table> </center> <p class="MsoNormal"><font face="Verdana" size="2"><span style="mso-bidi-font-size: 12.0pt">The character array <b>userip </b>is used to store the line typed by the user. Another array <b>keyword</b> is used to store the keyword, if any, found in that input. If a keyword is found, we make int <b>keyfound</b> to 1 else, it remains 0, as it is initialized to 0 in the Constructor. <b>keyno</b> stores the key number of the corresponding keyword.</span></font></p> <p class="MsoNormal"><font face="Verdana" size="2"><span style="mso-bidi-font-size: 12.0pt"><b>nullip </b>indicates whether the user has given any Null input ie, he is just pressing enter and doing nothing else.</span></font></p> <p class="MsoNormal"><font face="Verdana" size="2"><span style="mso-bidi-font-size: 12.0pt">Now let us come to the second class, <b>resp</b>. The first data member, <b>tot_resp</b> indicates the total number of responses for a given keyword. For example, for the keyword <code>Hello</code>, we have 2 responses (see Eliza.Dat). So, for that, <b>tot_resp</b> holds a value of 2. <b>last_resp </b>is used for the function processing and its use will be clear later on. </span></font></p> <p class="MsoNormal"><font face="Verdana" size="2"><span style="mso-bidi-font-size: 12.0pt">The replies are actually stored in </span></font><font face="Verdana" size="2"><b>replys[MAX_RESP_NO][MAX_RESP_LEN] </b>and the corresponding keyword is stored in the array <b>word. </b></font></p> <p class="MsoNormal"><font face="Verdana" size="2"><b>Description of Functions in Class resp :</b></font></p> <ul> <li> <p class="MsoNormal"><font face="Verdana" size="2"><b>Constructor: <br> </b>This is used to initialize the total number of responses to 0. Why last_resp is initialized to -1 will be clear when you look at the function add_resp.</font></li> <li> <p class="MsoNormal"><font face="Verdana" size="2"><b>int getcount():<br> </b>This function is used to get a count of how many responses are there for a given keyword. </font></li> <li> <p class="MsoNormal"><font face="Verdana" size="2"> <b>void addword(char str[MAX_KWD_LEN]):<br> </b>This is used to add a keyword.</font></li> <li> <p class="MsoNormal"><font face="Verdana" size="2"><b>char * getword():<br> </b>Used to return the keyword for a particular object of class resp.</font></li> <li> <p class="MsoNormal"><font face="Verdana" size="2"><b>void addresp(...):<br> </b>This is used to add a response corresponding to a given keyword.</font></li> <li> <p class="MsoNormal"><font face="Verdana" size="2"><b>void display_resp(int):<br> </b>This is used to display the response to the user corresponding to a given index number for the responses. (actually it does more than that !).</font></li> <li> <p class="MsoNormal"><font face="Verdana" size="2"><b>void quit_display_resp(int):<br> </b>Difference between this function and above function is that it is used in the end when the user is quitting. So, it does not return the prompt to the user.</font></li> </ul> <p class="MsoNormal"><font face="Verdana" size="2">Let us now create a function that reads the contents of file Eliza.Dat in an array of objects of class resp, which we name <b>keys</b>. Since I have already explained both - the format of .Dat file and data structures, this function should by clear with little or no effort. The code is commented wherever necessary.</font><p class="MsoNormal"> <center> <table border="1" cellpadding="0" cellspacing="0" width="80%" bordercolor="#008000" bgcolor="#CCFFCC"> <tr> <td width="100%"> <blockquote> <p><code><br> void read_from_file()<br> {<br> ifstream fin;<br> int index=-1;<br> fin.open("eliza.dat");<br> char line[MAX_RESP_LEN];<br> while(fin)<br> {<br> fin.getline(line,MAX_RESP_LEN);<br> char *ptr=NULL;<br> ptr=strstr("@KWD@",line);<br> if(strlen(line)<1)<br> {<br> break;<br> }<br> else if(ptr!=NULL)<br> {<br> // the next line is a keyword<br> fin.getline(line,MAX_RESP_LEN);<br> keys[++index].addword(line);<br> }<br> else<br> {<br> // it is a response<br> keys[index].addresp(line);<br> }<br> } // end of while<br> } // end of function<br> </code></p> <p align="center"><b><font face="Verdana" size="2" color="#FF0000">Read Contents of Eliza.Dat File</font></b></p> </blockquote> </td> </tr> </table> </center> <p class="MsoNormal"><font face="Verdana" size="2">Now let us create a function for global initialization of the transposition words. This function is easy and I will not belabor it.<br> </font> <center> <table border="1" cellpadding="0" cellspacing="0" width="80%" bordercolor="#008000" bgcolor="#CCFFCC"> <tr> <td width="100%"> <blockquote> <p><code><br> <b>void initialize_global()</b><br> {<br> strcpy(wordin[0],"ARE");<br> strcpy(wordout[0],"AM");<br> <br> strcpy(wordin[1],"AM");<br> strcpy(wordout[1],"ARE");<br> <br> strcpy(wordin[2],"WERE");<br> strcpy(wordout[2],"WAS");<br> <br> strcpy(wordin[3],"WAS");<br> strcpy(wordout[3],"WERE");<br> <br> strcpy(wordin[4],"YOU");<br> strcpy(wordout[4],"ME");<br> <br> strcpy(wordin[5]," I ");<br> strcpy(wordout[5],"YOU");<br> <br> strcpy(wordin[6],"YOUR");<br> strcpy(wordout[6],"MY");<br> <br> strcpy(wordin[7],"MY");<br> strcpy(wordout[7],"YOUR");<br> <br> strcpy(wordin[8],"I'VE");<br> strcpy(wordout[8],"YOU'VE");<br> <br> strcpy(wordin[9],"YOU'VE");<br> strcpy(wordout[9],"I'VE");<br> <br> strcpy(wordin[10],"I'M");<br> strcpy(wordout[10],"YOU'RE");<br> <br> strcpy(wordin[11],"YOU'RE");<br> strcpy(wordout[11],"I'M");<br> <br> strcpy(wordin[12],"ME");<br> strcpy(wordout[12],"YOU");<br> <br> strcpy(wordin[13],"YOU");<br> strcpy(wordout[13],"ME");<br> }<br> </code></p> <p align="center"><b><font face="Verdana" size="2" color="#FF0000">Basic Transformations</font></b></p> </blockquote> </td> </tr> </table> </center> <p class="MsoNormal"><br> <font face="Verdana" size="2">Let us now write a function for displaying the responses to the user. The first if statement in the for loop is used to make a deliberate typing error to make it appear more human like ;-). One character is randomly chosen for typing error. Special cases like New Line and Backspace are separately considered. (Think why ?). Now I introduce something new. A special character - <b>*</b>. Char * represents all of the text found AFTER the identified keyword, and before one of the following punctuation marks. <br> <br> For example, consider the user input<br> <br> AMIT > CAN I <i><b>GO TO INDORE TOMORROW</b></i> ? <br> ELIZA > WHAT IF YOU DO NOT <b><u>GO TO INDORE TOMORROW </u></b>? <br> <br> The underlined portion is not stored in the dictionary, rather it is taken from the user input. In the file Eliza.Dat, we store this information as <br> <br> CAN I<br> WHAT IF YOU DO NOT *<br> <br> Star (*) asks the program to simply copy whatever is typed after the keyword (here CAN I ) in the user input, as it is. I hope that now the function of * as a special keyword is clear. So, let us consider a more complicated case.<br> <br> AMIT > CAN I <i><b>GO TO INDORE TOMORROW WITH MY FRIEND</b></i> ? <br> ELIZA > WHAT IF YOU DO NOT <b><u>GO TO INDORE TOMORROW WITH MY FRIEND</u></b>? <br> <br> Obviously this is not what we wanted. I am supposed to go with <i>my friend, </i>not one of Eliza ! So, we must perform some transformation also. When we think of transformation, the sentence gets divided in the following 3 sections:</font> <ol> <li> <p class="MsoNormal"><font face="Verdana" size="2"><b>Text Before Transposition Word. </b>(here, GO TO INDORE TOMORROW WITH )</font></li> <li> <p class="MsoNormal"><font face="Verdana" size="2"><b>The Transposed keyword. </b>(here, <b>YOUR</b>, in place of MY)</font></li> <li> <p class="MsoNormal"><font face="Verdana" size="2"><b>Text After Transposition Keyword. </b>(here, FRIEND ? )</font></li> </ol> <p class="MsoNormal"><font size="2" face="Verdana">The following code tackles the three cases in a very lucid manner.<br> </font> <center> <table border="1" cellpadding="0" cellspacing="0" width="80%" bordercolor="#008000" bgcolor="#CCFFCC"> <tr> <td width="100%"> <blockquote> <p><code> <b>void resp :: display_resp(int num)</b><br> {<br> cout<<"ELIZA > ";<br> for(int i=0;i<strlen(replys[num]);i++)<br> {<br> // for deliberate typing errors <br> // (for simulating the human behavoir ;-)<br> <br> if(RanNum(6)==0)<br> { <br> char c=RanNum(100);<br> if(c=='\n' || c=='\b' || c==13)<br> cout<<"w";<br> else<br> cout<<c;<br> delay(RanNum(DELAY));<br> // correcting the deliberate typing error<br> cout<<"\b";<br> }<br> <br> <br> // * is used to write anything after the keyword<br> // as it is, but with some transformations like <br> // converting <i>MY</i> to <i>YOUR</i>.<br> if(replys[num][i]=='*')<br> {<br> char * s1=ip.userip+strlen(ip.keyword);<br> short int flag=0;<br> for(int m=0;m<TRANSPOSE;m++)<br> {<br> char * s2=wordin[m];<br> char *ptr=NULL;<br> ptr=strstr(s1,s2);<br> <br> if(ptr!=NULL)<br> {<br> // transposition word found in the<br> // user input<br> flag=1;<br> <br> <b>// printing text before wordin[m]<br> </b> int times=ptr-s1;<br> for(int i=0;i<times;i++)<br> {<br> delay(DELAY);<br> cout<<ip.userip[strlen(ip.keyword)+i];<br> }<br> <br> <b>// printing the wordout<br> </b> cout<<wordout[m];<br> <b> // printing the left overs<br> </b> char c;<br> c=*(ptr+strlen(wordin[m]));<br> int t=0;<br> while(c!='\0')<br> {<br> cout<<*(ptr+strlen(wordin[m])+t);<br> t++;<br> c=*(ptr+strlen(wordin[m])+t);<br> }<br> }<br> } // end of for<br> <br> // if flag is still zero , this means no need for <br> // transposing any word.<br> if(0==flag)<br> {<br> char c;<br> c=*(s1+strlen(ip.keyword));<br> int t=0;<br> while(c!='\0')<br> {<br> cout<<*(s1+t);<br> t++;<br> c=*(s1+t);<br> }<br> } // end of if<br> break;<br> }<br> else<br> {<br> cout<<replys[num][i];<br> delay(RanNum(DELAY));<br> }<br> } // end of for<br> <br> // giving the prompt back to user<br> cout<<"\n"<<user<<" > ";<br> }<br> </code></p> <p align="center"><b><font face="Verdana" size="2" color="#FF0000">Function for displaying Eliza's Response</font></b></p> </blockquote> </td> </tr> </table> </center> <p class="MsoNormal"><font face="Verdana" size="2">Finally we can work out a procedure for searching the keyword in the user's input. MAX_KEY indicates the number of keywords in the DAT file. Here we are simply searching whether the keyword exists in the user input (anywhere).<br> </font> <center> <table border="1" cellpadding="0" cellspacing="0" width="80%" bordercolor="#008000" bgcolor="#CCFFCC"> <tr> <td width="100%"> <blockquote> <p><code><br> <b>void find_keyword()</b><br> {<br> int len=0;<br> int lenkey=0;<br> int key_no=0;<br> char teststr[50];<br> while((ip.keyfound==0) &&(key_no!=MAX_KEY))<br> {<br> // getting the length of the keyword<br> lenkey=strlen(keys[key_no].getword());<br> <br> char *ptr=NULL;<br> ptr=strstr(ip.userip,keys[key_no].getword());<br> if (ptr!=NULL)<br> {<br> // keyword found !<br> ip.keyfound=1;<br> ip.keyno=key_no;<br> strcpy(ip.keyword,keys[key_no].getword());<br> break;<br> }<br> key_no++;<br> }<br> }<br> <br> </code></p> <p align="center"><b><font face="Verdana" size="2" color="#FF0000">Simple Search Routine.</font></b></p> </blockquote> </td> </tr> </table> </center> <p class="MsoNormal"><font face="Verdana" size="2"><br> When all these routines are made, we integrate them in the main function. For complete source code, please download the accompanying Zip file with the article.</font> <p class="MsoNormal"><b><span style="font-size:16.0pt;mso-bidi-font-size:12.0pt; font-family:Verdana"><br> Future Prospects:</span><span style="font-size:16.0pt;mso-bidi-font-size:12.0pt; font-family:Verdana"> </span><span style="mso-bidi-font-size: 12.0pt; font-family: Verdana"><font size="2"><o:p> </o:p> </font></span></b></p> <p class="MsoNormal"><span style="font-family:Verdana"><font size="2">Like other AI programs, this program also has immense possibilities of improvement. </font><font size="2"><o:p> </o:p> </font></span></p> <p class="MsoNormal"><span style="font-family:Verdana"><font size="2">The following are the improvements, which can be made in it.</font><font size="2"> <o:p> </o:p> </font></span></p> <ul> <li> <p class="MsoNormal" style="margin-left:.3in;text-indent:-.3in;mso-list:l1 level1 lfo2; tab-stops:list .3in"><font size="2"><span style="font-family:Verdana">Learning by Time or Experience can be Implemented.</span></font></li> <li> <p class="MsoNormal" style="margin-left:.3in;text-indent:-.3in;mso-list:l1 level1 lfo2; tab-stops:list .3in"><font size="2"><span style="font-family:Verdana">All the previous talking can be stored in a array of strings, so that in case of user contradicting himself/herself, ELIZA can contradict him.</span></font></li> <li> <p class="MsoNormal" style="margin-left:.3in;text-indent:-.3in;mso-list:l1 level1 lfo2; tab-stops:list .3in"><font size="2"><span style="font-family:Verdana">A database or a flat file, at least, can be used for the data and talk storage.</span></font></li> <li> <p class="MsoNormal" style="margin-left:.3in;text-indent:-.3in;mso-list:l1 level1 lfo2; tab-stops:list .3in"><font size="2"><span style="font-family:Verdana">Sessions and User-Password Pairs can be established, so that, even after the completion of one session, next time, whenever the user enters his User Name and Password, ELIZA, will get all the relevant data and previous talks, related to the user, from the database itself.</span></font></li> <li> <p class="MsoNormal" style="margin-left:.3in;text-indent:-.3in;mso-list:l1 level1 lfo2; tab-stops:list .3in"><font size="2"><span style="font-family:Verdana">A prospect for the <b>cache</b> <b>Memory</b> can be made, so as make the retrieval of data and information can be faster.<span style="mso-spacerun: yes"> I had written a Research Paper on this topic one year ago, <b><a href="http://amitmathur.8m.com/resources/rp.html">Intelligent Information Retriever - Inception of Artificial Intelligence in Search Engines</a>. </b>Follow the link to download it if you are interested.</span></span></font></li> <li> <p class="MsoNormal" style="margin-left:.3in;text-indent:-.3in;mso-list:l1 level1 lfo2; tab-stops:list .3in"><font size="2"><span style="font-family:Verdana">Standard Search algorithm can be used for the faster or better search of the relevant result or answer.</span></font></li> <li> <p class="MsoNormal" style="margin-left:.3in;text-indent:-.3in;mso-list:l1 level1 lfo2; tab-stops:list .3in"><font size="2"><span style="font-family:Verdana">Various Graphical signs and/or symbols can be incorporated to show emotions, making the conversations more lively and more realistic in nature.</span></font></li> </ul> <p class="MsoNormal" ><b><span style="font-size: 16.0pt; mso-bidi-font-size: 12.0pt; font-family: Verdana">Important Note About Sample Code:</span></b></p> <p class="MsoNormal"><span style="mso-bidi-font-size: 12.0pt"><font face="Verdana" size="2">Please note that the current functionality and features of this program are very limited and they are just for accompanying the article. If you want to make this program more intelligent, make entries in Eliza.Dat file.</font></span></p> <p class="MsoNormal"><span style="mso-bidi-font-size: 12.0pt"><font face="Verdana" size="2">You can also increase the string manipulation power of the program, like considering multiple lines from the user, etc. I had written this code in 1 1/2 hr. just to make it more easier for the readers of my article about what is happening.</font></span></p> <p class="MsoNormal"><b><span style="mso-bidi-font-size: 12.0pt"><font face="Verdana" size="2">HOW SMART YOU MAKE YOUR ELIZA DEPENDS ON HOW FAR YOU EXTEND THIS PROGRAM. THERE IS PRACTICALLY NO LIMIT ! <br> THIS CODE IS THE MINIMAL WORKING SKELETON !!</font></span><span style="font-size: 16.0pt; mso-bidi-font-size: 12.0pt; font-family: Verdana"> </span></b></p> <p class="MsoNormal"><b><span style="mso-bidi-font-size: 12.0pt"><font face="Verdana" color="#0000FF">Don't forget to read README.TXT before making conclusions about the program !</font></span></b></p> <p class="MsoNormal" ><b><span style="font-size: 16.0pt; mso-bidi-font-size: 12.0pt; font-family: Verdana">Complete Source Code:<br> </span></b><span style="mso-bidi-font-size: 12.0pt"><font face="Verdana" size="2"><br> Included in the accompanying Zip File.</font></span></p> </body> </html>