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

otherresponsemap4

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