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

Aces two words 5-39

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