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

otherresponsemap2

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