void Eliza::handle_user_repetition() { ++m_nUserRepeatCount; if(m_nUserRepeatCount > 1) { response_list = inputRepeat; } else { find_response(); } }
void ofxEliza::handle_short_input() { ++m_nShortInputCount; if(m_nShortInputCount > 2) { response_list = shortInput; } else { find_response(); } }
string ofxEliza::ask(string _inputString){ // gets input from the user save_prev_input(); m_sInput = _inputString; saveLog("USER"); // Finds and display a response // to the current input of the user. // removes punctuation from the input // and do some more preprocessing if(m_sInput.length() > 0) { tok.cleanString(m_sInput, " ?!,;"); trimRight(m_sInput, '.'); UpperCase(m_sInput); m_sInput.insert(0, " "); m_sInput.append(" "); } save_prev_responses(); save_prev_response(); if(null_input()) { handle_null_input(); } else if(user_repeat()) { handle_user_repetition(); } else if(short_input()) { handle_short_input(); } else { reset_repeat_count(); reset_short_input_count(); find_response(); } select_response(); preProcessResponse(); handle_repetition(); saveLog("ELIZA"); return m_sResponse; }
// these function finds and display a response // to the current input of the user. void Eliza::respond() { preProcessInput(); save_prev_responses(); save_prev_response(); if(null_input()) { handle_null_input(); } else if(user_repeat()) { handle_user_repetition(); } else { reset_repeat_count(); find_response(); } select_response(); preProcessResponse(); check_quit_message(); handle_repetition(); print_response(); save_log("ELIZA"); }
/* Return values: * * -2: timeout * -1: read error or response not found * 0...N: response index in **needles array */ static int modem_wait_reply (int fd, guint32 timeout_secs, const char **needles, const char **terminators, int *out_terminator, char **out_response) { char buf[SERIAL_BUF_SIZE + 1]; int reply_index = -1, bytes_read; GString *result = g_string_sized_new (RESPONSE_LINE_MAX * 2); time_t end; const char *response = NULL; gboolean done = FALSE; *out_terminator = -1; end = time (NULL) + timeout_secs; do { bytes_read = read (fd, buf, SERIAL_BUF_SIZE); if (bytes_read < 0 && errno != EAGAIN) { g_string_free (result, TRUE); g_printerr ("read error: %d\n", errno); return -1; } if (bytes_read == 0) break; /* EOF */ else if (bytes_read > 0) { char **lines, **iter, *tmp; buf[bytes_read] = 0; g_string_append (result, buf); verbose ("Got: '%s'", result->str); lines = g_strsplit_set (result->str, "\n\r", 0); /* Find response terminators */ for (iter = lines; *iter && !done; iter++) { tmp = g_strstrip (*iter); if (tmp && strlen (tmp)) { *out_terminator = find_terminator (tmp, terminators); if (*out_terminator >= 0) done = TRUE; } } /* If the terminator is found, look for expected responses */ if (done) { for (iter = lines; *iter && (reply_index < 0); iter++) { tmp = g_strstrip (*iter); if (tmp && strlen (tmp)) { response = find_response (tmp, needles, &reply_index); if (response) { g_free (*out_response); *out_response = g_strdup (response); } } } } g_strfreev (lines); } if (!done) g_usleep (1000); } while (!done && (time (NULL) < end) && (result->len <= SERIAL_BUF_SIZE)); /* Handle timeout */ if (*out_terminator < 0 && !*out_response) reply_index = -2; g_string_free (result, TRUE); return reply_index; }