예제 #1
0
/**
 * function find_aiml_matches()
 * This function builds the sql to use to get a match from the tbl
 * @param array $convoArr - conversation array
 * @return array $convoArr
 **/
function find_aiml_matches($convoArr)
{
    global $dbConn, $dbn, $error_response, $use_parent_bot;
    runDebug(__FILE__, __FUNCTION__, __LINE__, "Finding the aiml matches from the DB", 4);
    $i = 0;
    //TODO convert to get_it
    $bot_id = $convoArr['conversation']['bot_id'];
    $bot_parent_id = $convoArr['conversation']['bot_parent_id'];
    $default_aiml_pattern = $convoArr['conversation']['default_aiml_pattern'];
    #$lookingfor = get_convo_var($convoArr,"aiml","lookingfor");
    $convoArr['aiml']['lookingfor'] = str_replace('  ', ' ', $convoArr['aiml']['lookingfor']);
    $lookingfor = trim(strtoupper($convoArr['aiml']['lookingfor']));
    //get the first and last words of the cleaned user input
    $lastInputWord = get_last_word($lookingfor);
    $firstInputWord = get_first_word($lookingfor);
    //get the stored topic
    $storedtopic = get_topic($convoArr);
    runDebug(__FILE__, __FUNCTION__, __LINE__, "Stored topic = '{$storedtopic}'", 4);
    //get the cleaned user input
    $lastthat = isset($convoArr['that'][1][1]) ? $convoArr['that'][1][1] : '';
    //build like patterns
    if ($lastthat != '') {
        $thatPatternSQL = " OR " . make_like_pattern($lastthat, 'thatpattern');
        $thatPatternSQL = rtrim($thatPatternSQL, ' OR');
    } else {
        $thatPattern = '';
        $thatPatternSQL = '';
    }
    //get the word count
    $word_count = wordsCount_inSentence($lookingfor);
    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}' ";
    }
    if (!empty($storedtopic)) {
        $topic_select = "AND (`topic`='' OR `topic`='{$storedtopic}')";
    } else {
        $topic_select = '';
    }
    if ($word_count == 1) {
        //if there is one word do this
        $sql = "SELECT `id`, `pattern`, `thatpattern`, `topic` FROM `{$dbn}`.`aiml` WHERE\n  {$sql_bot_select} AND (\n  `pattern` = '_' OR\n  `pattern` = '*' OR\n  `pattern` = '{$lookingfor}' OR\n  `pattern` = '{$default_aiml_pattern}'\n  {$thatPatternSQL}\n  ) {$topic_select} order by `topic` desc, `pattern` asc, `thatpattern` asc,`id` asc;";
    } else {
        //otherwise do this
        $sql_add = make_like_pattern($lookingfor, 'pattern');
        $sql = "SELECT `id`, `bot_id`, `pattern`, `thatpattern`, `topic` FROM `{$dbn}`.`aiml` WHERE\n  {$sql_bot_select} AND (\n  `pattern` = '_' OR\n  `pattern` = '*' OR\n  `pattern` = '{$lookingfor}' OR {$sql_add} OR\n  `pattern` = '{$default_aiml_pattern}'\n  {$thatPatternSQL}\n  ) {$topic_select}\n  order by `topic` desc, `pattern` asc, `thatpattern` asc,`id` asc;";
    }
    runDebug(__FILE__, __FUNCTION__, __LINE__, "Core Match AIML sql: {$sql}", 3);
    $result = db_fetchAll($sql, null, __FILE__, __FUNCTION__, __LINE__);
    $num_rows = count($result);
    if ($result && $num_rows > 0) {
        $tmp_rows = number_format($num_rows);
        runDebug(__FILE__, __FUNCTION__, __LINE__, "FOUND: ({$num_rows}) potential AIML matches", 2);
        $tmp_content = date('H:i:s') . ": SQL:\n{$sql}\nRows = {$tmp_rows}\n\n";
        //loop through results
        foreach ($result as $row) {
            $row['aiml_id'] = $row['id'];
            $row['bot_id'] = $bot_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'] = $bot_id;
        $allrows[$i]['pattern'] = "no results";
        $allrows[$i]['thatpattern'] = '';
        $allrows[$i]['topic'] = '';
    }
    return $allrows;
}
예제 #2
0
/**
 * function find_aiml_matches()
 * This function builds the sql to use to get a match from the tbl
 * @param array $convoArr - conversation array
 * @return array $convoArr
**/
function find_aiml_matches($convoArr)
{
    global $con, $dbn, $error_response, $use_parent_bot;
    runDebug(__FILE__, __FUNCTION__, __LINE__, "Finding the aiml matches from the DB", 4);
    $i = 0;
    //TODO convert to get_it
    $bot_id = get_convo_var($convoArr, "conversation", "bot_id");
    $bot_parent_id = get_convo_var($convoArr, "conversation", "bot_parent_id");
    $default_aiml_pattern = get_convo_var($convoArr, "conversation", "default_aiml_pattern");
    #$lookingfor = get_convo_var($convoArr,"aiml","lookingfor");
    $lookingfor = mysql_real_escape_string(get_convo_var($convoArr, "aiml", "lookingfor"));
    //get the first and last words of the cleaned user input
    $lastInputWord = get_last_word($lookingfor);
    $firstInputWord = get_first_word($lookingfor);
    //get the stored topic
    $storedtopic = mysql_real_escape_string(get_convo_var($convoArr, "topic"));
    //get the cleaned user input
    $lastthat = get_convo_var($convoArr, 'that', '', 1, 1);
    //build like patterns
    if ($lastthat != "") {
        $thatPatternSQL = " OR " . make_like_pattern($lastthat, 'thatpattern');
    } else {
        $thatPattern = "";
        //$lastThatPattern = "";
        //$firstThatPattern = "";
        $thatPatternSQL = "";
        //$storedthatpattern ="";
    }
    //get the word count
    $word_count = wordsCount_inSentance($lookingfor);
    if ($bot_parent_id != 0) {
        $sql_bot_select = " (bot_id = '{$bot_id}' OR bot_id = '{$bot_parent_id}') ";
    } else {
        $sql_bot_select = " bot_id = '{$bot_id}' ";
    }
    if ($word_count == 1) {
        //if there is one word do this
        $sql = "SELECT * FROM `{$dbn}`.`aiml` WHERE\n\t\t{$sql_bot_select} AND (\n\t\t((`pattern` = '_') OR (`pattern` = '*') OR (`pattern` = '{$lookingfor}') OR (`pattern` = '{$default_aiml_pattern}' ) )\n\t\tAND\t((`thatpattern` = '_') OR (`thatpattern` = '*') OR (`thatpattern` = '') OR (`thatpattern` = '{$lastthat}') {$thatPatternSQL} )\n\t\tAND ( (`topic`='') OR (`topic`='" . $storedtopic . "')))";
    } else {
        //otherwise do this
        $sql_add = make_like_pattern($lookingfor, 'pattern');
        $sql = "SELECT * FROM `{$dbn}`.`aiml` WHERE \n\t\t{$sql_bot_select} AND (\n\t\t((`pattern` = '_') OR \n\t\t (`pattern` = '*') OR \n\t\t (`pattern` like '{$lookingfor}') OR \n\t\t ({$sql_add}) OR \n\t\t (`pattern` = '{$default_aiml_pattern}' ))\n\t\tAND\t((`thatpattern` = '_') OR (`thatpattern` = '*') OR (`thatpattern` = '') OR (`thatpattern` = '{$lastthat}') {$thatPatternSQL} )\n\t\tAND ((`topic`='') OR (`topic`='" . $storedtopic . "')))";
    }
    runDebug(__FILE__, __FUNCTION__, __LINE__, "Match AIML sql: {$sql}", 3);
    $result = db_query($sql, $con);
    if ($result && mysql_num_rows($result) > 0) {
        runDebug(__FILE__, __FUNCTION__, __LINE__, "FOUND: '" . mysql_num_rows($result) . "' potential AIML matches", 2);
        //loop through results
        while ($row = mysql_fetch_array($result)) {
            $allrows[$i]['aiml_id'] = $row['id'];
            $allrows[$i]['bot_id'] = $row['bot_id'];
            $allrows[$i]['pattern'] = $row['pattern'];
            $allrows[$i]['thatpattern'] = $row['thatpattern'];
            $allrows[$i]['template'] = $row['template'];
            $allrows[$i]['topic'] = $row['topic'];
            $allrows[$i]['aiml_to_php'] = $row['php_code'];
            $i++;
        }
    } 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]['template'] = "<say>{$error_response}</say>";
        $allrows[$i]['topic'] = "";
        $allrows[$i]['aiml_to_php'] = "\$botsay=\"{$error_response}\"";
    }
    return $allrows;
}