萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網路程式設計 >> PHP 程式設計

PHP 權重計算類實現與應用詳解

前言

在處理關鍵字權重、分詞、詞頻統計或垃圾內容檢測等場景時,高效的字典匹配算法至關重要。本文分享一個基於 Trie 樹(字典樹)實現的 PHP 權重計算類,支持多模式匹配並可靈活擴展。

核心類結構

weight 類封裝了字典構建、模式匹配與結果聚合功能:

  • 屬性:$aDict 存儲 Trie 節點,$aItems 待匹配項目,$aShow 緩存結果。
  • 初始化:init() 清空匹配緩存。
  • 導入數據:newItems() 設置待匹配字符串陣列;newTable() 解析規則表並構建字典。
  • 匹配邏輯:genDict() 逐字符插入 Trie,終節點記錄規則鍵值;matchWord() 掃描文本,命中時輸出對應鍵值並重置狀態。
  • 結果生成:getShow() 根據規則(如 'max' 取最高頻)返回最終匹配結果。

代碼示例

<?php
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 .= "";
        $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 weight();
$oWeight->newItems($aItems);
$oWeight->newTable($aTable);
$result = $oWeight->getShow('max');
print_r($result);

應用場景

此類別可用於:

  1. 關鍵字權重評分
  2. 中文分詞輔助
  3. 詞頻統計分析
  4. 全文檢索預處理
  5. 垃圾內容(Spam)特徵檢測

通過調整規則表與匹配策略,能快速適配不同業務需求。

copyright © 萬盛學電腦網 all rights reserved