Chapter 12. Chatbot::Eliza

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.)

While taking a course in Natural Language Processing, I 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 Chatbot::Eliza module from 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. Here’s a sample run:

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?

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 watch Eliza think, you can turn on the debugging output before you launch your session:

$mybot->debug(1);
$mybot->command_interface;

Let’s look at the session we saw earlier but with 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?

How It Works

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

  • 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.

As we saw above, 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 (e.g., * 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.

Here, we create two bots and make them talk to one another:

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.";

srand( time ^ ($$ + ($$ << 15)) );    # Seed the random number generator.

print $harry->name, ": $he_says 
";

while (1) {
    $she_says = $sally->transform( $he_says );
    print $sally->name, ": $she_says 
";

    $he_says  = $harry->transform( $she_says );
    print $harry->name, ": $he_says 
";
}

Two bots conversing produces interesting results only if we have clever scripts. Below, we see sample output from the program, with both bots using the default Eliza script:

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?

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 to a flesh-and-blood psychiatrist.

Here’s an interactive session, this time with a human deliberately trying to expose Eliza’s weaknesses:

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.

What Now?

The CPAN includes many modules that allow a script to interact easily with resources on the Internet. For example, it is straightforward to write scripts that 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.)

References

The CMU Artificial Intelligence Repository

http://www.cs.cmu.edu/afs/cs/project/ai-repository/ai/areas/classics/0.html

Hayden’s Java Eliza

http://chayden.net/eliza/Eliza.html

Goerlich’s Java Eliza

http://philly.cyberloft.com/bgoerlic/eliza.htm

The white paper on the Loebner Prize competition

http://www.lazytd.com/lti/pub/aaai94.html

ftp://ftp.nl.cs.cmu.edu/usr/mlm/ftp/pubs/aaai94.ps.Z

Julia, an Eliza-like chatterbot that roams on TinyMUDs

http://www.lazytd.com/lti/julia/

The CYC Project

http://www.cyc.com

BotSpot

http://www.botspot.com

UMBC AgentWeb

http://www.cs.umbc.edu/agents/

The Simon Laven page (“Chatterbot Central”)

http://www.simonlaven.com/

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset