php权重计算方法代码分享


Posted in PHP onJanuary 09, 2014
<?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);
PHP 相关文章推荐
Windows下PHP的任意文件执行漏洞
Oct 09 PHP
php的curl实现get和post的代码
Aug 23 PHP
PHP 防恶意刷新实现代码
May 16 PHP
php获取远程图片的两种 CURL方式和sockets方式获取远程图片
Nov 07 PHP
用PHP提取中英文词语以及数字的首字母的方法介绍
Apr 23 PHP
分享PHP header函数使用教程
Sep 05 PHP
PHP对文件进行加锁、解锁实例
Jan 23 PHP
PHP超牛逼无限极分类生成树方法
May 11 PHP
PHP实现的防止跨站和xss攻击代码【来自阿里云】
Jan 29 PHP
php基于Redis消息队列实现的消息推送的方法
Nov 28 PHP
PHP判断函数是否被定义的方法
Jun 21 PHP
php实现网页上一页下一页翻页过程详解
Jun 28 PHP
php实现分页工具类分享
Jan 09 #PHP
codeigniter框架批量插入数据
Jan 09 #PHP
eaglephp使用微信api接口开发微信框架
Jan 09 #PHP
百度站点地图(百度sitemap)生成方法分享
Jan 09 #PHP
利用phpexcel把excel导入数据库和数据库导出excel实现
Jan 09 #PHP
php将mysql数据库整库导出生成sql文件的具体实现
Jan 08 #PHP
PHP修改session_id示例代码
Jan 08 #PHP
You might like
浅析SVN常见问题及解决方法
2013/06/21 PHP
在laravel中实现ORM模型使用第二个数据库设置
2019/10/24 PHP
javascript中的location用法简单介绍
2007/03/07 Javascript
中文输入法不触发onkeyup事件的解决办法
2014/07/09 Javascript
初始Nodejs
2014/11/08 NodeJs
JavaScript检测鼠标移动方向的方法
2015/05/22 Javascript
基于Bootstrap实现的下拉菜单手机端不能选择菜单项的原因附解决办法
2016/07/22 Javascript
AngularJS 指令的交互详解及实例代码
2016/09/14 Javascript
微信小程序 swiper组件详解及实例代码
2016/10/25 Javascript
jQuery实现倒计时重新发送短信验证码功能示例
2017/01/12 Javascript
webpack vue项目开发环境局域网访问方法
2018/03/20 Javascript
layui从数据库中获取复选框的值并默认选中方法
2018/08/15 Javascript
使vue实现jQuery调用的两种方法
2019/05/12 jQuery
Vue路由守卫及页面登录权限控制的设置方法(两种)
2020/03/31 Javascript
js实现省级联动(数据结构优化)
2020/07/17 Javascript
vue点击Dashboard不同内容 跳转到同一表格的实例
2020/11/13 Javascript
如何利用nodejs实现命令行游戏
2020/11/24 NodeJs
[58:58]2018DOTA2亚洲邀请赛 4.4 淘汰赛 TNC vs VG 第二场
2018/04/05 DOTA
[02:28]PWL开团时刻DAY3——Ink Ice与DeMonsTer之间的勾心斗角
2020/11/03 DOTA
跟老齐学Python之一个免费的实验室
2014/09/14 Python
在Gnumeric下使用Python脚本操作表格的教程
2015/04/14 Python
Python实现合并字典的方法
2015/07/07 Python
在arcgis使用python脚本进行字段计算时是如何解决中文问题的
2015/10/18 Python
python3使用PyMysql连接mysql数据库实例
2017/02/07 Python
Python Paramiko模块的使用实际案例
2018/02/01 Python
Python下载网络文本数据到本地内存的四种实现方法示例
2018/02/05 Python
python计算列表内各元素的个数实例
2018/06/29 Python
Python多进程与服务器并发原理及用法实例分析
2018/08/21 Python
Python类和对象的定义与实际应用案例分析
2018/12/27 Python
Python3实现统计单词表中每个字母出现频率的方法示例
2019/01/28 Python
python单线程文件传输的实例(C/S)
2019/02/13 Python
CSS3中的元素过渡属性transition示例详解
2016/11/30 HTML / CSS
法国设计制造的扫帚和刷子:Andrée Jardin
2018/12/06 全球购物
Brasty波兰:香水、化妆品、手表网上商店
2019/04/15 全球购物
如何理解transaction事务的概念
2015/05/27 面试题
个人评价范文分享
2014/01/11 职场文书