예제 #1
0
/**
 * function intialise_convoArray()
 * A function to intialise the conversation array 
 * This is the array that is built throught the conversation
 * @param  string $convo_id - unique session id
 * @param  int $bot_id - the id of the bot
 * @param  string $format - the return format of the response (html,json,xml)
 * @param  array $convoArr - the current state of the conversation array
 * @return $convoArr (updated)
**/
function intialise_convoArray($convoArr)
{
    //set the initial convoArr values
    /*$convoArr['conversation']['convo_id']=$convo_id;
      $convoArr['conversation']['bot_id']=$bot_id;
      $convoArr['conversation']['format']=$format;*/
    runDebug(__FILE__, __FUNCTION__, __LINE__, "Intialising conversation", 4);
    //load blank topics
    $convoArr = load_blank_convoArray('topic', "", $convoArr);
    //load blank thats
    $convoArr = load_blank_convoArray('that', "", $convoArr);
    //load blank stars
    $convoArr = load_blank_convoArray('star', "", $convoArr);
    //load blank stars
    $convoArr = load_blank_convoArray('input', "", $convoArr);
    //load blank stack
    $convoArr = load_blank_stack($convoArr);
    //load the new client defaults
    $convoArr = load_new_client_defaults($convoArr);
    return $convoArr;
}
예제 #2
0
/**
 * function intialise_convoArray()
 * A function to intialise the conversation array
 * This is the array that is built throught the conversation
 * @link http://blog.program-o.com/?p=1242
 * @param  array $convoArr - the current state of the conversation array
 * @return array $convoArr (updated)
 **/
function intialise_convoArray($convoArr)
{
    if (!isset($convoArr['conversation'])) {
        $convoArr['conversation'] = array();
    }
    //set the initial convoArr values
    runDebug(__FILE__, __FUNCTION__, __LINE__, "Intialising conversation", 4);
    //load blank topics
    $convoArr = load_blank_array_element('topic', "", $convoArr);
    //load blank thats
    $convoArr = load_blank_array_element('that', "", $convoArr);
    //load blank stars
    $convoArr = load_blank_array_element('star', "", $convoArr);
    //load blank stars
    $convoArr = load_blank_array_element('input', "", $convoArr);
    //load blank stack
    $convoArr = load_blank_stack($convoArr);
    //load bot properties
    $convoArr = load_default_bot_values($convoArr);
    //load the new client defaults
    $convoArr = load_new_client_defaults($convoArr);
    return $convoArr;
}
예제 #3
0
/**
 * 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 . " ";
}