中文字幕日韩一区二区_国产一区二区av_国产毛片av_久久久久国产一区_色婷婷电影_国产一区二区精品

php權重計算方法代碼分享

復制代碼 代碼如下:
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +------------------------------------------------------------------------
//  Name       :   權重計算                                        
//  Description:   稍加修改,亦可用于分詞,詞頻統計,全文檢索和垃圾檢測
//  Date       :   2013/12/16 08:51

class weight {
    protected $aDict = array(array());
    protected $aItems = array();
    protected $sLastRule;
    protected $aMatchs = array();
    protected $aShow = array();

 private function init() {
  //清空記錄的匹配表和輸出結果
  unset($this->aShow);
 }

    public function newItems($mItems) {
  //導入新的項目
  $this->aItems = (is_array($mItems))? $mItems: array($mItems);
  $this->init();
 }

 public function newTable(array $aTable) {
        //導入新的對照表,并生成字典
        foreach($aTable as $iTableKey=>$sTableLine) {
            $aTableLine = explode(',', str_replace('|', ',', $sTableLine));
            $setter = function($v, $k, $paraMeter) {
                $k1 = $paraMeter[0]; $oWeight = $paraMeter[1];
                $oWeight->genDict($v, $k1);
            };
            array_walk($aTableLine, $setter, array($iTableKey, $this));
        }
        $this->init();
 }

    public function getShow($sRule = 'max') {
  //獲取最終的顯示結果
        if(empty($this->aItems) || empty($this->aDict))
            return array();
  if (empty($this->aShow) || $sRule != $this->sLastRule)
            return $this->genShow($sRule);
        return $this->aShow;
 }

    public function genShow($sRule) {
        $aShow = array();
        $aMatchs = array();
  $getter = function($v, $k, $oWeight) use(&$aShow, &$aMatchs, $sRule) {
   $t = array_count_values($oWeight->matchWord($v));
            $aMatchs[] = $t;
            switch ($sRule) {
                case 'max':
                    $aShow[$k] = array_keys($t, max($t)); 
                    break;
            }
  };
  array_walk($this->aItems, $getter, $this);
  $this->aShow = $aShow;
  $this->aMatchs = $aMatchs;
  return $aShow;
    }

    private function genDict($mWord, $iKey = '') {
        $iInsertPonit = count($this->aDict);
        $iCur = 0; //當前節點號
        foreach (str_split($mWord) as $iChar) {
            if (isset($this->aDict[$iCur][$iChar])) {
                $iCur = $this->aDict[$iCur][$iChar];
                continue;
            }
            $this->aDict[$iInsertPonit] = array();
            $this->aDict[$iCur][$iChar] = $iInsertPonit;
            $iCur = $iInsertPonit;
            $iInsertPonit++;
        }
        $this->aDict[$iCur]['acc'][] = $iKey;

    }

        function matchWord($sLine) {
            $iCur = $iOffset = $iPosition = 0;
            $sLine .= "/0";
            $iLen = strlen($sLine);
            $aReturn = array();
            while($iOffset < $iLen) {
                $sChar = $sLine{$iOffset};
                if(isset($this->aDict[$iCur][$sChar])) {
                    $iCur = $this->aDict[$iCur][$sChar];
                    if(isset($this->aDict[$iCur]['acc'])) {
                        $aReturn = array_merge($aReturn, $this->aDict[$iCur]['acc']);

                        $iPosition = $iOffset + 1;
                        $iCur = 0;
                    }
                } else {
                    $iCur = 0;
                    $iOffset = $iPosition;
                    $iPosition = $iOffset + 1;
                }
                ++$iOffset;
            }
            return $aReturn;
        }
}

?>

外部調用示例
復制代碼 代碼如下:
$aItems = array(
    'chinaisbig',
    'whichisnot',
    'totalyrightforme',
);
$aTable = array(
    'china,is|small',
    'china,big|me',
    'china,is|big,which|not,me',
    'totaly|right,for,me',
);

$oWeight = new ttrie;
$oWeight->newItems($aItems);
$aResult = $oWeight->newTable($aTable);

php技術php權重計算方法代碼分享,轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

主站蜘蛛池模板: 亚洲国产精品视频一区 | 成人精品啪啪欧美成 | 美女激情av| 国产精品伦一区二区三级视频 | 欧美成人精品一区二区男人看 | av男人的天堂av | 欧美日韩国产一区二区三区 | 成人看片在线观看 | 国产精品美女久久久 | 一区精品国产欧美在线 | 伊人久久精品一区二区三区 | 日韩欧美精品一区 | 成人精品一区二区三区中文字幕 | 99精品九九 | 久久久久欧美 | 成人高清视频在线观看 | 天天干天天色 | 久久久久久综合 | 精品国产一区二区三区观看不卡 | 宅女噜噜66国产精品观看免费 | 久久久久一区二区三区四区 | 久久久久久国产一区二区三区 | 日韩精品在线一区 | h片在线免费观看 | 99视频在线 | 欧美成人免费在线视频 | 成人在线中文字幕 | 色婷婷av99xx| 成人在线视频免费播放 | 亚洲精品毛片av | 91麻豆产精品久久久久久 | 欧美精品一区二区三区在线播放 | 成人免费一区二区三区视频网站 | 国产中文字幕网 | 日韩看片 | 欧美日韩一区二区在线观看 | 日韩资源 | 91就要激情 | 色婷婷精品国产一区二区三区 | 国产在线精品一区二区三区 | 精品亚洲一区二区三区 |