Eliza 7 - Adding all format free patterns

by Ravi Bhavnani


If you take a closer look at Weizenbaum's paper , you'll notice there are more patterns like BECAUSE, YES and NO that generate responses that are independent of the format of the user's input. We'll call these format free patterns. The complete list of format-free patterns is:

    ALIKE, ALWAYS, BECAUSE, COMPUTER, DREAM, FOREIGN, HELLO, NAME, NO, PERHAPS, SORRY, WHAT, YES

Write classes for these patterns by modeling them on BecausePattern.  You'll need to examine the Eliza script in Weizenbaum's paper to determine each pattern's responses.

When you've finished, your project should look like this:

Matching multiple phrases

If you look closely at Eliza's script , you'll notice some patterns match more than one phrase. For example, the DREAM pattern also works if the user's input contains DREAMS. And the COMPUTER pattern works for COMPUTER, COMPUTERS, MACHINE and MACHINES.

Modify the Pattern class so it recognizes multiple phrases.

Pattern rank

Weizenbaum's Eliza assigns a rank (i.e. importance level) to each pattern. The higher the rank, the more significant the pattern. By default, a pattern has a rank of zero. If you look carefully at the Eliza script , you'll notice the DREAM pattern has a rank of 3, while the COMPUTER pattern is ranked 50. Weizenbaum gave great importance to this pattern because in the 1960s, most people were unfamiliar with (and likely fearful of) computers.

While one might argue that the COMPUTER pattern doesn't deserve a rank of 50 today, let's at least support the notion of ranking patterns, so that our Eliza is able to identify some patterns as being more important than others.

Modify the Pattern class so that it stores a rank.

Using the pattern rank

Before we introduced the concept of ranking patterns, Eliza responded by having the first matching pattern generate its response.

    /// <summary>
    /// Finds the matching pattern for the user's input.
    /// </summary>
    /// <param name="input">The input.</param>
    /// <returns>The pattern or <see langword="null"/> if none was found.</returns>
    private Pattern FindMatchingPatternForInput
        (string input)
    {
        // Check if the input matches a pattern.  If so, return that pattern.
        foreach(Pattern p in this._patterns) {
            if (p.MatchesInput(input)) {
                return p;
            }
        }

        // No pattern was matched, so return null
        return null;
    }

Modify Eliza.FindMatchingPatternForInput() so that it picks the matching pattern with the highest rank instead of just the first matching pattern.

Testing your changes

Test your changes by running the following conversation. Do your changes produce the same result?

NOW TAKE A LOOK AT THIS STEP'S SOLUTION AND COMPARE IT WITH YOUR CODE.
 

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