Pastebin launched a little side project called HostCabi.net, check it out ;-)Don't like ads? PRO users don't see any ads ;-)
Guest

acesResponder

By: ace on Feb 6th, 2010  |  syntax: None  |  size: 7.57 KB  |  hits: 10  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  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.  
  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.  
  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.     }
  40.  
  41.     /**
  42.      * Generate a response from a given set of input words.
  43.      *
  44.      * @param words  A set of words entered by the user
  45.      * @return       A string that should be displayed as the response
  46.      */
  47.     public String generateResponse(HashSet<String> words)
  48.     {
  49.         Iterator<String> it = words.iterator();
  50.         while(it.hasNext())
  51.         {
  52.             String word = it.next();
  53.             String response = null;
  54.             ArrayList<String> keys = new ArrayList<String>();
  55.             keys.addAll(responseMap.keySet());
  56.             Iterator<String> keysIt = keys.iterator();
  57.             while(keysIt.hasNext())
  58.             {
  59.                  String key = keysIt.next();
  60.                  if (word.contains(key))
  61.                  {    
  62.                      response = responseMap.get(word);
  63.                  }
  64.             }
  65.             if(response != null)
  66.             {
  67.                  return response;
  68.             }
  69.      
  70.         }
  71. /*    public String generateResponse(HashSet<String> words)
  72.     {
  73.         Iterator<String> it = words.iterator();
  74.         while(it.hasNext()) {
  75.             String word = it.next();
  76.             String response = null;
  77.             ArrayList<String> keys = new ArrayList<String>();
  78.             keys.addAll(responseMap.keySet());
  79.             for(String s : keys){
  80.                 if(word.contains(s)){
  81.                     response = responseMap.get(s);
  82.                 }
  83.             }
  84.             if(response != null) {
  85.                 return response;
  86.             }
  87.         }*/
  88.         // If we get here, none of the words from the input line was recognized.
  89.         // In this case we pick one of our default responses (what we say when
  90.         // we cannot think of anything else to say...)
  91.         return pickDefaultResponse();
  92.     }
  93.  
  94.     /**
  95.      * Enter all the known keywords and their associated responses
  96.      * into our response map.
  97.      */
  98.     private void fillResponseMap()
  99.     {
  100.         responseMap.put("crash",
  101.                         "Well, it never crashes on our system. It must have something\n" +
  102.                         "to do with your system. Tell me more about your configuration.");
  103.         responseMap.put("slow",
  104.                         "I think this has to do with your hardware. Upgrading your processor\n" +
  105.                         "should solve all performance problems. Have you got a problem with\n" +
  106.                         "our software?");
  107.         responseMap.put("perform",
  108.                         "Performance was quite adequate in all our tests. Are you running\n" +
  109.                         "any other processes in the background?");
  110.         responseMap.put("bug",
  111.                         "Well, you know, all software has some bugs. But our software engineers\n" +
  112.                         "are working very hard to fix them. Can you describe the problem a bit\n" +
  113.                         "further?");
  114.         responseMap.put("windows",
  115.                         "This is a known bug to do with the Windows operating system. Please\n" +
  116.                         "report it to Microsoft. There is nothing we can do about this.");
  117.         responseMap.put("mac",
  118.                         "This is a known bug to do with the Mac operating system. Please\n" +
  119.                         "report it to Apple. There is nothing we can do about this.");
  120.         responseMap.put("expensive",
  121.                         "The cost of our product is quite competitive. Have you looked around\n" +
  122.                         "and really compared our features?");
  123.         responseMap.put("install",
  124.                         "The installation is really quite straight forward. We have tons of\n" +
  125.                         "wizards that do all the work for you. Have you read the installation\n" +
  126.                         "instructions?");
  127.         responseMap.put("memory",
  128.                         "If you read the system requirements carefully, you will see that the\n" +
  129.                         "specified memory requirements are 1.5 giga byte. You really should\n" +
  130.                         "upgrade your memory. Anything else you want to know?");
  131.         responseMap.put("linux",
  132.                         "We take Linux support very seriously. But there are some problems.\n" +
  133.                         "Most have to do with incompatible glibc versions. Can you be a bit\n" +
  134.                         "more precise?");
  135.         responseMap.put("bluej",
  136.                         "Ahhh, BlueJ, yes. We tried to buy out those guys long ago, but\n" +
  137.                         "they simply won't sell... Stubborn people they are. Nothing we can\n" +
  138.                         "do about it, I'm afraid.");
  139.     }
  140.  
  141.     /**
  142.      * Build up a list of default responses from which we can pick one
  143.      * if we don't know what else to say.
  144.      */
  145.     private void fillDefaultResponses()
  146.     {
  147.         defaultResponses.add("That sounds odd. Could you describe that problem in more detail?");
  148.         defaultResponses.add("No other customer has ever complained about this before. \n" +
  149.                              "What is your system configuration?");
  150.         defaultResponses.add("That sounds interesting. Tell me more...");
  151.         defaultResponses.add("I need a bit more information on that.");
  152.         defaultResponses.add("Have you checked that you do not have a dll conflict?");
  153.         defaultResponses.add("That is explained in the manual. Have you read the manual?");
  154.         defaultResponses.add("Your description is a bit wishy-washy. Have you got an expert\n" +
  155.                              "there with you who could describe this more precisely?");
  156.         defaultResponses.add("That's not a bug, it's a feature!");
  157.         defaultResponses.add("Could you elaborate on that?");
  158.     }
  159.  
  160.     /**
  161.      * Randomly select and return one of the default responses.
  162.      * @return     A random default response
  163.      */
  164.     private String pickDefaultResponse()
  165.     {
  166.         // Pick a random number for the index in the default response list.
  167.         // The number will be between 0 (inclusive) and the size of the list (exclusive).
  168.         int index = randomGenerator.nextInt(defaultResponses.size());
  169.         return defaultResponses.get(index);
  170.     }
  171. }