Advertisement
Guest User

Untitled

a guest
Jan 16th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.06 KB | None | 0 0
  1. /**
  2. * A program to carry on conversations with a human user.
  3. * This version:
  4. *<ul><li>
  5. * Uses advanced search for keywords
  6. *</li><li>
  7. * Will transform statements as well as react to keywords
  8. *</li></ul>
  9. * @author Laurie White
  10. * @version April 2012
  11. *
  12. */
  13. public class Magpie4
  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. if (statement.length() == 0)
  35. {
  36. response = "Say something, please.";
  37. }
  38.  
  39. else if (findKeyword(statement, "no") >= 0)
  40. {
  41. response = "Why so negative?";
  42. }
  43. else if (findKeyword(statement, "mother") >= 0
  44. || findKeyword(statement, "father") >= 0
  45. || findKeyword(statement, "sister") >= 0
  46. || findKeyword(statement, "brother") >= 0)
  47. {
  48. response = "Tell me more about your family.";
  49. }
  50.  
  51. // Responses which require transformations
  52. else if (findKeyword(statement, "I want to", 0) >= 0)
  53. {
  54. response = transformIWantToStatement(statement);
  55. }
  56. else if (findKeyword(statement, "I want", 0) >= 0)
  57. {
  58. response = transformIWantStatement(statement);
  59. }
  60.  
  61. else if (findKeyword(statement, "I wish", 0) >= 0)
  62. {
  63. response = transformIWishStatement(statement);
  64. }
  65.  
  66. else if (findKeyword(statement, "I belive", 0) >= 0)
  67. {
  68. response = transformIBeliveStatement(statement);
  69. }
  70.  
  71. else if (findKeyword(statement, "I have", 0) >= 0)
  72. {
  73. response = transformIHaveStatement(statement);
  74. }
  75.  
  76. else
  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.  
  88. else
  89. {
  90. // Part of student solution
  91. // Look for a two word (I <something> you)
  92. // pattern
  93. psn = findKeyword(statement, "i", 0);
  94. if (psn >= 0 && findKeyword(statement, "you", psn) >= 0)
  95. {
  96. response = transformIYouStatement(statement);
  97. }
  98. else
  99. {
  100. response = getRandomResponse();
  101. }
  102. }
  103. }
  104. return response;
  105. }
  106.  
  107. /**
  108. * Take a statement with "I want to <something>." and transform it into
  109. * "What would it mean to <something>?"
  110. * @param statement the user statement, assumed to contain "I want to"
  111. * @return the transformed statement
  112. */
  113. private String transformIWantToStatement(String statement)
  114. {
  115. // Remove the final period, if there is one
  116. statement = statement.trim();
  117. String lastChar = statement.substring(statement
  118. .length() - 1);
  119. if (lastChar.equals("."))
  120. {
  121. statement = statement.substring(0, statement
  122. .length() - 1);
  123. }
  124. int psn = findKeyword (statement, "I want to", 0);
  125. String restOfStatement = statement.substring(psn + 6).trim();
  126. return "What would it mean to " + restOfStatement + "?";
  127. }
  128. private String transformIWantStatement(String statement)
  129. {
  130. statement = statement.trim();
  131. String lastChar = statement.substring(statement.length() - 1);
  132. if (lastChar.equals("."))
  133. {
  134. statement = statement.substring(0, statement.length() - 1);
  135. }
  136. int psn = findKeyword(statement, "I want", 0);
  137. String restOfStatement = statement.substring(psn + 7).trim();
  138. return "Would you be happy if you had " + restOfStatement + "?";
  139. }
  140.  
  141. private String transformIWishStatement(String statement)
  142. {
  143. statement = statement.trim();
  144. String lastChar = statement.substring(statement.length() - 1);
  145. if (lastChar.equals("."))
  146. {
  147. statement = statement.substring(0, statement.length() - 1);
  148. }
  149. int psn = findKeyword(statement, "I wish", 0);
  150. String restOfStatement = statement.substring(psn + 13).trim();
  151. return "why do you wish for " + restOfStatement + "?";
  152. }
  153.  
  154. private String transformIBeliveStatement(String statement)
  155. {
  156. statement = statement.trim();
  157. String lastChar = statement.substring(statement.length() - 1);
  158. if (lastChar.equals("."))
  159. {
  160. statement = statement.substring(0, statement.length() - 1);
  161. }
  162. int psn = findKeyword(statement, "I belive", 0);
  163. String restOfStatement = statement.substring(psn + 11).trim();
  164. return "For how long have you belived in " + restOfStatement + "?";
  165. }
  166.  
  167. private String transformIHaveStatement(String statement)
  168. {
  169. statement = statement.trim();
  170. String lastChar = statement.substring(statement.length() - 1);
  171. if (lastChar.equals("."))
  172. {
  173. statement = statement.substring(0, statement.length() - 1);
  174. }
  175. int psn = findKeyword(statement, "I have", 0);
  176. String restOfStatement = statement.substring(psn + 6).trim();
  177. return "why do you have " + restOfStatement + "?";
  178. }
  179.  
  180.  
  181. /**
  182. * Take a statement with "you <something> me" and transform it into
  183. * "What makes you think that I <something> you?"
  184. * @param statement the user statement, assumed to contain "you" followed by "me"
  185. * @return the transformed statement
  186. */
  187.  
  188. private String transformYouMeStatement(String statement)
  189. {
  190. // Remove the final period, if there is one
  191. statement = statement.trim();
  192. String lastChar = statement.substring(statement
  193. .length() - 1);
  194. if (lastChar.equals("."))
  195. {
  196. statement = statement.substring(0, statement
  197. .length() - 1);
  198. }
  199.  
  200. int psnOfYou = findKeyword (statement, "you", 0);
  201. int psnOfMe = findKeyword (statement, "me", psnOfYou + 3);
  202.  
  203. String restOfStatement = statement.substring(psnOfYou + 3, psnOfMe).trim();
  204. return "What makes you think that I " + restOfStatement + " you?";
  205. }
  206. private String transformIYouStatement(String statement)
  207. {
  208. statement = statement.trim();
  209. String lastChar = statement.substring(statement
  210. .length() - 1);
  211. if (lastChar.equals("."))
  212. {
  213. statement = statement.substring(0, statement
  214. .length() - 1);
  215. }
  216. int psnOfI =findKeyword (statement, "I", 0);
  217. int psnOfYou =findKeyword (statement, "you", psnOfI+1);
  218.  
  219. String restOfStatement = statement.substring(psnOfI + 1,psnOfYou).trim();
  220. return "Why do you " + restOfStatement + " me?";
  221. }
  222.  
  223.  
  224.  
  225.  
  226. /**
  227. * Search for one word in phrase. The search is not case
  228. * sensitive. This method will check that the given goal
  229. * is not a substring of a longer string (so, for
  230. * example, "I know" does not contain "no").
  231. *
  232. * @param statement
  233. * the string to search
  234. * @param goal
  235. * the string to search for
  236. * @param startPos
  237. * the character of the string to begin the
  238. * search at
  239. * @return the index of the first occurrence of goal in
  240. * statement or -1 if it's not found
  241. */
  242. private int findKeyword(String statement, String goal,
  243. int startPos)
  244. {
  245. String phrase = statement.trim().toLowerCase();
  246. goal = goal.toLowerCase();
  247.  
  248. // The only change to incorporate the startPos is in
  249. // the line below
  250. int psn = phrase.indexOf(goal, startPos);
  251.  
  252. // Refinement--make sure the goal isn't part of a
  253. // word
  254. while (psn >= 0)
  255. {
  256. // Find the string of length 1 before and after
  257. // the word
  258. String before = " ", after = " ";
  259. if (psn > 0)
  260. {
  261. before = phrase.substring(psn - 1, psn);
  262. }
  263. if (psn + goal.length() < phrase.length())
  264. {
  265. after = phrase.substring(
  266. psn + goal.length(),
  267. psn + goal.length() + 1);
  268. }
  269.  
  270. // If before and after aren't letters, we've
  271. // found the word
  272. if (((before.compareTo("a") < 0) || (before
  273. .compareTo("z") > 0)) // before is not a
  274. // letter
  275. && ((after.compareTo("a") < 0) || (after
  276. .compareTo("z") > 0)))
  277. {
  278. return psn;
  279. }
  280.  
  281. // The last position didn't work, so let's find
  282. // the next, if there is one.
  283. psn = phrase.indexOf(goal, psn + 1);
  284.  
  285. }
  286.  
  287. return -1;
  288. }
  289.  
  290. /**
  291. * Search for one word in phrase. The search is not case sensitive.
  292. * This method will check that the given goal is not a substring of a longer string
  293. * (so, for example, "I know" does not contain "no"). The search begins at the beginning of the string.
  294. * @param statement the string to search
  295. * @param goal the string to search for
  296. * @return the index of the first occurrence of goal in statement or -1 if it's not found
  297. */
  298. private int findKeyword(String statement, String goal)
  299. {
  300. return findKeyword (statement, goal, 0);
  301. }
  302.  
  303.  
  304.  
  305. /**
  306. * Pick a default response to use if nothing else fits.
  307. * @return a non-committal string
  308. */
  309. private String getRandomResponse()
  310. {
  311. final int NUMBER_OF_RESPONSES = 4;
  312. double r = Math.random();
  313. int whichResponse = (int)(r * NUMBER_OF_RESPONSES);
  314. String response = "";
  315.  
  316. if (whichResponse == 0)
  317. {
  318. response = "Interesting, tell me more.";
  319. }
  320. else if (whichResponse == 1)
  321. {
  322. response = "Hmmm.";
  323. }
  324. else if (whichResponse == 2)
  325. {
  326. response = "Do you really think so?";
  327. }
  328. else if (whichResponse == 3)
  329. {
  330. response = "You don't say.";
  331. }
  332.  
  333. return response;
  334. }
  335.  
  336. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement