A.I. - Activity 3 - Groups of Words - LiveCode - (using - WordOffset, Find Word )
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?” e.g. they say "I like apples" - we answer "Why do you like apples?"
they say "I like computers" - we answer "Why do you like computers?"
they say "I like cats" - we answer "Why do you like cats?"
This activity will respond to groupings
of words. To do this, we 1. Find where the keyword is ("like" , "want" , etc) - which offset (or word #) it is in the sentence 2. Add one to that offset - to go to the next word 3. Get that word to use in our response. For this we use another LiveCode function - wordoffset. It tells you where the word is, what word number it is
In the sentence "It is a beautiful day outside" wordoffset("beautiful") would return a 4 because "beautiful" is word 4 in the sentence. wordoffset("outside") would return a 6 because "beautiful" is word 4 in the sentence wordoffset("night") would return a 0 because that word is not in the sentence
We use it to get where the word is in the input. If the number is a 0, we go on to the next word (the next "else if"). If it is a number > 0, then we want to get the next word in the sentence for our response: put wordoffset("like",field "input") into theLoc // this gets the wordoffset (word #) if theLoc is not 0 then // 0 means that it was not found put theLoc+1 into x // point to the next word put word x of field "input" into nextWord // get the next word put "Why do you like " & nextWord & "?" into theResponse // build the response
else if findKeyword("no") then ...
or if it is in the variable - theInput
put wordoffset("like",theInput) into theLoc // this gets the wordoffset (word #) if theLoc is not 0 then // 0 means that it was not found put theLoc+1 into x // point to the next word put word x of theInput into nextWord // get the next word put "Why do you like " & nextWord & "?" into theResponse // build the response
else if findKeyword("no") then ...
so if the user types "I like cats" the response we give is "Why do you like cats?"
so if the user types "I like apples" the response we give is "Why do you like apples?" A. - Add the above code to your chatbot B. - Download the file Chatbot_3 here and use it
How does it respond to?:
- I want to build a robot.
- I want to understand French.
- Do you like me?
- You confuse me.
? Problem - sometimes, we need to get more than 1 word What if the user types "I like fried chicken" ? The response we give is "Why do you like fried?" Perhaps we should use more than 1 word. Would it make sense to use the rest of the sentence?
The code that does it is:
put wordoffset("like",theInput) into theLoc // this gets the wordoffset (word #) if theLoc is not 0 then // 0 means that it was not found put theLoc+1 into x // point to the next word put word x to 99 of theInput into nextWords // get the next word put "Why do you like " & nextWords & "?" into theResponse // build the response
else if findKeyword("no") then ...
Input: I like fried chicken // wordoffset = 2, (word #2 in the sentence) Response: Why do you like fried chicken? // get words 3 to 99 (word x to 99)
Input: I really really like to go swimming in the ocean // wordoffset = 4, get words 5 to 99 Response: Why do you like to go swimming in the ocean? Can you think of any sentences starting with "I like" that does not work with this?
Exercises
We could do the same with "want" and "hate" and other verbs
so if the user types "I hate cats" the response we give is "Why do you hate cats?" so if the user types "I want candy" the response we give is "Why do you want candy?"
Add code to handle other keywords...
Response: "Why do you want ...?"
Response: "Would you really be happy if you had ....
Response: Why do you hate ..."
Examples with other keywords...- I want to build a robot.
- I want to understand French.
- Do you like me?
- You confuse me.
Look at the code. See how it handles “I want to” and you/me statements.
Alter the code:
Have it respond to “I want something” statements with “Would you really be happy if you had
something?” In doing this, you need to be careful about where you place the check. Be sure you
understand why. For example:
Statement: I want chicken.
Response: Would you really be happy if you had chicken? or Response: Why do you want chicken?
Have it respond to statements of the form “I something you” with the restructuring “Why do you
something me?” For example:
Response: Why do you like me?
Find an example of when this structure does not work well. How can you improve it?
|
 Updating...
cyril.pruszko@pgcps.org, Feb 23, 2016, 7:11 PM
|