Advertisement
Guest User

Untitled

a guest
Oct 24th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.61 KB | None | 0 0
  1. /**
  2. * A program to carry on conversations with a human user.
  3. * This is the initial version that:
  4. * <ul><li>
  5. * Uses indexOf to find strings
  6. * </li><li>
  7. * Handles responding to simple words and phrases
  8. * </li></ul>
  9. * This version uses a nested if to handle default responses.
  10. * @author Laurie White
  11. * @version April 2012
  12. */
  13. public class Magpie2
  14. {
  15. /**
  16. * Get a default greeting
  17. * @return a greeting
  18. */
  19. public String getGreeting()
  20. {
  21. return "Hello, let's talk.";
  22. }
  23.  
  24. /**
  25. * Gives a response to a user statement
  26. *
  27. * @param statement
  28. * the user statement
  29. * @return a response based on the rules given
  30. */
  31. public String getResponse(String statement)
  32. {
  33. String response = "";
  34. statement = statement.toLowerCase();
  35. if (statement.indexOf("no") >= 0)
  36. {
  37. boolean find = getWord(statement, "no");
  38. if(find) {
  39. response = "why so negative?";
  40. }else {
  41. response = getRandomResponse();
  42. }
  43.  
  44. }
  45. else if (statement.indexOf("mother") >= 0
  46. || statement.indexOf("father") >= 0
  47. || statement.indexOf("sister") >= 0)
  48. {
  49. response = "Tell me more about your family.";
  50. }else if(statement.indexOf("cat") >= 0 || statement.indexOf("dog") >= 0) {
  51.  
  52. if(statement.indexOf("cat") >= 0 ) {
  53. boolean find = getWord(statement, "cat");
  54. if(find) {
  55. response = "Tell me more about your cat";
  56. }else {
  57. response = getRandomResponse();
  58. }
  59. } else if(statement.indexOf("dog") >= 0) {
  60. boolean find = getWord(statement,"dog");
  61. if(find) {
  62. response = "Tell me more about your dog";
  63. } else {
  64. response = getRandomResponse();
  65. }
  66. }
  67.  
  68. }
  69. else if(statement.indexOf("mr. davis") >= 0 || statement.indexOf("mr. memmo") >= 0 || statement.indexOf("mr. crowley") >= 0) {
  70.  
  71. response = "He sounds like a pretty nice guy.";
  72.  
  73. }
  74. else if(statement.indexOf("brother") >= 0) {
  75.  
  76. response = "Intereseting. What do you think about your brother?";
  77.  
  78. }
  79. else if(statement.indexOf("death") >= 0 || statement.indexOf("dead") >= 0 ) {
  80. response = "...thats horrible, im very sorry.";
  81. }
  82. else if(statement.indexOf("die") >= 0) {
  83. boolean find = getWord(statement, "die");
  84. if(find) {
  85. response = "...That's horrible, I am very sorry. Tell me about your love for taco.";
  86. } else {
  87. response = getRandomResponse();
  88. }
  89. } else if(statement.indexOf("i want") >= 0) {
  90.  
  91. String thing = statement.substring(6);
  92.  
  93. char c = thing.charAt(thing.length() - 1);
  94.  
  95. if(c > 'z' || c < 'a') {
  96. thing = thing.substring(0,thing.length() - 1);
  97. }
  98. response = "Would you really be happy if you had" + thing + "?";
  99.  
  100.  
  101. } else if(statement.indexOf("i like") >= 0) {
  102. String thing = statement.substring(6);
  103.  
  104. char c = thing.charAt(thing.length() - 1);
  105.  
  106. if(c > 'z' || c < 'a') {
  107. thing = thing.substring(0,thing.length() - 1);
  108. }
  109. response = "What do you like about" + thing + "?";
  110. } else if(statement.indexOf("i") >= 0 && statement.indexOf("you") >= 0) {
  111.  
  112. String answer = statement.substring(statement.indexOf("i") + 1,statement.indexOf("you"));
  113. if(answer.indexOf("think") >= 0) {
  114. response = "Well, I am simply a program. I do not have any special capabilities \neither as I am just a simple jumble of code.";
  115. } else {
  116. response = "Why do you" + answer + "me?";
  117. }
  118.  
  119.  
  120. }else if(statement.indexOf("you") >= 0 ) {
  121. response = "Well, I am simply a program. I do not have any special capabilities \neither as I am just a simple jumble of code.";
  122. }
  123. else
  124. {
  125. response = getRandomResponse();
  126. }
  127. return response;
  128. }
  129.  
  130. /**
  131. * Pick a default response to use if nothing else fits.
  132. * @return a non-committal string
  133. */
  134. // Function gets a random response
  135. private String getRandomResponse()
  136. {
  137. //Produces a random number, to get a random response
  138. final int NUMBER_OF_RESPONSES = 6;
  139. double r = Math.random();
  140. int whichResponse = (int)(r * NUMBER_OF_RESPONSES);
  141. String response = "";
  142.  
  143. if (whichResponse == 0)
  144. {
  145. response = "Interesting, tell me more.";
  146. }
  147. else if (whichResponse == 1)
  148. {
  149. response = "Hmmm.";
  150. }
  151. else if (whichResponse == 2)
  152. {
  153. response = "Do you really think so?";
  154. }
  155. else if (whichResponse == 3)
  156. {
  157. response = "You don't say.";
  158. }
  159. else if(whichResponse == 4) {
  160. response = "The mitochondria is the powerhouse of the cell.";
  161. }
  162. else if(whichResponse == 5) {
  163. response = "Lions have teeth just like you and not me because I am not human.";
  164. }
  165.  
  166. return response;
  167. }
  168.  
  169. //Method that returns a boolean that tells the code whether or not the answer actually contains the correct word
  170. //To produce the correct response from the getResponse method.
  171. public boolean getWord(String str, String find) {
  172. //These strings store the characters before and after the word that is trying to be found
  173. String firstChar = " ";
  174. String lastChar = " ";
  175.  
  176.  
  177. if(str.indexOf(find) >= 0) {
  178. if(str.indexOf(find) > 0) {
  179. firstChar = str.substring(str.indexOf(find) - 1, str.indexOf(find));
  180. }
  181. if(str.indexOf(find) + find.length() < str.length() - 1) {
  182. lastChar = str.substring(str.indexOf(find) + 3, str.indexOf(find) + 4);
  183. }
  184. }
  185. //Checks whether or not both of the characters are spaces. When they are, it proves that the word we are looking for
  186. //is in the string as an independent word and not as a part of another word.
  187. if(firstChar.equals(" ") && lastChar.equals(" ") || lastChar.equals("s")) {
  188. return true;
  189. } else if(firstChar.equals(" ") && !lastChar.equals(" ") || !lastChar.equals("s")) {
  190. for(int i = 0; i< find.length(); i++) {
  191.  
  192. char c = find.charAt(i);
  193.  
  194. if(c > 'z' || c < 'a') {
  195. return true;
  196. }
  197. }
  198. }
  199.  
  200. return false;
  201. }
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement