public function sqls(){
		$sqls_cy=include('sqls.config.php');
		if (!$sqls_cy){
			$sqls_cy=include($_SERVER['DOCUMENT_ROOT'].'/oa/config/sqls.config.php');
		}
		$sqls=sort2DArray($sqls_cy,'time','SORT_DESC');
		return $sqls;
	}
 public function sqls()
 {
     $sqls_cy = (include 'sql_cy.php');
     $sqls_ht = (include 'sql_ht.php');
     $sqls = array_merge($sqls_cy, $sqls_ht);
     $sqls = sort2DArray($sqls, 'time', 'SORT_DESC');
     return $sqls;
 }
Example #3
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 $pattern
 * @internal param int $bot_parent_id
 * @internal param string $current_thatpattern
 * @internal param string $current_topic
 * @return array
 **/
function score_matches($convoArr, $allrows, $pattern)
{
    global $common_words_array;
    runDebug(__FILE__, __FUNCTION__, __LINE__, "Scoring the matches.", 4);
    # obtain some values to test
    $topic = get_topic($convoArr);
    $that = isset($convoArr['that'][1][1]) ? $convoArr['that'][1][1] : '';
    $default_pattern = $convoArr['conversation']['default_aiml_pattern'];
    $bot_parent_id = $convoArr['conversation']['bot_parent_id'];
    $bot_id = $convoArr['conversation']['bot_id'];
    # set the scores for each type of word or sentence to be used in this function
    # full pattern match scores:
    $this_bot_match = 250;
    $underscore_match = 100;
    $topic_underscore_match = 80;
    $topic_direct_match = 50;
    $topic_star_match = 10;
    $thatpattern_underscore_match = 45;
    $thatpattern_direct_match = 15;
    $thatpattern_star_match = 2;
    $direct_pattern_match = 10;
    $pattern_direct_match = 7;
    $pattern_star_match = 1;
    $default_pattern_match = 5;
    # individual word match scores:
    $uncommon_word_match = 8;
    $common_word_match = 1;
    $direct_word_match = 2;
    $underscore_word_match = 25;
    $star_word_match = 1;
    $rejected = -1000;
    # loop through all relevant results
    foreach ($allrows as $all => $subrow) {
        $category_bot_id = isset($subrow['bot_id']) ? $subrow['bot_id'] : 1;
        $category_topic = $subrow['topic'];
        $category_thatpattern = $subrow['thatpattern'];
        $category_pattern = $subrow['pattern'];
        $check_pattern_words = true;
        # make it all lower case, to make it easier to test, and do it using mbstring functions if possible
        $category_pattern_lc = _strtolower($category_pattern);
        $category_thatpattern_lc = _strtolower($category_thatpattern);
        $category_topic_lc = _strtolower($category_topic);
        $default_pattern_lc = _strtolower($default_pattern);
        $pattern_lc = _strtolower($pattern);
        $topic_lc = _strtolower($topic);
        $that_lc = _strtolower($that);
        // Start scoring here
        $current_score = 0;
        $track_matches = '';
        # 1.) Check for current bot, rather than parent
        if ($category_bot_id == $bot_id) {
            $current_score += $this_bot_match;
            $track_matches .= 'current bot, ';
        } elseif ($category_bot_id == $bot_parent_id) {
            $current_score += 0;
            $track_matches .= 'parent bot, ';
        } else {
            $current_score = $rejected;
            runDebug(__FILE__, __FUNCTION__, __LINE__, 'Found an error trying to identify the chatbot.', 1);
            unset($allrows[$all]);
            continue;
        }
        # 2.) test for a non-empty  current topic
        if (!empty($topic)) {
            # 2a.) test for a non-empty topic in the current category
            if (empty($category_topic) || $category_topic == '*') {
                // take no action, as we're not looking for a topic here
                $track_matches .= 'no topic to match, ';
            } else {
                # 2b.) create a RegEx to test for underscore matches
                if (strpos($category_topic, '_') !== false) {
                    $regEx = str_replace('_', '(.*)', $category_topic);
                    if ($regEx != $category_topic && preg_match("/{$regEx}/", $topic) === 1) {
                        $current_score += $topic_underscore_match;
                        $track_matches .= 'topic match with underscore, ';
                    }
                } elseif ($topic == $category_topic) {
                    $current_score += $topic_direct_match;
                    $track_matches .= 'direct topic match, ';
                } else {
                    $regEx = str_replace(array('*', '_'), '(.*)', $category_topic);
                    if (preg_match("/{$regEx}/", $topic)) {
                        $current_score += $topic_star_match;
                        $track_matches .= 'topic match with wildcards';
                    }
                }
            }
        }
        # end topic testing
        # 3.) test for a category thatpattern
        if (empty($category_thatpattern) || $category_thatpattern == '*') {
            $current_score += 1;
            $track_matches .= 'no thatpattern to match, ';
        } else {
            if (strpos($category_thatpattern, '_') !== false) {
                # 3a.) Create a RegEx to search for underscore wildcards
                $regEx = str_replace('_', '(.*)', $category_thatpattern);
                if ($regEx !== $category_thatpattern && preg_match("/{$regEx}/i", $that) === 1) {
                    $current_score += $thatpattern_underscore_match;
                    $track_matches .= 'thatpattern match with underscore, ';
                }
            } elseif ($that_lc == $category_thatpattern_lc) {
                $current_score += $thatpattern_direct_match;
                $track_matches .= 'direct thatpattern match, ';
            } elseif (strstr($category_thatpattern_lc, '*') !== false) {
                $regEx = str_replace(array('*', '_'), '(.*)', $category_thatpattern);
                if (preg_match("/{$regEx}/", $that)) {
                    $current_score += $thatpattern_star_match;
                    $track_matches .= 'thatpattern match with star, ';
                }
            } else {
                $current_score = $rejected;
                $track_matches .= 'no thatpattern match at all, ';
                runDebug(__FILE__, __FUNCTION__, __LINE__, "Matching '{$that_lc}' with '{$category_thatpattern_lc}' failed. Drat!'", 4);
            }
        }
        # end thatpattern testing
        # 4.) pattern testing
        # 4a.) Create a RegEx to search for underscore wildcards
        if (strpos($category_pattern, '_') !== false) {
            $regEx = str_replace('_', '(.*)', $category_pattern);
            //save_file(_LOG_PATH_ . 'regex.txt', "$regEx\n", true);
            if ($regEx != $category_pattern && preg_match("/{$regEx}/", $pattern) === 1) {
                $current_score += $underscore_match;
                $track_matches .= 'pattern match with underscore, ';
            }
        } elseif ($pattern == $category_pattern) {
            $current_score += $pattern_direct_match;
            $track_matches .= 'direct pattern match, ';
            //$check_pattern_words  = false;
        } else {
            $regEx = str_replace(array('*', '_'), '(.*?)', $category_pattern);
            if ($category_pattern == '*') {
                $current_score += $pattern_star_match;
                $track_matches .= 'pattern star match, ';
                $check_pattern_words = false;
            } elseif ($regEx != $category_pattern && ($category_pattern != '*' || $category_pattern != '_') && preg_match("/{$regEx}/", $pattern) != 0) {
            }
        }
        # end pattern testing
        # 4d.) See if the current category is the default category
        if ($category_pattern == $default_pattern_lc) {
            runDebug(__FILE__, __FUNCTION__, __LINE__, 'This category is the default pattern!', 4);
            $current_score += $default_pattern_match;
            $track_matches .= 'default pattern match, ';
            $check_pattern_words = false;
        }
        #5.) check to see if we need to score word by word
        if ($check_pattern_words && $category_pattern_lc != $default_pattern_lc) {
            # 5a.) first, a little setup
            $pattern_lc = _strtolower($pattern);
            $category_pattern_lc = _strtolower($category_pattern);
            $pattern_words = explode(" ", $pattern_lc);
            # 5b.) break the input pattern into an array of individual words and iterate through the array
            $category_pattern_words = explode(" ", $category_pattern_lc);
            foreach ($category_pattern_words as $word) {
                $word = trim($word);
                switch (true) {
                    case $word === '_':
                        $current_score += $underscore_word_match;
                        $track_matches .= 'underscore word match, ';
                        break;
                    case $word === '*':
                        $current_score += $star_word_match;
                        $track_matches .= 'star word match, ';
                        break;
                    case in_array($word, $pattern_words):
                        $current_score += $direct_word_match;
                        $track_matches .= "direct word match: {$word}, ";
                        break;
                    case in_array($word, $common_words_array):
                        $current_score += $common_word_match;
                        $track_matches .= "common word match: {$word}, ";
                        break;
                    default:
                        $current_score += $uncommon_word_match;
                        $track_matches .= "uncommon word match: {$word}, ";
                }
            }
        }
        $allrows[$all]['score'] += $current_score;
        $allrows[$all]['track_score'] = rtrim($track_matches, ', ');
    }
    //runDebug(__FILE__, __FUNCTION__, __LINE__, "Unsorted array \$allrows:\n" . print_r($allrows, true), 4);
    $allrows = 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;
}
Example #4
0
 public function sqls()
 {
     $sqls_cy = (include 's.php');
     $sqls = sort2DArray($sqls_cy, 'time', 'SORT_DESC');
     return $sqls;
 }
Example #5
0
 function action_update()
 {
     $articleObj = bpBase::loadAppClass('articleObj', 'article');
     $channelObj = bpBase::loadAppClass('channelObj', 'channel');
     $thisContent = $articleObj->getContentByID($_GET['id']);
     $channelID = $thisContent->channel_id;
     $thisChannel = $channelObj->getChannelByID($channelID);
     $this->_access_ContentUpdate($thisChannel);
     if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST') {
         $dates = explode('-', $_POST['adddate']);
         $times = explode(':', $_POST['addtime']);
         $time = mktime(intval($times[0]), intval($times[1]), intval($times[2]), intval($dates[1]), intval($dates[2]), intval($dates[0]));
         //
         $title = $_POST['title'];
         if ($thisChannel->channeltype == 3) {
             //auto
             $auto_db = bpBase::loadModel('autoclassification_model');
             $thisAuto = $auto_db->getCfByID(intval($_POST['autoid']));
             if (!strlen($title)) {
                 $title = $thisAuto->name;
             }
         }
         //处理子页标题和内容
         $contentsArr = array();
         if ($_POST['content']) {
             foreach ($_POST['content'] as $k => $v) {
                 $pageTitle = $_POST['pageTitle'][$k];
                 $order = $_POST['order'][$k];
                 //
                 array_push($contentsArr, array('order' => $order, 'pageTitle' => $pageTitle, 'content' => $v));
             }
             $contentsArr = sort2DArray($contentsArr, 'order');
         }
         $titles = '';
         $contentStr = '';
         $comma = '';
         $contentSep = '';
         foreach ($contentsArr as $c) {
             $titles .= $comma . $c['pageTitle'];
             $comma = '|';
             if (strlen($c['content'])) {
                 $contentStr .= $contentSep . $c['content'];
                 $contentSep = '<div style="page-break-after:always"><span style="display:none">&nbsp;</span></div>';
             }
         }
         //clear link
         if (intval($_POST['clearhref'])) {
             $contentStr = clearHtmlTagA($contentStr);
         }
         $canComment = intval($_POST['closeComment']) ? 0 : 1;
         //thumb
         $thumb = $_POST['thumb'];
         if (!$thumb && $_POST['autoThumb']) {
             $thumb = $this->_setFirstImageAsThumb($thisChannel, $contentStr, $_POST['autoThumbNo']);
         }
         //$intro
         $intro = $_POST['intro'];
         if (!$intro && $_POST['autoIntro']) {
             $stag = bpBase::loadAppClass('stag', 'template');
             //
             $handledStagHtml = $stag->handleStag($contentStr);
             $handledStagHtml = remove_html_tag($handledStagHtml);
             $intro = mb_substr($handledStagHtml, 0, intval($_POST['autoIntroLen']), 'gbk');
         }
         $row = array('title' => $title, 'subtitle' => $_POST['subtitle'], 'link' => $_POST['link'], 'externallink' => intval($_POST['externallink'][0]), 'thumb' => $thumb, 'content' => $contentStr, 'intro' => $intro, 'author' => $_POST['author'], 'source' => $_POST['source'], 'uid' => intval($_SESSION['cmsuid']), 'time' => $time, 'last_update' => SYS_TIME, 'keywords' => $_POST['keywords'], 'cancomment' => $canComment, 'titles' => $titles, 'geoid' => intval($_POST['geo_id']), 'pagecount' => 0, 'sourcetype' => intval($_POST['sourcetype']));
         $updateCondition = array('id' => $thisContent->id);
         $ocontent = $thisContent;
         if (intval($ocontent->channel_id) != 1) {
             $row['content'] = $this->_addAutoLink($row['content']);
         }
         //
         $row['pagecount'] = $this->_calContentPageCount($row['content']);
         $row['content'] = $articleObj->autoSaveRemoteImage($row['content']);
         //
         $siteObj = bpBase::loadAppClass('siteObj', 'site');
         $thisSite = $siteObj->getSiteByID($thisChannel->site);
         $row['content'] = str_replace('src="/', 'src="http://' . $_SERVER['HTTP_HOST'] . '/', $row['content']);
         //
         if (substr($row['keywords'], 0, 1) != ',') {
             $row['keywords'] = ',' . $row['keywords'];
         }
         $row['content'] = str_replace(array("'"), array('&#039;'), $row['content']);
         //$row['content']=$this->closetags($row['content']);
         $rt = $this->article_db->update($row, $updateCondition);
         if ($rt) {
             if ($row['geoid']) {
                 $this->_clearContentCacheWithGeoid($thisChannel, $row['geoid']);
             }
             if ($ocontent->geoid != $row['geoid']) {
                 $this->_clearContentCacheWithGeoid($thisChannel, $ocontent->geoid);
             }
             delCache('autoContentsOfChannel' . $ocontent->channel_id);
             delCache('c_content' . $thisContent->id);
             //生成内容页,或者更新链接地址
             $tpl = bpBase::loadAppClass('template', 'template');
             $tpl->createContentPageR($thisContent->id, $thisChannel);
             //delete thumb
             if ($ocontent->thumb && $ocontent->thumb != $row['thumb']) {
                 $this->_deleteThumb($id, $ocontent->thumb);
             }
             //clear cache
             $articleObj->clearContentsCache($ocontent->channel_id, 'update', $thisChannel);
             //内容组
             $contentgroup_content_db = bpBase::loadModel('contentgroup_content_model');
             $contentid = $thisContent->id;
             if (defined('CMS_CITY_ID')) {
                 //zzqcw
                 $geoid = CMS_CITY_ID;
                 $groupIDsOfThisContent = $contentgroup_content_db->select(array('geoid' => $row['geoid'], 'contentid' => $thisContent->id), 'groupid');
             } else {
                 $geoid = $row['geoid'];
                 //$groupIDsOfThisContent=$contentgroup_content_db->select(array('contentid'=>$thisContent->id),'groupid');
             }
             //如果该内容不在选定的内容组里面,则添加
             $postGroupIDs = array();
             if ($_POST['contentGroup']) {
                 foreach ($_POST['contentGroup'] as $k => $v) {
                     if (!$groupIDsOfThisContent || $groupIDsOfThisContent && !in_array(array('groupid' => $v), $groupIDsOfThisContent)) {
                         $contentgroup_content_db->insert(array('groupid' => $v, 'contentid' => $thisContent->id, 'title' => $row['title'], 'geoid' => $geoid, 'taxis' => $thisContent->taxis));
                         delCache('contentsOfGroup' . $v . 'geoid' . $geoid);
                     }
                     array_push($postGroupIDs, $v);
                 }
             }
             //如果原来内容组有该内容,而现在没有则删除
             if ($groupIDsOfThisContent) {
                 foreach ($groupIDsOfThisContent as $k => $v) {
                     if (!in_array($v['groupid'], $postGroupIDs)) {
                         $this->_deleteContentInGroup($thisContent->id, $v['groupid']);
                     }
                 }
             }
         }
         //专题内容自动生成专题首页
         if ($thisContent->site > 99) {
             $tpl = bpBase::loadAppClass('template', 'template');
             $tpl->createIndexPage($thisContent->site);
         }
         delCache('c_contentsOf' . $thisContent->channel_id);
         showMessage(L('updateSuccess'), $_POST['referer']);
         //echo '<script>window.location.href=\'/'.CMS_DIR.'/cachesAction.php?actionType=content_update&autoids='.$row['autoid'].'&oldautoids='.$thisContent->autoid.'&contentid='.$thisContent->id.'&channelid='.$thisContent->channel_id.'&site='.$thisContent->site.'\';</script>';
     }
 }
Example #6
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
}
Example #7
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
}