Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * The responder class represents a response generator object.
- * It is used to generate an automatic response to an input string.
- *
- * @author Daffa Tristan Firdaus
- * @version 0.1 (05 November 2020)
- */
- import java.util.*;
- public class Responder
- {
- private Random randomGenerator;
- private ArrayList<String> defaultResponses;
- private HashMap<String, String>responseMap;
- /**
- * Construct a Responder - nothing to do
- */
- public Responder()
- {
- randomGenerator = new Random();
- defaultResponses = new ArrayList<String>();
- responseMap = new HashMap<String, String>();
- fillResponses();
- fillDefaultResponses();
- }
- /**
- * Generate a response.
- * @return A string that should be displayed as the
- * response
- */
- public String generateResponse(HashSet<String> words)
- {
- for(String word:words)
- {
- String response = responseMap.get(word);
- if(response != null)
- {
- return response;
- }
- }
- return pickDefaultResponse();
- }
- /**
- * Build up a list of default responses from which we can pick one
- * if we don't know what else to say.
- */
- private void fillResponses()
- {
- responseMap.put("hey", "Hello, How can i help you?");
- responseMap.put("hello", "Hi there! " + "How can i help?");
- responseMap.put("error", "I am sorry to hear that"
- + ", We are trying to fix the problem as soon as possible");
- responseMap.put("nice", "Thank you for your compliment");
- responseMap.put("issue", "I am sorry for your inconvenience"
- + ", What is the issue?");
- responseMap.put("thanks", "It's my pleasure!");
- }
- private void fillDefaultResponses()
- {
- defaultResponses.add("Sorry I can't understand.");
- defaultResponses.add("Please try again.");
- }
- private String pickDefaultResponse()
- {
- int index = randomGenerator.nextInt(defaultResponses.size());
- return defaultResponses.get(index);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment