1ack to AI - Chatbots A.I. - Activity 1 - Your Starter Chatbot - LiveCode - (using - Is In, Contains )In this activity, you will work with a simple implementation of a chatbot. You will see how it works with some keywords and add keywords of your own. Resources: LiveCode - Working with Text - look at "is in", "contains" and "find" 1. Start up the Sample Chatbot Download and run the Starter Chatbot LiveCode = Chatbot_1.livecode (download and open with LiveCode)
Get to know the Chatbot. Run it.
How does it respond to?:
• My mother and I talked last night. • I said no! • The weather is nice. • Do you know my brother? (What happens here?) What happens if you keep answering "no" a couple of times?
2. Getting to Know the Code Exploration The program has 2 fields: field "input" - for the user to type into field "log" - that has all the user input and the chatbot's responses ... On the card script:
There are 2 global variables to hold the Input (what the user types) and the Response (what your chatbot replies with) When the card opens up, the welcoming message is displayed... "Hello, Let's talk"
global theInput // what the user types global theResponse // what your chatbot returns
// There are 2 fields: // "input" - for the user to type something in // "log" - to show the input and the response from the program // // What is New: // - the "select" statement is used to position the cursor // - the back slash char "\" is used to continue statements on another line // - concatenating a "return" at the end of lines to skip to the next line // otherwise, it would all be 1 really long line
on openCard put "Hello, Let's talk" & return into field "log" // start the conversation select before line 1 of field "input" // put the cursor at the start of the field end openCard
When the user types something and presses the "Return" key (or "Enter" key), the script on the field:
Field "input" script:
on returnInField getResponse end returnInField
which sends a message "getResponse" to the card script which looks for a keyword and returns the response to it.
There are 2 ways to find letters in a sentence - "is in" or "contains" e.g. if theInput contains "mother" then put "Tell me more about your family" into theResponse end if
and if "mother" is in theInput then put "Tell me more about your family" into theResponse end if
Card script:
Look at the code. See how the "if" statement assigns a value to the response and returns that response. Look at the 2 handlers: getResponse() and getRandomResponse()
The handler getResponse looks for certain keywords and returns a specific response for each one
on getResponse put field "input" into theInput // save what the user typed put theInput & return after field "log" // put it into the log and go to next line put empty into theResponse // clear out the response variable // the following uses the "\" to break up the long statement into shorter lines // the "\" is a continuation mark ("to be continued on the next line...") if "no" is in theInput then
put "Why so negative?" into theResponse else if "mother" is in theInput \ or "father" is in theInput \ or "sister" is in theInput \ or "brother" is in theInput then put "Tell me more about your family" into theResponse else getRandomResponse end if put theResponse & return after field "log" // show the response in the log put empty into field "input" // clear out the input field // scroll the cursor to the last line of the log so that it shows on the screen put the number of lines of field "log" into y select line y of field "log" // put the cursor in the input field for the user to type select before line 1 of field "input" end getResponse
The handler getRandomResponse() picks a response from a group of responses.
on getRandomResponse put random(4) into x if x = 1 then put "Interesting, tell me more" into theResponse else if x = 2 then put "Hmm" into theResponse else if x = 3 then put "Do you really think so?" into theResponse else if x =4 then put "You don't say" into theResponse end if end getRandomResponse
Assignment - Making Your Chatbot Smarter
1. Add more words (if statements) to the code:
• Pick three more keywords, like "you", "weather" , "school", etc and make them separate entries as “no” and “brother” and edit the getResponse function to respond to each of these. Enter the three keywords and responses below.
e.g. "You" "This is not about me, it is about you" "hungry" "What foods do you like to eat?"
(make up some of your own)
add more else if statements
e.g.
if "no" is in theInput then put "Why so negative?" into theResponse else if "you" is in theInput then put "This is about you, not me" into theResponse else if "hungry" is in theInput then put "What foods do you like to eat? into theResponse else if "snow" is in theInput then put "Do you like snow?" into theResponse else if "pickle" is in theInput then put "I like pickles, what else?" into theResponse else if "school" is in theInput then put "what do you like about it" into theResponse else if "hamburger" is in theInput then put "how about hotdogs?" into theResponse
else if "mother" is in theInput \ ...
2. Check if the user did not type something:
• Have the code check that the statement has at least one character (check if it is empty). If there are no characters, the response should tell the user to enter something. For example, a possible statement and response would be: Statement: (nothing typed, the user just pressed the "Enter" key) Response: "Say something, please"
check if theInput has nothing in it...
e.g. if "no" is in theInput then put "Why so negative?" into theResponse else if theInput is empty then
put "Say something please" into theResponse
else if "mother" is in theInput \ ...
3. Add more random responses:
• Add two more noncommittal responses to the possible random responses (increase the count from 4 to 6).
e.g. "Are you sure?", "is that so?", ...make up some of your own general responses
add some more responses and bump up the counter from 4 to 6)
on getRandomResponse put random(6) into x if x = 1 then ...
else if x =4 then put "You don't say" into theResponse else if x =5 then put "Tell me more..." into theResponse else if x =6 then put "What about that?" into theResponse end if ...
4. Add more words (if statements) to the code:
• Have it respond “Tell me more about your pets” when the statement contains the word “dog” or “cat.” For example, a possible statement and response would be:
Statement: I like my cat Mittens.
Response: Tell me more about your pets.
(add another "else if" with your new words - you can copy and paste the code already there)
e.g. if "no" is in theInput then put "Why so negative?" into theResponse else if "cat" is in theInput \ or "dog" is in theInput \ or "fish" is in theInput then put "Tell me more about your pet" into theResponse else if "mother" is in theInput \ ...
If you just have many if statements, they each get done one after the other
if "mother" is in theInput \ // continues on next line
or "father" is in theInput \ // continues on next line
or "sister" is in theInput \ // continues on next line
or "brother" is in theInput then
put "Tell me more about your family" into theResponse
end if if "dog" is in theInput \
or "cat" is in theInput then
put "Tell me more about your pet" into theResponse
end if if "you" is in theInput then
put "This is about you, not me" into theResponse
end if if "weather" is in theInput then
put "I have not looked outside today" into theResponse
end if if "time" is in theInput then
put "time has no meaning for me" into theResponse
end if
if theInput is empty then
put "Say something please" into theResponse
else
getRandomResponse
end if
The last "if" will always be executed and always do a random response. We only want to do it if nothing else gets done. That is why we "nest" the if statements. if one does not get done, else try another. (if ...else...)
1. Nested if statements - Example 1
As you add more "if - else" statements, they indent and start creeping off the page
on getRandomResponse put random(6) into x if x = 1 then put "Interesting, tell me more" into theResponse else if x = 2 then put "Hmm" into theResponse else if x = 3 then put "Do you really think so?" into theResponse else if x =4 then put "You don't say" into theResponse else if x =5 then put "Okay, What else?" into theResponse else if x =6 then put "Are you serious?" into theResponse end if end if end if end if end if end if end getRandomResponse
Solution:
Make them "if - else - if" statements. They line up nicely and are easier to read and they do not need so many "end if"s
on getRandomResponse put random(6) into x if x=1 then put "Interesting, tell me more" into theResponse else if x=2 then put "Hmm" into theResponse else if x=3 then put "Do you really think so?" into theResponse else if x=4 then put "You don't say" into theResponse else if x=5 then put "Okay, What else?" into theResponse else if x=6 then put "Are you serious?" into theResponse end if end getRandomResponse
1. Nested if statements - Example 2
Another example
if "mother" is in theInput \ // continues on next line
or "father" is in theInput \ // continues on next line
or "sister" is in theInput \ // continues on next line
or "brother" is in theInput then
put "Tell me more about your family" into theResponse
else
if "dog" is in theInput \
or "cat" is in theInput then
put "Tell me more about your pet" into theResponse
else
if "you" is in theInput then
put "This is about you, not me" into theResponse
else
if "weather" is in theInput then
put "I have not looked outside today" into theResponse
else
if "time" is in theInput then
put "time has no meaning for me" into theResponse
else
if theInput is empty then
put "Say something please" into theResponse
else
getRandomResponse
end if
end if
end if
end if
end if
end if
end if
Solution: else - if's
if "mother" is in theInput \ // continues on next line
or "father" is in theInput \ // continues on next line
or "sister" is in theInput \ // continues on next line
or "brother" is in theInput then
put "Tell me more about your family" into theResponse
else if "dog" is in theInput \
or "cat" is in theInput then
put "Tell me more about your pet" into theResponse
else if "you" is in theInput then
put "This is about you, not me" into theResponse
else if "weather" is in theInput then
put "I have not looked outside today" into theResponse
else if "time" is in theInput then
put "time has no meaning for me" into theResponse
else if theInput is empty then
put "Say something please" into theResponse
else
getRandomResponse
end if
|
Subpages (1):
AI - Warm-up
 Updating...
cyril.pruszko@pgcps.org, Mar 21, 2017, 8:59 AM
Chatbot_table.livecode (7k) cyril.pruszko@pgcps.org, Mar 21, 2017, 1:23 PM
|