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

responder6

By: ace on Feb 10th, 2010  |  syntax: None  |  size: 9.31 KB  |  hits: 8  |  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.             String word = it.next();
  52.             String response = responseMap.get(word);
  53.             if(response != null) {
  54.                 return response;
  55.            
  56.         }
  57.         }*/
  58.          
  59.        
  60.       /* public String generateResponse(HashSet<String> words)
  61.     {
  62.         Iterator<String> it = words.iterator();
  63.         String response = null;
  64.         while(it.hasNext())
  65.             {
  66.                 String word = it.next();
  67.                 ArrayList<String> keys = new ArrayList<String>();
  68.                 keys.addAll(responseMap.keySet());
  69.                 Iterator<String> keyIT = keys.iterator();  
  70.                 while(keyIT.hasNext())
  71.                         {
  72.                             String key = keyIT.next();
  73.                             if (word.contains(key))
  74.                                 {    
  75.            
  76.                                 response = responseMap.get(word);
  77.                                 }  
  78.                         }
  79.                    if(response != null)
  80.             {
  81.                 return response;
  82.             }
  83.      
  84.         }
  85.        
  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.  
  96.  
  97.   public String generateResponse(ArrayList<String> words)
  98.     {
  99.             String response = null;
  100.             /*String personquery = trueInput;
  101.             System.out.println(personquery);
  102.             if (personquery.contains("Are you a person?"))
  103.             {
  104.                 response = "Of course I am a person.";
  105.                 return response;
  106.             }*/
  107.            
  108.        
  109.         for(String word : words){
  110.            
  111.         }
  112.        
  113.        
  114.         HashSet<String> keywords = new HashSet<String>(words);
  115.         Iterator<String> it = keywords.iterator();        
  116.         while(it.hasNext()) {
  117.             String word = it.next();
  118.             ArrayList<String> keys = new ArrayList<String>();
  119.             keys.addAll(responseMap.keySet());
  120.             for(String s : keys){
  121.                 if(word.contains(s)){
  122.                     response = responseMap.get(s);
  123.                 }
  124.             }
  125.             if(response != null) {
  126.                 return response;
  127.             }
  128.         }
  129.         // If we get here, none of the words from the input line was recognized.
  130.         // In this case we pick one of our default responses (what we say when
  131.         // we cannot think of anything else to say...)
  132.         return pickDefaultResponse();
  133.     }
  134.  
  135.    
  136.  
  137.     /**
  138.     /**
  139.      * Enter all the known keywords and their associated responses
  140.      * into our response map.
  141.      */
  142.     private void fillResponseMap()
  143.     {
  144.         responseMap.put("crash",
  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("crashes",
  148.                         "Well, it never crashes on our system. It must have something\n" +
  149.                         "to do with your system. Tell me more about your configuration.");
  150.         responseMap.put("slow",
  151.                         "I think this has to do with your hardware. Upgrading your processor\n" +
  152.                         "should solve all performance problems. Have you got a problem with\n" +
  153.                         "our software?");
  154.         responseMap.put("performance",
  155.                         "Performance was quite adequate in all our tests. Are you running\n" +
  156.                         "any other processes in the background?");
  157.         responseMap.put("bug",
  158.                         "Well, you know, all software has some bugs. But our software engineers\n" +
  159.                         "are working very hard to fix them. Can you describe the problem a bit\n" +
  160.                         "further?");
  161.         responseMap.put("buggy",
  162.                         "Well, you know, all software has some bugs. But our software engineers\n" +
  163.                         "are working very hard to fix them. Can you describe the problem a bit\n" +
  164.                         "further?");
  165.         responseMap.put("windows",
  166.                         "This is a known bug to do with the Windows operating system. Please\n" +
  167.                         "report it to Microsoft. There is nothing we can do about this.");
  168.         responseMap.put("macintosh",
  169.                         "This is a known bug to do with the Mac operating system. Please\n" +
  170.                         "report it to Apple. There is nothing we can do about this.");
  171.         responseMap.put("expensive",
  172.                         "The cost of our product is quite competitive. Have you looked around\n" +
  173.                         "and really compared our features?");
  174.         responseMap.put("installation",
  175.                         "The installation is really quite straight forward. We have tons of\n" +
  176.                         "wizards that do all the work for you. Have you read the installation\n" +
  177.                         "instructions?");
  178.         responseMap.put("memory",
  179.                         "If you read the system requirements carefully, you will see that the\n" +
  180.                         "specified memory requirements are 1.5 giga byte. You really should\n" +
  181.                         "upgrade your memory. Anything else you want to know?");
  182.         responseMap.put("linux",
  183.                         "We take Linux support very seriously. But there are some problems.\n" +
  184.                         "Most have to do with incompatible glibc versions. Can you be a bit\n" +
  185.                         "more precise?");
  186.         responseMap.put("bluej",
  187.                         "Ahhh, BlueJ, yes. We tried to buy out those guys long ago, but\n" +
  188.                         "they simply won't sell... Stubborn people they are. Nothing we can\n" +
  189.                         "do about it, I'm afraid.");
  190.     }
  191.  
  192.     /**
  193.      * Build up a list of default responses from which we can pick one
  194.      * if we don't know what else to say.
  195.      */
  196.     private void fillDefaultResponses()
  197.     {
  198.         defaultResponses.add("That sounds odd. Could you describe that problem in more detail?");
  199.         defaultResponses.add("No other customer has ever complained about this before. \n" +
  200.                              "What is your system configuration?");
  201.         defaultResponses.add("That sounds interesting. Tell me more...");
  202.         defaultResponses.add("I need a bit more information on that.");
  203.         defaultResponses.add("Have you checked that you do not have a dll conflict?");
  204.         defaultResponses.add("That is explained in the manual. Have you read the manual?");
  205.         defaultResponses.add("Your description is a bit wishy-washy. Have you got an expert\n" +
  206.                              "there with you who could describe this more precisely?");
  207.         defaultResponses.add("That's not a bug, it's a feature!");
  208.         defaultResponses.add("Could you elaborate on that?");
  209.     }
  210.  
  211.     /**
  212.      * Randomly select and return one of the default responses.
  213.      * @return     A random default response
  214.      */
  215.     private String pickDefaultResponse()
  216.     {
  217.         // Pick a random number for the index in the default response list.
  218.         // The number will be between 0 (inclusive) and the size of the list (exclusive).
  219.         int index = randomGenerator.nextInt(defaultResponses.size());
  220.         return defaultResponses.get(index);
  221.     }
  222. }