Beispiel #1
0
/**
 * Takes all the relevant sql results and scores them to find the most likely match with the aiml
 *
 * @param array  $convoArr
 * @param array  $allrows
 * @param string $lookingfor
 * @internal param int $bot_parent_id
 * @internal param string $current_thatpattern
 * @internal param string $current_topic
 * @return array
 **/
function score_matches($convoArr, $allrows, $lookingfor)
{
    global $common_words_array, $default_pattern;
    $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'];
    runDebug(__FILE__, __FUNCTION__, __LINE__, "Scoring the matches. Topic = {$current_topic}", 4);
    //set the scores for each type of word or sentence to be used in this function
    $this_bot_match = 500;
    $underscore_points = 100;
    $that_pattern_match = 75;
    $topic_match = 50;
    $that_pattern_match_general = 9;
    $uncommon_word_points = 8;
    $common_word_points = 1;
    $direct_word_match_points = 1;
    $pattern_points = 2;
    $starscore_points = 1;
    $direct_match = 1;
    //even the worst match from the actual bot is better than the best match from the base bot
    //loop through all relevant results
    foreach ($allrows as $all => $subrow) {
        //get items
        if (!isset($subrow['pattern']) || trim($subrow['pattern']) == '') {
            $aiml_pattern = '';
        } else {
            $aiml_pattern = trim($subrow['pattern']);
        }
        if (!isset($subrow['thatpattern']) || trim($subrow['thatpattern']) == '') {
            $aiml_thatpattern = '';
        } else {
            $aiml_thatpattern = trim($subrow['thatpattern']);
        }
        if (!isset($subrow['topic']) || trim($subrow['topic']) == '') {
            $aiml_topic = '';
        } else {
            $aiml_topic = trim($subrow['topic']);
        }
        if ($aiml_pattern == '') {
            continue;
        }
        //convert aiml wildcards to php wildcards
        if ($aiml_thatpattern != '') {
            $aiml_thatpattern_wildcards = match_wildcard_rows($aiml_thatpattern);
        } else {
            $aiml_thatpattern_wildcards = '';
        }
        //to debug the scoring...
        $allrows[$all]['track_score'] = '';
        //if the aiml is from the actual bot and not the base bot
        //any match in the current bot is better than the base bot
        if ($bot_parent_id != 0 && $allrows[$all]['bot_id'] != $bot_parent_id) {
            $allrows[$all]['score'] += $this_bot_match;
            $allrows[$all]['track_score'] .= "a";
        }
        //if the result aiml pattern matches the user input increase score
        if ($aiml_pattern == $lookingfor) {
            $allrows[$all]['score'] += $direct_match;
            $allrows[$all]['track_score'] .= "b";
        }
        //if the result topic matches the user stored aiml topic increase score
        if ($aiml_topic == $current_topic && $current_topic != '') {
            $allrows[$all]['score'] += $topic_match;
            $allrows[$all]['track_score'] .= "c";
        }
        //if the result that pattern matches the user stored that pattern increase score
        if ($aiml_thatpattern == $current_thatpattern && $aiml_thatpattern != '' && $aiml_pattern != "*") {
            $allrows[$all]['score'] += $that_pattern_match;
            $allrows[$all]['track_score'] .= "d";
        } elseif ($aiml_thatpattern_wildcards != '' && $aiml_thatpattern != '' && $aiml_pattern != "*" && preg_match($aiml_thatpattern_wildcards, $current_thatpattern, $m)) {
            $allrows[$all]['score'] += $that_pattern_match;
            $allrows[$all]['track_score'] .= "e";
        }
        //if the that pattern is just a star we need to score it seperately as this is very general
        if ($aiml_pattern == "*" && (substr($aiml_thatpattern, -1) == "*" || substr($aiml_thatpattern, 0, 1) == "*")) {
            //if the result that pattern matches the user stored that pattern increase score with a lower number
            if ($aiml_thatpattern == $current_thatpattern && $aiml_thatpattern != '') {
                $allrows[$all]['score'] += $that_pattern_match_general;
                $allrows[$all]['track_score'] .= "f";
            } elseif ($aiml_thatpattern_wildcards != '' && $aiml_thatpattern != '' && preg_match($aiml_thatpattern_wildcards, $current_thatpattern, $m)) {
                $allrows[$all]['score'] += $that_pattern_match_general;
                $allrows[$all]['track_score'] .= "g";
            }
        } elseif ($aiml_pattern == "*" && $aiml_thatpattern != "") {
            if ($aiml_thatpattern_wildcards != '' && preg_match($aiml_thatpattern_wildcards, $current_thatpattern, $m)) {
                $allrows[$all]['score'] += $that_pattern_match;
                $allrows[$all]['track_score'] = "general aiml that pattern match";
            }
        }
        //if stored result == default pattern increase score
        $aiml_pattern = IS_MB_ENABLED ? mb_strtolower($aiml_pattern) : strtolower($aiml_pattern);
        $default_pattern = IS_MB_ENABLED ? mb_strtolower($default_pattern) : strtolower($default_pattern);
        if ($aiml_pattern == $default_pattern) {
            $allrows[$all]['score'] += $pattern_points;
            $allrows[$all]['track_score'] .= "j";
        } elseif ($aiml_pattern == "*") {
            //if stored result == * increase score
            $allrows[$all]['score'] += $starscore_points;
            $allrows[$all]['track_score'] .= "k";
        } elseif ($aiml_pattern == "_") {
            //if stored result == _ increase score
            $allrows[$all]['score'] += $underscore_points;
            $allrows[$all]['track_score'] .= "l";
        } else {
            //if stored result == none of the above BREAK INTO WORDS AND SCORE INDIVIDUAL WORDS
            $lc_lookingFor = IS_MB_ENABLED ? mb_strtolower($convoArr['user_say'][1]) : strtolower($convoArr['user_say'][1]);
            $lookingforArray = explode(' ', trim($lc_lookingFor));
            //save_file(_DEBUG_PATH_ . 'lfa.txt', print_r($lookingforArray, true));
            $wordsArr = explode(" ", $aiml_pattern);
            foreach ($wordsArr as $index => $word) {
                $word = IS_MB_ENABLED ? mb_strtolower($word) : strtolower($word);
                $word = trim($word);
                if (in_Array($word, $lookingforArray)) {
                    // if it is a direct word match increase with (lower) score
                    $allrows[$all]['score'] += $direct_word_match_points;
                    $allrows[$all]['track_score'] .= "m";
                }
                if (in_Array($word, $common_words_array)) {
                    // if it is a commonword increase with (lower) score
                    $allrows[$all]['score'] += $common_word_points;
                    $allrows[$all]['track_score'] .= "n";
                } elseif ($word == "*") {
                    $allrows[$all]['score'] += $starscore_points;
                    //if it is a star wildcard increase score
                    $allrows[$all]['track_score'] .= "o";
                } elseif ($word == "_") {
                    $allrows[$all]['score'] += $underscore_points;
                    //if it is a underscore wildcard increase score
                    $allrows[$all]['track_score'] .= "p";
                } else {
                    $allrows[$all]['score'] += $uncommon_word_points;
                    //else it must be an uncommon word so increase the score
                    $allrows[$all]['track_score'] .= "q";
                }
            }
        }
    }
    //send off for debugging
    runDebug(__FILE__, __FUNCTION__, __LINE__, "Unsorted array \$allrows:\n" . print_r($allrows, true), 4);
    sort2DArray("show top scoring aiml matches", $allrows, "score", 1, 10);
    runDebug(__FILE__, __FUNCTION__, __LINE__, "Sorted array \$allrows:\n" . print_r($allrows, true), 4);
    return $allrows;
    //return the scored rows
}
Beispiel #2
0
/**
 * function score_matches()
 * This function takes all the relevant sql results and scores them
 * to find the most likely match with the aiml
 * @param int $bot_parent_id - the id of the parent bot
 * @param array $allrows - all the results
 * @param string $lookingfor - the user input
 * @param string $current_thatpattern - the current that pattern
 * @param string $current_topic - the current topic
 * @return array allrows - the SCORED results
**/
function score_matches($bot_parent_id, $allrows, $lookingfor, $current_thatpattern, $current_topic, $default_aiml_pattern)
{
    global $commonwordsArr;
    runDebug(__FILE__, __FUNCTION__, __LINE__, "Scoring the matches", 4);
    //set the scores for each type of word or sentance to be used in this function
    $default_pattern_points = 2;
    $underscore_points = 100;
    $starscore_points = 1;
    $uncommon_word_points = 6;
    $common_word_points = 3;
    $direct_match = 1;
    $topic_match = 50;
    $that_pattern_match = 75;
    $that_pattern_match_general = 9;
    $this_bot_match = 500;
    //even the worst match from the actual bot is better than the best match from the base bot
    //loop through all relevant results
    foreach ($allrows as $all => $subrow) {
        //get items
        if (!isset($subrow['pattern']) || trim($subrow['pattern']) == '') {
            $aiml_pattern = '';
        } else {
            $aiml_pattern = trim($subrow['pattern']);
        }
        if (!isset($subrow['thatpattern']) || trim($subrow['thatpattern']) == '') {
            $aiml_thatpattern = '';
        } else {
            $aiml_thatpattern = trim($subrow['thatpattern']);
        }
        if (!isset($subrow['topic']) || trim($subrow['topic']) == '') {
            $aiml_topic = '';
        } else {
            $aiml_topic = trim($subrow['topic']);
        }
        if ($aiml_pattern == '') {
            continue;
        }
        //convert aiml wildcards to php wildcards
        if ($aiml_thatpattern != "") {
            $aiml_thatpattern_wildcards = match_wildcard_rows($aiml_thatpattern);
        } else {
            $aiml_thatpattern_wildcards = "";
        }
        //to debug the scoring...
        $allrows[$all]['track_score'] = "";
        //if the aiml is from the actual bot and not the base bot
        //any match in the current bot is better than the base bot
        if ($bot_parent_id != 0 && $allrows[$all]['bot_id'] != $bot_parent_id) {
            $allrows[$all]['score'] += $this_bot_match;
            $allrows[$all]['track_score'] .= "a";
        }
        //if the result aiml pattern matches the user input increase score
        if ($aiml_pattern == $lookingfor) {
            $allrows[$all]['score'] += $direct_match;
            $allrows[$all]['track_score'] .= "b";
        }
        //if the result topic matches the user stored aiml topic increase score
        if ($aiml_topic == $current_topic && $aiml_topic != "") {
            $allrows[$all]['score'] += $topic_match;
            $allrows[$all]['track_score'] .= "c";
        }
        //if the result that pattern matches the user stored that pattern increase score
        if ($aiml_thatpattern == $current_thatpattern && $aiml_thatpattern != "" && $aiml_pattern != "*") {
            $allrows[$all]['score'] += $that_pattern_match;
            $allrows[$all]['track_score'] .= "d";
        } elseif ($aiml_thatpattern_wildcards != "" && $aiml_thatpattern != "" && $aiml_pattern != "*" && preg_match($aiml_thatpattern_wildcards, $current_thatpattern, $m)) {
            $allrows[$all]['score'] += $that_pattern_match;
            $allrows[$all]['track_score'] .= "e";
        }
        //if the that pattern is just a star we need to score it seperately as this is very general
        if ($aiml_pattern == "*" && (substr($aiml_thatpattern, -1) == "*" || substr($aiml_thatpattern, 0, 1) == "*")) {
            //if the result that pattern matches the user stored that pattern increase score with a lower number
            if ($aiml_thatpattern == $current_thatpattern && $aiml_thatpattern != "") {
                $allrows[$all]['score'] += $that_pattern_match_general;
                $allrows[$all]['track_score'] .= "f";
            } elseif ($aiml_thatpattern_wildcards != "" && $aiml_thatpattern != "" && preg_match($aiml_thatpattern_wildcards, $current_thatpattern, $m)) {
                $allrows[$all]['score'] += $that_pattern_match_general;
                $allrows[$all]['track_score'] .= "g";
            }
        }
        //if stored result == default pattern increase score
        if (strtolower($aiml_pattern) == strtolower($default_aiml_pattern)) {
            $allrows[$all]['score'] += $default_pattern_points;
            $allrows[$all]['track_score'] .= "h";
        } elseif ($aiml_pattern == "*") {
            //if stored result == * increase score
            $allrows[$all]['score'] += $starscore_points;
            $allrows[$all]['track_score'] .= "i";
        } elseif ($aiml_pattern == "_") {
            //if stored result == _ increase score
            $allrows[$all]['score'] += $underscore_points;
            $allrows[$all]['track_score'] .= "j";
        } else {
            //if stored result == none of the above BREAK INTO WORDS AND SCORE INDIVIDUAL WORDS
            $wordsArr = explode(" ", $aiml_pattern);
            foreach ($wordsArr as $index => $word) {
                $word = strtolower(trim($word));
                if (in_Array($word, $commonwordsArr)) {
                    // if it is a commonword increase with (lower) score
                    $allrows[$all]['score'] += $common_word_points;
                    $allrows[$all]['track_score'] .= "k";
                } elseif ($word == "*") {
                    $allrows[$all]['score'] += $starscore_points;
                    //if it is a star wildcard increase score
                    $allrows[$all]['track_score'] .= "l";
                } elseif ($word == "_") {
                    $allrows[$all]['score'] += $underscore_points;
                    //if it is a underscore wildcard increase score
                    $allrows[$all]['track_score'] .= "m";
                } else {
                    $allrows[$all]['score'] += $uncommon_word_points;
                    //else it must be an uncommon word so increase the score
                    $allrows[$all]['track_score'] .= "n";
                }
            }
        }
    }
    //send off for debugging
    sort2DArray("show top scoring aiml matches", $allrows, "score", 1, 10);
    /*
    echo "<pre>";
    print_r($allrows);
    echo "</pre>";
    */
    return $allrows;
    //return the scored rows
}