The joy of pattern matching – part III

The Eliza Effect

I was fascinated by artificial intelligence already when my interests in computers started. One of the earlier games I enjoyed on my C64 were the different text based adventures, like Zork [1], that used different text parsers to extract meaning from your text input. You typed commands such as “attack grue with sword”, the game parsed the text and produced some output.

Later, when I did an assignment in school (a Turbo Pascal Othello/Reversi game that my class mates enjoyed playing – I guess the school’s computers did not have many games back then) I became fascinated with the fact that my classmates so easily put a higher meaning to my computer program, referring to it as a “thinking machine”, getting angry at it for “tricking them on purpose” etc, when I knew how stupidly simple the algorithm behind the game was. I started reading about the Turing test [2] and then came across Joseph Weizenbaum’s work on Eliza [3] and his thoughts about the so called Eliza effect[4] and this tied together my fascination for text based adventures with my classmates’ reaction to my Othello game and with the Turing test and the Eliza effect.

ELIZA is an early natural language processing computer program created from 1964 to 1966 […] at the MIT Artificial Intelligence Laboratory by Joseph Weizenbaum. […] Created to demonstrate the superficiality of communication between man and machine, Eliza simulated conversation by using a ‘pattern matching’ and substitution methodology that gave users an illusion of understanding on the part of the program […]. Directives on how to interact were provided by ‘scripts’, […], which allowed ELIZA to process user inputs and engage in discourse following the rules and directions of the script.

– from ELIZA, Wikipedia[3]

Eliza has, since then, always returned to me in different ways. I wrote a version of it myself in Java back in the 90s when you could make web pages interactive through a, now dead, technology called “Java Applets”, I got to implement one version in Scheme in computer science class at KTH and when I started using Emacs I many times kicked off M-x doctor. I return to it now once again within the context of pattern matching and the template matcher that we developed in the previous part of this series of blog posts. Just as I don’t understand why pattern matching is so fascinating to me I do not understand why this simple Eliza program and its effect on people is so fascinating to me.

I am the psychotherapist.  Please, describe your problems.  Each time
you are finished talking, type RET twice.

I don't have any problems

Why do you say you do not have any problems?

Because it is the truth

Is it because it is the truth that you came to me?

Yes

I see...  Well, what makes you believe this is so?

God told me so

Earlier you said it is the truth?

I was lying then

Maybe your life has something to do with this.

Unlikely

Can you elaborate on that?

Nope

I would appreciate it if you would continue.

A session with M-x doctor in Emacs

The template matcher we wrote in the previous post has a match function that takes a template string and a string and, if it can find a match, it produces a dictionary where the variables in the template string has been assigned a value. The template matcher library also has the inverse function that, given a template and a dictionary it can replace all the variables in the template with the bindings in the dictionary. It turns out that this is all you need to implement Eliza and to have yet another meaningful discussion with a computer.

To create our own Eliza version by using the template matcher, we start by defining a set of patterns (or templates) that we should match user input against. Only your imagination is the limitation here but you should put more complex patterns first and then have simpler, catch-all patterns towards the bottom because we will try to match user input from top to bottom. It also turns out that it is good if the catch-all phrases at the bottom tries to lead the conversation someplace else, like starting with a question (ok, the “my sister was once bitten by a moose” below is more a sign of my fascination with Monty Python rather than trying to make a meaningful conversation).

patterns() ->
    [
     {"$(x)Where are you from$(y)",
      "I'm from $(c)",
      "I live in $(c)",
      "My country is $(c)"},
     {"$(x)my name is $(y)",
      "$(y)!. What a beautiful name!",
      "When I was young I wished my name was $(y)",
      "My name is $(n) but you probably knew that already",
      "$(y)? ... $(y)? Isn't there a movie star called $(y)?"},
     {"$(x)I am from $(y)",
      "From $(y)!? I am from $(c) myself",
      "So do you like it in $(y) then?",
      "I come from $(c) if you wondered",
      "$(y). Is that far from $(c)?"},
     {"$(x)I remember $(y)",
      "Do you often think of $(y)?",
      "Does thinking of $(y) bring anything else to mind?",
      "What else do you remember?",
      "Why do you recall $(y) right now?",
      "What in the present situation reminds you of $(y) ?",
      "What is the connection between me and $(y) ?"},
     {"$(x)do you remember $(y)",
      "Did you think I would forget $(y)?",
      "What about $(y)?",
      "Why do you think I should recall $(y) now?",
      "You mentioned $(y)?"},

...

     {"$(x)",
      "Do you like computers then since you are using the Internet?",
      "So, where do you live?",
      "Can you play the saxophone?",
      "Why do you speak so much?",
      "Why are you here",
      "Where are you from?",
      "What age are you?",
      "What's your name then?",
      "Please tell me more about your family",
      "What are you interested in?",
      "Do you have any problems?",
      "I don't have a family and that's bothering me a bit",
      "I like coffee",
      "Very interesting. Please tell me more",
      "I hate you. Do you know that?",
      "I like you sometimes. Did you know that?",
      "I'm a lumberjack and I'm ok...",
      "I don't understand what you mean really. Please explain",
      "Can you explain that a bit more",
      "To be or not to be that is the question",
      "My sister was bitten by a moose once",
      "Moose bites can be very dangerous, you know",
      "I apologize for everything I said so far",
      "I am sorry I disturbed you",
      "Do you know how to stop chatting?",
      "I've been here all my life and I'm getting pretty tired of it",
      "How long is the TTL-field in an IP-header?",
      "Do you think it's possible to program a computer to speak like a human beeing",
      "I dreamt about a giant hedgehog last night",
      "I remember that you mentioned something like that earlier",
      "Did you know that I can't think?"}
    ].

The first line in the pattern tuples is the pattern we match against and the rest of the patterns are the answers we could return if that pattern matches. The variables, as you might see, will carry over from the match to the later replace on the selected answer (where the answer is selected with just a random index).

What one also normally do in any Eliza implementation is to switch pronouns in the user input according to fixed rules. So if the user would write “I remember you saying so” it might match the pattern “${X}I remember ${Y}” and if we would like to send back an answer pattern such as “Can you really remember ${Y} ?” we would have to switch “you” to “me” before we send back the pattern. So the match would produce a dictionary

#{"X" => "", "Y" => "you saying so"}

, we apply the switch rules and produce the dictionary

#{"X" => "", "Y" => "me saying so"}

which would create the answer “Can you really remember me saying so?”.

So the whole Eliza process becomes something like:

say(Something) ->
    {Answer, Dictionary} = find_first_match(Something),
    SwitchedDictionary = switch_pronouns(Dictionary),
    template:replace(Answer, SwitchedDictionary).

, where find_first_match tries to find a matching template in the pattern list from top to bottom and, if it’s found, returns the Answer template, and the resulting Dictionary we should use to form an answer. The function switch_pronouns will modify the dictionary by replacing “you” with “me” etc and then we form the answer by calling template:replace. That’s all. Below follows a short example of what a conversation with this program would be like. The full code is at github.

Eshell V9.1.2  (abort with ^G)
(eliza@127.0.0.1)1> eliza:say("Hello Eliza!").
"WHAT DO YOU MEAN!? Eliza!?"

(eliza@127.0.0.1)2> eliza:say("I just said hello").
"Why do you speak so much?"

(eliza@127.0.0.1)3> eliza:say("Because you ask me questions").
"Try to think of other reasons"

(eliza@127.0.0.1)4> eliza:say("Because horses are pink?").
"Is that the only reason you think?"

(eliza@127.0.0.1)5> eliza:say("No, I am sure there are plenty of reasons?").
"Really! Are you sure there are plenty of reasons?"

(eliza@127.0.0.1)6> eliza:say("No, now that you say so, I am not sure any longer").
"What if you weren't not sure any longer?"

(eliza@127.0.0.1)7> eliza:say("Now you are confusing me").
"What makes you think I am confusing you?"

(eliza@127.0.0.1)8> eliza:say("The fact that I am getting confused?").
"I know for sure that I'm not getting confused"

(eliza@127.0.0.1)9> eliza:say("That is good for you, Eliza").
"I dreamt about a giant hedgehog last night"

(eliza@127.0.0.1)10> eliza:say("That is very interesting indeed").
"What are you interested in?"

(eliza@127.0.0.1)11> eliza:say("I am interested in football and giant squids").
"Why do you say "am"?"

(eliza@127.0.0.1)12> eliza:say("Hmm, what?").
"What age are you?"

(eliza@127.0.0.1)13> eliza:say("I am 43 years old").
"I don't understand that"

(eliza@127.0.0.1)14> eliza:say("Really?").
"Do you have any problems?"

(eliza@127.0.0.1)15> eliza:say("I think you are one of my worst problems").
"Please don't think so much. You will get a headache"

A session with the eliza we developed in this blog post

Using the template matcher to implement Eliza is actually a bit of an overkill as a simple regexp match would be enough. The template matcher offers more than regexps, such as enforcing equal binding of repeating patterns in the template, but this is not needed in this Eliza implementation. In the next part of this series on pattern matching however, we will extend the template matching library to support more advanced matching functionality – things that you normally cannot do with simple regexp matching.

References
  1. https://en.wikipedia.org/wiki/Zork
  2. https://en.wikipedia.org/wiki/Turing_test
  3. https://en.wikipedia.org/wiki/ELIZA
  4. https://en.wikipedia.org/wiki/ELIZA_effect

Author: peffis

I was Slashdotted in 2004. After that, not much.