back to AI - Chatbots
Activity 2: Better Keyword Detection - (using - Find )
In the previous activity, you discovered that simply searching for collections of letters in a string does not always work as intended.
The statement: "Do you know my brother" resulted in the response: "Why so negative?". That was because we were looking for the letters "no" which it found as part of the word "know". We should have looked for the word "no" and not just the letters "no".
Another example, the word “cat” is in the string “Let’s play catch.” but the string has nothing to do with the animal. If the user typed "Let's play catch", your chatbot would return with "Tell me more about your pet". That is not right.
Another example, is if you typed "It is snowing" it would say "Why so negative?" because it saw the "no" in "snow"
In this activity, you will create a method that searches for a full word in the string. It will check for the substring being a separate word.
1. Looking for whole words
The old code looked like this:
if "no" is in field "input" then
put "Why so negative?" into theResponse
else
But it created problems because the letters "no" also occur in other words like "know" and "snow". So if the user said "It looks like snow", our chatbot would reply with "Why so negative?"
Another example, the word “cat” is in the string “Let’s play catch!,” but the string has nothing to do with the animal.
In that code, we used "contains" or "is in".
To find whole words, we need to use the "find" function of LiveCode with the keyword - "word".
e.g.
find word "no" in the field "input"
- After using it, we have to check the built-in "result" variable to see if the word was found or not.
- If the word is found, the "result" is empty.
- If the word is not found, the "result" will have "not found" in it.
if the result is empty then // the word "no" was found
put "Why so negative?" into theResponse
We can try this code:
find word "no" in field "input" // look for the whole word
if the result is empty then // the word "no" was found
put "Why so negative?" into theResponse
else ...
But...
This is good for one time use. If we want to use it for other words, we have to repeat that code over and over. So to use it with other words, we will create our own function that uses it. Our function will take a word that is passed to it and return a true or false, depending on whether the word was found or not.
If we do the same for other words, we have
find word "no" in field "input" // look for the whole word
if the result is empty then // the word "no" was found
put "Why so negative?" into theResponse
else
find word "Roosevelt" in field "input"
if the result is empty then // the word "Roosevelt" was found
put "I went there too" into theResponse
else
find word "bird" in field "input"
if the result is empty then // the word "bird" was found
put "I like birds" into theResponse
else
find word "yes" in field "input" // look for the whole word
if the result is empty then // the word "yes" was found
put "I agree" into theResponse
else ...
This is getting tedious. We want to make it simpler. If we want to use it for other words, we have to repeat that code over and over. So to use it with other words, we will create our own function that uses it. Our function will take a word that is passed to it and return a true or false, depending on whether the word was found or not.
2. Making it into a function - to use with other words We need to make it into a function and use it "over and over"
We can put it into a function and use it with any word that is passed to it:
function findKeyword x
find word x in field "input" // look for the whole word
if the result is "Not found" then
return false // false: it was not found
else
return true // true: the word was found
end if
end findKeyword
We call it this way:
if findKeyword ("no") then // see if it was found
put "Why so negative?" into theResponse // true - give its response
else ... // go on to look for another word
and use it over and over for many words:
if findKeyword ("no") then
put "Why so negative?" into theResponse
else if findKeyword ("Roosevelt") then
put "I went to that school" into theResponse
else if findkeyword("bird") then
put "I like birds" into theResponse
else ...
By putting the "find" code in a function, we can call it over and over again with different words.
What words do we use it for?
We would use it for words or letters that can be found in different words.
e.g.
"no" is in "nothing", "know", "snow" and we do not want those words to return the response "Why so negative?" so we would use the findKeyword ("no")
if we were doing a sports chatbot and using the word "ball", it may not matter. Because you could respond "That is my favorite sport" to almost any word that has the word "ball" - basketball, baseball, football, etc. So we could just still check for
if "ball" is in theInput then...
Assignment:
A. Add the function FindKeyword to your Starter Chatbot_1
Put this code on your card script as it's own function
(add it after the line: "end getResponse" and before the line: "on getRandomResponse" )
function findKeyword x
find word x in field "input" // look for the whole word
if the result is "Not found" then
return false // false: it was not found
else
return true // true: the word was found
end if
end findKeyword
B. Add the code to call it for the word "no" and other words that might be inside of other words:
else if findKeyword ("no") then
put "Why so negative?" into theResponse
else if findKeyword ("yes") then
put "Are you sure about that?" into theResponse
else if findKeyword ("me") then
put "Why you?" into theResponse