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 相关文章推荐
用libTemplate实现静态网页的生成
Oct 09 PHP
php中文字母数字验证码实现代码
Apr 25 PHP
在服务端进行目录建立、删除,文件上传、删除的过程的php代码
Sep 10 PHP
开启CURL扩展,让服务器支持PHP curl函数(远程采集)
Mar 19 PHP
php curl模拟post提交数据示例
Dec 31 PHP
Yii实现多按钮保存与提交的方法
Dec 03 PHP
PHP实现通过Luhn算法校验信用卡卡号是否有效
Mar 23 PHP
PHP滚动日志的代码实现
Jun 10 PHP
浅谈PHP拦截器之__set()与__get()的理解与使用方法
Oct 18 PHP
PHP编程计算日期间隔天数的方法
Apr 26 PHP
PHP后台备份MySQL数据库的源码实例
Mar 18 PHP
laravel unique验证、确认密码confirmed验证以及密码修改验证的方法
Oct 16 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/09 PHP
PHP之生成GIF动画的实现方法
2013/06/07 PHP
PHP 实现explort() 功能的详解
2013/06/20 PHP
JAVASCRIPT车架号识别/验证函数代码 汽车车架号验证程序
2012/01/08 Javascript
js调用后台、后台调用前台等方法总结
2014/04/17 Javascript
Javascript学习笔记之相等符号与严格相等符号
2014/11/23 Javascript
node.js中的buffer.toString方法使用说明
2014/12/14 Javascript
jQuery实现设置、移除文本框默认值功能
2015/01/13 Javascript
JavaScript中判断函数、变量是否存在
2015/06/10 Javascript
JavaScript知识点总结(六)之JavaScript判断变量数据类型
2016/05/31 Javascript
微信小程序开发之改变data中数组或对象的某一属性值
2018/07/05 Javascript
使用VueCli3+TypeScript+Vuex一步步构建todoList的方法
2019/07/25 Javascript
layui实现数据表格隐藏列的示例
2019/10/25 Javascript
js编写简易的计算器
2020/07/29 Javascript
在RedHat系Linux上部署Python的Celery框架的教程
2015/04/07 Python
总结Python编程中三条常用的技巧
2015/05/11 Python
在Python中的Django框架中进行字符串翻译
2015/07/27 Python
python密码错误三次锁定(实例讲解)
2017/11/14 Python
python3中的md5加密实例
2018/05/29 Python
使用python将多个excel文件合并到同一个文件的方法
2019/07/09 Python
Pytorch提取模型特征向量保存至csv的例子
2020/01/03 Python
python定时截屏实现
2020/11/02 Python
Python利用socket模块开发简单的端口扫描工具的实现
2021/01/27 Python
科颜氏加拿大官方网站: Kiehl’s加拿大
2016/08/16 全球购物
党课培训主持词
2014/04/01 职场文书
小区物业门卫岗位职责
2014/04/10 职场文书
常务副总经理岗位职责
2014/04/12 职场文书
政治学专业毕业生求职信
2014/08/11 职场文书
创建绿色社区汇报材料
2014/08/22 职场文书
开展批评与自我批评发言材料
2014/10/17 职场文书
2015年药店店长工作总结
2015/04/29 职场文书
大学生实习证明
2015/06/16 职场文书
Python爬取科目四考试题库的方法实现
2021/03/30 Python
HAM-2000摩机图
2021/04/22 无线电
python迷宫问题深度优先遍历实例
2021/06/20 Python
浅谈Python从全局与局部变量到装饰器的相关知识
2021/06/21 Python