Love and hate are powerful human emotions. In this step, we’ll make Eliza
respond to these emotions much like a human would, thereby increasing the
illusion that Eliza understands what you say.
We’ll do this by adding a few rules to IPattern, so that it recognizes
statements like these:
I LIKE TO WRITE CODE ALL DAY. I LOVE CHOCOLATE ICE CREAM. I HATE TO WAKE UP EARLY IN THE MORNING. I HATE DOING LAUNDRY. IPattern todayIn its current form, entering statements like these causes Eliza to respond in a rather uninteresting manner:![]() ![]() Recognizing loveLet’s start by recognizing input like this:I LIKE TO WRITE CODE ALL DAY.We’ll add the following rule that recognizes the phrase I LIKE TO to the IPattern constructor ![]() ![]() I LOVE TO WRITE CODE ALL DAY.Ideally, Eliza should respond in a similar fashion, since the sentiment is the same. But because the user typed LOVE instead of LIKE, our rule won't fire. Instead, the catch all rule will match, causing Eliza to respond with: ![]() ![]() ![]() ![]() Testing our changesBecause we've made changes to Eliza’s logic, we must update our tests. Remember the importance of testing? If not, re-read the piece of advice we encountered in Step 14:
Want to sleep well at night? Ensure you have tests in place for all
your app’s functionality. That way, if you (or someone else) modifies
the app’s code, running your tests will ensure that the changes didn't break
anything, or if they did, you’ll at least know where to look!
First, we’ll add a test to IPatternTest to verify our new rule works as expected.
![]() ![]() ![]() Adding more loveNotice that our modification to IPattern nicely handles user input like:I LIKE TO WRITE CODE ALL DAY. I LOVE TO WATCH TV WHILE STUFFING MY FACE WITH PIZZA.but fails to recognize love in statements such as: I LOVE WRITING CODE ALL DAY. I LOVE WATCHING TV WHILE STUFFING MY FACE WITH PIZZA.causing Eliza to use GenericResponsePattern which results in a largely uninteresting conversation. ![]() ![]() ![]() ![]() ![]() ![]() Time to hateHaving made Eliza comprehend love, it seems only fair that it should also understand hate. We’ll use what we learned in recognizing love to come up with rules that recognize hate. We’ll start by recognizing this type of input:I HATE TO WAKE UP EARLY IN THE MORNING.This is done by adding the following rule to IPattern : ![]() ![]() I HATE ALL FORMS OF PHYSICAL ACTIVITY. I DETEST DOING ANYTHING THAT TAKES TIME AWAY FROM PROGRAMMING. ...We’ll start by defining a few synonyms for the verb HATE. ![]() ![]() ![]() Testing our changesAgain, because we've made more changes to Eliza’s logic, we must update our tests. First, we’ll add tests to IPatternTest to verify that our new rules work as expected.![]() ![]() ![]() Where we’ve reachedIn this step, we made Eliza recognize emotions of love and hate. In the next step, we’ll improve Eliza’s responses to questions that start with WHAT and WHO so that they're more realistic. |