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

wholekeytestcode

By: ace on Feb 6th, 2010  |  syntax: None  |  size: 7.96 KB  |  hits: 4  |  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.         while(it.hasNext())
  64.         {
  65.             String word = it.next();
  66.            
  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 = keys.next();
  73.                             if (word.contains(key))
  74.                             {    
  75.            
  76.                                 String response = responseMap.get(word);
  77.                             }  
  78.                         }
  79.                    if(response != null)
  80.             {
  81.                 return response;
  82.             }
  83.      
  84.         }
  85.  
  86.         // If we get here, none of the words from the input line was recognized.
  87.         // In this case we pick one of our default responses (what we say when
  88.         // we cannot think of anything else to say...)
  89.         return pickDefaultResponse();
  90.     }
  91. }
  92.     /**
  93.      * Enter all the known keywords and their associated responses
  94.      * into our response map.
  95.      */
  96.     private void fillResponseMap()
  97.     {
  98.         responseMap.put("crash",
  99.                         "Well, it never crashes on our system. It must have something\n" +
  100.                         "to do with your system. Tell me more about your configuration.");
  101.         responseMap.put("crashes",
  102.                         "Well, it never crashes on our system. It must have something\n" +
  103.                         "to do with your system. Tell me more about your configuration.");
  104.         responseMap.put("slow",
  105.                         "I think this has to do with your hardware. Upgrading your processor\n" +
  106.                         "should solve all performance problems. Have you got a problem with\n" +
  107.                         "our software?");
  108.         responseMap.put("performance",
  109.                         "Performance was quite adequate in all our tests. Are you running\n" +
  110.                         "any other processes in the background?");
  111.         responseMap.put("bug",
  112.                         "Well, you know, all software has some bugs. But our software engineers\n" +
  113.                         "are working very hard to fix them. Can you describe the problem a bit\n" +
  114.                         "further?");
  115.         responseMap.put("buggy",
  116.                         "Well, you know, all software has some bugs. But our software engineers\n" +
  117.                         "are working very hard to fix them. Can you describe the problem a bit\n" +
  118.                         "further?");
  119.         responseMap.put("windows",
  120.                         "This is a known bug to do with the Windows operating system. Please\n" +
  121.                         "report it to Microsoft. There is nothing we can do about this.");
  122.         responseMap.put("macintosh",
  123.                         "This is a known bug to do with the Mac operating system. Please\n" +
  124.                         "report it to Apple. There is nothing we can do about this.");
  125.         responseMap.put("expensive",
  126.                         "The cost of our product is quite competitive. Have you looked around\n" +
  127.                         "and really compared our features?");
  128.         responseMap.put("installation",
  129.                         "The installation is really quite straight forward. We have tons of\n" +
  130.                         "wizards that do all the work for you. Have you read the installation\n" +
  131.                         "instructions?");
  132.         responseMap.put("memory",
  133.                         "If you read the system requirements carefully, you will see that the\n" +
  134.                         "specified memory requirements are 1.5 giga byte. You really should\n" +
  135.                         "upgrade your memory. Anything else you want to know?");
  136.         responseMap.put("linux",
  137.                         "We take Linux support very seriously. But there are some problems.\n" +
  138.                         "Most have to do with incompatible glibc versions. Can you be a bit\n" +
  139.                         "more precise?");
  140.         responseMap.put("bluej",
  141.                         "Ahhh, BlueJ, yes. We tried to buy out those guys long ago, but\n" +
  142.                         "they simply won't sell... Stubborn people they are. Nothing we can\n" +
  143.                         "do about it, I'm afraid.");
  144.     }
  145.  
  146.     /**
  147.      * Build up a list of default responses from which we can pick one
  148.      * if we don't know what else to say.
  149.      */
  150.     private void fillDefaultResponses()
  151.     {
  152.         defaultResponses.add("That sounds odd. Could you describe that problem in more detail?");
  153.         defaultResponses.add("No other customer has ever complained about this before. \n" +
  154.                              "What is your system configuration?");
  155.         defaultResponses.add("That sounds interesting. Tell me more...");
  156.         defaultResponses.add("I need a bit more information on that.");
  157.         defaultResponses.add("Have you checked that you do not have a dll conflict?");
  158.         defaultResponses.add("That is explained in the manual. Have you read the manual?");
  159.         defaultResponses.add("Your description is a bit wishy-washy. Have you got an expert\n" +
  160.                              "there with you who could describe this more precisely?");
  161.         defaultResponses.add("That's not a bug, it's a feature!");
  162.         defaultResponses.add("Could you elaborate on that?");
  163.     }
  164.  
  165.     /**
  166.      * Randomly select and return one of the default responses.
  167.      * @return     A random default response
  168.      */
  169.     private String pickDefaultResponse()
  170.     {
  171.         // Pick a random number for the index in the default response list.
  172.         // The number will be between 0 (inclusive) and the size of the list (exclusive).
  173.         int index = randomGenerator.nextInt(defaultResponses.size());
  174.         return defaultResponses.get(index);
  175.     }
  176. }