Beispiel #1
0
/**
 * Performs a pattern matching pass on an input string, checking
 * whether it matches a given pattern based on the AIML specification.
 *
 * @param string $pattern Pattern to match.
 * @param string $input Input string to check.
 * @return bool True if the string matches the pattern, false otherwise.
 **/
function aiml_pattern_match($pattern, $input)
{
    if (empty($input)) {
        return false;
    }
    $pattern_regex = build_wildcard_RegEx(_strtolower($pattern));
    return preg_match($pattern_regex, _strtolower($input)) === 1;
}
Beispiel #2
0
/**
 * Takes all the sql results passed to the function and filters out the irrelevant ones
 *
 * @param array  $convoArr
 * @param array  $allrows
 * @param string $lookingfor
 * @internal param string $current_thatpattern
 * @internal param string $current_topic
 * @return array
 **/
function unset_all_bad_pattern_matches($convoArr, $allrows, $lookingfor)
{
    global $error_response;
    $lookingfor_lc = make_lc($lookingfor);
    $current_topic = get_topic($convoArr);
    $current_thatpattern = isset($convoArr['that'][1][1]) ? $convoArr['that'][1][1] : '';
    $default_pattern = $convoArr['conversation']['default_aiml_pattern'];
    $relevantRow = array();
    //if default pattern keep
    //if wildcard pattern matches found aiml keep
    //if wildcard pattern and wildard thatpattern keep
    //the end......
    runDebug(__FILE__, __FUNCTION__, __LINE__, "NEW FUNC Searching through " . count($allrows) . " rows to unset bad matches", 4);
    if ($allrows[0]['pattern'] == "no results" && count($allrows) == 1) {
        $tmp_rows[0] = $allrows[0];
        $tmp_rows[0]['score'] = 1;
        runDebug(__FILE__, __FUNCTION__, __LINE__, "Returning error as no results where found", 1);
        return $tmp_rows;
    }
    //loop through the results array
    runDebug(__FILE__, __FUNCTION__, __LINE__, 'Blue 5 to Blue leader. Starting my run now!', 4);
    $i = 0;
    foreach ($allrows as $all => $subrow) {
        //get the pattern
        $aiml_pattern = make_lc($subrow['pattern']);
        $aiml_pattern_wildcards = build_wildcard_RegEx($aiml_pattern);
        $default_pattern_lc = make_lc($default_pattern);
        //get the that pattern
        $aiml_thatpattern = make_lc($subrow['thatpattern']);
        //get topic pattern
        $topicMatch = FALSE;
        $aiml_topic = make_lc(trim($subrow['topic']));
        $current_topic_lc = make_lc($current_topic);
        #Check for a matching topic
        $aiml_topic_wildcards = !empty($aiml_topic) ? build_wildcard_RegEx($aiml_topic) : '';
        if ($aiml_topic == '') {
            $topicMatch = TRUE;
        } elseif ($aiml_topic == $current_topic_lc) {
            $topicMatch = TRUE;
        } elseif (!empty($aiml_topic_wildcards)) {
            preg_match($aiml_topic_wildcards, $current_topic_lc, $matches);
            $topicMatch = count($matches) > 0 ? true : false;
        } else {
            $topicMatch = FALSE;
        }
        # check for a matching pattern
        preg_match($aiml_pattern_wildcards, $lookingfor, $matches);
        $aiml_patternmatch = count($matches) > 0 ? true : false;
        # look for a thatpattern match
        if (!empty($aiml_thatpattern)) {
            $aiml_thatpattern_wildcards = build_wildcard_RegEx($aiml_thatpattern);
            preg_match($aiml_thatpattern_wildcards, $current_thatpattern, $matches);
            $aiml_thatpatternmatch = count($matches) > 0 ? true : false;
        } else {
            $aiml_thatpattern_wildcards = FALSE;
        }
        if ($aiml_pattern == $default_pattern_lc) {
            //if it is a direct match with our default pattern then add to tmp_rows
            $tmp_rows[$i]['score'] = 1;
            $tmp_rows[$i]['track_score'] = "default pick up line ({$aiml_pattern} = {$default_pattern}) ";
        } elseif (!$aiml_thatpattern_wildcards && $aiml_patternmatch) {
            $tmp_rows[$i]['score'] = 1;
            $tmp_rows[$i]['track_score'] = " no thatpattern in result and a pattern match";
        } elseif ($aiml_thatpattern_wildcards && $aiml_thatpatternmatch && $aiml_patternmatch) {
            $tmp_rows[$i]['score'] = 2;
            $tmp_rows[$i]['track_score'] = " thatpattern match and a pattern match";
        } elseif ($aiml_pattern == $lookingfor_lc) {
            $tmp_rows[$i]['score'] = 1;
            $tmp_rows[$i]['track_score'] = " direct pattern match";
        } else {
            $tmp_rows[$i]['score'] = -1;
            $tmp_rows[$i]['track_score'] = "dismissing nothing is matched";
        }
        if ($topicMatch === FALSE) {
            $tmp_rows[$i]['score'] = -1;
            $tmp_rows[$i]['track_score'] = "dismissing wrong topic";
        }
        if ($tmp_rows[$i]['score'] >= 0) {
            $relevantRow[] = $subrow;
        }
        $i++;
    }
    $rrCount = count($relevantRow);
    if ($rrCount == 0) {
        $i = 0;
        runDebug(__FILE__, __FUNCTION__, __LINE__, "Error: FOUND NO AIML matches in DB", 1);
        $relevantRow[$i]['aiml_id'] = "-1";
        $relevantRow[$i]['bot_id'] = "-1";
        $relevantRow[$i]['pattern'] = "no results";
        $relevantRow[$i]['thatpattern'] = '';
        $relevantRow[$i]['topic'] = '';
        $relevantRow[$i]['score'] = 0;
    }
    sort2DArray("show top scoring aiml matches", $relevantRow, "good matches", 1, 10);
    runDebug(__FILE__, __FUNCTION__, __LINE__, "Found " . count($relevantRow) . " relevant rows", 4);
    return $relevantRow;
}