document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1.  
  2. /**
  3.  * class Responder merepresentasikan sebuah respon-generator object.
  4.  * Digunakan untuk generate sebuah respon otomatis ke input string.
  5.  *
  6.  * @author Daffa Amanullah Setyawan
  7.  * @version 0.1 11 November 2020
  8.  */
  9.  
  10. import java.util.*;
  11. public class Responder
  12. {
  13.         private Random randomGenerator;
  14.         private ArrayList<String> defaultResponses;
  15.         private HashMap<String, String> responseMap;
  16.        
  17.         /**
  18.          * Membuat responder.
  19.          */
  20.         public Responder()
  21.         {
  22.             defaultResponses = new ArrayList<String>();
  23.             responseMap = new HashMap<String, String>();
  24.             fillResponses();
  25.             fillDefaultResponses();
  26.             randomGenerator = new Random();
  27.         }
  28.        
  29.         /**
  30.          * Generate sebuah respon.
  31.          * @return Sebuah string yang seharusnya ditampilkan sebagai respon.
  32.          */
  33.         public String generateResponse(HashSet<String> words)
  34.         {
  35.             for(String word : words)
  36.             {
  37.                 String response = responseMap.get(word);
  38.                 if(response != null)
  39.                 {
  40.                     return response;
  41.                 }
  42.             }
  43.             return pickDefaultResponse();
  44.         }
  45.        
  46.         /**
  47.          * Build up a list of default responses from which we can pick one
  48.          * if we don\'t know what else to say.
  49.          */
  50.         private void fillResponses()
  51.         {
  52.             responseMap.put("hi", "Hello, It is a pleasure to meet you.");
  53.             responseMap.put("you?", "I am a bot, here to help you");
  54.             responseMap.put("hello", "Hi there! How are you today?");
  55.             responseMap.put("good", "I see that you are well.");
  56.             responseMap.put("happy", "Wonderful, I also happy to hear that.");
  57.             responseMap.put("bad", "Something bugging your mind?");
  58.             responseMap.put("nothing", "I see, but please remember that I will always open to you.");
  59.             responseMap.put("sad", "I hope yo can get better.");    
  60.             responseMap.put("thank", "You are welcome.");
  61.         }
  62.        
  63.         private void fillDefaultResponses()
  64.         {
  65.             defaultResponses.add("Sorry I can\'t understand what you are saying.");
  66.         }
  67.        
  68.         private String pickDefaultResponse()
  69.         {
  70.             int index = randomGenerator.nextInt(defaultResponses.size());
  71.             return defaultResponses.get(index);
  72.         }
  73.    
  74. }
');