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 相关文章推荐
PHP中for循环语句的几种变型
Nov 26 PHP
PHP中cookies使用指南
Mar 16 PHP
php一些公用函数的集合
Mar 27 PHP
PHP+SQL 注入攻击的技术实现以及预防办法
Dec 29 PHP
php统计文件大小,以GB、MB、KB、B输出
May 29 PHP
解析PHP获取当前网址及域名的实现代码
Jun 23 PHP
解析yahoo邮件用phpmailer发送的实例
Jun 24 PHP
微信公众平台消息接口校验与消息接口响应实例
Dec 23 PHP
php实现微信发红包
Dec 05 PHP
iOS自定义提示弹出框实现类似UIAlertView的效果
Nov 16 PHP
php安装php_rar扩展实现rar文件读取和解压的方法
Nov 17 PHP
利用laravel搭建一个迷你博客实战教程
Aug 13 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
php使用Image Magick将PDF文件转换为JPG文件的方法
2015/04/01 PHP
php生成固定长度纯数字编码的方法
2015/07/09 PHP
PHP生成图像验证码的方法小结(2种方法)
2016/07/18 PHP
JavaScript初学者建议:不要去管浏览器兼容
2014/02/04 Javascript
编程语言JavaScript简介
2014/10/16 Javascript
使用 TypeScript 重新编写的 JavaScript 坦克大战游戏代码
2015/04/07 Javascript
js闭包所用的场合以及优缺点分析
2015/06/22 Javascript
jQuery Easyui实现左右布局
2016/01/26 Javascript
js停止冒泡和阻止浏览器默认行为的简单方法
2016/05/15 Javascript
基于gulp合并压缩Seajs模块的方式说明
2016/06/14 Javascript
jQuery解析XML 详解及方法总结
2016/09/28 Javascript
Bootstrap CSS布局之表格
2016/12/17 Javascript
JS去掉字符串前后空格或去掉所有空格的用法
2017/03/25 Javascript
node.js遍历目录的方法示例
2018/08/01 Javascript
新版小程序登录授权的方法
2018/12/12 Javascript
Jquery $.map使用方法实例详解
2020/09/01 jQuery
vscode自定义vue模板的实现
2021/01/27 Vue.js
Python的Flask框架中实现简单的登录功能的教程
2015/04/20 Python
通过python实现随机交换礼物程序详解
2019/07/10 Python
django一对多模型以及如何在前端实现详解
2019/07/24 Python
python利用openpyxl拆分多个工作表的工作簿的方法
2019/09/27 Python
python不同系统中打开方法
2020/06/23 Python
解决Python中导入自己写的类,被划红线,但不影响执行的问题
2020/07/13 Python
详解python中的lambda与sorted函数
2020/09/04 Python
python 爬取免费简历模板网站的示例
2020/09/27 Python
美国领先的家居装饰和礼品商店:Kirkland’s
2017/01/30 全球购物
新西兰最大的品牌运动鞋购物网站:Platypus NZ
2017/10/27 全球购物
编写strcpy函数
2014/06/24 面试题
2014年小学植树节活动方案
2014/03/02 职场文书
高一学生评语大全
2014/04/25 职场文书
倡导文明标语
2014/06/16 职场文书
2014年国庆节庆祝建国65周年比赛演讲稿
2014/09/21 职场文书
工作失职造成投诉的检讨书范文
2014/10/05 职场文书
依法行政工作汇报材料
2014/10/28 职场文书
2015年世界艾滋病日活动总结
2015/03/24 职场文书
MySQL创建管理RANGE分区
2022/04/13 MySQL