Skip to main content

Build Your Own ELIZA

This challenge is to build your own version of the original AI chatbot - ELIZA!

ELIZA is an early natural language processing computer program developed by Joseph Weizenbaum at MIT between 1964 to 1967. Eliza was created to explore communication between humans and machines. To do that ELIZA simulated conversations by using pattern matching and substitution methodologies that created an illusion of understanding on the part of ELIZA.

Weizenbaum was surprised to find that some people attributed human-like feelings to the ELIZA a phenomenon that came to be called the ELIZA effect. I can understand why people did this, when I first came across ELIZA as a child in the 1980s I was blown away at how human it seemed, particularly compared to the computers I was used to dealing with.

Given how many early users were convinced of ELIZA's intelligence and understanding, despite Weizenbaum's insistence to the contrary it’s no surprise people confuse the far more capable and advanced modern LLM based systems for intelligence.

Arthur C. Clark was right when he said:

Any sufficiently advanced technology is indistinguishable from magic.

The Challenge - Building ELIZA

ELIZA was the worlds first chatbot. Some people considered it part of the early AI work as it is doing natural language processing (NLP). As we’ll see there is no intelligence, simply an algorithm designed to mimic it using some relatively simple rules.

If you’ve never used ELIZA before you can give it a go on the ELIZA Archaeology site:

eliza.png

If you really want to dig into it check out Weizenbaum's 1966 CACM paper.

Step Zero

Coding Challenges is zero-based, just like all good programming languages. As we’re building the first ever chatbot, zero seems like a great place to start!

Please set up your IDE / editor and programming language of choice and proceed directly to Step 1 once you’re ready.

Step 1

In this step your goal is to display a welcome banner and prompt the user for input. For now just print the input back out so you can test your program.

You should allow the user to quit if they type a response that would end the conversation, for example: quit, goodbye, or bye.

Step 2

In this step your goal is to match the input to a response pattern and print the response. For example if the user enters a pattern that matches the regex: /^hello(.*)/gi then respond with the text: “Hello, how are you feeling today?”.

I suggest you don’t hard code the rules, but create a suitable data structure that stores a list of patterns and the output. Be prepared to iterate on the structure in Step 3 and 4.

Step 3

In this step your goal is to load the rules from a file. The DOCTOR script Weizenbaum created so ELIZA could mimic a Rogerian psychotherapist can be found on the ELIZA Archaeology blog post about the DOCTOR script., or a review of the script here.

As an example consider:

(WHY
((0 WHY DON'T I 0) ; Pattern 1
(DO YOU BELIEVE I DON'T 5) ; Response 1
(PERHAPS I WILL 5 IN GOOD TIME) ; Response 2
(SHOULD YOU 5 YOURSELF) ; Response 3
(YOU WANT ME TO 5) ; Response 4
(=WHAT))) ; Fallback rule

This rule applies when the user input contains the keyword "WHY". ELIZA tries to match the input against each pattern in set of rules.

The original rules were defined in a Lisp-like structure and they can be understood as follows.

Pattern: (0 WHY DON'T I 0)

This pattern matches any input that contains "WHY DON'T I" surrounded by any text (the 0 tokens represent wildcards).

For example, the input "Why don't I go outside?" would match this pattern.

Responses:

Each corresponding response contains 5 — a placeholder for the text that followed "WHY DON'T I" in the user's input. So if the user said:

"Why don't I try harder?", then 5 would be "try harder".

ELIZA could respond with one of:

  1. "Do you believe I don't try harder?"
  2. "Perhaps I will try harder in good time."
  3. "Should you try harder yourself?"
  4. "You want me to try harder?"

These simulate plausible therapist-style responses by mirroring the user's concerns or turning the question back on them.

Fallback: =WHAT

If no pattern matches, ELIZA falls back to the rules defined under the WHAT keyword. That is, it delegates to the WHAT rule set.

Step 4

In this step your goal is to implement more of the logic of ELIZA. The first step of which was to identify keywords in the input, keywords are words matched in the rules. The matches are ordered based on priority and the transformation with the highest priority returned. Priority is defined by the order of the rules in the input file, from highest to lowest.

The transformation rule should have two parts: the decomposition rule and the reassembly rule. Using the keywords and other nearby words from the input, different disassembly rules are tested until an appropriate pattern is found. Using the script's rules, the sentence is then broken up and arranged into sections of the component parts based on the decomposition rule selected.

There is precedence to each rule and the highest precedence rule is selected. The reassembly rule is then used to reassemble the parts into the response. The process is described on the Wikipedia page for ELIZA.

Step 5

In this step your goal is to implement handling of the edge cases, such as no keyword that hatches a rule, in which case a response that doesn’t require context, such as: “I see”.

Going Further

To take this further why not develop your own scripts and make it even more comprehensive?

If you haven’t already done so as part of your build, perhaps you could put it online and share it with your friends. See if any of them feel it’s human-like.

Help Others by Sharing Your Solutions!

If you think your solution is an example other developers can learn from please share it, put it on GitHub, GitLab or elsewhere. Then let me know - ping me a message on the Discord Server, via Twitter or LinkedIn or just post about it there and tag me. Alternately please add a link to it in the Coding Challenges Shared Solutions Github repo.

Get The Challenges By Email

If you would like to receive the coding challenges by email, you can subscribe to the weekly newsletter on SubStack here: