ML - Better Keyword Detection (Lesson)
Better Keyword Detection
Introduction
The previous lesson brought to light that with the current Magpie program, there are instances where it does not always work as intended. For example, a sentence containing the word “know” would have the same response as a sentence containing the word “no”, since “no” is contained in “know”. This gives a response that doesn’t make sense.
How can that be fixed?
Better Keyword Detection
Question:
How can we tell if when we find “no” in a sentence it is actually “no” and not just part of another word that contains the combination of letters “no”?
Answer:
Check for space in front and behind the word so that we can be sure that it isn’t part of a larger word.
We will be adding a method named findKeyword to detect keywords. This method will only find exact matches of the keyword, instead of cases where the keyword is embedded in a longer word.
Add the following two overloaded findKeyword methods to your Magpie code as follows. Be sure to type carefully. If your code does not compile, you have probably made a typing error. Contact your teacher if you need further assistance.
This lab is from AP Central Collegeboard Links to an external site..
Change the code for getResponse so that it utilizes the findKeyword method. For example, instead of:
You should type:
You will need to make these changes for all the if conditions that are currently using indexOf in getResponse.
Better Keyword Detection
Test your Magpie class several times to be sure the code works as intended. Compile and Run the MapgieRunner.java file and enter different responses. Check to make sure the responses are what you expect.
IMAGES CREATED BY GAVS