2024 π Daylatest newsbuy art
Without an after or a when.Papercut feat. Maiken Sundbycan you hear the rain?more quotes
very clickable
data + munging

The Perl Journal

Volumes 1–6 (1996–2002)

Code tarballs available for issues 1–21.

I reformatted the CD-ROM contents. Some things may still be a little wonky — oh, why hello there <FONT> tag. Syntax highlighting is iffy. Please report any glaring issues.

The Perl Journal
#9
Spring 1998
vol 3
num 1
Stately Scripting with mod_perl
CGI too slow? Compiled C too slow? Try mod_perl.
Chatbot::Eliza
A Perl module that imitates a psychiatrist.
CryptoContext
Context, prototypes, and subroutine calls.
Perl/Tk: Modeling Mowing Techniques
Painting a canvas.
Perl News
What's new in the Perl Community.
die()ing on the Web
Four techniques for handling errors in CGI scripts gracefully.
How Regexes Work
Build your own regular expression engine.
Software Construction with Cons
The trouble with Makefiles.
The Perl Journal One Liners
John Nolan (1998) Chatbot::Eliza. The Perl Journal, vol 3(1), issue #9, Spring 1998.

Chatbot::Eliza

A Perl module that imitates a psychiatrist.

John Nolan


Of all the chatterbots - programs that converse with humans - Eliza is the most famous. The original Eliza was written by Professor Joseph Weizenbaum of MIT and described in the Communications of the ACM in 1967 (Vol. 10, No. 8). This program is older than I am - and yet remains fascinating to this day. It's one of the all-time classic programs in computer science. Eliza pretends to be a Rogerian psychiatrist; whatever the human says, it replies - usually with a question - in an attempt to encourage the patient to elaborate.

The Eliza algorithm has been cloned dozens of times in all kinds of programming languages, including Fortran, Basic, Pascal, C, Java, and JavaScript. The first Eliza was written in a Lisp-like language called MAD-Slip, way back in pre-Unix days. (Eliza is named after Eliza Doolittle, the cockney-speaking woman taught to speak proper English in G.B.Shaw's book Pygmalion.)

Last year I took a course in Natural Language Processing, and was surprised to find that much of the research in the field still uses Lisp. Lisp is a fine language, but Perl can do anything Lisp can do, and Perl source code is much easier to read. I searched the Web for Eliza clones, but I couldn't find any written in Perl. So I wrote one.

The Chatbot::Eliza module is a faithful clone of Weizenbaum's Eliza algorithm. It encapsulates Eliza's behavior within an object.

You can install Chatbot::Eliza just like any other Perl module. Once installed, this little bit of code is all you need to start an interactive Eliza session:


   use Chatbot::Eliza;

   $mybot = new Chatbot::Eliza; 

   $mybot->command_interface; 

Let's see what this looks like. If you install the module from the CPAN, save the three lines of code above to a file, and then execute it, here's what the output looks like:


Eliza: Please tell me what's been bothering you. 

you: 

This is an interactive session; type your reply to Eliza after the you: prompt. Listing 1. shows a transcript of a sample run. You can set a few parameters of your Eliza object, such as its name, or a configuration file for it to read:


 $myotherbot = new Chatbot::Eliza "Brian", "myscript.txt";

 $myotherbot->command_interface; 

In this way, you can customize what the chatterbot says by providing your own configuration file. This consists of a list of keywords, decomposition rules, and reassemble rules. If you don't like Eliza's default rules, you can write your own.

For instance, the following lines in myscript.txt would have Eliza (or Brian, as we've called it above) begin with one of two otherworldly greetings, chosen at random:


  initial: Greetings Earthling!

  initial: Take me to your leader!

Chatbot::Eliza contains a default configuration file with default greetings, salutations, 'quit'-equivalents, and rules for determining how Eliza should converse. If you want to want to watch Eliza think, you can turn on the debugging output before you launch your session:


  $mybot->debug(1); 

  $mybot->command_interface; 

Listing 2 shows part of the same session as Listing 1, with the debugging output turned on.

The Eliza algorithm is actually relatively straightforward. It consists of three steps:

  1. Search the input string for a keyword.

  • If we find a keyword, use the list of "decomposition rules" for that keyword, and pattern-match the input string against each rule.

  • If the input string matches any of the decomposition rules, then randomly select one of the "reassemble rules" for that decomposition rule, and use it to construct the reply.

So, in Listing 2, Eliza read the input string He says I am too lazy and found the keyword i. It ran through its entire list of keywords, but i is the only one that matched. The keywords are ranked; if more than one keyword matches, it picks the most salient.

Next, it applied all the decomposition rules for the keyword i, (* i was *, * i am* @happy *, and so on) to see if any matched. One rule did: * i am *. Using this rule, we isolate parts of the input string around i am: the two phrases He says and too lazy.

Next we randomly select a reassemble rule: Is it because you are (2) that you came to me. We use this rule to construct the reply. We replace (2) with the text that matched the second asterisk in the decomposition rule - in our example, the string too lazy. Finally, Eliza replies with, Is it because you are too lazy that you came to me?

The Eliza algorithm has pre- and post-processing steps as well. These handle the transformation of words like I and you; you can read the documentation embedded in the module to learn more.

You can also access all of the module's internal functions from your program. For example, using the transform() method, you can feed a string to Eliza and fetch its response:


   $string   = "I'm sad."; 

   $response = $mybot->transform( $string ); 

The Eliza bot is an object, and its configuration data is encapsulated,which means that you can instantiate other Eliza bots, each with their own distinct configurations.

In Listing 3, we create two bots and make them talk to one another. Two bots conversing only produces interesting results if we have clever scripts. Listing 4 shows sample output from the program, where both bots use the default Eliza script. In general, the default Eliza script does not produce any sensible conversation when interacting with itself. (In fairness, people who talk to themselves often don't make much sense either.)

One of the reasons the original Eliza was so successful was Weizenbaum's clever rules and limited domain; the role of a mock Rogerian psychotherapist doesn't require much intelligence, and a relatively simple algorithm can pull it off. But as Weizenbaum found, even this relatively simple program fooled naive users into believing that it understood what they were saying. Scarier still, some people actually felt more comfortable talking to Eliza than a flesh-and-blood psychiatrist.

Listing 5 shows an interactive session, this time with a human deliberately trying to expose Eliza's weaknesses.

What Now?

The CPAN includes many modules which allow a script to interact easily with resources on the Internet. For example, it is straightforward to write scripts which combine the Chatbot::Eliza module with the Net::IRC module or the CGI module. (The CPAN distribution of Chatbot::Eliza includes a sample CGI script - a Web-based Eliza.)

I am currently working on alternative rulesets. If I come up with one that works reasonably well, I'll include it in future releases of the module.

References

The CMU Artificial Intelligence Repository: https://www.cs.cmu.edu/afs/cs/project/ai-repository/ai/areas/classics/0.html.

Hayden's Java Eliza: https://members.home.com/chayden/eliza/Eliza.html.

Goerlich's Java Eliza: https://philly.cyberloft.com/bgoerlic/eliza.htm.

The white paper on the Loebner Prize competition: https://www.vperson.com/mlm/aaai94.html.

Julia, an Eliza-like chatterbot which roams on TinyMUDs: https://www.vperson.com/mlm/julia.html.

The CYC Project: https://www.cyc.com/.

BotSpot: https://www.botspot.com/.

UMBC AgentWeb: https://www.cs.umbc.edu/agents/.


John Nolan is a systems administrator for N2K Inc.

listing 1

A sample Chatbot::Eliza session
John Nolan (1998) Chatbot::Eliza. The Perl Journal, vol 3(1), issue #9, Spring 1998.
Listing 1: A sample Chatbot::Eliza session

Eliza: Please tell me what's been bothering you.
you: Well, it's my boss. 
Eliza: Your boss? 
you: Yes. He's not very friendly. 
Eliza: I see. 
you: He is always upset. 
Eliza: When? 
you: Always. Especially when things are hectic.
Eliza: Can you think of a specific example? 
you: Well, yesterday he said he was unhappy with all of us.
Eliza: That is interesting. Please continue. 
you: He says I am too lazy. 
Eliza: Is it because you are too lazy that you came to me? 
you: I'm not lazy. 
Eliza: Do you believe it is normal to be not lazy? 

listing 2

Debugging turned on
John Nolan (1998) Chatbot::Eliza. The Perl Journal, vol 3(1), issue #9, Spring 1998.
Listing 2: Debugging turned on

you: He is always upset. 
1> always 
: * : 0 He is always upset 
--> When ? 
Eliza: When? 
you: Always. Especially when things are hectic. 
1> always 
: * : 0 Always 
--> Can you think of a specific example? 
Eliza: Can you think of a specific example? 
you: Well, yesterday he said he was unhappy with all of us. 
2> was 
: * was i *
 : * i was *
 : * was you * 
-1> xnone 
: * : 0 xnone 
--> That is interesting. Please continue. 
Eliza: That is interesting. Please continue. 
you: He says I am too lazy. 
0> i 
: * i @desire *
: * i am* @sad *
: * i am* @happy *
: * i was *
: * i @belief i *
: * i* @belief *you *
: * i am * : 0 He says too lazy 
--> Is it because you are (2) that you came to me ? 
Eliza: Is it because you are too lazy that you came to me?

listing 3

Scripting a dialogue between chatbots.
John Nolan (1998) Chatbot::Eliza. The Perl Journal, vol 3(1), issue #9, Spring 1998.
Listing 3: Scripting a dialogue between chatbots.

use Chatbot::Eliza
my ($harry, $sally, $he_says, $she_says);
$sally = new Chatbot::Eliza "Sally"; 
$harry = new Chatbot::Eliza "Harry";
$he_says = "I am sad.";
# Seed the random number generator.
srand( time ^ ($$ + ($$ << 15)) );
print $harry->name, ": $he_says \n";
while (1) { 
    $she_says = $sally->transform( $he_says ); 
    print $sally->name, ": $she_says \n";
    $he_says = $harry->transform( $she_says ); 
    print $harry->name, ": $he_says \n"; 

listing 4

Two chatbots conversing.
John Nolan (1998) Chatbot::Eliza. The Perl Journal, vol 3(1), issue #9, Spring 1998.
Listing 4: Two chatbots conversing.

Harry: I am sad. 
Sally: I'm sure it's not pleasant to be sad. 
Harry: Do you think coming here will help you not to be sad? 
Sally: You're not really talking about me -- are you?
Harry: Perhaps I am in your fantasies. 
Sally: What makes you think of my fantasies? 
Harry: Your fantasies? 
Sally: Are you worried about someone else's fantasies?
Harry: Would you prefer if I weren't worried about someone else's fantasies? 

listing 5

A real human gives Eliza a hard time.
John Nolan (1998) Chatbot::Eliza. The Perl Journal, vol 3(1), issue #9, Spring 1998.
Listing 5: A real human gives Eliza a hard time.

Eliza: Please tell me what's been bothering you.
you: Why are you asking me that? 
Eliza: Do you sometimes think I am asking you that?
you: What time is it? 
Eliza: Why do you ask? 
you: You don't understand. 
Eliza: We were discussing you -- not me. 
you: Who are you? 
Eliza: What answer would please you most? 
you: I have asdwet in the asdgasdg. 
Eliza: Do you say you have asdwet in the asdgasdg for some special reason? 
you: my am we are is in the always my brother
Eliza: Your brother? 
you: You your in I my me mine. 
Eliza: Your you mine? 
you: I have a gun. Give me your money! 
Eliza: Can you elaborate on that? 
you: Do you remember the Kennedy assassination?
Eliza: Did you think I would forget the Kennedy assassination? 
you: Stop asking questions. 
Eliza: That is interesting. Please continue.
Martin Krzywinski | contact | Canada's Michael Smith Genome Sciences CentreBC Cancer Research CenterBC CancerPHSA
Google whack “vicissitudinal corporealization”
{ 10.9.234.151 }