在處理關鍵字權重、分詞、詞頻統計或垃圾內容檢測等場景時,高效的字典匹配算法至關重要。本文分享一個基於 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);
此類別可用於:
通過調整規則表與匹配策略,能快速適配不同業務需求。