Eliza 3 - Getting Eliza to talk - the first Pattern

by Ravi Bhavnani


In this version of Eliza, we'll implement an actual pattern. We'll start by selecting an easy one - the "BECAUSE" pattern - that works like this:

  • The "BECAUSE" pattern checks if the user's input contains the phrase "BECAUSE".

  • If it does, it responds with one of these responses:

    • "Is that the real reason?"
    • "Don't any other reasons come to mind?"
    • "Does that reason seem to explain anything else?"
    • "What other reasons might there be?"

Eliza will use use our pattern by constructing an instance of it, and calling the instance's MatchesInput() and GenerateResponse() methods. Something like this:

    string input = <something the user said>
    Pattern p = new Pattern("BECAUSE");
    if (p.MatchesInput(input)) {
        Console.Writeline (p.GenerateResponse());
    }

Our job is to write the code for:

    MatchesInput()
    GenerateResponse()

We'll get to this in a minute. First, let's simplify Eliza a bit.

Simplifying Eliza

Because we've introduced the Pattern class, we can simply the Eliza's logic a bit.

  1. Eliza.GetResponse() should define the "pattern" variable as being of type Pattern, not object.

  2. Because a Pattern is capable of generating a response (by calling its GenerateResponse() method), we no longer need a separate Eliza method GenerateResponseForPattern(). Instead, we'll just call the matching pattern's GenerateResponse() method.

MAKE THESE CHANGES IN Eliza.cs.

Implementing the BECAUSE pattern

Our job is to write the code for these methods in the Pattern class:

    MatchesInput()
    GenerateResponse()

GO AHEAD AND DO THIS.

Testing

Testing is a crucial part of software development. Before you can say you're done, you should test your code with different inputs to verify all parts of it work correctly.

Run the driver program and verify that it behaves as expected, by giving it this test input and comparing the results with your app.

    WELCOME!  PLEASE TELL ME YOUR PROBLEM.
    > I love pizza.

    PLEASE GO ON.
    > I also love burgers.

    PLEASE GO ON.
    > I think I'm fat because I eat too much.

    WHAT OTHER REASONS MIGHT THERE BE?
    > There's really not much more to say.

    PLEASE GO ON.
    > Nah - I'm done talking with you.

    PLEASE GO ON.
    > Bye.

    GOODBYE!  IT WAS NICE TALKING WITH YOU.

IS THERE ANYTHING YOU'D LIKE TO IMPROVE IN YOUR CODE?
COMPARE YOUR Pattern.cs and Eliza.cs WITH THE FILES IN THE SOLUTION.
 

Most of the content at this site is copyright © Ravi Bhavnani.
Questions or comments?  Send mail to ravib@ravib.com