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 相关文章推荐
弄了个检测传输的参数是否为数字的Function
Dec 06 PHP
PHP nl2br函数 将换行字符转成 &amp;lt;br&amp;gt;
Aug 21 PHP
php下通过IP获取地理位置的代码(小偷程序)
Jun 09 PHP
php 字符串替换的方法
Jan 10 PHP
探讨PHP删除文件夹的三种方法
Jun 09 PHP
解析PHP正则提取或替换img标记属性
Jun 26 PHP
Drupal7中常用的数据库操作实例
Mar 02 PHP
PHP缓冲区用法总结
Feb 14 PHP
PHP strip_tags() 去字符串中的 HTML、XML 以及 PHP 标签的函数
May 22 PHP
mac下多个php版本快速切换的方法
Oct 09 PHP
laravel 数据迁移与 Eloquent ORM的实现方法
Apr 12 PHP
thinkphp框架实现路由重定义简化url访问地址的方法分析
Apr 04 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
一个可以找出源代码中所有中文的工具
2006/10/25 PHP
解析php file_exists无效的解决办法
2013/06/26 PHP
9个实用的PHP代码片段分享
2015/01/22 PHP
thinkphp中多表查询中防止数据重复的sql语句(必看)
2016/09/22 PHP
php实现JWT验证的实例教程
2020/11/26 PHP
jquery 简单的进度条实现代码
2010/03/11 Javascript
Jquery Select操作方法集合脚本之家特别版
2010/05/17 Javascript
iframe 异步加载技术及性能分析
2011/07/19 Javascript
JS判断对象是否存在的10种方法总结
2013/12/23 Javascript
jQuery AjaxUpload 上传图片代码
2016/02/02 Javascript
Bootstrap分页插件之Bootstrap Paginator实例详解
2016/10/15 Javascript
浅谈JS如何实现真正的对象常量
2017/06/25 Javascript
JavaScript实现无刷新上传预览图片功能
2017/08/02 Javascript
深入研究jQuery图片懒加载 lazyload.js使用方法
2017/08/16 jQuery
vue.js实现的经典计算器/科学计算器功能示例
2018/07/11 Javascript
详解Python中的__init__和__new__
2014/03/12 Python
详解Python中expandtabs()方法的使用
2015/05/18 Python
pycharm 解除默认unittest模式的方法
2018/11/30 Python
使用python 打开文件并做匹配处理的实例
2019/01/02 Python
Python获取一个用户名的组ID过程解析
2019/09/03 Python
使用Python实现画一个中国地图
2019/11/23 Python
你可能不知道的Python 技巧小结
2020/01/29 Python
Python 如何实现数据库表结构同步
2020/09/29 Python
雷朋巴西官方商店:Ray-Ban Brasil
2020/07/21 全球购物
出纳岗位职责
2013/11/09 职场文书
教育专业个人求职信
2013/12/02 职场文书
生物科学专业个人求职信范文
2013/12/07 职场文书
护士辞职信模板
2014/01/20 职场文书
11月升旗仪式讲话稿
2014/02/15 职场文书
2014年迎新年活动方案
2014/02/19 职场文书
生产部厂长助理职位说明书
2014/03/03 职场文书
2014高中生入党思想汇报范文
2014/09/13 职场文书
党员教师个人对照检查材料(群众路线)
2014/09/26 职场文书
学会感恩主题班会
2015/08/12 职场文书
公安纪律作风整顿心得体会
2016/01/23 职场文书
分享:关于学习的励志名言赏析
2019/08/16 职场文书