Question: Question 1: Multiple Tokens The currrent replace() method will find a token, %1%, in the response String, and replace the token with the match (search)

Question 1: Multiple Tokens

The currrent replace() method will find a token, %1%, in the response String,

and replace the token with the match (search) word that was found in the user statement.

You must improve the replace() method must to allow multiple tokens.

The tokens will be identified as %1%, %2%, %3%, %4%, etc.

There may be zero to 4 tokens embedded in the response String ( in the resp[][] array ).

Remove the code in replace() method, and write your own new code.

Question 2: Extended Word Characters

Currently, Magpie identifies words by a consecutive letters only. For example, a hypen would

currently break a word since it is not a letter. You will improve/expand word recognition.

You need to rewrite the findKeyword() method and write the chkLtr() method so it will recognize

numbers and hyphens as part of a word, not a delimiter as it currently does.

import java.util.Random;

public class Magpie8a_exam

{

private String[][] match = { {"no","nope","no-no","no1","negatron","negative"}, {"yes","yup","yes","affirmative", "1yes", "1yes123"}, {"family","d4d","s1ster","unc13","mother","father","brother","sister","uncle","aunt","dad","daddy","mom"}, {"vehicle","car","truck","train","plane","ship","boat","motorcycle","bike"}, {"color","red","green","blue","black","white","gray","orange","purple","yellow","pink"}, {"","","","","","","","",""},

};

private String[][] resp = { {"greet", "Hello, let's talk.", "Attention! Let's begin.", "Shape up and let's start!"},

{"blank", "Hello, WAKE UP! Don't just hit enter!", "Hey, you forgot to enter a response!", "Kirk to Enterprise!", "I'm drawing a blank here...", "Shape up! RESPOND!"},

{"random", "Interesting, tell me more.", "Hmmm.", "Do you really think so?", "You don't say.", "Why are fire engines red?"},

{"no", "%1% is so negative!", "%2% a baaaad day, %1%?", "%1% is a displeasing word, like %2% or %3% or %4% !", "I never make misteaks!"},

{"yes", "%2% Why so %1% !", "%2% a greaaaat day, %1%?", "%1% is a good word, like %2% or %3% or %4% !", "%1%... one of my favorite concepts, like %2% or %3% or %4%!"},

{"family", "Tell me about your %1%!", "%2% How was last Christmas with your %1%?", "Family life is awesome, but who is best, %1%, %2%, %3% or %4%?", "Does %2% in your family annoy you at times?", "Do you have any siblings?"},

{"vehicle", "Hmmm... a %1%... or is %2% better than a bicycle?", "Is %1% your favorite transportation, or %2% or %3%?", "Do you have license to operate a %1%?", "Have you ever been in a %1%, %2%, %3% or %4%?"},

{"color", "What is your favorite color, like %1%, %2%, %3% or %4%?", "Why is %1% your favorite color, like %2%, %3% or %4%?", "%1%... what a great color, but I like %3% and %4% best!", "I don't like %1%, though %2% or %4% is ok." }, };

// ------> BEGIN: INSERT CODE HERE <------ //

private String[] recChars = { "-" };

// ------> END: INSERT CODE HERE <------ //

private final String strEmpty = "";

private final String strSpace = " ";

private final String blank = "blank";

private final String random = "random";

private final String token = "%1%";

private final String[] tokens = {"%1%","%2%","%3%","%4%"};

public String getResponse(String statement)

{

if (statement.trim().length()==0) return chooseResp(blank);

for(int r=0; r

{

String typeRow=match[r][0].toLowerCase();

String tMatch=checkStr(statement,match[r]);

if (tMatch!=strEmpty)

return replace(chooseResp(typeRow),tMatch,match[r]);

}

return chooseResp(random);

}

private String checkStr(String s1,String[] m)

{

for (int i=1; i

if (findKeyword(s1, m[i])>=0) return m[i];

return strEmpty;

}

public String chooseResp(String x)

{

for(int row=0; row

if (resp[row][0]==x)

return resp[row][ getRandomInt(1,resp[row].length-1)];

return "Error in chooseResp(): No match for "+x;

}

private String replace(String s, String rep, String[] m)

{

// ------> BEGIN: INSERT CODE HERE <------ //

// remove code through to END //

// replace with your new code //

int pos = s.indexOf(token);

if (pos>-1) s=s.substring(0,pos)+rep+s.substring(pos+3);

// ------> END: INSERT CODE HERE <------ //

return s;

}

private int getRandomInt(int begin, int end)

{

if (end

return (int)(Math.random()*(end-begin)+begin);

}

private int findKeyword(String statement, String goal)

{

return findKeyword(statement, goal, 0);

}

private int findKeyword(String statement, String goal, int startPos)

{

String s = statement.trim().toLowerCase();

goal=goal.toLowerCase();

int pos = s.indexOf(goal, startPos);

while (pos>-1)

{

String before=strSpace, after=strSpace;

if (pos>0)

before = s.substring(pos-1, pos);

if (pos+goal.length() < s.length())

after=s.substring(pos+goal.length(),pos+goal.length()+1);

// ------> BEGIN: INSERT CODE HERE <------ //

// remove code through to END //

// replace with your new code //

if (((before.compareTo("a")<0) || (before.compareTo("z")>0)) && ((after.compareTo("a")<0) || (after.compareTo("z")>0))) return pos;

// ------> END: INSERT CODE HERE <------ //

pos = s.indexOf(goal,pos+1);

}

return -1;

}

private boolean chkLtr(String x)

{

// ------> BEGIN: INSERT CODE HERE <------ //

// remove & replace the return //

return true;

// ------> END: INSERT CODE HERE <------ //

}

public static void PLN(Object arg) {System.out.println(arg);

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!