Advertisement
Guest User

Untitled

a guest
Jan 16th, 2018
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.39 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
  62. {
  63. // Look for a two word (you <something> me)
  64. // pattern
  65. int psn = findKeyword(statement, "you", 0);
  66.  
  67. if (psn >= 0
  68. && findKeyword(statement, "me", psn) >= 0)
  69. {
  70. response = transformYouMeStatement(statement);
  71. }
  72.  
  73. else
  74. {
  75. // Part of student solution
  76. // Look for a two word (I <something> you)
  77. // pattern
  78. psn = findKeyword(statement, "i", 0);
  79. if (psn >= 0 && findKeyword(statement, "you", psn) >= 0)
  80. {
  81. response = transformIYouStatement(statement);
  82. }
  83. else
  84. {
  85. response = getRandomResponse();
  86. }
  87. }
  88. }
  89. return response;
  90. }
  91.  
  92. /**
  93. * Take a statement with "I want to <something>." and transform it into
  94. * "What would it mean to <something>?"
  95. * @param statement the user statement, assumed to contain "I want to"
  96. * @return the transformed statement
  97. */
  98. private String transformIWantToStatement(String statement)
  99. {
  100. // Remove the final period, if there is one
  101. statement = statement.trim();
  102. String lastChar = statement.substring(statement
  103. .length() - 1);
  104. if (lastChar.equals("."))
  105. {
  106. statement = statement.substring(0, statement
  107. .length() - 1);
  108. }
  109. int psn = findKeyword (statement, "I want to", 0);
  110. String restOfStatement = statement.substring(psn + 6).trim();
  111. return "What would it mean to " + restOfStatement + "?";
  112. }
  113. private String transformIWantStatement(String statement)
  114. {
  115. statement = statement.trim();
  116. String lastChar = statement.substring(statement.length() - 1);
  117. if (lastChar.equals("."))
  118. {
  119. statement = statement.substring(0, statement.length() - 1);
  120. }
  121. int psn = findKeyword(statement, "I want", 0);
  122. String restOfStatement = statement.substring(psn + 7).trim();
  123. return "Would you be happy if you had " + restOfStatement + "?";
  124. }
  125.  
  126.  
  127. /**
  128. * Take a statement with "you <something> me" and transform it into
  129. * "What makes you think that I <something> you?"
  130. * @param statement the user statement, assumed to contain "you" followed by "me"
  131. * @return the transformed statement
  132. */
  133.  
  134. private String transformYouMeStatement(String statement)
  135. {
  136. // Remove the final period, if there is one
  137. statement = statement.trim();
  138. String lastChar = statement.substring(statement
  139. .length() - 1);
  140. if (lastChar.equals("."))
  141. {
  142. statement = statement.substring(0, statement
  143. .length() - 1);
  144. }
  145.  
  146. int psnOfYou = findKeyword (statement, "you", 0);
  147. int psnOfMe = findKeyword (statement, "me", psnOfYou + 3);
  148.  
  149. String restOfStatement = statement.substring(psnOfYou + 3, psnOfMe).trim();
  150. return "What makes you think that I " + restOfStatement + " you?";
  151. }
  152. private String transformIYouStatement(String statement)
  153. {
  154. statement = statement.trim();
  155. String lastChar = statement.substring(statement
  156. .length() - 1);
  157. if (lastChar.equals("."))
  158. {
  159. statement = statement.substring(0, statement
  160. .length() - 1);
  161. }
  162. int psnOfI =findKeyword (statement, "I", 0);
  163. int psnOfYou =findKeyword (statement, "you", psnOfI+1);
  164.  
  165. String restOfStatement = statement.substring(psnOfI + 1,psnOfYou).trim();
  166. return "Why do you " + restOfStatement + " me?";
  167. }
  168.  
  169.  
  170.  
  171.  
  172. /**
  173. * Search for one word in phrase. The search is not case
  174. * sensitive. This method will check that the given goal
  175. * is not a substring of a longer string (so, for
  176. * example, "I know" does not contain "no").
  177. *
  178. * @param statement
  179. * the string to search
  180. * @param goal
  181. * the string to search for
  182. * @param startPos
  183. * the character of the string to begin the
  184. * search at
  185. * @return the index of the first occurrence of goal in
  186. * statement or -1 if it's not found
  187. */
  188. private int findKeyword(String statement, String goal,
  189. int startPos)
  190. {
  191. String phrase = statement.trim().toLowerCase();
  192. goal = goal.toLowerCase();
  193.  
  194. // The only change to incorporate the startPos is in
  195. // the line below
  196. int psn = phrase.indexOf(goal, startPos);
  197.  
  198. // Refinement--make sure the goal isn't part of a
  199. // word
  200. while (psn >= 0)
  201. {
  202. // Find the string of length 1 before and after
  203. // the word
  204. String before = " ", after = " ";
  205. if (psn > 0)
  206. {
  207. before = phrase.substring(psn - 1, psn);
  208. }
  209. if (psn + goal.length() < phrase.length())
  210. {
  211. after = phrase.substring(
  212. psn + goal.length(),
  213. psn + goal.length() + 1);
  214. }
  215.  
  216. // If before and after aren't letters, we've
  217. // found the word
  218. if (((before.compareTo("a") < 0) || (before
  219. .compareTo("z") > 0)) // before is not a
  220. // letter
  221. && ((after.compareTo("a") < 0) || (after
  222. .compareTo("z") > 0)))
  223. {
  224. return psn;
  225. }
  226.  
  227. // The last position didn't work, so let's find
  228. // the next, if there is one.
  229. psn = phrase.indexOf(goal, psn + 1);
  230.  
  231. }
  232.  
  233. return -1;
  234. }
  235.  
  236. /**
  237. * Search for one word in phrase. The search is not case sensitive.
  238. * This method will check that the given goal is not a substring of a longer string
  239. * (so, for example, "I know" does not contain "no"). The search begins at the beginning of the string.
  240. * @param statement the string to search
  241. * @param goal the string to search for
  242. * @return the index of the first occurrence of goal in statement or -1 if it's not found
  243. */
  244. private int findKeyword(String statement, String goal)
  245. {
  246. return findKeyword (statement, goal, 0);
  247. }
  248.  
  249.  
  250.  
  251. /**
  252. * Pick a default response to use if nothing else fits.
  253. * @return a non-committal string
  254. */
  255. private String getRandomResponse()
  256. {
  257. final int NUMBER_OF_RESPONSES = 4;
  258. double r = Math.random();
  259. int whichResponse = (int)(r * NUMBER_OF_RESPONSES);
  260. String response = "";
  261.  
  262. if (whichResponse == 0)
  263. {
  264. response = "Interesting, tell me more.";
  265. }
  266. else if (whichResponse == 1)
  267. {
  268. response = "Hmmm.";
  269. }
  270. else if (whichResponse == 2)
  271. {
  272. response = "Do you really think so?";
  273. }
  274. else if (whichResponse == 3)
  275. {
  276. response = "You don't say.";
  277. }
  278.  
  279. return response;
  280. }
  281.  
  282. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement