3.12.4. Activity 4: Responses that Transform Statements

If your class has time, your teacher may have you do Activity 4 below.

As stated previously, 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 “I like something”. The response could be “What do you like about something?” This activity will respond to groupings of words.

Try each of the following as the value for the statement in the main method and see what they print. Or you can try it on repl.it version 4.

You can also step through the code in the Java visualizer. It may take a minute or two to load. Click the forward button at the bottom of the code to execute the next statement.

3.12.4.1. Exercises:

In this activity, the chatbot is altered to look not only for keywords, but also specific phrases. Magpie4.java adds two new methods, transformIWantToStatement and transformYouMeStatement and getResponse has been modified to add tests to find “I want to something” statements and “You something me” statements.

Look at the code. See how it handles “I want to” and you/me statements.

Then add two new methods, transformIWantStatement and transformIYouStatement, and calls to each as described below. Alter the code either above in the active code window or on repl.it version 4 or in an IDE of your choice:

  • In a method transformIWantStatement, have it respond to “I want something” statements with “Would you really be happy if you had something?” You can use the already written transformIWantToStatement method as a guide. In doing this, you need to be careful about where you place the call to the method so it calls the right one. Test with the following:

    • Statement: I want fried chicken.

    • Response: Would you really be happy if you had fried chicken?

  • In a method transformIYouStatement, have it respond to statements of the form “I something you” with the restructuring “Why do you something me?”. You can use the transformYouMeStatement method as a guide. Test with the following:

    • Statement: I like you.

    • Response: Why do you like me?

Find an example of when this structure does not work well. How can you improve it?

You have attempted of activities on this page
Next Section - 3.12.5. Mixed Up Code Practice