/** * function get_aiml_to_parse() * This function controls all the process to match the aiml in the db to the user input * @param array $convoArr - conversation array * @return array $convoArr **/ function get_aiml_to_parse($convoArr) { runDebug(__FILE__, __FUNCTION__, __LINE__, "Running all functions to get the correct aiml from the DB", 4); $lookingfor = $convoArr['aiml']['lookingfor']; $current_thatpattern = isset($convoArr['that'][1][1]) ? $convoArr['that'][1][1] : ''; $current_topic = get_topic($convoArr); $aiml_pattern = $convoArr['conversation']['default_aiml_pattern']; $bot_parent_id = $convoArr['conversation']['bot_parent_id']; $raw_that = isset($convoArr['that']) ? print_r($convoArr['that'], true) : ''; //check if match in user defined aiml $allrows = find_userdefined_aiml($convoArr); //if there is no match in the user defined aiml table if (!isset($allrows) || count($allrows) <= 0) { //look for a match in the normal aiml tbl $allrows = find_aiml_matches($convoArr); //unset all irrelvant matches $allrows = unset_all_bad_pattern_matches($convoArr, $allrows, $lookingfor); //score the relevant matches $allrows = score_matches($convoArr, $allrows, $lookingfor); //get the highest $allrows = get_highest_scoring_row($convoArr, $allrows, $lookingfor); //READY FOR v2.5 do not uncomment will not work //check if this is an unknown input and place in the unknown_inputs table if true //check_and_add_unknown_inputs($allrows,$convoArr); } //Now we have the results put into the conversation array $convoArr['aiml']['pattern'] = $allrows['pattern']; $convoArr['aiml']['thatpattern'] = $allrows['thatpattern']; $convoArr['aiml']['template'] = $allrows['template']; $convoArr['aiml']['html_template'] = ''; $convoArr['aiml']['topic'] = $allrows['topic']; $convoArr['aiml']['score'] = $allrows['score']; $convoArr['aiml']['aiml_id'] = $allrows['aiml_id']; //return runDebug(__FILE__, __FUNCTION__, __LINE__, "Will be parsing id:" . $allrows['aiml_id'] . " (" . $allrows['pattern'] . ")", 4); return $convoArr; }
/** * function run_srai() * This function controls the SRAI recursion calls * @param array $convoArr - a reference to the existing conversation array * @param string $now_look_for_this - the text to search for * @return string $srai_parsed_template - the result of the search **/ function run_srai(&$convoArr, $now_look_for_this) { global $srai_iterations, $error_response, $dbConn, $dbn; $bot_parent_id = $convoArr['conversation']['bot_parent_id']; $bot_id = $convoArr['conversation']['bot_id']; if ($bot_parent_id != 0 and $bot_parent_id != $bot_id) { $sql_bot_select = " (bot_id = '{$bot_id}' OR bot_id = '{$bot_parent_id}') "; } else { $sql_bot_select = " bot_id = '{$bot_id}' "; } $bot_id = $convoArr['conversation']['bot_id']; runDebug(__FILE__, __FUNCTION__, __LINE__, 'Checking for entries in the srai_lookup table.', 2); runDebug(__FILE__, __FUNCTION__, __LINE__, "google bot_id = {$bot_id}", 2); $lookingfor = $convoArr['aiml']['lookingfor']; //$now_look_for_this = strtoupper($now_look_for_this); $sql = "select `template_id` from `{$dbn}`.`srai_lookup` where `pattern` = '{$now_look_for_this}' and {$sql_bot_select};"; runDebug(__FILE__, __FUNCTION__, __LINE__, "lookup SQL = {$sql}", 2); $sth = $dbConn->prepare($sql); $sth->execute(); $row = $sth->fetchAll(); runDebug(__FILE__, __FUNCTION__, __LINE__, 'Result = ' . print_r($row, true), 2); $num_rows = count($row); if ($num_rows > 0) { runDebug(__FILE__, __FUNCTION__, __LINE__, "Found {$num_rows} rows in lookup table: " . print_r($row, true), 2); $template_id = $row[0]['template_id']; runDebug(__FILE__, __FUNCTION__, __LINE__, "Found a matching entry in the lookup table. Using ID# {$template_id}.", 2); $sql = "select `template` from `{$dbn}`.`aiml` where `id` = '{$template_id}';"; $sth = $dbConn->prepare($sql); $sth->execute(); $row = $sth->fetch(); runDebug(__FILE__, __FUNCTION__, __LINE__, "Row found in AIML for ID {$template_id}: " . print_r($row, true), 2); if (!empty($row)) { $template = add_text_tags($row['template']); try { $sraiTemplate = new SimpleXMLElement($template, LIBXML_NOCDATA); } catch (exception $e) { trigger_error("There was a problem parsing the SRAI template as XML. Template value:\n{$template}", E_USER_WARNING); $sraiTemplate = new SimpleXMLElement("<text>{$error_response}</text>", LIBXML_NOCDATA); } $responseArray = parseTemplateRecursive($convoArr, $sraiTemplate); $response = implode_recursive(' ', $responseArray, __FILE__, __FUNCTION__, __LINE__); runDebug(__FILE__, __FUNCTION__, __LINE__, "Returning results from stored srai lookup.", 2); return $response; } } else { runDebug(__FILE__, __FUNCTION__, __LINE__, 'No match found in lookup table.', 2); } runDebug(__FILE__, __FUNCTION__, __LINE__, "Nothing found in the SRAI lookup table. Looking for a direct pattern match for '{$now_look_for_this}'.", 2); $sql = "SELECT `id`, `bot_id`, `pattern`, `thatpattern`, `topic` FROM `{$dbn}`.`aiml` where `pattern` = :pattern and {$sql_bot_select} order by `id` asc;"; $sth = $dbConn->prepare($sql); $sth->bindValue(':pattern', $now_look_for_this); $sth->execute(); $result = $sth->fetchAll(); $num_rows = count($result); runDebug(__FILE__, __FUNCTION__, __LINE__, "Found {$num_rows} potential responses.", 2); $allrows = array(); $i = 0; if ($result && $num_rows > 0) { $tmp_rows = number_format($num_rows); runDebug(__FILE__, __FUNCTION__, __LINE__, "FOUND: ({$num_rows}) potential AIML matches", 2); //loop through results foreach ($result as $row) { $row['aiml_id'] = $row['id']; $row['score'] = 0; $row['track_score'] = ''; $allrows[] = $row; $mu = memory_get_usage(true); if ($mu >= MEM_TRIGGER) { runDebug(__FILE__, __FUNCTION__, __LINE__, 'Current operation exceeds memory threshold. Aborting data retrieval.', 0); break; } } } else { runDebug(__FILE__, __FUNCTION__, __LINE__, "Error: FOUND NO AIML matches in DB", 1); $allrows[$i]['aiml_id'] = "-1"; $allrows[$i]['bot_id'] = "-1"; $allrows[$i]['pattern'] = "no results"; $allrows[$i]['thatpattern'] = ''; $allrows[$i]['topic'] = ''; } //unset all irrelvant matches $allrows = unset_all_bad_pattern_matches($convoArr, $allrows, $now_look_for_this); //score the relevant matches $allrows = score_matches($convoArr, $allrows, $now_look_for_this); //get the highest $allrows = get_highest_scoring_row($convoArr, $allrows, $lookingfor); if (isset($allrows['aiml_id']) && $allrows['aiml_id'] > 0) { $sql = "select `template` from `{$dbn}`.`aiml` where `id` = :id limit 1;"; $sth = $dbConn->prepare($sql); $aiml_id = $allrows['aiml_id']; $pattern = $allrows['pattern']; $sth->bindValue(':id', $aiml_id); $sth->execute(); $row = $sth->fetch(); $sth->closeCursor(); $template = add_text_tags($row['template']); try { $sraiTemplate = new SimpleXMLElement($template, LIBXML_NOCDATA); } catch (exception $e) { trigger_error("There was a problem parsing the SRAI template as XML. Template value:\n{$template}", E_USER_WARNING); $sraiTemplate = new SimpleXMLElement("<text>{$error_response}</text>", LIBXML_NOCDATA); } $responseArray = parseTemplateRecursive($convoArr, $sraiTemplate); $response = implode_recursive(' ', $responseArray, __FILE__, __FUNCTION__, __LINE__); try { // code to try here $sql = "insert into `{$dbn}`.`srai_lookup` (`id`, `bot_id`, `pattern`, `template_id`) values(null, :bot_id, :pattern, :template_id);"; $sth = $dbConn->prepare($sql); $sth->bindValue(':bot_id', $bot_id); $sth->bindValue(':pattern', $pattern); $sth->bindValue(':template_id', $aiml_id); $sth->execute(); $affectedRows = $sth->rowCount(); if ($affectedRows > 0) { runDebug(__FILE__, __FUNCTION__, __LINE__, "Successfully inserted entry for '{$pattern}'.", 1); } } catch (Exception $e) { //something to handle the problem here, usually involving $e->getMessage() $err = $e->getMessage(); runDebug(__FILE__, __FUNCTION__, __LINE__, "Unable to insert entry for '{$pattern}'! Error = {$err}.", 1); runDebug(__FILE__, __FUNCTION__, __LINE__, "SQL = {$sql}", 1); } // runDebug(__FILE__, __FUNCTION__, __LINE__, "Returning results from stored srai lookup.", 2); return $response; } runDebug(__FILE__, __FUNCTION__, __LINE__, "Running SRAI {$srai_iterations} on {$now_look_for_this}", 3); runDebug(__FILE__, __FUNCTION__, __LINE__, $convoArr['aiml']['html_template'], 4); //number of srai iterations - will stop recursion if it is over 10 $srai_iterations++; runDebug(__FILE__, __FUNCTION__, __LINE__, "Incrementing srai iterations to {$srai_iterations}", 4); if ($srai_iterations > 10) { runDebug(__FILE__, __FUNCTION__, __LINE__, "ERROR - Too much recursion breaking out", 1); $convoArr['aiml']['parsed_template'] = $error_response; return $error_response; } //$tmp_convoArr = array(); $tmp_convoArr = $convoArr; if (!isset($tmp_convoArr['stack'])) { $tmp_convoArr = load_blank_stack($tmp_convoArr); } if (!isset($tmp_convoArr['topic'])) { $tmp_convoArr['topic'] = array(); $tmp_convoArr['topic'][1] = ''; } $tmp_convoArr['aiml'] = array(); $tmp_convoArr['that'][1][1] = ""; //added $tmp_convoArr['aiml']['parsed_template'] = ""; $tmp_convoArr['aiml']['lookingfor'] = $now_look_for_this; $tmp_convoArr['aiml']['pattern'] = $now_look_for_this; $tmp_convoArr['aiml']['thatpattern'] = $convoArr['aiml']['thatpattern']; $tmp_convoArr = get_aiml_to_parse($tmp_convoArr); $tmp_convoArr = parse_matched_aiml($tmp_convoArr, "srai"); $srai_parsed_template = $tmp_convoArr['aiml']['parsed_template']; runDebug(__FILE__, __FUNCTION__, __LINE__, "SRAI Found. Returning '{$srai_parsed_template}'", 2); $convoArr['client_properties'] = $tmp_convoArr['client_properties']; $convoArr['topic'] = $tmp_convoArr['topic']; $convoArr['stack'] = $tmp_convoArr['stack']; $srai_iterations--; runDebug(__FILE__, __FUNCTION__, __LINE__, "Decrementing srai iterations to {$srai_iterations}", 4); return $srai_parsed_template . " "; }
/** * function get_aiml_to_parse() * This function controls all the process to match the aiml in the db to the user input * @param array $convoArr - conversation array * @return array $convoArr **/ function get_aiml_to_parse($convoArr) { global $offset; runDebug(__FILE__, __FUNCTION__, __LINE__, "Running all functions to get the correct aiml from the DB", 4); $lookingfor = get_convo_var($convoArr, "aiml", "lookingfor"); $current_thatpattern = get_convo_var($convoArr, 'that', '', 1, 1); $current_topic = get_convo_var($convoArr, 'topic'); $default_aiml_pattern = get_convo_var($convoArr, 'conversation', 'default_aiml_pattern'); $bot_parent_id = get_convo_var($convoArr, 'conversation', 'bot_parent_id'); $sendConvoArr = $convoArr; //check if match in user defined aiml $allrows = find_userdefined_aiml($convoArr); //if there is no match in the user defined aiml tbl if (!isset($allrows) || count($allrows) <= 0) { //look for a match in the normal aiml tbl $allrows = find_aiml_matches($convoArr); //unset all irrelvant matches $allrows = unset_all_bad_pattern_matches($allrows, $lookingfor, $current_thatpattern, $current_topic, $default_aiml_pattern); //score the relevant matches $allrows = score_matches($bot_parent_id, $allrows, $lookingfor, $current_thatpattern, $current_topic, $default_aiml_pattern); //get the highest $allrows = get_highest_score_rows($allrows, $lookingfor); } //Now we have the results put into the conversation array $convoArr['aiml']['pattern'] = $allrows['pattern']; $convoArr['aiml']['thatpattern'] = $allrows['thatpattern']; $convoArr['aiml']['template'] = $allrows['template']; $convoArr['aiml']['html_template'] = htmlentities($allrows['template']); $convoArr['aiml']['topic'] = $allrows['topic']; $convoArr['aiml']['score'] = $allrows['score']; $convoArr['aiml']['aiml_to_php'] = $allrows['aiml_to_php']; $convoArr['aiml']['aiml_id'] = $allrows['aiml_id']; //return runDebug(__FILE__, __FUNCTION__, __LINE__, "Will be parsing id:" . $allrows['aiml_id'] . " (" . $allrows['pattern'] . ")", 4); return $convoArr; }