Is it Live? Or is it Chatbot::Eliza?

When we were in college, my wife (then, girlfriend) had the best answering machine greeting message, ever. When people called her, the answering machine would answer, “Hello?” and wait. Invariably, the caller would start talking as though they had actually reached a live person. They'd be talking about last weeks assignments, or a party next week. Then the other shoe would drop. The answering machine would then continue, “Oh, I'm sorry. I'm not here at the moment. If you'd like to leave a message, please wait for the beep!” The caller would then have to start over, probably feeling a bit sheepish. Eventually, she changed her message to something more conventional, probably under threat of severe beatings from her friends and family. But before she changed it, she got to where she wouldn't even bother answering the phone if she was available; it was a lot of fun listening to people talk to a machine, thinking they were talking to a person.

I can relate. Ever since High School, I've wanted to write programs that acted like, or looked like real people. I remember playing with a program called Eliza when I was young and thinking how amazing it was that it worked as well as it id. The Eliza program would impersonate a Psychiatrist and attempt to “analyze” your “problem” by holding a natural language conversation with you. However, as soon as you realized that it was just matching simple patterns, it would mention something that you had said earlier, just to throw you for a loop. It was primitive, but effective.

When I was in college, I got heavily involved in playing in Multi-User Dungeons, or MUDs. These games were essentially text-based MMORPG's where people all over the world could talk to each other and explore places that had been created by other players. It was fun. Then came the 'bots. It became interesting, at least to me. The 'bots were programs just like Eliza. In fact, many of them used the same algorithm to appear as though they were another player in the game. Instead of a Psychiatrist, some of the 'bots posed as girlfriends, bar maids, mailmen, and even pets. But there was something new; they could respond to specific orders. For example, you could ask a 'bot how to get to a certain location in the MUD, or when a certain player was last on-line. You could even ask them about themselves. In addition to being fun and interesting, they became useful.

So a program that pretends to be human must be difficult to write, right? Well actually, it's embarrassingly simple, thanks to Perl's Chatbot::Eliza module. Let's take a look at a (very) trivial example:

#!/usr/bin/perl

use Chatbot::Eliza;

$bot = new Chatbot::Eliza("Jill", "");

$bot->command_interface();

That's right, 4 lines of code and no configuration needed! The module comes with a default database that allows it to carry on the tradition of the original Eliza program. Here is a sample of an interaction with “Jill.”

Jill: Please tell me what's been bothering you.
you: I'm bored.
Jill: Do you enjoy being bored?
you: No.
Jill: Does this make you feel unhappy?
you: Yes.
Jill: Please go on.
you: I want something to do.
Jill: Suppose you got something to do soon.
you: That would be great!
Jill: Tell me more about that.
you: I'm bored.
Jill: Is it because you are bored that you came to me?
you: yes.
Jill: I see.

Not bad for 4 lines of code, but our program isn't very flexible. For one thing, it's relegated to a command-line interface. Let's make a change to the program to demonstrate using the module from our own run-loop:

#!/usr/bin/perl

use Chatbot::Eliza;

$bot = new Chatbot::Eliza("Jill", "");

print $bot->{initial}->[0] . "\n";

$true++;

while ($true) {
print "You: ";
$message = <STDIN>;

$message = $bot->transform($message);
print "$message\n";
}

exit;

This program behaves almost exactly like the first program, but it demonstrates how the module could be integrated into a Jabber client, or an IRC client, or whatever. All you'd have to do is get a message from the user, pass it to the transform() method and send the result back to the user.

Still, a faux psychiatrist is kind of old hat; let's see how hard it is to change the program's “personality.” The module comes with a default data-set that implements the psychiatrist, but you can pass the name of a new database as the second parameter of the object's constructor. Depending on your political bent, this might resemble:

$bot = new Chatbot::Eliza("Sarah", "palin.txt");
or
$bot = new Chatbot::Eliza("Barak", "obama.txt");

Either of those could be a lot of fun, but I try to keep out of politics here. Anyway, now that we know how to load a different database, what does this database look like? Here is an excerpt from the default database:

key: remember 5
decomp: * i remember *
reasmb: Do you often think of (2) ?
reasmb: Does thinking of (2) bring anything else to mind ?
reasmb: What else do you recollect ?
reasmb: Why do you remember (2) just now ?
reasmb: What in the present situation reminds you of (2) ?
reasmb: What is the connection between me and (2) ?
reasmb: What else does (2) remind you of ?

The rest of the patterns resemble this one, so let's discuss what this does and how. The key: line introduces a keyword, “remember” and assigns it a ranking of 5. This represents a slight performance optimization. The module first scans the input message for key words. When a keyword is found, it's ranking is noted. The keyword with the highest rank wins and we move on the the “decomp” section. The decomp section is simply a pattern that we apply to an input sentence. The asterisks are wildcards and cause parts of the sentence to be stored for later substitution into the reply. If the decomp matches, the module will chose, randomly, from the list of reasmb choices. The reasmb, or reassembly patterns are what the module returns as a reply. Note that the numbers in parenthesis correspond to the asterisks in the decomp section. For example, (2) represents the second asterisk in the decomp pattern.

From here, it's easy to imagine creating a database that mimics almost any type of personality. But, we could do more. With just a little bit of work, we could slightly change the database format so that the returned message was a tab-delimited list. The first item on the list could be the return message, as before. The next item could be the name of a Perl function and parameters to evaluate. Such a pattern might look like:

key: like 5

decomp: * like you

reasmb: It's nice to hear that you like me <tab> karma(+1);

Then we could implement a subroutine called karma() that keeps track of how “nice” a person is. We would have to change our main loop so that when a given variable exceeds certain thresholds, a new database could be loaded resulting in “Jill” becoming increasingly “flirty,” or “witchy” depending on how she's spoken to. We could also implement functions that interact with the rest of the world. We could then ask “Jill” what the load average is on a given server, or we could ask her for the first link returned by Google for a given word or phrase. The sky is the limit.

Now I'm not seriously proposing an Eliza bot as the next generation of computer user interface. However, the Chatbot::Eliza Perl module is a fun toy that's easy to use and simple to expand and integrate. Have fun.

______________________

Mike Diehl is a freelance Computer Nerd specializing in Linux administration, programing, and VoIP. Mike lives in Albuquerque, NM. with his wife and 3 sons. He can be reached at mdiehl@diehlnet.com

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

who are you

Anonymous's picture

who are you

Nice try

Anonymous's picture

> He can be reached at mdiehl@diehlnet.com

Nice try. I'm sure anyone that tries to send an email to that email will get an Elizabot reply.
:-)

your wife

paul (the unverified)'s picture

You are one very fortunate guy. What a great sense of humor! And after nearly 37 years of marriage, my wife and I can attest to just how important a good sense of humor is for maintaining an enjoyable, enduring relationship. I wish you and your wife many, many years of laughs together.

Thank you.

Mike Diehl's picture

Thank you for the kind words. It's not just anyone who appreciates my "sense of humor."

Mike Diehl is a freelance Computer Nerd specializing in Linux administration, programing, and VoIP. Mike lives in Albuquerque, NM. with his wife and 3 sons. He can be reached at mdiehl@diehlnet.com

AI Chat

Anonymous's picture

I remember a program written for BBS systems calles AI Chat. It took care of the annoying users who always wanted to chat with the sysop (as if the sysop had nothing better to do). It had a text list of responses to comments and questions that you could edit. This program fooled many users of a friends BBS , especially one bored kid that was always wanting to chat. My friend didn't mind the ocassional chat, but this kid was really annoying.

We customized the text list of responses to include phrases my friend used. It was quite a neat program.

White Paper
Linux Management with Red Hat Satellite: Measuring Business Impact and ROI

Linux has become a key foundation for supporting today's rapidly growing IT environments. Linux is being used to deploy business applications and databases, trading on its reputation as a low-cost operating environment. For many IT organizations, Linux is a mainstay for deploying Web servers and has evolved from handling basic file, print, and utility workloads to running mission-critical applications and databases, physically, virtually, and in the cloud. As Linux grows in importance in terms of value to the business, managing Linux environments to high standards of service quality — availability, security, and performance — becomes an essential requirement for business success.

Learn More

Sponsored by Red Hat

White Paper
Private PaaS for the Agile Enterprise

If you already use virtualized infrastructure, you are well on your way to leveraging the power of the cloud. Virtualization offers the promise of limitless resources, but how do you manage that scalability when your DevOps team doesn’t scale? In today’s hypercompetitive markets, fast results can make a difference between leading the pack vs. obsolescence. Organizations need more benefits from cloud computing than just raw resources. They need agility, flexibility, convenience, ROI, and control.

Stackato private Platform-as-a-Service technology from ActiveState extends your private cloud infrastructure by creating a private PaaS to provide on-demand availability, flexibility, control, and ultimately, faster time-to-market for your enterprise.

Learn More

Sponsored by ActiveState