/** * Returns the response to a non-internal input, using a Responder. * * @param input the "non-internal" (possibly multi-sentence, non-substituted) input * @param userid the userid for whom the response will be generated * @param botid the botid from which to get the response * @param responder the Responder who cares about this response */ public static synchronized String getResponse( String input, String userid, String botid, Responder responder) { // Get the specified bot object. Bot bot = Bots.getBot(botid); // Split sentences (after performing substitutions and responder pre-processing). ArrayList sentenceList = bot.sentenceSplit(bot.applyInputSubstitutions(responder.preprocess(input, HOST_NAME))); // Get an iterator on the replies. Iterator replies = getReplies(sentenceList, userid, botid).iterator(); // Start by assuming an empty response. String response = EMPTY_STRING; // Get an iterator over the input sentences. Iterator sentences = sentenceList.iterator(); // For each input sentence... while (sentences.hasNext()) { // ...ask the responder to append the reply to the response, and accumulate the result. response = responder.append((String) sentences.next(), (String) replies.next(), response); } // Log the response. responder.log(input, response, HOST_NAME, userid, botid); // Finally, ask the responder to postprocess the response, and return the result. response = responder.postprocess(response); // Return the response (may be just EMPTY_STRING!) return response; }
/** * Produces a response to an "internal" input sentence -- i.e., an input that has been * produced by a <code>srai</code>. * * <p>The main differences between this and {@link * #getResponse(String,String,TemplateParser,boolean,Responder)} are that this method takes an * already-existing <code>TemplateParser</code>, <i>doesn't</i> take a <code>Responder</code>, and * assumes that the inputs have already been normalized. * * @see {@link #getResponse(String,String,TemplateParser,boolean,Responder)} * @param input the input sentence * @param userid the userid requesting the response * @param botid the botid from which to get the response * @param parser the parser object to update when generating the response */ public static String getInternalResponse( String input, String userid, String botid, TemplateParser parser) { // Get the requested bot. Bot bot = Bots.getBot(botid); // Ready the that and topic predicates for constructing the match path. ArrayList thatSentences = bot.sentenceSplit(PredicateMaster.get(THAT, 1, userid, botid)); String that = InputNormalizer.patternFitIgnoreCase((String) thatSentences.get(thatSentences.size() - 1)); if (that.equals(EMPTY_STRING) || that.equals(PREDICATE_EMPTY_DEFAULT)) { that = ASTERISK; } String topic = InputNormalizer.patternFitIgnoreCase(PredicateMaster.get(TOPIC, userid, botid)); if (topic.equals(EMPTY_STRING) || topic.equals(PREDICATE_EMPTY_DEFAULT)) { topic = ASTERISK; } return getMatchResult(input, that, topic, userid, botid, parser); }
/** * Gets the list of replies to some input sentences. Assumes that the sentences have already had * all necessary pre-processing and substitutions performed. * * @param sentenceList the input sentences * @param userid the userid requesting the replies * @param botid * @return the list of replies to the input sentences */ private static ArrayList getReplies(ArrayList sentenceList, String userid, String botid) { // All replies will be assembled in this ArrayList. ArrayList replies = new ArrayList(sentenceList.size()); // Get the requested bot. Bot bot = Bots.getBot(botid); // Ready the that and topic predicates for constructing the match path. ArrayList thatSentences = bot.sentenceSplit(PredicateMaster.get(THAT, 1, userid, botid)); String that = InputNormalizer.patternFitIgnoreCase((String) thatSentences.get(thatSentences.size() - 1)); if (that.equals(EMPTY_STRING) || that.equals(PREDICATE_EMPTY_DEFAULT)) { that = ASTERISK; } String topic = InputNormalizer.patternFitIgnoreCase(PredicateMaster.get(TOPIC, userid, botid)); if (topic.equals(EMPTY_STRING) || topic.equals(PREDICATE_EMPTY_DEFAULT)) { topic = ASTERISK; } Iterator sentences = sentenceList.iterator(); // We might use this to track matching statistics. long time = 0; // If match trace info is on, mark the time just before matching starts. if (SHOW_MATCH_TRACE) { time = System.currentTimeMillis(); } // Get a reply for each sentence. while (sentences.hasNext()) { replies.add(getReply((String) sentences.next(), that, topic, userid, botid)); } // Increment the (static) response count. responseCount++; ; // If match trace info is on, produce statistics about the response time. if (SHOW_MATCH_TRACE) { // Mark the time that processing is finished. time = System.currentTimeMillis() - time; // Calculate the average response time. totalTime += time; avgResponseTime = (float) totalTime / (float) responseCount; Trace.userinfo( RESPONSE_SPACE + responseCount + SPACE_IN_SPACE + time + MS_AVERAGE + avgResponseTime + MS); } // Invoke targeting if appropriate. if (responseCount % TARGET_SKIP == 0) { if (USE_TARGETING) { Graphmaster.checkpoint(); } } // If no replies, return an empty string. if (replies.size() == 0) { replies.add(EMPTY_STRING); } return replies; }