/** * function push_on_front_convoArr() * A function to push items on the front of a subarray in convoArr * @param array $arrayIndex - the subarray index to push to * @param array $value - the value to push on teh subarray * @param array $convoArr - the current state of the conversation array * @return $convoArr (updated) * TODO BETTER COMMENTING **/ function push_on_front_convoArr($arrayIndex, $value, $convoArr) { global $offset, $rememLimit; runDebug(__FILE__, __FUNCTION__, __LINE__, "Pushing {$value} to front of {$arrayIndex} array", 4); $remember_up_to = $convoArr['conversation']['remember_up_to']; //these subarray indexes are 2d $two_d_arrays = array("that", "that_raw"); $arrayIndex = trim($arrayIndex); //mini clean $value = trim($value); $value = preg_replace('/\\s\\s+/', ' ', $value); $value = preg_replace('/\\s\\./', '.', $value); //there is a chance the subarray has not been set yet so check and if not set here if (!isset($convoArr[$arrayIndex][$offset])) { $convoArr[$arrayIndex] = array(); $convoArr = load_blank_convoArray($arrayIndex, "", $convoArr); } //if the subarray is itself an array check it here if (in_array($arrayIndex, $two_d_arrays)) { $matches = preg_match_all("# ?(([^\\.\\?!]*)+(?:[\\.\\?!]|(?:<br ?/?>))*)#ui", $value, $sentances); $cmatch = 0; //do another check to make sure the array is not just full of blanks foreach ($sentances as $temp) { foreach ($temp as $chk) { if (trim($chk) != "") { $cmatch++; } } } runDebug(__FILE__, __FUNCTION__, __LINE__, print_r($convoArr[$arrayIndex], true), 4); //if there definately is something in the sentance array build the temp sentance array if ($cmatch > 0 && $matches !== FALSE) { foreach ($sentances[1] as $index => $value) { if ($arrayIndex == "that") { $t = clean_that($value); if ($t != "") { $tmp_sentance[] = $t; } } else { $tmp_sentance[] = $value; } } //reverse the array and store $sentances = array(); $sentances = array_reverse($tmp_sentance); } else { $sentances = array(); if ($arrayIndex == "that") { $sentances[0] = clean_that($value); } else { $sentances[0] = $value; } } //make a space so that [0] is null (in accordance with the AIML array offset) array_unshift($sentances, NULL); unset($sentances[0]); //push this onto the subarray and then clear [0] element (in accordance with the AIML array offset) array_unshift($convoArr[$arrayIndex], $sentances); array_unshift($convoArr[$arrayIndex], null); unset($convoArr[$arrayIndex][0]); } else { array_unshift($convoArr[$arrayIndex], $value); array_unshift($convoArr[$arrayIndex], NULL); } if (trim($arrayIndex) == 'star' || trim($arrayIndex) == 'topic') { //keep 5 times as many topics and stars as lines of conversation $rememLimit_tmp = $rememLimit; } else { $rememLimit_tmp = $remember_up_to; } for ($i = $rememLimit_tmp + 1; $i <= count($convoArr[$arrayIndex]); $i++) { if (isset($convoArr[$arrayIndex][$i])) { unset($convoArr[$arrayIndex][$i]); } } unset($convoArr[$arrayIndex][0]); if ($arrayIndex == "topic") { push_stack($convoArr, $value); } 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; }