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 相关文章推荐
实现了一个PHP5的getter/setter基类的代码
Feb 25 PHP
php设计模式 Chain Of Responsibility (职责链模式)
Jun 26 PHP
PHP的可变变量名的使用方法分享
Feb 05 PHP
基于PHP静态类的原罪详解
May 06 PHP
php中利用str_pad函数生成数字递增形式的产品编号
Sep 30 PHP
PHP取余函数介绍MOD(x,y)与x%y
May 15 PHP
PHP实现的连贯操作、链式操作实例
Jul 08 PHP
功能强大的PHP图片处理类(水印、透明度、旋转)
Oct 21 PHP
PHP实现的oracle分页函数实例
Jan 25 PHP
php强大的时间转换函数strtotime
Feb 18 PHP
Zend Framework框架之Zend_Mail实现发送Email邮件验证功能及解决标题乱码的方法
Mar 21 PHP
作为PHP程序员你要知道的另外一种日志
Jul 30 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迅雷、快车、旋风下载专用链转换代码
2010/06/15 PHP
深入php socket的讲解与实例分析
2013/06/13 PHP
php遍历删除整个目录及文件的方法
2015/03/13 PHP
捕获键盘事件(且兼容各浏览器)
2013/07/03 Javascript
巧用js提交表单轻松解决一个页面有多个提交按钮
2013/11/17 Javascript
javascript 使用for循环时该注意的问题-附问题总结
2015/08/19 Javascript
iscroll.js的上拉下拉刷新时无法回弹的解决方法
2016/02/18 Javascript
Bootstrap表单布局样式源代码
2016/07/04 Javascript
jQuery ajaxForm()的应用
2016/10/14 Javascript
Javascript+CSS3实现进度条效果
2016/10/28 Javascript
Bootstrap jquery.twbsPagination.js动态页码分页实例代码
2017/02/20 Javascript
jquery 禁止鼠标右键并监听右键事件
2017/04/27 jQuery
Grunt针对静态文件的压缩,版本控制打包的实例讲解
2017/09/29 Javascript
Vue组件创建和传值的方法
2018/08/17 Javascript
Vue 中如何正确引入第三方模块的方法步骤
2019/05/05 Javascript
vue实现多级菜单效果
2019/10/19 Javascript
jQuery实现动态加载瀑布流
2020/09/01 jQuery
vuex的使用步骤
2021/01/06 Vue.js
Python paramiko模块的使用示例
2018/04/11 Python
python 利用turtle模块画出没有角的方格
2019/11/23 Python
tensorflow 实现打印pb模型的所有节点
2020/01/23 Python
如何写python的配置文件
2020/06/07 Python
Python使用tkinter实现摇骰子小游戏功能的代码
2020/07/02 Python
python实现sm2和sm4国密(国家商用密码)算法的示例
2020/09/26 Python
英国最大的奢侈珠宝和手表网站:C W Sellors
2017/02/10 全球购物
澳大利亚足球鞋和服装购物网站:Ultra Football
2018/10/11 全球购物
安德玛菲律宾官网:Under Armour菲律宾
2020/07/28 全球购物
程序员经常用到的UNIX命令
2015/04/13 面试题
团员学习总结的自我评价范文
2013/10/14 职场文书
财务管理专业推荐信
2013/11/19 职场文书
工程管理专业个人求职信范文
2013/12/07 职场文书
毕业生求职信范文
2014/06/29 职场文书
公务员爱岗敬业演讲稿
2014/08/26 职场文书
物流管理专业推荐信
2014/09/06 职场文书
2015年第十五个全民国防教育日宣传活动方案
2015/05/06 职场文书
浅谈Redis跟MySQL的双写问题解决方案
2022/02/24 Redis