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 相关文章推荐
JAVA/JSP学习系列之四
Oct 09 PHP
session 的生命周期是多长
Oct 09 PHP
php 验证码制作(网树注释思想)
Jul 20 PHP
php win下Socket方式发邮件类
Aug 21 PHP
基于HTTP长连接的&quot;服务器推&quot;技术的php 简易聊天室
Oct 31 PHP
php中理解print EOT分界符和echo EOT的用法区别小结
Feb 21 PHP
PHP原生模板引擎 最简单的模板引擎
Apr 25 PHP
php无限遍历目录示例
Feb 21 PHP
PHP实现取得HTTP请求的原文
Aug 18 PHP
异步加载技术实现当滚动条到最底部的瀑布流效果
Sep 16 PHP
PHP实现的62进制转10进制,10进制转62进制函数示例
Jun 06 PHP
laravel数据库查询结果自动转数组修改实例
Feb 27 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
windows7下php开发环境搭建图文教程
2015/01/06 PHP
php导出中文内容excel文件类实例
2015/07/06 PHP
tp5(thinkPHP5)操作mongoDB数据库的方法
2018/01/20 PHP
PHP+redis实现的购物车单例类示例
2019/02/02 PHP
List all the Databases on a SQL Server
2007/06/21 Javascript
初学JavaScript_03(ExtJs Grid的简单使用)
2008/10/02 Javascript
js/html光标定位的实现代码
2013/09/23 Javascript
动态加载iframe时get请求传递中文参数乱码解决方法
2014/05/07 Javascript
Javascript学习笔记之函数篇(六) : 作用域与命名空间
2014/11/23 Javascript
JavaScript中的方法重载实例
2015/03/16 Javascript
JavaScript中的Math.LN2属性用法详解
2015/06/12 Javascript
Javascript编写俄罗斯方块思路及实例
2015/07/07 Javascript
IE6兼容透明背景图片及解决方案
2015/08/19 Javascript
原生js实现日期计算器功能
2017/02/17 Javascript
微信小程序 开发之滑块视图容器(swiper)详解及实例代码
2017/02/22 Javascript
vue.js实现含搜索的多种复选框(附源码)
2017/03/23 Javascript
jQuery实现鼠标滑过预览图片大图效果的方法
2017/04/26 jQuery
浅谈Vue页面级缓存解决方案feb-alive(上)
2019/04/14 Javascript
微信小程序动画组件使用解析,类似vue,且更强大
2019/08/01 Javascript
Angular value与ngValue区别详解
2019/11/27 Javascript
Vue实现图书管理案例
2021/01/20 Vue.js
Python利用pyHook实现监听用户鼠标与键盘事件
2014/08/21 Python
跟老齐学Python之关于循环的小伎俩
2014/10/02 Python
Jupyter安装nbextensions,启动提示没有nbextensions库
2020/04/23 Python
浅谈Python2、Python3相对路径、绝对路径导入方法
2018/06/22 Python
python两个list[]相加的实现方法
2020/09/23 Python
Application Cache未缓存文件无法访问无法加载问题
2014/05/31 HTML / CSS
独特的礼品和创新的科技产品:The Grommet
2018/02/24 全球购物
总结表彰大会主持词
2014/03/26 职场文书
刊首寄语大全
2014/04/11 职场文书
感恩小明星事迹材料
2014/05/23 职场文书
法人委托书
2014/07/31 职场文书
财务检查整改报告
2014/11/06 职场文书
2014年妇女工作总结
2014/12/06 职场文书
奖学金感谢信
2015/01/21 职场文书
Spring IOC容器Bean的作用域及生命周期实例
2022/05/30 Java/Android