ace

response4 fixed

ace
Feb 17th, 2010
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.28 KB | None | 0 0
  1. import java.util.HashMap;
  2. import java.util.HashSet;
  3. import java.util.ArrayList;
  4. import java.util.Iterator;
  5. import java.util.Random;
  6. import java.util.Set;
  7. /**
  8. * The responder class represents a response generator object.
  9. * It is used to generate an automatic response, based on specified input.
  10. * Input is presented to the responder as a set of words, and based on those
  11. * words the responder will generate a String that represents the response.
  12. *
  13. * Internally, the reponder uses a HashMap to associate words with response
  14. * strings and a list of default responses. If any of the input words is found
  15. * in the HashMap, the corresponding response is returned. If none of the input
  16. * words is recognized, one of the default responses is randomly chosen.
  17. *
  18. * @version 1.0
  19. * @author Michael Kolling and David J. Barnes
  20. */
  21. public class Responder
  22. {
  23. // Used to map key words to responses.
  24. private HashMap<String, String> responseMap;
  25. // Default responses to use if we don't recognise a word.
  26. private ArrayList<String> defaultResponses;
  27. private Random randomGenerator;
  28. private HashMap<String, String> otherresponseMap;
  29. /**
  30. * Construct a Responder
  31. */
  32. public Responder()
  33. {
  34. responseMap = new HashMap<String, String>();
  35. defaultResponses = new ArrayList<String>();
  36. fillResponseMap();
  37. fillDefaultResponses();
  38. randomGenerator = new Random();
  39. otherresponseMap = new HashMap<String, String>();
  40. fillotherresponseMap();
  41. }
  42.  
  43.  
  44. public String generateOtherResponse(ArrayList<String> words, String trueInput)
  45. {
  46. String response = null;
  47. HashSet<String>otherwords = new HashSet<String>(words);
  48. Iterator<String> otherwordsit = otherwords.iterator();
  49. while (otherwordsit.hasNext())
  50. {
  51. String word = otherwordsit.next();
  52. Set<String> otherkeys = otherresponseMap.keySet();
  53. for(String s : otherkeys)
  54. {
  55. if(word.contains(s))
  56. {
  57. response = otherresponseMap.get(s);
  58. }
  59. }
  60. if(response != null)
  61. {
  62. return response;
  63. }
  64. }
  65. //if we get here, we didn't find an OtherResponse either.
  66. return pickDefaultResponse();
  67. }
  68.  
  69.  
  70. public String generateResponse(ArrayList<String> words, String trueInput)
  71. {
  72. String response = null;
  73. String query = trueInput;
  74. System.out.println(query);
  75. if (query.contains("are you a person?"))
  76. {
  77. response = "Of course I am a person.";
  78. return response;
  79. }
  80.  
  81. if (query.contains ("macintosh") && query.contains ("expensive"))
  82. {
  83. response = "Yes macintoshes are expensive. Ha ha!";
  84. return response;
  85. }
  86. HashSet<String> keywords = new HashSet<String>(words);
  87. Iterator<String> keywordsit = keywords.iterator();
  88. while(keywordsit.hasNext())
  89. {
  90. String word = keywordsit.next();
  91. Set<String> keys = responseMap.keySet();
  92. for(String s : keys)
  93. {
  94. if(word.contains(s))
  95. {
  96. response = responseMap.get(s);
  97. }
  98. }
  99. if(response != null)
  100. {
  101. return response;
  102. }
  103. }
  104. return generateOtherResponse(words, trueInput);
  105. }
  106.  
  107.  
  108. /**
  109. /**
  110. * Enter all the known keywords and their associated responses
  111. * into our response map.
  112. */
  113. private void fillResponseMap()
  114. {
  115. responseMap.put("crash",
  116. "Well, it never crashes on our system. It must have something\n" +
  117. "to do with your system. Tell me more about your configuration.");
  118. responseMap.put("crashes",
  119. "Well, it never crashes on our system. It must have something\n" +
  120. "to do with your system. Tell me more about your configuration.");
  121. responseMap.put("slow",
  122. "I think this has to do with your hardware. Upgrading your processor\n" +
  123. "should solve all performance problems. Have you got a problem with\n" +
  124. "our software?");
  125. responseMap.put("performance",
  126. "Performance was quite adequate in all our tests. Are you running\n" +
  127. "any other processes in the background?");
  128. responseMap.put("bug",
  129. "Well, you know, all software has some bugs. But our software engineers\n" +
  130. "are working very hard to fix them. Can you describe the problem a bit\n" +
  131. "further?");
  132. responseMap.put("buggy",
  133. "Well, you know, all software has some bugs. But our software engineers\n" +
  134. "are working very hard to fix them. Can you describe the problem a bit\n" +
  135. "further?");
  136. responseMap.put("windows",
  137. "This is a known bug to do with the Windows operating system. Please\n" +
  138. "report it to Microsoft. There is nothing we can do about this.");
  139. responseMap.put("macintosh",
  140. "This is a known bug to do with the Mac operating system. Please\n" +
  141. "report it to Apple. There is nothing we can do about this.");
  142. responseMap.put("expensive",
  143. "The cost of our product is quite competitive. Have you looked around\n" +
  144. "and really compared our features?");
  145. responseMap.put("installation",
  146. "The installation is really quite straight forward. We have tons of\n" +
  147. "wizards that do all the work for you. Have you read the installation\n" +
  148. "instructions?");
  149. responseMap.put("memory",
  150. "If you read the system requirements carefully, you will see that the\n" +
  151. "specified memory requirements are 1.5 giga byte. You really should\n" +
  152. "upgrade your memory. Anything else you want to know?");
  153. responseMap.put("linux",
  154. "We take Linux support very seriously. But there are some problems.\n" +
  155. "Most have to do with incompatible glibc versions. Can you be a bit\n" +
  156. "more precise?");
  157. responseMap.put("bluej",
  158. "Ahhh, BlueJ, yes. We tried to buy out those guys long ago, but\n" +
  159. "they simply won't sell... Stubborn people they are. Nothing we can\n" +
  160. "do about it, I'm afraid.");
  161.  
  162. }
  163.  
  164. /**
  165. * Enter other key words to otherwordresponseMap
  166. */
  167. private void fillotherresponseMap()
  168. {
  169.  
  170. otherresponseMap.put ("who",
  171. "We blame Bill Gates!");
  172. otherresponseMap.put ("what",
  173. "Uninstall and then reinstall your operating system.\n" +
  174. "Or switch to linux. It is better anyway!");
  175. otherresponseMap.put ("how",
  176. "Call the geeksquad, they will help you out.");
  177. }
  178.  
  179. /**
  180. * Build up a list of default responses from which we can pick one
  181. * if we don't know what else to say.
  182. */
  183. private void fillDefaultResponses()
  184. {
  185. defaultResponses.add("That sounds odd. Could you describe that problem in more detail?");
  186. defaultResponses.add("No other customer has ever complained about this before. \n" +
  187. "What is your system configuration?");
  188. defaultResponses.add("That sounds interesting. Tell me more...");
  189. defaultResponses.add("I need a bit more information on that.");
  190. defaultResponses.add("Have you checked that you do not have a dll conflict?");
  191. defaultResponses.add("That is explained in the manual. Have you read the manual?");
  192. defaultResponses.add("Your description is a bit wishy-washy. Have you got an expert\n" +
  193. "there with you who could describe this more precisely?");
  194. defaultResponses.add("That's not a bug, it's a feature!");
  195. defaultResponses.add("Could you elaborate on that?");
  196. }
  197.  
  198. /**
  199. * Randomly select and return one of the default responses.
  200. * @return A random default response
  201. */
  202. private String pickDefaultResponse()
  203. {
  204. // Pick a random number for the index in the default response list.
  205. // The number will be between 0 (inclusive) and the size of the list (exclusive).
  206. int index = randomGenerator.nextInt(defaultResponses.size());
  207. return defaultResponses.get(index);
  208. }
  209. }
Add Comment
Please, Sign In to add comment