document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. /**
  2.  * The responder class represents a response generator object.
  3.  * It is used to generate an automatic response to an input string.
  4.  *
  5.  * @author Daffa Tristan Firdaus
  6.  * @version 0.1 (05 November 2020)
  7.  */
  8.  
  9. import java.util.*;
  10. public class Responder
  11. {
  12.     private Random randomGenerator;
  13.     private ArrayList<String> defaultResponses;
  14.     private HashMap<String, String>responseMap;
  15.     /**
  16.      * Construct a Responder - nothing to do
  17.      */
  18.     public Responder()
  19.     {
  20.         randomGenerator = new Random();
  21.         defaultResponses = new ArrayList<String>();
  22.         responseMap = new HashMap<String, String>();
  23.         fillResponses();
  24.         fillDefaultResponses();
  25.     }
  26.     /**
  27.      * Generate a response.
  28.      * @return A string that should be displayed as the
  29.      * response
  30.      */
  31.     public String generateResponse(HashSet<String> words)
  32.     {
  33.         for(String word:words)
  34.         {
  35.             String response = responseMap.get(word);
  36.             if(response != null)
  37.                 {
  38.                     return response;
  39.                 }
  40.         }
  41.         return pickDefaultResponse();
  42.     }
  43.     /**
  44.      * Build up a list of default responses from which we can pick one
  45.      * if we don\'t know what else to say.
  46.      */
  47.     private void fillResponses()
  48.     {
  49.         responseMap.put("hey", "Hello, How can i help you?");
  50.         responseMap.put("hello", "Hi there! " + "How can i help?");
  51.         responseMap.put("error", "I am sorry to hear that"
  52.         + ", We are trying to fix the problem as soon as possible");
  53.         responseMap.put("nice", "Thank you for your compliment");
  54.         responseMap.put("issue", "I am sorry for your inconvenience"
  55.         + ", What is the issue?");
  56.         responseMap.put("thanks", "It\'s my pleasure!");
  57.     }
  58.     private void fillDefaultResponses()
  59.     {
  60.         defaultResponses.add("Sorry I can\'t understand.");
  61.         defaultResponses.add("Please try again.");
  62.     }
  63.     private String pickDefaultResponse()
  64.     {
  65.         int index = randomGenerator.nextInt(defaultResponses.size());
  66.         return defaultResponses.get(index);
  67.     }
  68. }
  69.  
');