This book is now obsolete Please use CSAwesome instead.
6.8. 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.
6.8.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?
Open the API for String in the Java documentation (http://docs.oracle.com/javase/7/docs/api/java/lang/String.html) 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.
lab-1c-2: What value is returned by indexOf
if the substring does not occur in the string?
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.
6.8.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 }
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | private int findKeyword(String statement, String goal,
int startPos)
{
String phrase = statement.trim();
// The only change to incorporate the startPos is in
// the line below
int psn = phrase.toLowerCase().indexOf(goal.toLowerCase(),
startPos);
// 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();
}
/* determine the values of psn, before, and after at this point */
// If before and after aren't letters, we've
// found the word
if (((before.compareTo("a") < 0) ||
(before.compareTo("z") > 0)) // before is not a letter
&& ((after.compareTo("a") < 0) ||
(after.compareTo("z") > 0)))
{
return psn;
}
// The last position didn't work, so let's find
// the next, if there is one.
psn = phrase.indexOf(goal.toLowerCase(),psn + 1);
}
return -1;
}
|
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.
Try replacing line 178 with each of the following
maggie.findKeyword("She's my sister", "sister", 0);
maggie.findKeyword("Brother Tom is helpful", "brother", 0);
maggie.findKeyword("I can't catch wild cats.", "cat", 0);
maggie.findKeyword("I know nothing about snow plows.", "no", 0);
Record each of the values in a table.
You can also step through the code here.
It may take a minute or two to load. Click the forward button to execute the next statement (the one with the red arrow).
6.8.3. Exercise: Use the new method¶
Repeat the changes you made to the program in Activity 2, using this new method to detect keywords.
6.8.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.