Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * A program to carry on conversations with a human user.
- * This is the initial version that:
- * <ul><li>
- * Uses indexOf to find strings
- * </li><li>
- * Handles responding to simple words and phrases
- * </li></ul>
- * This version uses a nested if to handle default responses.
- * @author Laurie White
- * @version April 2012
- */
- 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 = "";
- statement=statement.toLowerCase();
- if (isAlone(statement, "no"))
- response = "Why so negative?";
- else if (isAlone(statement, "i want to")){
- String str="i want to";
- int index=statement.indexOf(str);
- int length=statement.length();
- int eIndex=index+str.length()-1;
- String rest=statement.substring(eIndex+1,length);
- response="what would it mean to you to"+rest;
- }
- //extra feature
- else if (isAlone(statement, "i don't like")){
- String str="i want to";
- int index=statement.indexOf(str);
- int length=statement.length();
- int eIndex=index+str.length()-1;
- String rest=statement.substring(eIndex+1,length);
- response="why don't you like"+rest;
- }
- else if (isAlone(statement, "mother")
- || isAlone(statement, "father")
- || isAlone(statement, "brother")
- || isAlone(statement, "sister"))
- response = "Tell me more about your family.";
- else if (isAlone(statement, "dog")|| isAlone(statement, "cat"))
- response = "Tell me more about your pets.";
- else if (isAlone(statement, "memmo"))
- response = "He seems like a good teacher.";
- else if (isAlone(statement, "math"))
- response = "Do you enjoy doing math?";
- else if (isAlone(statement, "school"))
- response = "School is great for learning.";
- else if (isAlone(statement, "mad"))
- response = "Maybe you should take a walk.";
- 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;
- }
- public static boolean isAlone (String in, String str){
- int index=in.indexOf(str);
- int length=in.length();
- int eIndex=index+str.length()-1;
- //input equals keyword
- if(in.indexOf(str)>=0){
- if (in.equals(str))
- return true;
- //space,end of string
- else if(eIndex+1==length && in.substring(index-1,index).equals(" "))
- return true;
- //space,space/puntuation
- else if (index==0 || in.substring(index-1,index).equals(" ")){
- if (in.substring(eIndex+1,eIndex+2).equals(" ")
- || in.substring(eIndex+1,eIndex+2).equals(".")
- || in.substring(eIndex+1,eIndex+2).equals("?")
- || in.substring(eIndex+1,eIndex+2).equals("!"))
- return true;
- else return false;
- }
- return false;
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement