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

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

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

復制代碼 代碼如下:

<?php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +------------------------------------------------------------------------
//  Name       :   權(quán)重計算                                        
//  Description:   稍加修改,亦可用于分詞,詞頻統(tǒng)計,全文檢索和垃圾檢測
//  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) {
  //導入新的項目
  $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') {
  //獲取最終的顯示結(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; //當前節(jié)點號
        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)用示例

復制代碼 代碼如下:

$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站點的全流程記錄,使用最大眾的Apache服務器和MySQL數(shù)據(jù)庫環(huán)境,需要的朋友可以參考下
    2016-04-04
  • Thinkphp5.0 框架視圖view的比較標簽用法分析

    Thinkphp5.0 框架視圖view的比較標簽用法分析

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

    安顺市| 商丘市| 禹州市| 西林县| 江城| 泾阳县| 太仓市| 麻城市| 武城县| 麻城市| 乳山市| 保定市| 聂荣县| 泌阳县| 屏南县| 浦北县| 林甸县| 冕宁县| 临猗县| 都昌县| 封丘县| 上栗县| 田林县| 明星| 侯马市| 乐业县| 普格县| 仁寿县| 贡觉县| 博白县| 吉首市| 会东县| 微山县| 五原县| 锦屏县| 龙门县| 潍坊市| 合作市| 喜德县| 永吉县| 谢通门县|