Activity 2.29.1.
Run to see the results. Try changing the input in main.
https://apcentral.collegeboard.org/courses/ap-computer-science-a/classroom-resources/lab-resource-page
http://secure-media.collegeboard.org/digitalServices/pdf/ap/ap-compscia-magpie-lab-student-guide.pdf
String
class and conditionals with a chatbot. A chatbot is a computer program that tries to hold a conversation with a user. This chapter will walk you through the activities in the Magpie chatbot lab.
Scanner
class and record the responses. But, instead you can run this simplified version below and just call the getResponse
method with each string as input as shown in the main
method below.
http://www.pythontutor.com/visualize.html#code=public+class+Magpie2%0A%7B%0A+++public+String+getGreeting(%29%0A+++%7B%0A+++++return+%22Hello,+let's+talk.%22%3B%0A+++%7D%0A%0A+++public+String+getResponse(String+statement%29%0A+++%7B%0A+++++String+response+%3D+%22%22%3B%0A+++++if+(statement.indexOf(%22no%22%29+%3E%3D+0%29+%7B%0A+++++++response+%3D+%22Why+so+negative%3F%22%3B%0A+++++%7D+else+if+(statement.indexOf(%22mother%22%29+%3E%3D+0%0A+++++++++++++++++%7C%7C+statement.indexOf(%22father%22%29+%3E%3D+0%0A+++++++++++++++++%7C%7C+statement.indexOf(%22sister%22%29+%3E%3D+0%0A+++++++++++++++++%7C%7C+statement.indexOf(%22brother%22%29+%3E%3D+0%29+%7B%0A+++++++response+%3D+%22Tell+me+more+about+your+family.%22%3B%0A+++++%7D+else+%7B%0A+++++++response+%3D+getRandomResponse(%29%3B%0A+++++%7D%0A+++++return+response%3B%0A+++%7D%0A%0A+++private+String+getRandomResponse(%29%0A+++%7B%0A+++++final+int+NUMBER_OF_RESPONSES+%3D+4%3B%0A+++++double+r+%3D+Math.random(%29%3B%0A+++++int+whichResponse+%3D+(int%29(r+*+NUMBER_OF_RESPONSES%29%3B%0A+++++String+response+%3D+%22%22%3B%0A%0A+++++if+(whichResponse+%3D%3D+0%29+%7B%0A+++++++response+%3D+%22Interesting,+tell+me+more.%22%3B%0A+++++%7D+else+if+(whichResponse+%3D%3D+1%29+%7B%0A+++++++response+%3D+%22Hmmm.%22%3B%0A+++++%7D+else+if+(whichResponse+%3D%3D+2%29+%7B%0A+++++++response+%3D+%22Do+you+really+think+so%3F%22%3B%0A+++++%7D+else+if+(whichResponse+%3D%3D+3%29+%7B%0A+++++++response+%3D+%22You+don't+say.%22%3B%0A+++++%7D%0A+++++return+response%3B%0A+++++++%7D%0A%0A+++public+static+void+main(String%5B%5D+args%29%0A+++%7B%0A+++++Magpie2+maggie+%3D+new+Magpie2(%29%3B%0A%0A+++++System.out.println(maggie.getGreeting(%29%29%3B%0A+++++System.out.println(maggie.getResponse(%22My+mother+and+I+talked+last+night.%22%29%29%3B%0A+++++System.out.println(maggie.getResponse(%22I+said+no!%22%29%29%3B%0A+++++System.out.println(maggie.getResponse(%22The+weather+is+nice.%22%29%29%3B%0A+++++System.out.println(maggie.getResponse(%22Do+you+know+my+brother%3F%22%29%29%3B%0A+++%7D%0A%7D&mode=display&origin=opt-frontend.js&cumulative=false&heapPrimitives=false&textReferences=false&py=java&rawInputLstJSON=%5B%5D&curInstr=9
https://replit.com/@BerylHoffman/Magpie-ChatBot-Lab-v2#Main.java
getResponse
method of Magpie2 looks for certain keywords like "mother"
and "brother"
. Why do you think the response to βDo you know my brother?β isnβt βTell me more about your family.β? Discuss this with partner in pairs and see if you can figure it out.
if
statement assigns a value to the response and returns that response. The method getRandomResponse
generates a random number and uses that to assign the response. Modify the code above to add other random responses.
trim
method to remove spaces from the beginning and end, and then checking the length of the trimmed string. If there are no characters, the response should tell the user to enter something. For example, a possible statement and response would be:
getResponse
method to respond to each of these.
My **mother** has a **dog** but **no** cat.
Which response did you get β was it the one about family or the one about pets or the negative one for no? Change the order of your if-else-if statements to make it so that one of the other responses is selected and try running it again.
https://replit.com/@BerylHoffman/Magpie-ChatBot-Lab-v2#Main.java
Scanner
class to read input from the user. The Scanner
class is not on the AP CSA exam. You can log in to replit.com and use this code and change it to do this lab.
import java.util.Scanner;
/**
* A simple class to run the Magpie class.
*
* @author Laurie White
* @version April 2012
*/
public class MagpieRunner2
{
/** Create a Magpie, give it user input, and print its replies. */
public static void main(String[] args)
{
Magpie2 maggie = new Magpie2();
System.out.println(maggie.getGreeting());
Scanner in = new Scanner(System.in);
String statement = in.nextLine();
while (!statement.equals("Bye"))
{
System.out.println(maggie.getResponse(statement));
statement = in.nextLine();
}
}
}
public class Magpie2
{
/**
* Get a default greeting
*
* @return a greeting
*/
public String getGreeting()
{
return "Hello, let's talk.";
}
/**
* Gives a response to a user statement
*
* @param statement the user statement
* @return a response based on the rules given
*/
public String getResponse(String statement)
{
String response = "";
if (statement.indexOf("no") >= 0)
{
response = "Why so negative?";
} else if (statement.indexOf("mother") >= 0
|| statement.indexOf("father") >= 0
|| statement.indexOf("sister") >= 0
|| statement.indexOf("brother") >= 0)
{
response = "Tell me more about your family.";
}
else
{
response = getRandomResponse();
}
return response;
}
/**
* Pick a default response to use if nothing else fits.
*
* @return a non-committal string
*/
private String getRandomResponse()
{
final int NUMBER_OF_RESPONSES = 4;
double r = Math.random();
int whichResponse = (int) (r * NUMBER_OF_RESPONSES);
String response = "";
if (whichResponse == 0)
{
response = "Interesting, tell me more.";
}
else if (whichResponse == 1)
{
response = "Hmmm.";
}
else if (whichResponse == 2)
{
response = "Do you really think so?";
}
else if (whichResponse == 3)
{
response = "You don't say.";
}
return response;
}
}
indexOf
and toLowerCase
methods. Do they do what you thought they would? The method indexOf
is on the exam and the method toLowerCase
is not. Why do you think you might want to change the string to all lowercase characters? Why doesnβt the value of sample
change?
sample
change? Do string methods change the string? Try some other string methods.
docs.oracle.com/en/java/javase/22/docs/api/java.base/java/lang/String.html
indexOf(String str)
method. Follow the link and read the description of the indexOf
method.
StringExplorer
in the ActiveCode above in the main
method above to see for yourself that indexOf
behaves as specified:
int notFoundPsn = sample.indexOf("slow");
System.out.println("sample.indexOf(\"slow\") = " + notFoundPsn);
indexOf(String str, int fromIndex)
. Add lines to StringExplorer
that illustrate how this version of indexOf
differs from the one with one parameter.
findKeyword
method below. It has a while
loop in it which we havenβt seen before. A while
loop repeats the code in the block below it while a condition is true. A block is all the code inside of an open curly brace {
and a close curly brace }
.
private int findKeyword(String statement, String goal,
int startPos)
{
String phrase = statement.trim();
// The only change to incorporate the startPos is in
// the line below
int psn = phrase.toLowerCase().indexOf(goal.toLowerCase(),
startPos);
// Refinement--make sure the goal isn't part of a word
while (psn >= 0)
{
// Find the string of length 1 before and after
// the word
String before = " ", after = " ";
if (psn > 0)
{
before = phrase.substring(psn - 1, psn).toLowerCase();
}
if (psn + goal.length() < phrase.length())
{
after = phrase.substring(
psn + goal.length(),
psn + goal.length() + 1)
.toLowerCase();
}
/* determine the values of psn, before, and after at this point */
// If before and after aren't letters, we've
// found the word
if (((before.compareTo("a") < 0) ||
(before.compareTo("z") > 0)) // before is not a letter
&& ((after.compareTo("a") < 0) ||
(after.compareTo("z") > 0)))
{
return psn;
}
// The last position didn't work, so let's find
// the next, if there is one.
psn = phrase.indexOf(goal.toLowerCase(),psn + 1);
}
return -1;
}
https://replit.com/@BerylHoffman/Magpie-ChatBot-Lab-v3#Main.java
findKeyword
in action. It is called from the getResponse
method to print out an appropriate response based on a keyword. For example, looking for the word "no"
to print out "Why so negative?"
, but it wonβt match no inside of another word like "another"
.
if (findKeyword(statement, "no") >= 0)
{
response = "Why so negative?";
}
http://www.pythontutor.com/visualize.html#code=public+class+Magpie3%0A+++%7B%0A%09++/**%0A%09+++*+Get+a+default+greeting%0A%09+++*+%0A%09+++*+%40return+a+greeting%0A%09+++*/%0A%09++public+String+getGreeting(%29%0A%09++%7B%0A%09+++++return+%22Hello,+let's+talk.%22%3B%0A%09++%7D%0A%0A%09++%0A%09++public+String+getResponse(String+statement%29%0A%09++%7B%0A%09+++++String+response+%3D+%22%22%3B%0A%09%09+if+(statement.length(%29+%3D%3D+0%29%0A%09%09+%7B%0A%09%09++++response+%3D+%22Say+something,+please.%22%3B%0A%09%09+%7D%0A%09%09+else+if+(findKeyword(statement,+%22no%22%29+%3E%3D+0%29%0A%09%09+%7B%0A%09%09%09response+%3D+%22Why+so+negative%3F%22%3B%0A%09%09+%7D%0A%09%09+else+if+(findKeyword(statement,+%22mother%22%29+%3E%3D+0%0A%09%09%09%09%7C%7C+findKeyword(statement,+%22father%22%29+%3E%3D+0%0A%09%09%09%09%7C%7C+findKeyword(statement,+%22sister%22%29+%3E%3D+0%0A%09%09%09%09%7C%7C+findKeyword(statement,+%22brother%22%29+%3E%3D+0%29%0A%09%09+%7B%0A%09%09%09response+%3D+%22Tell+me+more+about+your+family.%22%3B%0A%09%09+%7D%0A%09%09+else%0A%09%09+%7B%0A%09%09%09response+%3D+getRandomResponse(%29%3B%0A%09%09+%7D%0A%09%09+return+response%3B%0A%09++%7D%0A%0A%09++%0A%09++private+int+findKeyword(String+statement,+String+goal,%0A%09%09%09int+startPos%29%0A%09++%7B%0A%09+++++String+phrase+%3D+statement.trim(%29%3B%0A%09%09+//+The+only+change+to+incorporate+the+startPos+is+in%0A%09%09+//+the+line+below%0A%09%09+int+psn+%3D+phrase.toLowerCase(%29.indexOf(%0A%09%09%09%09goal.toLowerCase(%29,+startPos%29%3B%0A%0A%09%09+//+Refinement--make+sure+the+goal+isn't+part+of+a%0A%09%09+//+word%0A%09%09+while+(psn+%3E%3D+0%29%0A%09%09+%7B%0A%09%09%09//+Find+the+string+of+length+1+before+and+after%0A%09%09%09//+the+word%0A%09%09%09String+before+%3D+%22+%22,+after+%3D+%22+%22%3B%0A%09%09%09if+(psn+%3E+0%29%0A%09%09%09%7B%0A%09%09%09%09before+%3D+phrase.substring(psn+-+1,+psn%29%0A%09%09%09%09%09%09.toLowerCase(%29%3B%0A%09%09%09%7D%0A%09%09%09if+(psn+%2B+goal.length(%29+%3C+phrase.length(%29%29%0A%09%09%09%7B%0A%09%09%09%09after+%3D+phrase.substring(%0A%09%09%09%09%09%09psn+%2B+goal.length(%29,%0A%09%09%09%09%09%09psn+%2B+goal.length(%29+%2B+1%29%0A%09%09%09%09%09%09.toLowerCase(%29%3B%0A%09%09%09%7D%0A%0A++++++++++++/*+determine+the+values+of+psn,+before,+and+after+at+this+point+*/%0A++++++++++++%0A%09%09%09//+If+before+and+after+aren't+letters,+we've%0A%09%09%09//+found+the+word%0A%09%09%09if+(((before.compareTo(%22a%22%29+%3C+0%29+%7C%7C+(before%0A%09%09%09%09%09.compareTo(%22z%22%29+%3E+0%29%29+//+before+is+not+a%0A%09%09%09%09%09%09%09%09%09%09%09//+letter%0A%09%09%09%09%09%26%26+((after.compareTo(%22a%22%29+%3C+0%29+%7C%7C+(after%0A%09%09%09%09%09%09%09.compareTo(%22z%22%29+%3E+0%29%29%29%0A%09%09%09%7B%0A%09%09%09%09return+psn%3B%0A%09%09%09%7D%0A%0A%09%09%09//+The+last+position+didn't+work,+so+let's+find%0A%09%09%09//+the+next,+if+there+is+one.%0A%09%09%09psn+%3D+phrase.indexOf(goal.toLowerCase(%29,%0A%09%09%09%09%09psn+%2B+1%29%3B%0A%0A%09%09+%7D%0A%0A%09%09return+-1%3B%0A%09++%7D%0A%0A%09++%0A%09++private+int+findKeyword(String+statement,+String+goal%29%0A%09++%7B%0A%09%09+return+findKeyword(statement,+goal,+0%29%3B%0A%09++%7D%0A%0A%09++/**%0A%09+++*+Pick+a+default+response+to+use+if+nothing+else+fits.%0A%09+++*+%0A%09+++*+%40return+a+non-committal+string%0A%09+++*/%0A%09++private+String+getRandomResponse(%29%0A%09++%7B%0A%09%09+final+int+NUMBER_OF_RESPONSES+%3D+4%3B%0A%09%09+double+r+%3D+Math.random(%29%3B%0A%09%09+int+whichResponse+%3D+(int%29+(r+*+NUMBER_OF_RESPONSES%29%3B%0A%09%09+String+response+%3D+%22%22%3B%0A%0A%09%09+if+(whichResponse+%3D%3D+0%29%0A%09%09+%7B%0A%09%09+%09response+%3D+%22Interesting,+tell+me+more.%22%3B%0A%09%09+%7D%0A%09%09+else+if+(whichResponse+%3D%3D+1%29%0A%09%09+%7B%0A%09%09+%09response+%3D+%22Hmmm.%22%3B%0A%09%09+%7D%0A%09%09+else+if+(whichResponse+%3D%3D+2%29%0A%09%09+%7B%0A%09%09+%09response+%3D+%22Do+you+really+think+so%3F%22%3B%0A%09%09+%7D%0A%09%09+else+if+(whichResponse+%3D%3D+3%29%0A%09%09+%7B%0A%09%09+%09response+%3D+%22You+don't+say.%22%3B%0A%09%09+%7D%0A%0A%09%09+return+response%3B%0A%09++%7D%0A%09++%0A%09++public+static+void+main(String%5B%5D+args%29%0A%09++%7B%0A%09%09Magpie3+maggie+%3D+new+Magpie3(%29%3B%0A%09%09%0A%09%09maggie.findKeyword(%22yesterday+is+today's+day+before.%22,+%22day%22,+0%29%3B%0A%09%09%09%0A%09++%7D%0A%0A+++%7D&mode=display&origin=opt-frontend.js&cumulative=false&heapPrimitives=false&textReferences=false&py=java&rawInputLstJSON=%5B%5D&curInstr=0
psn
, before
, and after
right after the comment on line 100 in the findKeyword
method below. Record each of the values in a table. The College Board student guide for the Magpie Chatbot Labhttp://secure-media.collegeboard.org/digitalServices/pdf/ap/ap-compscia-magpie-lab-student-guide.pdf
https://replit.com/@BerylHoffman/Magpie-ChatBot-Lab-v3#Main.java
statement
in the main method and see what they print. Or you can try it on replit.com version 4https://replit.com/@BerylHoffman/Magpie-ChatBot-Lab-v4#Main.java
http://www.pythontutor.com/visualize.html#code=public%20class%20Magpie4%0A%20%20%20%7B%20%0A%20%20%20%20%20%20public%20String%20getResponse%28String%20statement%29%0A%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20String%20response%20%3D%20%22%22%3B%0A%20%20%20%20%20%20%20%20%20if%20%28statement.length%28%29%20%3D%3D%200%29%0A%20%20%20%20%20%20%20%20%20%20%20%20response%20%3D%20%22Say%20something,%20please.%22%3B%0A%0A%20%20%20%20%20%20%20%20%20else%20if%20%28findKeyword%28statement,%20%22no%22%29%20%3E%3D%200%29%0A%20%20%20%20%20%20%20%20%20%20%20%20response%20%3D%20%22Why%20so%20negative%3F%22%3B%0A%20%20%20%20%20%20%20%20%20else%20if%20%28findKeyword%28statement,%20%22mother%22%29%20%3E%3D%200%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7C%7C%20findKeyword%28statement,%20%22father%22%29%20%3E%3D%200%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7C%7C%20findKeyword%28statement,%20%22sister%22%29%20%3E%3D%200%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7C%7C%20findKeyword%28statement,%20%22brother%22%29%20%3E%3D%200%29%0A%20%20%20%20%20%20%20%20%20%20%20%20response%20%3D%20%22Tell%20me%20more%20about%20your%20family.%22%3B%0A%0A%20%20%20%20%20%20%20%20%20else%20if%20%28findKeyword%28statement,%20%22I%20want%20to%22,%200%29%20%3E%3D%200%29%0A%20%20%20%20%20%20%20%20%20%20%20%20response%20%3D%20transformIWantToStatement%28statement%29%3B%0A%20%20%20%20%20%20%20%20%20else%20if%20%28findKeyword%28statement,%20%22I%20want%22,%200%29%20%3E%3D%200%29%0A%20%20%20%20%20%20%20%20%20%20%20%20response%20%3D%20transformIWantStatement%28statement%29%3B%0A%20%20%20%20%20%20%20%20%20else%0A%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20int%20psn%20%3D%20findKeyword%28statement,%20%22you%22,%200%29%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20%28psn%20%3E%3D%200%20%26%26%20findKeyword%28statement,%20%22me%22,%20psn%29%20%3E%3D%200%29%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20response%20%3D%20transformYouMeStatement%28statement%29%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20else%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20response%20%3D%20getRandomResponse%28%29%3B%0A%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20return%20response%3B%0A%20%20%20%20%20%20%7D%0A%20%0A%20%20%20%20%20%20private%20String%20transformIWantToStatement%28String%20statement%29%0A%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20statement%20%3D%20statement.trim%28%29%3B%0A%20%20%20%20%20%20%20%20%20String%20lastChar%20%3D%20statement.substring%28statement.length%28%29%20-%201%29%3B%0A%20%20%20%20%20%20%20%20%20if%20%28lastChar.equals%28%22.%22%29%29%0A%20%20%20%20%20%20%20%20%20%20%20%20statement%20%3D%20statement.substring%280,%20statement.length%28%29%20-%201%29%3B%0A%20%20%20%20%20%20%20%20%20int%20psn%20%3D%20findKeyword%20%28statement,%20%22I%20want%20to%22,%200%29%3B%0A%20%20%20%20%20%20%20%20%20String%20restOfStatement%20%3D%20statement.substring%28psn%20%2B%209%29.trim%28%29%3B%0A%20%20%20%20%20%20%20%20%20return%20%22What%20would%20it%20mean%20to%20%22%20%2B%20restOfStatement%20%2B%20%22%3F%22%3B%0A%20%20%20%20%20%20%7D%0A%20%0A%20%20%20%20%20%20private%20String%20transformIWantStatement%28String%20statement%29%0A%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20statement%20%3D%20statement.trim%28%29%3B%0A%20%20%20%20%20%20%20%20%20String%20lastChar%20%3D%20statement.substring%28statement.length%28%29%20-%201%29%3B%0A%20%20%20%20%20%20%20%20%20if%20%28lastChar.equals%28%22.%22%29%29%0A%20%20%20%20%20%20%20%20%20%20%20%20statement%20%3D%20statement.substring%280,%20statement.length%28%29%20-%201%29%3B%0A%20%20%20%20%20%20%20%20%20int%20psn%20%3D%20findKeyword%20%28statement,%20%22I%20want%22,%200%29%3B%0A%20%20%20%20%20%20%20%20%20String%20restOfStatement%20%3D%20statement.substring%28psn%20%2B%207%29%3B%0A%20%20%20%20%20%20%20%20%20return%20%22Would%20you%20really%20be%20happy%20if%20you%20had%20%22%20%2B%20restOfStatement%20%2B%20%22%3F%22%3B%0A%20%20%20%20%20%20%7D%0A%0A%20%20%20%20%20%20private%20String%20transformYouMeStatement%28String%20statement%29%0A%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20statement%20%3D%20statement.trim%28%29%3B%0A%20%20%20%20%20%20%20%20%20String%20lastChar%20%3D%20statement.substring%28statement.length%28%29%20-%201%29%3B%0A%20%20%20%20%20%20%20%20%20if%20%28lastChar.equals%28%22.%22%29%29%0A%20%20%20%20%20%20%20%20%20%20%20%20statement%20%3D%20statement.substring%280,%20statement.length%28%29%20-%201%29%3B%20%20%0A%20%20%20%20%20%20%20%20%20int%20psnOfYou%20%3D%20findKeyword%20%28statement,%20%22you%22,%200%29%3B%0A%20%20%20%20%20%20%20%20%20int%20psnOfMe%20%3D%20findKeyword%20%28statement,%20%22me%22,%20psnOfYou%20%2B%203%29%3B%0A%20%20%0A%20%20%20%20%20%20%20%20%20String%20restOfStatement%20%3D%20statement.substring%28psnOfYou%20%2B%203,%20psnOfMe%29.trim%28%29%3B%0A%20%20%20%20%20%20%20%20%20return%20%22What%20makes%20you%20think%20that%20I%20%22%20%2B%20restOfStatement%20%2B%20%22%20you%3F%22%3B%0A%20%20%20%20%20%20%7D%0A%20%0A%20%20%20%20%20%20private%20int%20findKeyword%28String%20statement,%20String%20goal,%20int%20startPos%29%0A%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20String%20phrase%20%3D%20statement.trim%28%29%3B%0A%20%20%20%20%20%20%20%20%20int%20psn%20%3D%20phrase.toLowerCase%28%29.indexOf%28goal.toLowerCase%28%29,%20startPos%29%3B%0A%20%20%20%20%20%20%20%20%20while%20%28psn%20%3E%3D%200%29%20%0A%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20String%20before%20%3D%20%22%20%22,%20after%20%3D%20%22%20%22%3B%20%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20%28psn%20%3E%200%29%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20before%20%3D%20phrase.substring%20%28psn%20-%201,%20psn%29.toLowerCase%28%29%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20%28psn%20%2B%20goal.length%28%29%20%3C%20phrase.length%28%29%29%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20after%20%3D%20phrase.substring%28psn%20%2B%20goal.length%28%29,%20psn%20%2B%20goal.length%28%29%20%2B%201%29.toLowerCase%28%29%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20%28%28%28before.compareTo%20%28%22a%22%29%20%3C%200%20%29%20%7C%7C%20%28before.compareTo%28%22z%22%29%20%3E%200%29%29%20%20%26%26%20%28%28after.compareTo%20%28%22a%22%29%20%3C%200%20%29%20%7C%7C%20%28after.compareTo%28%22z%22%29%20%3E%200%29%29%29%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20return%20psn%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20psn%20%3D%20phrase.indexOf%28goal.toLowerCase%28%29,%20psn%20%2B%201%29%3B%0A%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20return%20-1%3B%0A%20%20%20%20%20%20%7D%0A%20%0A%20%20%20%20%20%20%0A%20%20%20%20%20%20private%20int%20findKeyword%28String%20statement,%20String%20goal%29%0A%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20return%20findKeyword%20%28statement,%20goal,%200%29%3B%0A%20%20%20%20%20%20%7D%0A%0A%20%20%20%20%20%20private%20String%20getRandomResponse%28%29%0A%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20return%20%22Interesting,%20tell%20me%20more.%22%3B%0A%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20public%20static%20void%20main%28String%5B%5D%20args%29%0A%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20Magpie4%20maggie%20%3D%20new%20Magpie4%28%29%3B%0A%20%20%20%20%20%20%20%20String%20statement%20%3D%20%22I%20want%20to%20build%20a%20robot.%22%3B%0A%20%20%20%20%20%20%20%20System.out.println%28%22Statement%3A%20%22%20%2B%20statement%29%3B%0A%20%20%20%20%20%20%20%20System.out.println%28%22Response%3A%20%22%20%2B%20maggie.getResponse%28statement%29%29%3B%20%20%20%20%0A%20%20%20%20%20%20%7D%0A%20%20%20%7D&cumulative=false&heapPrimitives=false&mode=display&origin=opt-frontend.js&py=java&rawInputLstJSON=%5B%5D&textReferences=false&curInstr=0
transformIWantToStatement
and transformYouMeStatement
and getResponse
has been modified to add tests to find βI want to somethingβ statements and βYou something meβ statements.
transformIWantStatement
and transformIYouStatement
, and calls to each as described below. Alter the code either above in the active code window or on replit.com version 4https://replit.com/@BerylHoffman/Magpie-ChatBot-Lab-v4#Main.java
transformIWantStatement
, have it respond to βI want somethingβ statements with βWould you really be happy if you had something?β You can use the already written transformIWantToStatement
method as a guide. In doing this, you need to be careful about where you place the call to the method so it calls the right one. Test with the following: