4.13.3. Activity 3: Better Keyword Detection¶
This activity introduces you to some new String methods including some that are not on the exam, but are useful. Your teacher will tell you whether your class is doing this activity or not.
4.13.3.1. More String Methods¶
Run the StringExplorer below. It currently has code to illustrate the use of the indexOf
and toLowerCase
methods. Do they do what you thought they would? The method indexOf
is on the exam and the method toLowerCase
is not. Why do you think you might want to change the string to all lowercase characters? Why doesn’t the value of sample
change?
Run the code below. Why do you think you might want to change the string to all lowercase characters? Why doesn’t the value of sample
change? Do string methods change the string? Try some other string methods.
Open the API for String in Java documentation| in another tab. Scroll down to the Method Summary section and find the
indexOf(String str)
method. Follow the link and read the description of the indexOf
method.
Copy the following lines to StringExplorer
in the ActiveCode above in the main
method above to see for yourself that indexOf
behaves as
specified:
int notFoundPsn = sample.indexOf("slow");
System.out.println("sample.indexOf(\"slow\") = " + notFoundPsn);
Read the description of indexOf(String str, int fromIndex)
. Add lines to
StringExplorer
that illustrate how this version of indexOf
differs from the one with
one parameter.
4.13.3.2. Better Keyword Detection¶
In activity 2, you discovered that simply searching for collections of letters in a string does not always work as intended. 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 trace a method 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.
Take a look at the findKeyword
method below. It has a while
loop in it which we haven’t seen before. A while
loop repeats the code in the block below it while a condition is true. A block is all the code inside of an open curly brace {
and a close curly brace }
.
1private int findKeyword(String statement, String goal,
2 int startPos)
3{
4 String phrase = statement.trim();
5 // The only change to incorporate the startPos is in
6 // the line below
7 int psn = phrase.toLowerCase().indexOf(goal.toLowerCase(),
8 startPos);
9
10 // Refinement--make sure the goal isn't part of a word
11 while (psn >= 0)
12 {
13 // Find the string of length 1 before and after
14 // the word
15 String before = " ", after = " ";
16 if (psn > 0)
17 {
18 before = phrase.substring(psn - 1, psn).toLowerCase();
19 }
20 if (psn + goal.length() < phrase.length())
21 {
22 after = phrase.substring(
23 psn + goal.length(),
24 psn + goal.length() + 1)
25 .toLowerCase();
26 }
27
28 /* determine the values of psn, before, and after at this point */
29
30 // If before and after aren't letters, we've
31 // found the word
32 if (((before.compareTo("a") < 0) ||
33 (before.compareTo("z") > 0)) // before is not a letter
34 && ((after.compareTo("a") < 0) ||
35 (after.compareTo("z") > 0)))
36 {
37 return psn;
38 }
39
40 // The last position didn't work, so let's find
41 // the next, if there is one.
42 psn = phrase.indexOf(goal.toLowerCase(),psn + 1);
43
44 }
45
46 return -1;
47}
Run the code below or this replit.com version 3 to see this new method findKeyword
in action. It is called from the getResponse
method to print out an appropriate response based on a keyword. For example, looking for the word "no"
to print out "Why so negative?"
, but it won’t match no inside of another word like "another"
.
1if (findKeyword(statement, "no") >= 0)
2{
3 response = "Why so negative?";
4}
You can also step through the code in the Java visualizer or using the CodeLens button below. It may take a minute or two to load. Click the forward button at the bottom of the code to execute the next statement.
Modify the code below to print the values of psn
, before
, and after
right after the comment on line 100 in the findKeyword
method below. Record each of the values in a table. The College Board student guide for the Magpie Chatbot Lab has a table on page 8 that can be printed. Use the CodeLens button to step through the code.
4.13.3.3. Exercise: Use the new method¶
Repeat the changes you made to the program in Activity 2, using this new method to detect keywords. You can use the active code window above, or the replit.com version 3 or your own IDE.
4.13.3.4. Questions: Prepare for the next activity¶
Single keywords are interesting, but better chatbots look for groups of words. Consider statements like “I like cats,” “I like math class,” and “I like Spain.” All of these have the form “I like something.” The response could be “What do you like about something?” The next activity will expand on these groups. You will get to add one of your own, so it’s a good idea to start paying close attention to common phrases in your own conversations.