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