0% found this document useful (0 votes)
331 views

A+ Computer Science Magpie Chatbot Activity 4 Worksheet KEY

Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
331 views

A+ Computer Science Magpie Chatbot Activity 4 Worksheet KEY

Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2

A+ Computer Science

Magpie Chatbot Activity 4 Worksheet KEY

Consider the modified getResponse method:

public String getResponse(String statement)


{
String response = "";
if (statement.length() == 0)
{
response = "Say something, please.";
}

else if (findKeyword(statement, "no") >= 0)


{
response = "Why so negative?";
}
else if (findKeyword(statement, "mother") >= 0
|| findKeyword(statement, "father") >= 0
|| findKeyword(statement, "sister") >= 0
|| findKeyword(statement, "brother") >= 0)
{
response = "Tell me more about your family.";
}

else if (findKeyword(statement, "I want to", 0) >= 0)


{
response = transformIWantToStatement(statement);
}

else
{
int pos = findKeyword(statement, "you", 0);

if (pos >= 0 && findKeyword(statement, "me", pos) >= 0)


{
response = transformYouMeStatement(statement);
}
else
{
response = getRandomResponse();
}
}
return response;
}
Questions:
1. Which version of the findKeyword method is used by the you/me statements? Why?

The three parameter findKeyword method is used. The method is called the first time with a
starting position of 0 to search the string from the beginning to find the keyword "you". If "you" is
found, then the findKeyword method calls the transformYouMeStatement method. The
transformYouMeStatement method calls the three parameter findKeyword method twice to find
the position of you (starting position of 0) and then me (starting position of where you is found + 3).
Calling the three paramenter version of findKeyword ensures that me is found after you.

© A+ Computer Science – Magpie Activity 4 Worksheet - www.apluscompsci.com


How does the new version of your chatbot respond to the following input?

a. I want to go on vacation. What would it mean to go on vacation?

b. I want to understand French. What would it mean to study French?

c. Do you like me? Why do you like me?

d. You confuse me. What makes you think I confuse you?

e. I want some ice cream. Would you really be happy if you had some ice cream?

f. I would like you to do your homework. Why do you would like me?

g. I do not like liver. Random response

h. I see you. Why do you see me?

2. In your lab, you altered the code to have the chatbot respond to "I want something" statements.
Would the order in which you test "I want something" statements and "I want to" statements affect the
way in which the chatbot responds to both types of statements? Explain your answer.

If "I want something" is tested before "I want to", the chatbot will never respond correctly to "I want to"
statements. All of the "I want to" statements will be answered with "Would you really be happy if you
had…", the "I want something" response.

3. Explain how this portion of the getResponse method ensures that the word you is found before the
word me in statement.

else
{
int pos = findKeyword(statement, "you", 0);

if (pos >= 0 && findKeyword(statement, "me", pos) >= 0)


{
response = transformYouMeStatement(statement);
}
else
{
response = getRandomResponse();
}
}

The transformYouMeStatement method calls the three parameter findKeyword method twice to
find the position of you (starting position of 0) and then me (starting position of where you is found +
3). Calling the three paramenter version of findKeyword ensures that me is found after you.

© A+ Computer Science – Magpie Activity 4 Worksheet - www.apluscompsci.com

You might also like