Advertisement
Crenox

MagPie Java Program

Nov 13th, 2014
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.56 KB | None | 0 0
  1. // Sammy Samkough
  2. // MagPie
  3. // Spec: Create a program to carry on conversations with a human user.
  4.  
  5. /**
  6. * A program to carry on conversations with a human user.
  7. * This is the initial version that:
  8. * <ul><li>
  9. * Uses indexOf to find strings
  10. * </li><li>
  11. * Handles responding to simple words and phrases
  12. * </li></ul>
  13. * This version uses a nested if to handle default responses.
  14. * @author Laurie White
  15. * @version April 2012
  16. */
  17. public class MagPie
  18. {
  19. /**
  20. * Get a default greeting
  21. * @return a greeting
  22. */
  23. public String getGreeting()
  24. {
  25. return "Hello, let's talk.";
  26. }
  27.  
  28. /**
  29. * Gives a response to a user statement
  30. *
  31. * @param statement
  32. * the user statement
  33. * @return a response based on the rules given
  34. */
  35. public String getResponse(String statement)
  36. {
  37. String response = "";
  38. if (findKeyword(statement, "no", 0) >= 0)
  39. {
  40. response = "Why so negative?";
  41. }
  42. else if (findKeyword(statement, "mother", 0) >= 0
  43. || findKeyword(statement, "father", 0) >= 0
  44. || findKeyword(statement, "sister", 0) >= 0
  45. || findKeyword(statement, "brother", 0) >= 0)
  46. {
  47. response = "Tell me more about your family.";
  48. }
  49. else if (findKeyword(statement, "cat", 0) >= 0
  50. || findKeyword(statement, "dog", 0) >= 0)
  51. {
  52. if (findKeyword(statement, "cancat", 0) >= 0)
  53. {
  54. response = "Cancat is cool.";
  55. }
  56. else if (findKeyword(statement, "hotdog", 0) >= 0)
  57. {
  58. response = "I love hotdogs!";
  59. }
  60. response = "Tell me more about your pets.";
  61. }
  62. else if (findKeyword(statement, "Ascione", 0) >= 0)
  63. {
  64. response = "He's a great programmer!";
  65. }
  66. else if (findKeyword(statement, "I like", 0) >= 0)
  67. {
  68. response = "What do you like about it?";
  69. }
  70. else if (findKeyword(statement, "What's your name?", 0) >= 0
  71. || findKeyword(statement, "what's your name?", 0) >= 0
  72. || findKeyword(statement, "Whats your name?", 0) >= 0
  73. || findKeyword(statement, "whats your name?", 0) >= 0
  74. || findKeyword(statement, "What's your name", 0) >= 0
  75. || findKeyword(statement, "what's your name", 0) >= 0
  76. || findKeyword(statement, "Whats your name", 0) >= 0
  77. || findKeyword(statement, "whats your name", 0) >= 0)
  78. {
  79. response = "MagPie";
  80. }
  81. else if (statement.length() == 0)
  82. {
  83. response = "What was that?";
  84. }
  85. else
  86. {
  87. response = getRandomResponse();
  88. }
  89.  
  90. return response;
  91. }
  92.  
  93. /**
  94. * Pick a default response to use if nothing else fits.
  95. * @return a non-committal string
  96. */
  97. private String getRandomResponse()
  98. {
  99. final int NUMBER_OF_RESPONSES = 5;
  100. double r = Math.random();
  101. int whichResponse = (int)(r * NUMBER_OF_RESPONSES);
  102. String response = "";
  103.  
  104. if (whichResponse == 0)
  105. {
  106. response = "Interesting, tell me more.";
  107. }
  108. else if (whichResponse == 1)
  109. {
  110. response = "Hmmm.";
  111. }
  112. else if (whichResponse == 2)
  113. {
  114. response = "Do you really think so?";
  115. }
  116. else if (whichResponse == 3)
  117. {
  118. response = "You don't say.";
  119. }
  120. else if (whichResponse == 4)
  121. {
  122. response = "I don't care.";
  123. }
  124.  
  125. return response;
  126. }
  127.  
  128. /**
  129. * Search for one word in phrase. The search is not case
  130. * sensitive. This method will check that the given goal
  131. * is not a substring of a longer string (so, for
  132. * example, "I know" does not contain "no").
  133. *
  134. * @param statement
  135. * the string to search
  136. * @param goal
  137. * the string to search for
  138. * @param startPos
  139. * the character of the string to begin the
  140. * search at
  141. * @return the index of the first occurrence of goal in
  142. * statement or -1 if it's not found
  143. */
  144. private int findKeyword(String statement, String goal, int startPos)
  145. {
  146. String phrase = statement.trim();
  147. // The only change to incorporate the startPos is in
  148. // the line below
  149. int psn = phrase.toLowerCase().indexOf(goal.toLowerCase(), startPos);
  150.  
  151. // Refinement--make sure the goal isn't part of a
  152. // word
  153. while (psn >= 0)
  154. {
  155. // Find the string of length 1 before and after
  156. // the word
  157. String before = " ", after = " ";
  158. if (psn > 0)
  159. {
  160. before = phrase.substring(psn - 1, psn).toLowerCase();
  161. }
  162. if (psn + goal.length() < phrase.length())
  163. {
  164. after = phrase.substring(psn + goal.length(), psn + goal.length() + 1).toLowerCase();
  165. }
  166.  
  167. // If before and after aren't letters, we've
  168. // found the word
  169. if (((before.compareTo("a") < 0) || (before.compareTo("z") > 0)) // before is not a letter
  170. && ((after.compareTo("a") < 0) || (after.compareTo("z") > 0)))
  171. {
  172. return psn;
  173. }
  174.  
  175. // The last position didn't work, so let's find
  176. // the next, if there is one.
  177. psn = phrase.indexOf(goal.toLowerCase(), psn + 1);
  178.  
  179. }
  180.  
  181. return -1;
  182. }
  183. }
  184.  
  185. -------------------------------------------------------------------------------------------------------------------------------
  186. // Sammy Samkough
  187. // MagPie
  188. // Spec: Create a program to carry on conversations with a human user.
  189.  
  190. import java.util.Scanner;
  191.  
  192. /**
  193. * A simple class to run the Magpie class.
  194. * @author Laurie White
  195. * @version April 2012
  196. */
  197. public class MagPieClient
  198. {
  199. /**
  200. * Create a Magpie, give it user input, and print its replies.
  201. */
  202. public static void main(String[] args)
  203. {
  204. MagPie maggie = new MagPie();
  205.  
  206. System.out.println (maggie.getGreeting());
  207. Scanner in = new Scanner (System.in);
  208. String statement = in.nextLine();
  209.  
  210. while (!statement.equalsIgnoreCase("Bye"))
  211. {
  212. System.out.println(maggie.getResponse(statement));
  213. statement = in.nextLine();
  214. }
  215. }
  216.  
  217. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement