Lab4: Chatbot II

You have completed the first version of Chatbot, congratulations! Now it is time to make it more intelligent.

Step0:

Make sure your Chatbot I is working, before you start to work on Chatbot II. Below is an example code which you can reference:

https://www.onlinegdb.com/KxOtzVXR9(From YUAN Zihao)

Step1:

In Chatbot I, we used indexOf() to find keywords in the statement. However, the statement has to contain the exact keyword in order to detect it.

For example, if our keyword is "mother", the second line below is going to return false, since "mother" and "Mother" are treated as different things in Java, even though they mean the same.

if (statement.indexOf("mother") >= 0) -> return true
if (statement.indexOf("Mother") >= 0) -> return false

One way to solve this problem is to convert the statement to all lowercase. For example, you want to make the statement "How I Met Your Mother" to "how i met your mother" first. Since every character is lowercase now, you don't have to check "Mother" or "MOTHER".

There is a method called toLowerCase() in Java String Class, which you will use in the step. You can go to https://www.w3schools.com/java/ref_string_tolowercase.asp to learn its definition and usage. Try the example code in the tutorial.

Modify your code to convert all statements to lowercase before you start to detect any keywords. After you are done, test it and make sure the result is as expected(and nothing breaks).

Step2:

What happens when a keyword is included in another word?

For example, the word “cat” is in the string “Let’s play catch!,” but the string has nothing to do with the animal.

In this activity, you will use a method public boolean findKeyword(String statement, String goal)that searches for a full word in the string. It will check the substring before and after the string to ensure that the keyword is actually found.

findKeyword("Let's play catch", "cat") -> false

findKeyword("I like cat", "cat") -> true

findKeyword("I hate black") -> true(asssume "black" is the keyword)

findKeyword("I hate blackpink") -> false

Below is part of the method findKeyword(), based on which you will continue to work on. This method will only find exact matches of the keyword, instead of cases where the keyword is embedded in a longer word.

Don't rush to write code. Read the code, come up with a solution, and start to write code.

private boolean findKeyword(String statement, String goal)
{
       String phrase = statement.trim();
       int psn = phrase.toLowerCase().indexOf(goal.toLowerCase());

       // Refinement--make sure the goal isn't part of a word
       while (psn >= 0)
       {
          // Find the string of length 1 before and after
              // the word
              String before = " ", after = " ";
              if (psn > 0)
              {
                 before = phrase.substring(psn - 1, psn).toLowerCase();
              }
              if (psn + goal.length() < phrase.length())
              {
                 after = phrase.substring(
                                  psn + goal.length(),
                                      psn + goal.length() + 1)
                                      .toLowerCase();
              }
              //add your code here....

       }
       return false;
}

You may want to use compareTo() method.

After you are done, test findKeyword() separately in main() before you add it in getResponse(). Once you are confident it works, you can call it in getResponse() and test again. Make sure nothing breaks!

Step3:

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.

Modify your code have it respond to “I want something” statements with “Would you really be happy if you had something?” In doing this, you need to be careful about where you place the check. Be sure you understand why.

For example:

  • Statement: I want fried chicken.

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

  • Statement: I want water.

  • Response: Would you really be happy if you had water?

You want to write a method to implement this functionality, so that you can test it separately. Don't put everything into getResponse(), it will make your code difficult to debug.

Step4:

Have it respond to statements of the form “I something you” with the restructuring “Why do you something me?”

For example:

  • Statement: I like you.

  • Response: Why do you like me?

  • Statement: I envy you.

  • Response: Why do you envy me?

Submit

If you would like to add additional features to your chatbot, feel free to do so! Be sure to save everything before you submit or close your browser! You will work on an updated version of chatbot in the next lab. Submit all your work on 钉钉作业本 by Oct 31th, 8AM.

Last updated