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

fixed response4

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