Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.14 KB | None | 0 0
  1. /*
  2. * A program to carry on conversations with a human user.
  3. * This is the initial version that:
  4. * <ul><li>
  5. * Uses indexOf to find strings
  6. * </li><li>
  7. * Handles responding to simple words and phrases
  8. * </li></ul>
  9. * This version uses a nested if to handle default responses.
  10. * @author Laurie White
  11. * @version April 2012
  12. */
  13. public class Magpie2
  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.  
  35. statement=statement.toLowerCase();
  36.  
  37. if (isAlone(statement, "no"))
  38. response = "Why so negative?";
  39.  
  40. else if (isAlone(statement, "i want to")){
  41. String str="i want to";
  42. int index=statement.indexOf(str);
  43. int length=statement.length();
  44. int eIndex=index+str.length()-1;
  45. String rest=statement.substring(eIndex+1,length);
  46. response="what would it mean to you to"+rest;
  47. }
  48.  
  49. //extra feature
  50. else if (isAlone(statement, "i don't like")){
  51. String str="i want to";
  52. int index=statement.indexOf(str);
  53. int length=statement.length();
  54. int eIndex=index+str.length()-1;
  55. String rest=statement.substring(eIndex+1,length);
  56. response="why don't you like"+rest;
  57. }
  58.  
  59. else if (isAlone(statement, "mother")
  60. || isAlone(statement, "father")
  61. || isAlone(statement, "brother")
  62. || isAlone(statement, "sister"))
  63. response = "Tell me more about your family.";
  64.  
  65. else if (isAlone(statement, "dog")|| isAlone(statement, "cat"))
  66. response = "Tell me more about your pets.";
  67.  
  68. else if (isAlone(statement, "memmo"))
  69. response = "He seems like a good teacher.";
  70.  
  71. else if (isAlone(statement, "math"))
  72. response = "Do you enjoy doing math?";
  73.  
  74. else if (isAlone(statement, "school"))
  75. response = "School is great for learning.";
  76.  
  77. else if (isAlone(statement, "mad"))
  78. response = "Maybe you should take a walk.";
  79.  
  80. else
  81. response = getRandomResponse();
  82.  
  83. return response;
  84. }
  85.  
  86. /**
  87. * Pick a default response to use if nothing else fits.
  88. * @return a non-committal string
  89. */
  90. private String getRandomResponse()
  91. {
  92. final int NUMBER_OF_RESPONSES = 4;
  93. double r = Math.random();
  94. int whichResponse = (int)(r * NUMBER_OF_RESPONSES);
  95. String response = "";
  96.  
  97. if (whichResponse == 0)
  98. {
  99. response = "Interesting, tell me more.";
  100. }
  101. else if (whichResponse == 1)
  102. {
  103. response = "Hmmm.";
  104. }
  105. else if (whichResponse == 2)
  106. {
  107. response = "Do you really think so?";
  108. }
  109. else if (whichResponse == 3)
  110. {
  111. response = "You don't say.";
  112. }
  113.  
  114. return response;
  115. }
  116.  
  117. public static boolean isAlone (String in, String str){
  118. int index=in.indexOf(str);
  119. int length=in.length();
  120. int eIndex=index+str.length()-1;
  121.  
  122. //input equals keyword
  123. if(in.indexOf(str)>=0){
  124.  
  125. if (in.equals(str))
  126. return true;
  127.  
  128. //space,end of string
  129. else if(eIndex+1==length && in.substring(index-1,index).equals(" "))
  130. return true;
  131.  
  132. //space,space/puntuation
  133. else if (index==0 || in.substring(index-1,index).equals(" ")){
  134. if (in.substring(eIndex+1,eIndex+2).equals(" ")
  135. || in.substring(eIndex+1,eIndex+2).equals(".")
  136. || in.substring(eIndex+1,eIndex+2).equals("?")
  137. || in.substring(eIndex+1,eIndex+2).equals("!"))
  138. return true;
  139.  
  140. else return false;
  141. }
  142. return false;
  143. }
  144. return false;
  145. }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement