源码网,源码论坛,源码之家,商业源码,游戏源码下载,discuz插件,棋牌源码下载,精品源码论坛

 找回密码
 立即注册
查看: 336|回复: 11

[PHP编程] php权重计算方法代码分享

[复制链接]

7万

主题

861

回帖

32万

积分

论坛元老

Rank: 8Rank: 8

积分
329525
发表于 2014-1-9 15:10:07 | 显示全部楼层 |阅读模式
权重计算,稍加修改亦可用于分词,词频统计,全文和spam检测等

复制代码 代码如下:
<?php
/* vim: set expandtab tabstop=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]['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;
        }
}

?>

外部调用示例
复制代码 代码如下:
$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);

回复

使用道具 举报

3

主题

1万

回帖

172

积分

注册会员

Rank: 2

积分
172
发表于 2023-7-20 16:21:25 | 显示全部楼层
看看怎么样再说
回复 支持 反对

使用道具 举报

2

主题

1万

回帖

380

积分

中级会员

Rank: 3Rank: 3

积分
380
发表于 2023-8-27 19:21:34 | 显示全部楼层
谢谢下载来看看
回复 支持 反对

使用道具 举报

3

主题

1万

回帖

50

积分

注册会员

Rank: 2

积分
50
发表于 2023-10-13 03:54:10 | 显示全部楼层
啦啦啦啦啦德玛西亚
回复 支持 反对

使用道具 举报

2

主题

1万

回帖

73

积分

注册会员

Rank: 2

积分
73
发表于 2023-11-30 09:42:06 | 显示全部楼层
看看怎么样再说
回复 支持 反对

使用道具 举报

0

主题

1万

回帖

0

积分

中级会员

Rank: 3Rank: 3

积分
0
发表于 2024-5-2 03:47:25 | 显示全部楼层
2222222222222222
回复 支持 反对

使用道具 举报

26

主题

4470

回帖

140

积分

注册会员

Rank: 2

积分
140
发表于 2024-5-27 19:21:52 | 显示全部楼层
呵呵呵呵呵呵呵a
回复 支持 反对

使用道具 举报

2

主题

1万

回帖

73

积分

注册会员

Rank: 2

积分
73
发表于 2024-7-29 14:41:37 | 显示全部楼层
来看看怎么样
回复 支持 反对

使用道具 举报

1

主题

1万

回帖

59

积分

注册会员

Rank: 2

积分
59
发表于 2024-8-19 04:11:53 | 显示全部楼层
灌灌灌灌水
回复 支持 反对

使用道具 举报

20

主题

412

回帖

733

积分

高级会员

Rank: 4

积分
733
发表于 2024-8-26 02:20:22 | 显示全部楼层
很不错的源码论坛
回复 支持 反对

使用道具 举报

高级模式
B Color Image Link Quote Code Smilies

本版积分规则

手机版|小黑屋|网站地图|源码论坛 ( 海外版 )

GMT+8, 2024-9-20 23:38 , Processed in 0.164431 second(s), 26 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表