php权重计算方法代码分享
<?php
/ vim: set expandtab tabs=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]['a'][] = $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]['a'])) {
$aReturn = array_merge($aReturn, $this->aDict[$iCur]['a']);
$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);
编程语言
- 如何快速学会编程 如何快速学会ug编程
- 免费学编程的app 推荐12个免费学编程的好网站
- 电脑怎么编程:电脑怎么编程网咯游戏菜单图标
- 如何写代码新手教学 如何写代码新手教学手机
- 基础编程入门教程视频 基础编程入门教程视频华
- 编程演示:编程演示浦丰投针过程
- 乐高编程加盟 乐高积木编程加盟
- 跟我学plc编程 plc编程自学入门视频教程
- ug编程成航林总 ug编程实战视频
- 孩子学编程的好处和坏处
- 初学者学编程该从哪里开始 新手学编程从哪里入
- 慢走丝编程 慢走丝编程难学吗
- 国内十强少儿编程机构 中国少儿编程机构十强有
- 成人计算机速成培训班 成人计算机速成培训班办
- 孩子学编程网上课程哪家好 儿童学编程比较好的
- 代码编程教学入门软件 代码编程教程