Eliza 11 - Adding more complex patterns

by Ravi Bhavnani


In this step, we'll add the remaining complex patterns that resemble RememberPattern. That is, patterns whose decomposition rules look like this:

    <optional text before key phrase>  <key phrase>  <text after key phrase>

If you refer to Eliza's script , you'll see these patterns are:

    IF, DREAMED, AM, ARE, YOUR, WAS, WERE, CAN, WHY

But there's a bit more happening. See the pre section in the script? These are pre-transforms, or transforms applied to the user's input before pattern matching is performed. This allows multiple keywords to be handled by the same pattern.

For example:

    pre: how what
    pre: when what
causes the presence of HOW and WHEN in the user's input to both be handled by the pattern that handles WHAT.

I don't agree with Eliza's script that says WERE should be handled by the WAS pattern. For this reason, I've omitted it from the list of pre-transforms and have moved WERE's decomp/reassembly rule from WasPattern to its own WerePattern.

Performing pre-transforms

Let's extend StringTransformer to do a pre-transform.

GO AHEAD AND WRITE StringTransformer.PreTransform(). WHEN YOU'RE DONE, COMPARE YOUR CODE WITH THIS.

Now call StringTransformer.PreTransform() in Eliza.FindMatchingPatternForInput() before finding the highest ranked pattern that matches the user's input.

Simplifying existing patterns

Because we're transforming the user's input prior to finding a matching pattern, we can simplify some of our patterns to no longer consider multiple phrases. Specifically:

  • RememberPattern only needs to handle "REMEMBER"
  • ComputerPattern only needs to handle "COMPUTER"
  • DreamPattern only needs to handle "DREAM"
  • PerhapsPattern only needs to handle "PERHAPS"

GO AHEAD AND MAKE THESE CHANGES.

Adding the complex patterns

Now add the complex patterns we identified above. When you're done, your project should look like this:

Testing your changes

Let's run Eliza to ensure our changes haven't broken anything. Give Eliza the following input:

    WHY CAN'T I BE RICH AND FAMOUS?

If all goes well, this should match the WHY pattern, triggering its "* why can't I *" decomposition rule, producing:

    DO YOU THINK YOU SHOULD BE ABLE TO BE RICH AND FAMOUS?

Instead, this happens:

WHY DID THIS HAPPEN?  HOW WILL YOU FIX IT?

TO FIND THE PROBLEM, STEP THROUGH YOUR CODE IN THE DEBUGGER.  IF YOU NEED HELP, LOOK AT THIS STEP'S SOLUTION.  SPECIFICALLY, UNCOMMENT THE Debug.WriteLine() CALLS IN Eliza.FindMatchingPatternForInput().

Exhaustive testing

After fixing the problem, proceed to test each complex pattern and verify that it cycles through each expected response.

 

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