Advertisement
SammonNazgul

Untitled

Dec 13th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.46 KB | None | 0 0
  1. /**
  2. * A program to carry on conversations with a human user.
  3. * This version:
  4. * Uses advanced search for keywords
  5. * Will transform statements as well as react to keywords
  6. * @author Laurie White
  7. * @version April 2012
  8. *
  9. */
  10. public class Magpie4{
  11. /**
  12. * Get a default greeting
  13. * @return a greeting
  14. */
  15. public String getGreeting(){
  16. return "Hello, let's talk.";
  17. }
  18.  
  19. /**
  20. * Gives a response to a user statement
  21. *
  22. * @param statement
  23. * the user statement
  24. * @return a response based on the rules given
  25. */
  26. public String getResponse(String statement){
  27. String response = "";
  28. if (statement.length() == 0){
  29. response = "Say something, please.";
  30. }
  31.  
  32. else if (findKeyword(statement, "no") >= 0){
  33. response = "Why so negative?";
  34. }
  35. else if (findKeyword(statement, "mother") >= 0
  36. || findKeyword(statement, "father") >= 0
  37. || findKeyword(statement, "sister") >= 0
  38. || findKeyword(statement, "brother") >= 0){
  39. response = "Tell me more about your family.";
  40. }
  41.  
  42. // Responses which require transformations
  43. else if (findKeyword(statement, "I want to", 0) >= 0){
  44. response = transformIWantToStatement(statement);
  45. }
  46.  
  47. else{
  48. // Look for a two word (you <something> me) pattern
  49. int psn = findKeyword(statement, "you", 0);
  50.  
  51. if (psn >= 0 && findKeyword(statement, "me", psn) >= 0){
  52. response = transformYouMeStatement(statement);
  53. }
  54. else{
  55. response = getRandomResponse();
  56. }
  57. }
  58. return response;
  59. }
  60.  
  61. /**
  62. * Take a statement with "I want to <something>." and transform it into
  63. * "What would it mean to <something>?"
  64. * @param statement the user statement, assumed to contain "I want to"
  65. * @return the transformed statement
  66. */
  67. private String transformIWantToStatement(String statement){
  68. // Remove the final period, if there is one
  69. statement = statement.trim();
  70. String lastChar = statement.substring(statement.length() - 1);
  71. if (lastChar.equals(".")){
  72. statement = statement.substring(0, statement.length() - 1);
  73. }
  74.  
  75. // Chop off "I want" to and add the rest to "What would it mean to"
  76. int psn = findKeyword (statement, "I want to", 0);
  77. String restOfStatement = statement.substring(psn + 9).trim();
  78. return "What would it mean to " + restOfStatement + "?";
  79. }
  80.  
  81. private String transformIWantStatement(String statement
  82.  
  83. /**
  84. * Take a statement with "you <something> me" and transform it into
  85. * "What makes you think that I <something> you?"
  86. * @param statement the user statement, assumed to contain "you" followed by "me"
  87. * @return the transformed statement
  88. */
  89. private String transformYouMeStatement(String statement)
  90. {
  91. // Remove the final period, if there is one
  92. statement = statement.trim();
  93. String lastChar = statement.substring(statement.length() - 1);
  94. if (lastChar.equals(".")){
  95. statement = statement.substring(0, statement.length() - 1);
  96. }
  97.  
  98. // Find the location of the words "you" and "me"
  99. int psnOfYou = findKeyword (statement, "you", 0);
  100. int psnOfMe = findKeyword (statement, "me", psnOfYou + 3);
  101.  
  102. // Chop out restOfStatement which is the words between "you" and "me"
  103. String restOfStatement = statement.substring(psnOfYou + 3, psnOfMe).trim();
  104. return "What makes you think that I " + restOfStatement + " you?";
  105. }
  106.  
  107.  
  108.  
  109.  
  110.  
  111. /**
  112. * Search for one word in phrase. The search is not case
  113. * sensitive. This method will check that the given goal
  114. * is not a substring of a longer string (so, for
  115. * example, "I know" does not contain "no").
  116. *
  117. * @param statement
  118. * the string to search
  119. * @param goal
  120. * the string to search for
  121. * @param startPos
  122. * the character of the string to begin the
  123. * search at
  124. * @return the index of the first occurrence of goal in
  125. * statement or -1 if it's not found
  126. */
  127. private int findKeyword(String statement, String goal, int startPos){
  128. String phrase = statement.trim().toLowerCase();
  129. goal = goal.toLowerCase();
  130.  
  131. // The only change to incorporate the startPos is in
  132. // the line below
  133. int psn = phrase.indexOf(goal, startPos);
  134.  
  135. // Refinement--make sure the goal isn't part of a word
  136. while (psn >= 0){
  137. // Find the string of length 1 before and after the word
  138. String before = " ", after = " ";
  139. if (psn > 0){
  140. before = phrase.substring(psn - 1, psn);
  141. }
  142. if (psn + goal.length() < phrase.length()){
  143. after = phrase.substring(psn + goal.length(), psn + goal.length() + 1);
  144. }
  145.  
  146. // If before and after aren't letters, we've
  147. // found the word
  148. if (((before.compareTo("a") < 0) || (before.compareTo("z") > 0)) // before is not a letter
  149. && ((after.compareTo("a") < 0) || (after.compareTo("z") > 0))){ // after is not a letter
  150. return psn;
  151. }
  152.  
  153. // The last position didn't work, so let's find
  154. // the next, if there is one.
  155. psn = phrase.indexOf(goal, psn + 1);
  156.  
  157. }
  158.  
  159. return -1;
  160. }
  161.  
  162. /**
  163. * Search for one word in phrase. The search is not case sensitive.
  164. * This method will check that the given goal is not a substring of a longer string
  165. * (so, for example, "I know" does not contain "no"). The search begins at the beginning of the string.
  166. * @param statement the string to search
  167. * @param goal the string to search for
  168. * @return the index of the first occurrence of goal in statement or -1 if it's not found
  169. */
  170. private int findKeyword(String statement, String goal){
  171. return findKeyword (statement, goal, 0);
  172. }
  173.  
  174.  
  175.  
  176. /**
  177. * Pick a default response to use if nothing else fits.
  178. * @return a non-committal string
  179. */
  180. private String getRandomResponse(){
  181. final int NUMBER_OF_RESPONSES = 4;
  182. double r = Math.random();
  183. int whichResponse = (int)(r * NUMBER_OF_RESPONSES);
  184. String response = "";
  185.  
  186. if (whichResponse == 0){
  187. response = "Interesting, tell me more.";
  188. }
  189. else if (whichResponse == 1){
  190. response = "Hmmm.";
  191. }
  192. else if (whichResponse == 2){
  193. response = "Do you really think so?";
  194. }
  195. else if (whichResponse == 3){
  196. response = "You don't say.";
  197. }
  198.  
  199. return response;
  200. }
  201.  
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement