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 (statement.indexOf("no") >= 0)
- {
- boolean find = getWord(statement, "no");
- if(find) {
- response = "why so negative?";
- }else {
- response = getRandomResponse();
- }
- }
- else if (statement.indexOf("mother") >= 0
- || statement.indexOf("father") >= 0
- || statement.indexOf("sister") >= 0)
- {
- response = "Tell me more about your family.";
- }else if(statement.indexOf("cat") >= 0 || statement.indexOf("dog") >= 0) {
- if(statement.indexOf("cat") >= 0 ) {
- boolean find = getWord(statement, "cat");
- if(find) {
- response = "Tell me more about your cat";
- }else {
- response = getRandomResponse();
- }
- } else if(statement.indexOf("dog") >= 0) {
- boolean find = getWord(statement,"dog");
- if(find) {
- response = "Tell me more about your dog";
- } else {
- response = getRandomResponse();
- }
- }
- }
- else if(statement.indexOf("mr. davis") >= 0 || statement.indexOf("mr. memmo") >= 0 || statement.indexOf("mr. crowley") >= 0) {
- response = "He sounds like a pretty nice guy.";
- }
- else if(statement.indexOf("brother") >= 0) {
- response = "Intereseting. What do you think about your brother?";
- }
- else if(statement.indexOf("death") >= 0 || statement.indexOf("dead") >= 0 ) {
- response = "...thats horrible, im very sorry.";
- }
- else if(statement.indexOf("die") >= 0) {
- boolean find = getWord(statement, "die");
- if(find) {
- response = "...That's horrible, I am very sorry. Tell me about your love for taco.";
- } else {
- response = getRandomResponse();
- }
- } else if(statement.indexOf("i want") >= 0) {
- String thing = statement.substring(6);
- char c = thing.charAt(thing.length() - 1);
- if(c > 'z' || c < 'a') {
- thing = thing.substring(0,thing.length() - 1);
- }
- response = "Would you really be happy if you had" + thing + "?";
- } else if(statement.indexOf("i like") >= 0) {
- String thing = statement.substring(6);
- char c = thing.charAt(thing.length() - 1);
- if(c > 'z' || c < 'a') {
- thing = thing.substring(0,thing.length() - 1);
- }
- response = "What do you like about" + thing + "?";
- } else if(statement.indexOf("i") >= 0 && statement.indexOf("you") >= 0) {
- String answer = statement.substring(statement.indexOf("i") + 1,statement.indexOf("you"));
- if(answer.indexOf("think") >= 0) {
- response = "Well, I am simply a program. I do not have any special capabilities \neither as I am just a simple jumble of code.";
- } else {
- response = "Why do you" + answer + "me?";
- }
- }else if(statement.indexOf("you") >= 0 ) {
- response = "Well, I am simply a program. I do not have any special capabilities \neither as I am just a simple jumble of code.";
- }
- else
- {
- response = getRandomResponse();
- }
- return response;
- }
- /**
- * Pick a default response to use if nothing else fits.
- * @return a non-committal string
- */
- // Function gets a random response
- private String getRandomResponse()
- {
- //Produces a random number, to get a random response
- final int NUMBER_OF_RESPONSES = 6;
- 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.";
- }
- else if(whichResponse == 4) {
- response = "The mitochondria is the powerhouse of the cell.";
- }
- else if(whichResponse == 5) {
- response = "Lions have teeth just like you and not me because I am not human.";
- }
- return response;
- }
- //Method that returns a boolean that tells the code whether or not the answer actually contains the correct word
- //To produce the correct response from the getResponse method.
- public boolean getWord(String str, String find) {
- //These strings store the characters before and after the word that is trying to be found
- String firstChar = " ";
- String lastChar = " ";
- if(str.indexOf(find) >= 0) {
- if(str.indexOf(find) > 0) {
- firstChar = str.substring(str.indexOf(find) - 1, str.indexOf(find));
- }
- if(str.indexOf(find) + find.length() < str.length() - 1) {
- lastChar = str.substring(str.indexOf(find) + 3, str.indexOf(find) + 4);
- }
- }
- //Checks whether or not both of the characters are spaces. When they are, it proves that the word we are looking for
- //is in the string as an independent word and not as a part of another word.
- if(firstChar.equals(" ") && lastChar.equals(" ") || lastChar.equals("s")) {
- return true;
- } else if(firstChar.equals(" ") && !lastChar.equals(" ") || !lastChar.equals("s")) {
- for(int i = 0; i< find.length(); i++) {
- char c = find.charAt(i);
- if(c > 'z' || c < 'a') {
- return true;
- }
- }
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement