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 w notes

By: ace on Feb 17th, 2010  |  syntax: None  |  size: 11.53 KB  |  hits: 7  |  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>(); //Here we make a new HashSet to contain the *single* words which are also included in *combinations*
  41.         questionWordResponses = new HashMap<String, String>(); //here we make a new HashMap to contain the responses to the question words (who, what, why, etc)
  42.         fillResponseMaps();//we fill all of them in the same method
  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. /* THIS IS THE "ARE YOU A PERSON" SECTION*/
  56.             String response = null;
  57.             String personquery = trueInput;
  58.             if (personquery.contains("are you a person?"))
  59.             {
  60.                 response = "Of course I am a person.";
  61.                 return response;
  62.             }
  63.  
  64. /* THIS IS THE SINGLE-WORD RESPONSE SECTION, WITH A LITTLE CODE FOR COMBINATION OF TWO WORDS*/            
  65.         //first we check for specific responses
  66.         Iterator<String> it = words.iterator();        
  67.         Set<String> keys = responseMap.keySet();
  68.         ArrayList<String> hits = new ArrayList<String>();//"hits" is a list of all the words which match our keys
  69.         while(it.hasNext()) {
  70.             String word = it.next();
  71.             for(String s : keys){
  72.                 if(word.contains(s)){
  73.                     //if the word is one of our key words, then we check to see if it's one of our combo-keywords too
  74.                     //we do this by checking to see if it's in the comboKeys set.
  75.                     if(comboKeys.contains(s)){
  76.                         hits.add(s); //if so we add it to the Hits list
  77.                     }
  78.                     response = responseMap.get(s);//we go ahead and set response to the single-word response
  79.                 }
  80.             }
  81.         }
  82.  
  83. /* THIS SECTION DEALS WITH COMBINATIONS OF 2 WORDS */
  84.         //if hits has more than one entry, we have a candidate for a
  85.         //combination response
  86.         String combo = "";
  87.         if(hits.size() > 1){
  88.             Collections.sort(hits);//this alphabetizes the hits list.
  89.             for(String s : hits){
  90.                 //since the list is alphabetized, we can be certain that the combo words will be added in alphabetical order
  91.                 combo += s;
  92.             }
  93.             if(keys.contains(combo)){//now we check to see if the combination of the two words is contained in the keys
  94.                 response = responseMap.get(combo);//if so, we CHANGE the response to the combo response
  95.             }
  96.         }
  97.         //if we've managed to get a response by now, return it.
  98.             if(response != null) {
  99.                 return response;
  100.         }
  101.        
  102. /*  THIS IS THE WHO, WHAT, WHY, ETC. SECTION*/
  103.         //if we haven't returned a response yet, try the question words
  104.         Set<String> qKeys = questionWordResponses.keySet();//make a key set
  105.         //for each word in the ArrayList words
  106.         for(String word : words){
  107.             //if the Set qKeys contains the word
  108.             if(qKeys.contains(word)){
  109.                 //set the response equal to the string from the questionWordResponses map
  110.                 response = questionWordResponses.get(word);
  111.             }
  112.         }
  113.         //if we've managed to get a response, return it
  114.         if(response != null) {
  115.             return response;
  116.         }      
  117.  
  118. /* IF ALL ELSE FAILS, SEND A DEFAULT RESPONSE */
  119.         return pickDefaultResponse();
  120.        
  121.         /*
  122.          * Suggested test phrases:
  123.          * "Your software is buggy."
  124.          * "I have a very expensive macintosh!"
  125.          * "Why is the sky blue?"
  126.          * "Are you a person?"
  127.          * "something you won't recognize"
  128.          * "bye"
  129.          */
  130.     }
  131.  
  132.    
  133.  
  134.     /**
  135.     /**
  136.      * Enter all the known keywords and their associated responses
  137.      * into our response map.
  138.      */
  139.     private void fillResponseMaps()
  140.     {
  141.         responseMap.put("crash",
  142.                         "Well, it never crashes on our system. It must have something\n" +
  143.                         "to do with your system. Tell me more about your configuration.");
  144.         responseMap.put("crashes",
  145.                         "Well, it never crashes on our system. It must have something\n" +
  146.                         "to do with your system. Tell me more about your configuration.");
  147.         responseMap.put("slow",
  148.                         "I think this has to do with your hardware. Upgrading your processor\n" +
  149.                         "should solve all performance problems. Have you got a problem with\n" +
  150.                         "our software?");
  151.         responseMap.put("performance",
  152.                         "Performance was quite adequate in all our tests. Are you running\n" +
  153.                         "any other processes in the background?");
  154.         responseMap.put("bug",
  155.                         "Well, you know, all software has some bugs. But our software engineers\n" +
  156.                         "are working very hard to fix them. Can you describe the problem a bit\n" +
  157.                         "further?");
  158.         responseMap.put("buggy",
  159.                         "Well, you know, all software has some bugs. But our software engineers\n" +
  160.                         "are working very hard to fix them. Can you describe the problem a bit\n" +
  161.                         "further?");
  162.         responseMap.put("windows",
  163.                         "This is a known bug to do with the Windows operating system. Please\n" +
  164.                         "report it to Microsoft. There is nothing we can do about this.");
  165.         responseMap.put("macintosh",
  166.                         "This is a known bug to do with the Mac operating system. Please\n" +
  167.                         "report it to Apple. There is nothing we can do about this.");
  168.         responseMap.put("expensive",
  169.                         "The cost of our product is quite competitive. Have you looked around\n" +
  170.                         "and really compared our features?");
  171.         responseMap.put("installation",
  172.                         "The installation is really quite straight forward. We have tons of\n" +
  173.                         "wizards that do all the work for you. Have you read the installation\n" +
  174.                         "instructions?");
  175.         responseMap.put("memory",
  176.                         "If you read the system requirements carefully, you will see that the\n" +
  177.                         "specified memory requirements are 1.5 giga byte. You really should\n" +
  178.                         "upgrade your memory. Anything else you want to know?");
  179.         responseMap.put("linux",
  180.                         "We take Linux support very seriously. But there are some problems.\n" +
  181.                         "Most have to do with incompatible glibc versions. Can you be a bit\n" +
  182.                         "more precise?");
  183.         responseMap.put("bluej",
  184.                         "Ahhh, BlueJ, yes. We tried to buy out those guys long ago, but\n" +
  185.                         "they simply won't sell... Stubborn people they are. Nothing we can\n" +
  186.                         "do about it, I'm afraid.");
  187.         responseMap.put("drepper",
  188.                         "Ulrich Drepper is an embarassment to free software.");
  189.         responseMap.put("glibc",
  190.                         "Oh, glibc is one of the key components of Linux.");
  191.         responseMap.put("drepperglibc",
  192.                         "Asking Drepper about anything is like talking to a very large brick \n" +
  193.                         "wall that would be terribly pleased to fall on you.");
  194.         responseMap.put("expensivemacintosh",
  195.                         "You do generally get your money's worth when you buy a Mac. \n" +
  196.                         "Still, that doesn't mean they're completely problem-free.");
  197.                        
  198.         comboKeys.add("macintosh");
  199.         comboKeys.add("expensive");
  200.         comboKeys.add("drepper");
  201.         comboKeys.add("glibc");
  202.         questionWordResponses.put("who",
  203.                         "Its probably Bill Gates' fault.");
  204.         questionWordResponses.put("how",
  205.                         "I believe you'll find instructions for how to do that \n" +
  206.                         "in our instruction manual.");
  207.         questionWordResponses.put("why",
  208.                         "Hey, in my line of work, you don't worry about 'why'.");
  209.     }
  210.  
  211.     /**
  212.      * Build up a list of default responses from which we can pick one
  213.      * if we don't know what else to say.
  214.      */
  215.     private void fillDefaultResponses()
  216.     {
  217.         defaultResponses.add("That sounds odd. Could you describe that problem in more detail?");
  218.         defaultResponses.add("No other customer has ever complained about this before. \n" +
  219.                              "What is your system configuration?");
  220.         defaultResponses.add("That sounds interesting. Tell me more...");
  221.         defaultResponses.add("I need a bit more information on that.");
  222.         defaultResponses.add("Have you checked that you do not have a dll conflict?");
  223.         defaultResponses.add("That is explained in the manual. Have you read the manual?");
  224.         defaultResponses.add("Your description is a bit wishy-washy. Have you got an expert\n" +
  225.                              "there with you who could describe this more precisely?");
  226.         defaultResponses.add("That's not a bug, it's a feature!");
  227.         defaultResponses.add("Could you elaborate on that?");
  228.     }
  229.  
  230.     /**
  231.      * Randomly select and return one of the default responses.
  232.      * @return     A random default response
  233.      */
  234.     private String pickDefaultResponse()
  235.     {
  236.         // Pick a random number for the index in the default response list.
  237.         // The number will be between 0 (inclusive) and the size of the list (exclusive).
  238.         int index = randomGenerator.nextInt(defaultResponses.size());
  239.         return defaultResponses.get(index);
  240.     }
  241. }