ML - Responses that Transform Statements (Lesson)

APCompSci_LessonTopBanner.png

Responses that Transform Statements

Introduction

Single keywords are interesting, but better chatbots look for groups of words. Statements like “I like cats”, “I like math class,” and “I like Spain” all have the form of “I like something.” The response could be “What do you like about something?” This lesson will respond to groupings of words.

To improve our chatbot we would like it to respond to phrases as well as single words. For example, saying, “I want to ________.” We’d like for it to respond, “What would it mean to _________.”

Add the following method to your Magpie class code:

/**
* Take a statement with "I want to <something>." and transform it into
* "What would it mean to <something>?"
* @param statement the user statement, assumed to contain "I want to"
* @return the transformed statement
*/
private String transformIWantToStatement (String statement)
{
// Remove the final period, if there is one
statement = statement.trim();
String lastChar = statement.substring(statement.length() -1);
if (lastChar.equals(".")) {
statement = statement.substring(0, statement.length() -1);}
int psn = findKeyword (statement.substring(psn + 9).trim();
return "What would it mean to " + restOfStatement + "?";
} We need to also add the following call to the getResponse method prior to the call to the getRandomResponse call.

// Responses which require transformations
else if (findKeyword (statement, "I want to", 0) >= 0)
{
response = transformIWantToStatement(statement);
} 

Try out this code by typing an “I want to” statement.

Now add a method that looks for a relationship between you and me.

/**
* Take a statement with "you <something> me" and transform it into
* "What makes you think that I <something> you?"
* @param statement the user statement, assumed to contain "you" followed by "me"
* @return the transformed statement
*/ Also add this call to getResponse prior to getRandomResponse:

else
{
// Look for a two word (you <something> me)
// pattern
int psn = findKeyword (statement, "you", 0);

if (psn >= 0
&& findKeyword(statement, "me", psn) >=0)
{
response = transformYouMeStatement(statement);
} 

Compile the code and try out this code by typing in a statement that includes “you love to go to the movies with me.”

The response should be: “What makes you think I like to go to the movies with you?”

This lab is from AP Central College Board Links to an external site..

Adding More Complexity to Magpie

Write a method that will have it respond to statements in the from “I ________ you” with the response “Why do you ________ me?”

For example:

Statement: I like you.

Response: Why do you like me?

These changes will be part of the final Magpie.java file that you will submit at the end of this lab.

APCompSci_LessonBottomBanner.pngIMAGES CREATED BY GAVS