Advertisement
TheEpicKiller

Magpie 5

Feb 9th, 2016
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.74 KB | None | 0 0
  1. import java.util.Random;
  2.  
  3. /**
  4. * A program to carry on conversations with a human user.
  5. * This version:
  6. *<ul><li>
  7. * Uses advanced search for keywords
  8. *</li><li>
  9. * Will transform statements as well as react to keywords
  10. *</li></ul>
  11. * This version uses an array to hold the default responses.
  12. * @author Laurie White
  13. * @version April 2012
  14. */
  15. public class Magpie5
  16. {
  17. /**
  18. * Get a default greeting
  19. * @return a greeting
  20. */
  21. public String getGreeting()
  22. {
  23. return "Hello, let's talk.";
  24. }
  25.  
  26. /**
  27. * Gives a response to a user statement
  28. *
  29. * @param statement
  30. * the user statement
  31. * @return a response based on the rules given
  32. */
  33. public String getResponse(String statement)
  34. {
  35. String response = "";
  36. if (statement.length() == 0)
  37. {
  38. response = "Say something, please.";
  39. }
  40.  
  41. else if (findKeyword(statement, "no") >= 0)
  42. {
  43. response = "Why so negative?";
  44. }
  45. else if (findKeyword(statement, "mother") >= 0
  46. || findKeyword(statement, "father") >= 0
  47. || findKeyword(statement, "sister") >= 0
  48. || findKeyword(statement, "brother") >= 0)
  49. {
  50. response = "Tell me more about your family.";
  51. }
  52.  
  53. // Responses which require transformations
  54. else if (findKeyword(statement, "I want to", 0) >= 0)
  55. {
  56. response = transformIWantToStatement(statement);
  57. }
  58. // Part of student solution
  59. else if (findKeyword(statement, "I want", 0) >= 0)
  60. {
  61. response = transformIWantStatement(statement);
  62. }
  63. //added
  64. else if (findKeyword(statement, "I have a question about computers.") >= 0)
  65. {
  66. response = "Can you be more specific about computers? Does it have to do with " + getRandomComputerResponse() + "?";
  67. }
  68.  
  69. else if (findKeyword(statement, "I have a question about video games.") >= 0)
  70. {
  71. response = "Can you be more specific about video games? Does it have to do with " + getRandomVideoGameResponse() + "?";
  72. }
  73.  
  74.  
  75. else
  76. {
  77.  
  78. // Look for a two word (you <something> me)
  79. // pattern
  80. int psn = findKeyword(statement, "you", 0);
  81.  
  82. if (psn >= 0
  83. && findKeyword(statement, "me", psn) >= 0)
  84. {
  85. response = transformYouMeStatement(statement);
  86. }
  87. else
  88. {
  89. // Part of student solution
  90. // Look for a two word (I <something> you)
  91. // pattern
  92. psn = findKeyword(statement, "i", 0);
  93.  
  94. if (psn >= 0
  95. && findKeyword(statement, "you", psn) >= 0)
  96. {
  97. response = transformIYouStatement(statement);
  98. }
  99. else
  100. {
  101. response = getRandomResponse();
  102. }
  103. }
  104. }
  105. return response;
  106. }
  107.  
  108. /**
  109. * Take a statement with "I want to <something>." and transform it into
  110. * "What would it mean to <something>?"
  111. * @param statement the user statement, assumed to contain "I want to"
  112. * @return the transformed statement
  113. */
  114. private String transformIWantToStatement(String statement)
  115. {
  116. // Remove the final period, if there is one
  117. statement = statement.trim();
  118. String lastChar = statement.substring(statement
  119. .length() - 1);
  120. if (lastChar.equals("."))
  121. {
  122. statement = statement.substring(0, statement
  123. .length() - 1);
  124. }
  125. int psn = findKeyword (statement, "I want to", 0);
  126. String restOfStatement = statement.substring(psn + 9).trim();
  127. return "What would it mean to " + restOfStatement + "?";
  128. }
  129.  
  130.  
  131. /**
  132. * Take a statement with "I want <something>." and transform it into
  133. * "Would you really be happy if you had <something>?"
  134. * @param statement the user statement, assumed to contain "I want"
  135. * @return the transformed statement
  136. */
  137. private String transformIWantStatement(String statement)
  138. {
  139. // Remove the final period, if there is one
  140. statement = statement.trim();
  141. String lastChar = statement.substring(statement
  142. .length() - 1);
  143. if (lastChar.equals("."))
  144. {
  145. statement = statement.substring(0, statement
  146. .length() - 1);
  147. }
  148. int psn = findKeyword (statement, "I want", 0);
  149. String restOfStatement = statement.substring(psn + 6).trim();
  150. return "Would you really be happy if you had " + restOfStatement + "?";
  151. }
  152.  
  153. /**
  154. * Take a statement with "you <something> me" and transform it into
  155. * "What makes you think that I <something> you?"
  156. * @param statement the user statement, assumed to contain "you" followed by "me"
  157. * @return the transformed statement
  158. */
  159. private String transformYouMeStatement(String statement)
  160. {
  161. // Remove the final period, if there is one
  162. statement = statement.trim();
  163. String lastChar = statement.substring(statement
  164. .length() - 1);
  165. if (lastChar.equals("."))
  166. {
  167. statement = statement.substring(0, statement
  168. .length() - 1);
  169. }
  170.  
  171. int psnOfYou = findKeyword (statement, "you", 0);
  172. int psnOfMe = findKeyword (statement, "me", psnOfYou + 3);
  173.  
  174. String restOfStatement = statement.substring(psnOfYou + 3, psnOfMe).trim();
  175. return "What makes you think that I " + restOfStatement + " you?";
  176. }
  177.  
  178. /**
  179. * Take a statement with "I <something> you" and transform it into
  180. * "Why do you <something> me?"
  181. * @param statement the user statement, assumed to contain "I" followed by "you"
  182. * @return the transformed statement
  183. */
  184. private String transformIYouStatement(String statement)
  185. {
  186. // Remove the final period, if there is one
  187. statement = statement.trim();
  188. String lastChar = statement.substring(statement
  189. .length() - 1);
  190. if (lastChar.equals("."))
  191. {
  192. statement = statement.substring(0, statement
  193. .length() - 1);
  194. }
  195.  
  196. int psnOfI = findKeyword (statement, "I", 0);
  197. int psnOfYou = findKeyword (statement, "you", psnOfI);
  198.  
  199. String restOfStatement = statement.substring(psnOfI + 1, psnOfYou).trim();
  200. return "Why do you " + restOfStatement + " me?";
  201. }
  202.  
  203.  
  204.  
  205.  
  206. /**
  207. * Search for one word in phrase. The search is not case sensitive.
  208. * This method will check that the given goal is not a substring of a longer string
  209. * (so, for example, "I know" does not contain "no").
  210. * @param statement the string to search
  211. * @param goal the string to search for
  212. * @param startPos the character of the string to begin the search at
  213. * @return the index of the first occurrence of goal in statement or -1 if it's not found
  214. */
  215. private int findKeyword(String statement, String goal, int startPos)
  216. {
  217. String phrase = statement.trim();
  218. // The only change to incorporate the startPos is in the line below
  219. int psn = phrase.toLowerCase().indexOf(goal.toLowerCase(), startPos);
  220.  
  221. // Refinement--make sure the goal isn't part of a word
  222. while (psn >= 0)
  223. {
  224. // Find the string of length 1 before and after the word
  225. String before = " ", after = " ";
  226. if (psn > 0)
  227. {
  228. before = phrase.substring (psn - 1, psn).toLowerCase();
  229. }
  230. if (psn + goal.length() < phrase.length())
  231. {
  232. after = phrase.substring(psn + goal.length(), psn + goal.length() + 1).toLowerCase();
  233. }
  234.  
  235. // If before and after aren't letters, we've found the word
  236. if (((before.compareTo ("a") < 0 ) || (before.compareTo("z") > 0)) // before is not a letter
  237. && ((after.compareTo ("a") < 0 ) || (after.compareTo("z") > 0)))
  238. {
  239. return psn;
  240. }
  241.  
  242. // The last position didn't work, so let's find the next, if there is one.
  243. psn = phrase.indexOf(goal.toLowerCase(), psn + 1);
  244.  
  245. }
  246.  
  247. return -1;
  248. }
  249.  
  250. /**
  251. * Search for one word in phrase. The search is not case sensitive.
  252. * This method will check that the given goal is not a substring of a longer string
  253. * (so, for example, "I know" does not contain "no"). The search begins at the beginning of the string.
  254. * @param statement the string to search
  255. * @param goal the string to search for
  256. * @return the index of the first occurrence of goal in statement or -1 if it's not found
  257. */
  258. private int findKeyword(String statement, String goal)
  259. {
  260. return findKeyword (statement, goal, 0);
  261. }
  262.  
  263.  
  264.  
  265. /**
  266. * Pick a default response to use if nothing else fits.
  267. * @return a non-committal string
  268. */
  269. private String getRandomResponse ()
  270. {
  271. Random r = new Random ();
  272. return randomResponses [r.nextInt(randomResponses.length)];
  273. }
  274.  
  275. private String [] randomResponses = {"Interesting, tell me more",
  276. "Hmmm.",
  277. "Do you really think so?",
  278. "You don't say."
  279. };
  280.  
  281. private String getRandomComputerResponse ()
  282. {
  283. Random r = new Random ();
  284.  
  285.  
  286. String [] randomComputerResponse = {"SSDs",
  287. "CPUS.",
  288. "GPUS",
  289. "RAM",
  290. "Power Supplies",
  291. "Hard Drives"
  292. };
  293.  
  294. return (randomComputerResponse[r.nextInt(6)]);
  295. }
  296.  
  297. private String getRandomVideoGameResponse ()
  298. {
  299. Random r = new Random ();
  300.  
  301.  
  302. String [] randomVideoGameResponse = {"CSGO",
  303. "Super Mario Bros",
  304. "Halo",
  305. "Minecraft",
  306. "GTA V",
  307. "Outlast"
  308. };
  309. return (randomVideoGameResponse[r.nextInt(6)]);
  310. }
  311. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement