php權(quán)重計(jì)算方法代碼分享
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +------------------------------------------------------------------------
// Name : 權(quán)重計(jì)算
// Description: 稍加修改,亦可用于分詞,詞頻統(tǒng)計(jì),全文檢索和垃圾檢測(cè)
// 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)入新的項(xiàng)目
$this->aItems = (is_array($mItems))? $mItems: array($mItems);
$this->init();
}
public function newTable(array $aTable) {
//導(dǎo)入新的對(duì)照表,并生成字典
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)號(hào)
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);
- php編寫的抽獎(jiǎng)程序中獎(jiǎng)概率算法
- php實(shí)現(xiàn)可以設(shè)置中獎(jiǎng)概率的抽獎(jiǎng)程序代碼分享
- 適用于抽獎(jiǎng)程序、隨機(jī)廣告的PHP概率算法實(shí)例
- php抽獎(jiǎng)小程序的實(shí)現(xiàn)代碼
- php實(shí)現(xiàn)概率性隨機(jī)抽獎(jiǎng)代碼
- 基于PHP實(shí)現(xiàn)簡(jiǎn)單的隨機(jī)抽獎(jiǎng)小程序
- 基于PHP代碼實(shí)現(xiàn)中獎(jiǎng)概率算法可用于刮刮卡、大轉(zhuǎn)盤等抽獎(jiǎng)算法
- php實(shí)現(xiàn)自定義中獎(jiǎng)項(xiàng)數(shù)和概率的抽獎(jiǎng)函數(shù)示例
- PHP編程實(shí)現(xiàn)計(jì)算抽獎(jiǎng)概率算法完整實(shí)例
- php簡(jiǎn)單計(jì)算權(quán)重的方法示例【適合抽獎(jiǎng)?lì)悜?yīng)用】
相關(guān)文章
php下獲取Discuz論壇登錄用戶名、用戶組、用戶ID等信息的實(shí)現(xiàn)代碼
前面有一篇文章講過自己從cookie和discuz數(shù)據(jù)庫中取用戶名、用戶組、用戶ID等信息,但那個(gè)并不一定準(zhǔn)確,因?yàn)閐iscuz登錄時(shí)會(huì)使用COOKIE中的auth值來動(dòng)態(tài)登錄,而cookie信息是滯后的,所以獲取到的信息并不完全準(zhǔn)確。2010-12-12
OAuth認(rèn)證協(xié)議中的HMACSHA1加密算法(實(shí)例)
下面小編就為大家?guī)硪黄狾Auth認(rèn)證協(xié)議中的HMACSHA1加密算法(實(shí)例)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-10-10
合并ThinkPHP配置文件以消除代碼冗余的實(shí)現(xiàn)方法
這篇文章主要介紹了合并ThinkPHP配置文件以消除代碼冗余的實(shí)現(xiàn)方法,非常實(shí)用的技巧,需要的朋友可以參考下2014-07-07
Zend Framework框架的數(shù)據(jù)庫追蹤器使用示例
這篇文章主要介紹了Zend Framework框架的數(shù)據(jù)庫追蹤器使用示例,現(xiàn)把追蹤器的使用方法分享給大家,需要的朋友可以參考下2014-03-03
從零開始學(xué)YII2框架(五)快速生成代碼工具 Gii 的使用
用過Yii1框架的Coder都知道,Gii可以為你快速生成代碼,也就是說搭建一個(gè)可以增刪改查的WebApp可能一行代碼都不用寫。上次介紹了如何安裝Yii框架,本次介紹一下如何使用gii工具快速實(shí)現(xiàn)CRUD功能。2014-08-08
在CentOS系統(tǒng)上從零開始搭建WordPress博客的全流程記錄
這篇文章主要介紹了在CentOS系統(tǒng)中從零開始搭建WordPress站點(diǎn)的全流程記錄,使用最大眾的Apache服務(wù)器和MySQL數(shù)據(jù)庫環(huán)境,需要的朋友可以參考下2016-04-04
Thinkphp5.0 框架視圖view的比較標(biāo)簽用法分析
這篇文章主要介紹了Thinkphp5.0 框架視圖view的比較標(biāo)簽用法,結(jié)合實(shí)例形式分析了thinkPHP5框架eq、equal、neq、notequal、egt及switch、range、between等標(biāo)簽相關(guān)用法,需要的朋友可以參考下2019-10-10

