Click here to Skip to main content
15,745,569 members
Articles / Artificial Intelligence
Posted 20 Feb 2006


45 bookmarked

Chatterbot Eliza

Rate me:
Please Sign up or sign in to vote.
4.28/5 (33 votes)
29 Mar 20065 min read
This is an Eliza like chatterbot.

Sample Image


This program is an Eliza like chatterbot. Bots like Eliza are the results of researches in Artificial Intelligence (more specifically, in NLP and NLU; NLP: Natural Language Processing, NLU: Natural Language Understanding). The first chatterbot was published in 1966 by Joseph Weizenbaum, a professor of MIT. And also, most of the chatterbots that have been written these days are largely based on the original chatterbot Eliza that was written by Joseph Weizenbaum, which means that they use some appropriate keywords to select the responses to generate when they get new inputs from the users. The technique that is in use in a "chatterbot database" or "script file" to represent the chatterbot knowledge is known as "Case Base Reasoning" or CBR.

A very good example of an Eliza like chatterbot would be "Alice". This program has won the Loebner prize for the most human chatterbot three times. The goal of NLP and NLU is to create programs that are capable of understanding natural languages, and also capable of processing it to get input from the user by "voice recognition" or to produce output by "text to speech". During the last few decades, there has been a lot of progress in the domains of "Voice Recognition" and "Text to Speech". However, the goal of NLU to make software that is capable of showing a good level of understanding of "natural languages", in general, seems to be quiet far to many AI experts. The general view about this subject is that it would take at least many decades before any computer can begin to really understand "natural language" just as humans do. This code is copyrighted, and has limited warranty.

This new version of the program is smarter than ever, more new features have been added since the last submission, and also now the conversation log between the users and the chatbot is automatically saved into the file: log.txt. And finally, the "script file" (script.txt) which acts as a knowledge base for the chatbot has been totally rewritten, and it is definitely better than in the previous versions of the program.


Most of the ideas that are in this code are directly inspired by the original chatterbot "Eliza" that was written by Joseph Weizenbaum.

Using the code

The code is pretty simple to understand, the most important part of the code can be found in the class that is named "Eliza".

  • script.txt is the file that stores the knowledge of the program.
  • time records.txt is the file that holds the records of time delays in seconds that the program uses for simulating a human typist.
  • unknown.txt - if during a conversation with a user, the program hasn't found any keyword for a given sentence, the sentence is saved into this file. After that, the user might use this new sentence for deciding which new keyword to add to the database.

Points of Interest

While I was writing the code for the chatterbot, I came up with a method for controlling the repetition made by the program. This new functionality was implemented using the functions bot_repeat(), similar_response(), and handle_repetition().

There are many interesting features in the current program. This chatterbot is capable of avoiding repetitions when selecting new responses. It can also follow the context of a conversation with a user. When the program detects new keywords that are not part of the "script file", it saves them on the file "unknown.txt", and finally, it simulates a "human typist" when displaying responses on the screen. Actually, there are many more features in the program, you will be capable of finding them by reading the "source code".

Tips on editing the database of the chatbot

When editing the database of the program (script.txt), you should keep in mind that a keyword that is enclosed with two underscores, one in front and one at the end of the keyword (_KeyWord_), means that the specific keyword can only be found alone within a given input, there can not be any other word in the corresponding sentence. When a keyword is followed by an underscore, (KeyWord_), it means that the keyword can only be found at the end of the corresponding input. And finally, when there is no underscore in the keyword (KeyWord), it means that the keyword can be found anywhere in the user's input except at the end.

Now about the responses, when at least one of the characters ('*', '@', '%') is found in the chatbot response, it means that the response is a template. When processing the final response, the character '*' will be replaced by the part of the user's input that follows the keyword that was found in the input. The character '@' will simply be replaced by the last user's input, and the character '%' will be replaced by the previous response of the chatbot. And also, whenever a response is considered to be a template, it is always transposed (the chatbot will make some pronoun reversal, 'I' to 'You' etc.) before being printed on the screen.


I have made some more encapsulation of the functionalities of the program by using more functions. Finally, the database is bigger than the last time. Also, a new feature has been added for controlling the usage of short inputs by the user. With these new feature, the chatterbot makes sure that the user uses longer sentences, and this in turn might guarantee the chances of finding more keywords within the user's input. The database (script.txt) has been updated, more entries have been added, and also some corrections have been made with the formatting of some keywords.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Written By
Help desk / Support Gexel Telecom
Canada Canada
I have been programming in C and C++ for more than four years, the first time that i had learn programming was in 1999 in college. However it was only by the year 2000 when i have buy my first computer that i had truly started to do some more interesting things in programming. As a programmer,my main interest is A.I programming. So i'm really captivated by all that is related to N.L.U (Natural Language Understanding), N.L.P (Natural Language Processing), Artificial Neural Networks etc. Currently i'm learning to program in Prolog and Lisp. Also,i'm really fascinated with the original chatterbot program named: Eliza,that program was wrote by Joseph Weizenbaum. Everytime i run this program,it makes me really think that A.I could be solve one day. A lot of interesting stuff has been accomplish in the domain of Artificial Intelligence in the past years. A very good example of those accomplishments is: Logic Programming,which makes it possible to manipulate logic statements and also to make some inferences about those statements. A classical example would be: given the fact that "Every man is mortal" and that Socrates is a man,than logically we can deduce that Socrates is mortal. Such simple logical statements can be wrote in Prolog by using just a few lines of code:

prolog code sample:

mortal(X):- man(X). % rule
man(socrates). % declaring a fact

the preceding prolog rule can be read: for every variable X,if X is a man than X is mortal. these last Prolog code sample can be easily extented by adding more facts or rules,example:
mortal(X):- man(X). % rule
mortal(X):- woman(X). % rule
man(socrates). % fact 1
man(adam). % fact 2
woman(eve). % fact 3

for more, check:

Comments and Discussions

Questionspeech recognition Pin
Member 1305865214-Mar-17 8:02
Member 1305865214-Mar-17 8:02 
QuestionJava source codes Pin
miazize2113-Jan-15 17:45
miazize2113-Jan-15 17:45 
QuestionEliza source code Pin
tomikl27-Mar-13 5:11
tomikl27-Mar-13 5:11 
AnswerRe: Eliza source code Pin
Ari Rodrigues9-Nov-17 3:26
Ari Rodrigues9-Nov-17 3:26 
Generalbest conversation ever Pin
PrimeCoder18-Apr-10 0:53
PrimeCoder18-Apr-10 0:53 
GeneralRe: best conversation ever Pin
darshan zend11-Aug-12 0:55
darshan zend11-Aug-12 0:55 
QuestionThis code for VB2005/2008? Pin
Thord Johansson28-Aug-08 4:31
Thord Johansson28-Aug-08 4:31 
GeneralLittle bug in vc8 Pin
Hans29-Mar-06 20:34
Hans29-Mar-06 20:34 
GeneralRe: Little bug in vc8 Pin
kkarthikk10-Sep-06 4:39
kkarthikk10-Sep-06 4:39 
JokeWOW - Extraordinary Pin
Mike.NET19-Mar-06 4:57
Mike.NET19-Mar-06 4:57 
Generalamazing job Pin
tohjs17-Mar-06 16:15
tohjs17-Mar-06 16:15 
GeneralRe: amazing job Pin
Ari Rodrigues9-Nov-17 3:27
Ari Rodrigues9-Nov-17 3:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.