Reputation: 37
Currently with my program, if the user inputs "cat" then the chatbot would spit back out "Tell me more about your pets." as expected. However this is also true if the letters cat are part of another word. So a word like catch would be picked up as catch and the chatbot would return "Tell me more about your pets." How else would I go about to polish up the keyword detection?
if (statement.indexOf("cat") >=0) { response = "Tell me more about your pets.";
Upvotes: 0
Views: 338
Reputation: 1723
You can use regex:
Pattern pattern = Pattern.compile("\\bcat\\b");
Matcher matcher = pattern.matcher("Hello the cat is cool");
boolean cat;
if(matcher.find()) {
cat = true;
} else {
cat = false;
}
Using \\b
in the pattern, ensures that only full words are matched.
Upvotes: 2
Reputation: 115
A couple of ways to improve keyword detection:
Upvotes: 3