php權(quán)重計算方法代碼分享
<?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) {
//導(dǎo)入新的項目
$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)用示例
$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)文章
網(wǎng)頁的分頁下標(biāo)生成代碼(PHP后端方法)
網(wǎng)頁的分頁選擇效果直接影響用戶的使用體驗。類似功能的方法有很多,我在這里寫的方法主要是的優(yōu)勢在于前后端分離,可以自己定義長度和分頁的行數(shù)2016-02-02php數(shù)字運(yùn)算驗證碼的實現(xiàn)代碼
這篇文章主要介紹了php實現(xiàn)數(shù)字運(yùn)算驗證碼的方法,具有一定借鑒價值,需要的朋友可以參考下2015-07-07解決laravel上傳圖片之后,目錄有圖片,但是訪問不到(404)的問題
今天小編就為大家分享一篇解決laravel上傳圖片之后,目錄有圖片,但是訪問不到(404)的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-10-10Yii入門教程之目錄結(jié)構(gòu)、入口文件及路由設(shè)置
本文從YII的目錄結(jié)構(gòu)開始分析,到入口文件分析,到路由設(shè)置詳解,視圖詳解,十分全面的向我們展示了YII框架的方方面面,是篇非常不錯的文章,這里推薦給大家。2014-11-11PHP中把數(shù)據(jù)庫查詢結(jié)果輸出為json格式簡單實例
這篇文章主要介紹了PHP中把數(shù)據(jù)庫查詢結(jié)果輸出為json格式簡單實例,本文直接給出示例代碼,都是非?;A(chǔ)的寫法,大家應(yīng)該一看就明白,需要的朋友可以參考下2015-04-04