Created
October 11, 2018 12:57
-
-
Save landjd19/32d73b3af61eeb54f1aad6b22c36a2e7 to your computer and use it in GitHub Desktop.
Chatbot part 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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 Jake Landaiche | |
* @version 10/10/18 | |
*/ | |
public class Magpie2 | |
{ | |
/** | |
* Get a default greeting | |
* @return a greeting | |
*/ | |
public String getGreeting() | |
{ | |
return "Hello, let's talk."; | |
} | |
/** | |
* Gives a response to a user response | |
* | |
* @param statement | |
* the user statement | |
* @return a response based on the rules given | |
*/ | |
public String getResponse(String statement) | |
{ | |
String response = ""; | |
if(statement.indexOf("i want") >= 0 && statement.indexOf("i want you") < 0){ | |
iWant(statement); | |
}else if(iThink(statement) == true && statement.indexOf("i want you") < 0){ | |
myQuestion(statement); | |
}else if(keywordFound(statement, "no") == true){ | |
response = "Why so negative?"; | |
}else if(keywordFound(statement, "Memmo") == true || keywordFound(statement, "memmo") == true){ | |
response = "He sounds like a great teacher."; | |
}else if (keywordFound(statement, "mother") == true|| keywordFound(statement, "father") == true || keywordFound(statement, "sister") == true || keywordFound(statement, "brother") == true) | |
{ | |
response = "Tell me more about your family."; | |
}else if(keywordFound(statement, "cat") == true || keywordFound(statement, | |
"dog") == true ){ | |
response = "Tell me more about your pets."; | |
}else if(keywordFound(statement, "bathroom") == true){ | |
response = "Why do you need to tell me this?"; | |
}else if(keywordFound(statement, "fight") == true){ | |
response = "Violence is never the answer."; | |
}else if(keywordFound(statement, "sad") == true){ | |
response = "Do you have any pets to make you feel better?"; | |
}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 = 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 = "Could you repeat that?"; | |
}else if(whichResponse == 5){ | |
response = "Cool, what else?"; | |
} | |
return response; | |
} | |
public boolean keywordFound(String statement, String keyword){ | |
int spot = statement.indexOf(keyword); | |
if(spot > 0 && statement.substring(spot - 1, spot).equals (" ") && statement.substring(spot + keyword.length(), spot + keyword.length() + 1).equals(" ")){ | |
return true; | |
}else if (spot == 0 /* && statement.substring(spot + keyword.length(), spot + keyword.length() + 1).equals(" ") */ || statement.length() == keyword.length()){ | |
return true; | |
}else{ | |
return false; | |
} | |
} | |
public void iWant(String statement){ | |
String desire = statement.substring(statement.indexOf("i want") + 6); | |
System.out.println("Would you really be happy if you had" + desire + "?"); | |
} | |
public boolean iThink(String statement){ | |
if(statement.indexOf("i") >= 0 && statement.indexOf("you") > statement.indexOf("i")){ | |
return true; | |
}else{ | |
return false; | |
} | |
} | |
public void myQuestion(String statement){ | |
String thought = statement.substring(statement.indexOf("i") + 1, statement.indexOf("you")).trim(); | |
System.out.println("Why do you " + thought + " me?"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment