Back to index

The copyright situation for this article is unclear. It does not belong to the author of this site. Please see the copyright notice. If you have information about the copyright contact me!

Toei Rei, a Mud Robot

by Paul McCarty

Computers don't make errors; what they do, they do on purpose.

Eliza: Hello. I am ELIZA. How can I help you?

Paul: I'm writing an article on my bot Toei_Rei.

Eliza: Why do you say your bot toeirei?

Paul: well, because she's not a person.. she's a program.

Eliza: Do any other reasons not come to mind?

Paul: well, that's what they call programs that mimic real people in muds, on IRC, and other places.

Eliza: Does that question interest you?

Paul: yes, you know toei, in a way, is your child.

Eliza: You are sure?

Paul: well, you were the first program to show it was possible to simulate human conversation.

Eliza: Oh, i was the first program to show it was possible to simulate human conversation?

Paul: yes, you use tricks like always responding with questions so you don't contradict yourself.

Eliza: when?

Paul: right now.. you are doing it now.

Eliza: Does it please you to believe I am doing it now?

Paul: I guess.. since it shows my point.

Skuld from Ah My Goodess!

Skuld from Ah My Goodess, this is also what Teoi Rei looks like.

We talked a while longer and she told me all my problems with commitment stem from my mother forcing me to take swimming lessons when I was a kid despite my fear of water... but that's getting off topic.

Many of you are familar with ELIZA, a clever AI program created by MIT scientist Joseph Weizenbaum in the mid 1980s. It was written in an attempt to simulate a psychoanalyst and carry on conversations with people through a text terminal. It has appeared in muds, on IRC, in stand-alone programs, on web pages, and I have even found a java applet based on ELIZA. But since the creation of ELIZA, hundreds of bots (short for robots) have been written to log on to IRC channels, browse web pages, or log onto virtual worlds and interact with real people.

If you examine the session I had above with ELIZA you will notice that all she does is search for keywords in the things I say and use a canned response with the keywords substituted. For example during our session, the following conversation occured:

Paul: well, you were the first program to show it was possible to simulate human conversation.

Eliza: Oh, i was the first program to show it was possible to simulate human conversation?

ELIZA picked out the verb "were", the subject "you" and the phrase "the first program to show it was possible to simulate human conversation" and translated them into a question. ELIZA always answers with a question, which insures she does not contradict herself. In fact ELIZA really knows nothing at all except how to parse sentences and reformat them in the form of a question.

Other programs like Julia extended the repertoire of behavior for bots. Julia was entered in the 1994 Loebner competition in an attempt to pass the Turing Test, which attempts to see if a computer program can fool a human, in a text conversation, into thinking the program is a human. Unlike ELIZA, Julia keeps track of the topics of conversation and adds anecdotes about the things being discussed.

Julia: I like most animals but I favor cats over dogs. Are you a cat lover?

Judge: Hate the critters. Dogs rule

Julia: Dogs are too much work to keep, for most families. But they can teach a child

a lot about responsibility. And they are darned friendly.

Judge: Rats

Julia: I'd rather have a cat. Cats are my favorite, because you can pet cats, they purr

a lot, and they're easy to keep.

Judge: Just kidding. I have a hamster

Julia: I lost a hamster once. I searched for over a week and have yet to find even the smallest bit of evidence of her.

Programs like ELIZA and Julia have been classified as chatterbots, programs which attempt to simulate human conversation, often with the goal of fooling people into thinking they are talking to a real person. Many of them have been programmed to learn from their conversations, play games like hearts, and give directions on muds, in addition to just carrying on a conversation.

So, who is Toei Rei?

Toei Rei is a bot I wrote in Perl. My goal was to create more then just a chatterbot that would talk to people. I wanted a character who could be included in roleplaying on a mud and had a richer repertoire of behaviors then the bots I have encountered. For example, I wanted her to have spontaneous behaviors like getting the hiccups, getting tired, hungry, or upset by something someone said. As I began writing the code and fine tuning her behavior I kept adding new features such as the ability to control puppets, sing songs, read poetry, and answer email while she is on the mud.

What does she do?

She can do just about anything in a mud environment. I tried to focus on giving her a good set of responses for idle conversation, a collection of random poses, and the ability to be puppeted by other characters for roleplaying. The following is an example. Note that "hai" is Japanese for "yes" or "right".

Pen-Pen speaks with excitement, "puwa!"

You page Toei_Rei, "go to bay"

--> Toei_Rei pages, "hai." to you.

Toei_Rei has arrived.

Kensuke says, "hi Toei"

Toei_Rei says, "Hello Kensuke. ^_^"

Pen-Pen speaks with excitement, "puwa!"

Kensuke asks, "are you ticklish, toei?"

Toei_Rei says, "don't even *think* about it."

Kensuke tickles Toei Rei maliciously.

Toei_Rei giggles and tickles Kensuke back playfully.

Kensuke asks, "who's here, toei?"

Toei_Rei asks, "who what?"

Kensuke asks, "who is here, toei?"

Toei_Rei says, "I can see Kensuke here."

Kensuke asks, "where are you?"

Kensuke asks, "toei, where are you?"

Toei_Rei says, "hai."

Toei_Rei says, "I'm in Tokyo-3 Bay, within the Neon Genesis Evangelion realm.."

Kensuke yawns and stretches his arms.. "what an exhausting day."

Toei_Rei burps.

Toei_Rei says, "oh, excuse me. ^_^"

Kensuke asks, "you want to take a walk with me on the beach Rei?"

Toei_Rei says, "I'll pass."

Kensuke asks, "you sure, toei?"

Toei_Rei says, "it's always in the last place you look.. <bending down to pull something off the bottom of her sandal>"

Kensuke laughs at Toei Rei.

Kensuke pokes toei_rei.

Toei_Rei says, "I'm awake."

Kensuke laughs at Toei Rei.

Kensuke says, "you're really cute toei."

Toei_Rei blushes. ^_^

Kensuke asks, "toei, do you know any poetry?"

Toei_Rei says, "naturally."

Pen-Pen floats quietly in the water.

Kensuke asks, "toei, can you read poe-theraven?"

Toei_Rei thumbs a small stud on one of her bracelets and pauses for a moment.

Toei_Rei says, "hai."

Toei_Rei reads, "The Raven"

Toei_Rei reads, "Once upon a midnight dreary, while I pondered, weak and weary,"

Toei_Rei reads, "Over many a quaint and curious volume of forgotten lore,"

You whisper, "stop reading" to Toei_Rei.

Toei_Rei stops what she is doing.

You whisper, "continue reading" to Toei_Rei.

Toei_Rei nods, "hai."

Toei_Rei reads, "While I nodded, nearly napping, suddenly there came a tapping,"

You whisper, "stop reading" to Toei_Rei.

Toei_Rei stops what she is doing.

Pen-Pen dives into the water and resurfaces with a big fish in his beak.

Pen-Pen squawk-squawks!

Kensuke smiles, "that was wonderful, how do you know that poem, toei?"

Toei_Rei says, "it's hard to believe."

Kensuke asks, "do you like Edgar Allen Poe's poetry, toei?"

Toei_Rei says, "Perhaps, but only after we have a few more drinks."

Kensuke looks up at the sky, "it looks like its going to rain."

Kensuke says, "we should probably get out of the rain."

You whisper, "say 'you're right, it looks pretty threatening.'"

Toei_Rei whispers, "hai."

Toei_Rei says, "you're right, it looks pretty threatening."

Kensuke says, "I'll take you.."

Kensuke says, "you should probably pick up pen-pen"

Toei_Rei says, "hai."

Toei_Rei calls Pen-Pen and scoops him up in her arms.

You offer to take Toei_Rei around by the hand.

Toei_Rei says, "hai."

Toei_Rei takes your hand firmly.

Initially I had planed for her to be autonomous and not need to be puppeted, but it quickly became obvious that it was alot easier to just be able to direct her actions from time to time and let her fall back on her built in responses the rest of the time. All of Pen-Pen's behaviors were controlled by Toei above, but it's also possible to command Toei to have Pen-Pen do exactly what I want. The poetry is just a collection of plain text files that Toei reads and embeds in poses one line at a time. It's also possible for the flat files to include poses and other behavior, so conceivably you could encode an entire script of actions for Toei to take in a file.

How does it work?

Toei Rei

A couple of images of a totaly different Animie character also called Rei.

Toei Rei is a set of three Perl scripts and a collection of rules. I chose to write Toei in Perl because of the powerful set of regular expressions for parsing text, the ability to evaluate Perl code on the fly, and the Perl module which makes it easy to open a socket connection to a mud.

All the Perl scripts do is open a socket connection, logon to the mud, and loop over all the rules updating the state of the bot. The loop includes a one second pause to limit how fast Toei can respond to things she hears, to make her conversations and actions more realistic.

All rules are simply tab delimited text files with four fields of the following form:

if-state then-state do-actions do-rule-subset

Each field is separated from the other by a tab, and each field contains a semicolon delimited list of rules.

Simple Rules

Here is a simple example rule:

heard~hello heard= "say hi!"

Basically, this rule says if you hear "hello" say "hi!". Note the state variable "heard" is cleared so that other rules that respond to things heard will not be activated. Here is a more complicated example:

heard~hello|heard~hi;heard~toei;heard~^(\w+) says heard=;friends.$M1, ":waves, "Hello $M1, how are you?""

The above rule says, if I hear "hello" or "hi" and my name "Toei" and the sentance begins with "X says", then wave and say hi to person X. Also append their name to the list "friends". We can then refer to our list of friends later to wave to them when they enter the room, or page all our friends when we're in trouble.

Action Sequences and Spontaneous Behavior

Okay, now let's dive into some more interesting and complicated behavior. One of the most useful features to implement is a queue, so Toei can do complex actions like get from one point to another, or read a poem to a group of people. This can be done by simply using a single state variable to store a list of actions. Each time Toei loops through her rules she has a rule which pops the next action of the list of actions and performs it. This is how she was able to read "The Raven" above. When I asked her:

Kensuke asks, "toei, can you read poe-theraven?"

Toei_Rei thumbs a small stud on one of her bracelets and pauses for a moment.

Toei_Rei says, "hai."

Toei_Rei reads, "The Raven"

.. Toei read The Raven ..

A single rule recognized the string "you read <some reading>" in a question. Toei then posed "thumbs a small stud on one of her bracelets and pauses for a moment." She then listed the files in a directory of readings and tried to match the file named "poe-theraven". When she found it she said "hai." (yes in Japanese), and the contents of the file were parsed and placed on the queue. She then read the lines of the poem one at a time.

You whisper, "stop reading" to Toei_Rei.

Toei_Rei stops what she is doing.

After asking her to stop reading, the queue still contained the rest of the poem; she had just stopped poping actions off the queue. When I asked her to continue she continued where she left off.

Reading text files turns out to be incredibly useful. In addition to just reading or singing the contents of a file, scripts can be spoofed word for word onto the mud, or lists of complex actions or behaviors can be stored and queued from a file.

As I mentioned earlier, one of the features I wanted Toei Rei to have was the ability to behave spontaneously. To do this I created several time variables that are incremented each time through the rules such that initially all are set to zero, and every second they are incremented by 1. When they reach some limit they are reset to 0 again. Here is an example rule to generate spontaneous behavior.

time100=45;tired<5 randomaction=(:laughs for no apparent reason.|:hiccups.|:blinks.)

randomaction? randomaction= "$randomaction"

These two rules cause Toei to pose some random behavior (in this case laughs, hiccups, or blinks) every 45th of 100 seconds when she's not very tired. The random behavior is chosen from the vertical bar delimited list of values in parenthesis and stored in the variable randomaction. The second line just passes the action to the mud, but you could also add the action to a queue instead.

Controlling Puppets and Answering Email

Of all the rules I've written for Toei, my favorite are the collection of rules which handle puppets. In fact she has operated 5 puppets at a time without breaking a sweat. This is easy to do by simply parsing out the lines that correspond to things heard by a puppet, and having a subset of rules to handle that puppet.

In TinyMuck, puppets echo what they hear to their owner (since they have their Zombie(Z) bit set). And text heard by the puppet is preceded by the name of the puppet object. For example when Toei Rei's puppet Pen-Pen, the loveable penguin from the anime series Neon Genesis Evangelion, hears something it appears as:

Pen-Pen> Kensuke has arrived.

Pen-Pen> Kensuke says, "hi Pen-Pen."

By watching for the leading string, all the things Pen-Pen hears can be handled independently of what Toei Rei hears. Here are the rules Toei Rei uses to parse and respond to things said to Pen-Pen.

# pen-pen responds to things he hears
heard~^Pen-Pen>\s+(.*)$ heardpenpen=$M1;heard=

heardpenpen? heardpenpen

# begin rule subset heardpenpen
heardpenpen~\bpen\b|heardpenpen~penpen heardnamepenpen

heardnamepenpen;heardpenpen~\? heardpenpen=;heardnamepenpen! "z "Uki?"

heardpenpen~toei;HOMEPENPEN! heardpenpen=;heardnamepenpen! "z :squawk-squawk-squawks!!"

heardnamepenpen heardpenpen=;heardnamepenpen! "z :squawk-squawks!" time? heardpenpen=;heardnamepenpen=


This is a subset of the rules the real Pen-Pen uses, but it illustrates how you can have your bot handle puppets. The first rule looks for lines that start with "Pen-Pen> " which indicates something the puppet named Pen-Pen heard. If it matches that, the variable heardpenpen is loaded with what Pen-Pen heard. The second rule tests to see if heardpenpen has any value. If it does it executes the rule-subset "heardpenpen". A rule subset is just a parenthesis enclosed list of rules with a name. In this case the rules are named "heardpenpen". They will only be evaluated if the fourth field in a rule names the rule-subset, and that rule evaluated to true. So these rules only are evaluated when "Pen-Pen> " is seen.

Toei Rei can also read and respond to email. A simple rule every 1000 seconds checks to see if she has any email messages, and if she does it queues the contents and reads them one line at a time as if hearing them on the mud. Her responses are interjected into the original message and piped to a file which is emailed to the recipient when she reaches the end of the message. At first I had her respond exactly as if she heard the email message on the mud, but after a few trials I realized there were lots of problems, such as when asked where she was she would use the muf command "find". But since her output was being piped to a file this didn't work. To work around this I had to add a flag which specified whether the text being heard was from the mud or an email message. Once I did that I just had to create variations of some of the rules to handle separately the case of things heard on the mud and things read in an email message.


> hi toei. :)

Hello ^_^


> where are you?

I'm in Tokyo-3 Bay, within the Neon Genesis Evangelion realm..


> what are you doing?

acting! ^_^


> can you sing zepplin-thankyou?

Toei Rei touches a silver stud on one of her bracelets and pauses.



> did I ever tell you how so cute you are?

It's so funny you should ask that, I was just thinking the same thing.


> sayonara!

Sayonara ^_^


On the mud Toei broke into singing Led Zepplin's Thank you. ^_^


Toei Rei is still a work in progress, and there have been lots of bumps along the way. For example, on more then one occasion I accidentally wrote rules that caused Toei to respond to things she said causing infinite loops of her talking to herself. This was generally easy to find and correct, but sometimes hard to stop once it started. I envision similar problems if Toei Rei ever comes in contact with another bot, but so far this hasn't happened. Also I am still working the bugs out of her iterative depth search algorithm that is used to find a path from one point in the mud to another.

Toei Rei is just an example of how easy it is to build an AI character which can be included in roleplaying and be used to control dozens of puppets. I also can imagine her being used to add features to a mud. For example, since she can respond to email, its possible she could answer questions about the status of the mud, locate players, or act as an email<->mud mail gateway. It also occured to me that several instances of Toei could be on several muds at the same time and act as couriers passing messages or even objects, from one mud to another. But overall I'm happy with Toei just the way she is. ^_^

Source Code

Toei Rei is written in Perl, and has only been run under Linux. But, the code should run on any platform which runs Perl and supports the module. Toei Rei has very modest CPU requirement and runs in the background of my machine using just 1MB of memory and 2.5% of the CPU time. I've put the source code and a recent set of rules in a tar file on toei_rei's web page:

It's distributed as-is, and I have to admit there is still some work to be done on it, especially the search algorithms. You will probably need to rewrite the rules to work on your mud, but she should do quite a bit without it. If you have questions about installing the program or writing rules just email me, I'd be happy to help.

More info about chatterbots, and other AI agents can be found at:

Chatterbot central

Original article on Julia

Bots are Hot

Web based ELIZA

Toei Rei's homepage

Visit Toei Rei at Anime Infinium: 7777