天天躁日日躁狠狠躁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權重計算方法代碼分享,轉載需保留來源!

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

主站蜘蛛池模板: 老司机无码精品A | 在镜头里被CAO翻了H | 依人青青青在线观看 | 精品久久日日躁夜夜躁AV | 最近中文字幕在线中文视频 | 国产一区二区青青精品久久 | 无码137片内射在线影院 | 欧美人与动牲交XXXXBBBB免费 | 国产在线精彩亚洲 | 日本伦理片 中文字幕 | 好男人视频免费高清在线观看www | 嘟嘟嘟WWW免费高清在线中文 | 99在线视频免费观看视频 | 国产午夜在线观看视频播放 | 亚洲精品无码AAAAAA片 | 最近中文字幕MV免费看 | 精品国产乱码久久久久久免费流畅 | 久久三级视频 | 欧美精品成人一区二区在线观看 | 曰本xxⅹ孕妇性xxx | 九九免费精品视频 | 欧美日韩免费播放一区二区 | 97视频视频人人碰视频 | 国产亚洲精品久久久久久国 | 在线高清视频不卡无码 | 99福利在线| 哒哒哒高清视频在线观看 | 亚洲免费精品 | 全部免费特黄特色大片看片 | 最近中文字幕免费高清MV视频 | 桃隐社区最新最快地址 | 国产中文视频无码成人精品 | 久久欧洲AV无码精品色午夜麻豆 | 国模玲玲自拍337p | 看了n遍舍不得删的黄文 | 色多多深夜福利免费观看 | 无码任你躁久久久久久久 | 日韩在线中文字幕无码 | 泡妞高手在都市免费观看 | 熟妇久久无码人妻AV蜜桃 | 999久久久国产精品蜜臀AV |