/** * function make_conversation() * A controller function to run the instructions to make the conversation * * @link http://blog.program-o.com/?p=1209 * @param array $convoArr - the current state of the conversation array * @return array $convoArr (updated) */ function make_conversation($convoArr) { runDebug(__FILE__, __FUNCTION__, __LINE__, "Making conversation", 4); //get the user input and clean it $convoArr['aiml']['lookingfor'] = normalize_text($convoArr['user_say'][1]); //find an aiml match in the db $convoArr = get_aiml_to_parse($convoArr); $convoArr = parse_matched_aiml($convoArr, 'normal'); //parse the aiml to build a response //store the conversation $convoArr = push_on_front_convoArr('parsed_template', $convoArr['aiml']['parsed_template'], $convoArr); $convoArr = push_on_front_convoArr('template', $convoArr['aiml']['template'], $convoArr); //display conversation vars to user. $convoArr['conversation']['totallines']++; return $convoArr; }
/** * function save_for_nextturn() * This function puts the bot results of an srai search into the main convoArr * @param array $convoArr - the conversation array * @return array $convoArr - the updated conversation array **/ function save_for_nextturn($convoArr) { runDebug(__FILE__, __FUNCTION__, __LINE__, "Saving that and that_raw for next turn", 4); $savethis = $convoArr['aiml']['parsed_template']; $convoArr = push_on_front_convoArr('that_raw', $savethis, $convoArr); $convoArr = push_on_front_convoArr('that', $savethis, $convoArr); return $convoArr; }
/** * function add_new_conversation_vars() * A function add the new values from the user input into the conversation state * @param string $say - the user input * @param array $convoArr - the current state of the conversation array * @return $convoArr (updated) **/ function add_new_conversation_vars($say, $convoArr) { runDebug(__FILE__, __FUNCTION__, __LINE__, "New conversation vars", 4); //$convoArr['conversation']['convo_id']=$convo_id; //$convoArr['conversation']['bot_id']=$bot_id; //put what the user has said on the front of the 'user_say' and 'input' subarray with a minimum clean to prevent injection $convoArr = push_on_front_convoArr("user_say", strip_tags($say), $convoArr); $convoArr['aiml']['user_raw'] = strip_tags($say); $convoArr = push_on_front_convoArr('input', $convoArr['aiml']['user_raw'], $convoArr); return $convoArr; }
/** * function load_that(() * A function to load the previous bot responses into the convoArr['that'] array * * @link http://blog.program-o.com/?p=1283 * @param array $convoArr - the current state of the conversation array * @return array $convoArr (updated) */ function load_that($convoArr) { runDebug(__FILE__, __FUNCTION__, __LINE__, 'Loading the THAT array.', 2); global $dbConn, $dbn, $remember_up_to, $bot_id; $remember_up_to = !empty($convoArr['conversation']['remember_up_to']) ? $convoArr['conversation']['remember_up_to'] : $remember_up_to; $user_id = $convoArr['conversation']['user_id']; $bot_id = !empty($convoArr['conversation']['bot_id']) ? $convoArr['conversation']['bot_id'] : $bot_id; $limit = $remember_up_to; $sql = "select `input`, `response` from `{$dbn}`.`conversation_log` where `user_id` = {$user_id} and `bot_id` = {$bot_id} order by `id` desc limit {$limit};"; // desc runDebug(__FILE__, __FUNCTION__, __LINE__, "Getting conversation log entries for the current user. SQL:\n{$sql}", 3); $result = db_fetchAll($sql, null, __FILE__, __FUNCTION__, __LINE__); if ($result) { $tmpThatRows = array(); $tmpInputRows = array(); $tmpThat = array(); $tmpInput = array(); $puncuation = array(',', '?', ';', '!'); foreach ($result as $row) { $tmpThatRows[] = $row['response']; $tmpInputRows[] = $row['input']; } runDebug(__FILE__, __FUNCTION__, __LINE__, 'Inputs returned:' . print_r($tmpInputRows, true), 1); runDebug(__FILE__, __FUNCTION__, __LINE__, 'Loading previous responses into the ~THAT~ array.', 4); runDebug(__FILE__, __FUNCTION__, __LINE__, 'Responses returned:' . print_r($tmpThatRows, true), 1); $tmpThatRows = array_reverse($tmpThatRows); foreach ($tmpThatRows as $row) { $row = str_replace($puncuation, '.', $row); $tmpThat[] = explode('.', $row); } array_unshift($tmpThat, NULL); unset($tmpThat[0]); foreach ($tmpThat as $index => $value) { $value = implode_recursive(' ', $value, __FILE__, __FUNCTION__, __LINE__); $value = clean_that($value, __FILE__, __FUNCTION__, __LINE__); $convoArr = push_on_front_convoArr('that', $value, $convoArr); } runDebug(__FILE__, __FUNCTION__, __LINE__, 'Loading previous user inputs into the ~INPUT~ array.', 4); $tmpInputRows = array_reverse($tmpInputRows); foreach ($tmpInputRows as $row) { $row = str_replace($puncuation, '.', $row); $tmpInput[] = explode('.', $row); } array_unshift($tmpThat, NULL); unset($tmpThat[0]); foreach ($tmpInput as $index => $value) { $value = implode_recursive(' ', $value, __FILE__, __FUNCTION__, __LINE__); $value = clean_that($value, __FILE__, __FUNCTION__, __LINE__); $convoArr = push_on_front_convoArr('input', $value, $convoArr); } } else { runDebug(__FILE__, __FUNCTION__, __LINE__, 'Couldn\'t find any previous inputs or responses.', 4); } return $convoArr; }
/** * function set_simple() * This function sets simple client properties * @param array $convoArr - a reference to the existing conversation array * @param string $index - the array index to set * @param string $value - the value of the array index * @return string $value - the value of the array index **/ function set_simple(&$convoArr, $index, $value) { runDebug(__FILE__, __FUNCTION__, __LINE__, "Setting simple value {$index} to equal {$value}", 4); if ($index == "topic") { $convoArr = push_on_front_convoArr('topic', $value, $convoArr); } else { $value = clean_client_properties($value); $convoArr['client_properties'][$index] = $value; } return $value . " "; }