4 Reasoning with Eliza

So far, most of the programs we have considered are relatively straightforward in terms of their program flow, following a simple sequential structure that we can quite easily visualise using a flow chart.

However, we have also seen how it is possible to get quite complex emergent behaviours from a robot control program when the robot is placed in an environment and it starts to interact with that environment.

In this notebook, you will explore how another approach to programming in the form of a simple rule-based system can produce its own rich behaviours when combined with inputs that are in part provoked by the behaviour of the program itself.

Whilst the rule-based system has a sequential control flow regulating its own behaviour, the ‘program’ it implements has an emergent feel to it as different independent rules respond to particular inputs and create outputs that other rules can then use as inputs.

In many respects, the interacting rules set up a reasoning system that allow the program not only to act on pieces of information it is provided with, but also to create further pieces of information and add those to its reasoning process. In such a way, we can start to develop a sense–think–act system where the actions are determined by a potentially simple reasoning system but with its own complex emergent behaviour.

The detailed mechanics of how a rule-based system works is beyond the scope of this module, but we can still see what sort of effect it can have when we interact with a re-implementation of a classic rule-based system: Eliza.

4.1 Eliza

Although written fifty or so years ago, Joseph Weizenbaum’s Eliza program is often referred to as one of the first great milestones in computational natural-language interaction. This copy of the original paper includes examples of the code used to program the original Eliza engine.

Eliza has been re-implemented several times, such as in the Lisp programming language by Peter Norvig for his textbook Paradigms of Artificial Intelligence Programming, as well as a Python re-implementation of Norvig’s code by Daniel Connelly (Paip-python: Peter Norvig’s Paradigms of AI Programming implemented in Python).

A version of Connelly’s code, updated to run in the version of Python used in these notebooks, is contained in the file eliza.py.

4.2.1 Activity – Chatting to ‘Eliza’

Relive the past, perhaps in more ways than one – depending on how your conversation goes! – by chatting to Eliza for two or three minutes.

Run the following code cell to import the eliza package:

import eliza

Run the following code cell to enter Eliza’s treatment room. Start your conversation with a Hello; end the conversation by starting your response with Goodbye or force an exit to the program by clicking the stop button in the notebook toolbar.

eliza.hello_doctor(aloud=True)

If you want to hear Eliza speak the responses aloud to you, then start the program by passing in the parameter aloud=True in the following way: eliza.hello_doctor(aloud=True).

4.2.2 What makes Eliza tick?

If you look at the rules file, you will see that it contains a series of rules that have the form:

CONDITION: [
    POSSIBLE_RESPONSE_1,
    POSSIBLE_RESPONSE_2,
    ...
    ]

or more completely:

"?*x KEYPHRASE ?*y": [
        "RESPONSE_1 ?y?",
        "RESPONSE_2 ?y?",
        ...
        ]

For example:

    "?*x I want ?*y": [
        "What would it mean if you got ?y?",
        "Why do you want ?y?",
        "Suppose you got ?y soon."
        ]

The ?*x and ?*y elements in the condition part of the rule are pattern-matching operators that capture arbitrary text before and after the provided KEYPHRASE. A rule matches a provided input if the KEYPHRASE is contained in the text given to Eliza. The pattern-matched content in the text can then be extracted from the input and used in the output response given by Eliza.

A rule-matching engine, written in Python, takes the user input, tries to match it with one of the rules and then generates a response. If you are interested in the details, Connelly provides a commentary that explains how his version of the Eliza program works.

4.2.3 Optional activity – Creating your own Eliza rules

If you make a copy of the eliza.json file, for example, as dr_me.json and edit it to contain your own rules, then you can run Eliza using your ruleset by running the command: eliza.hello_doctor('dr_me.json').

You can also provide a set of custom default responses that Eliza will select between if no rules match by passing them into the hello_doctor() function via the default= parameter. For example:

eliza.hello_doctor('doolittle.json',
                   default = ["Very interesting",
                              "I am not sure I understand you fully"]
                  )

If you come up with an interesting script, then share it in your Cluster group forum.

4.3 Summary

In this notebook, you have seen how a rule-based agent called Eliza, originally created over fifty years ago, can still hold a conversation (of sorts!) today. By providing different scripts containing only a few dozen rules, quite wide-ranging conversations are possible if the human conversant adds the detail.

In the next notebook, you will be introduced to a far more powerful framework for developing far more elaborate rule-based systems: the Durable Rules Engine.