最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

php權(quán)重計(jì)算方法代碼分享

 更新時間:2014年01月09日 15:10:07   作者:  
權(quán)重計(jì)算,稍加修改亦可用于分詞,詞頻統(tǒng)計(jì),全文和spam檢測等

復(fù)制代碼 代碼如下:

<?php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +------------------------------------------------------------------------
//  Name       :   權(quán)重計(jì)算                                        
//  Description:   稍加修改,亦可用于分詞,詞頻統(tǒng)計(jì),全文檢索和垃圾檢測
//  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() {
  //清空記錄的匹配表和輸出結(jié)果
  unset($this->aShow);
 }

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

 public function newTable(array $aTable) {
        //導(dǎo)入新的對照表,并生成字典
        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') {
  //獲取最終的顯示結(jié)果
        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; //當(dāng)前節(jié)點(diǎn)號
        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;
        }
}

?>

外部調(diào)用示例

復(fù)制代碼 代碼如下:

$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);

相關(guān)文章

  • 在CentOS系統(tǒng)上從零開始搭建WordPress博客的全流程記錄

    在CentOS系統(tǒng)上從零開始搭建WordPress博客的全流程記錄

    這篇文章主要介紹了在CentOS系統(tǒng)中從零開始搭建WordPress站點(diǎn)的全流程記錄,使用最大眾的Apache服務(wù)器和MySQL數(shù)據(jù)庫環(huán)境,需要的朋友可以參考下
    2016-04-04
  • Thinkphp5.0 框架視圖view的比較標(biāo)簽用法分析

    Thinkphp5.0 框架視圖view的比較標(biāo)簽用法分析

    這篇文章主要介紹了Thinkphp5.0 框架視圖view的比較標(biāo)簽用法,結(jié)合實(shí)例形式分析了thinkPHP5框架eq、equal、neq、notequal、egt及switch、range、between等標(biāo)簽相關(guān)用法,需要的朋友可以參考下
    2019-10-10
  • 最新評論

    桃源县| 中西区| 文昌市| 新和县| 呼玛县| 志丹县| 囊谦县| 民县| 绥江县| 星子县| 淄博市| 灌云县| 武城县| 建始县| 淄博市| 通榆县| 通州区| 晋州市| 莫力| 蓝山县| 永顺县| 沂源县| 道真| 海口市| 东台市| 涿鹿县| 柳州市| 江津市| 崇仁县| 镇沅| 张家界市| 革吉县| 宁波市| 黎平县| 农安县| 濮阳县| 张北县| 东光县| 侯马市| 开江县| 和静县|