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读取本地文件常用函数(fopen与file_get_contents)
Sep 09 PHP
遭遇php的in_array低性能问题
Sep 17 PHP
codeigniter框架批量插入数据
Jan 09 PHP
php 使用GD库为页面增加水印示例代码
Mar 24 PHP
ThinkPHP调用common/common.php函数提示错误function undefined的解决方法
Aug 25 PHP
php通过记录IP来防止表单重复提交方法分析
Dec 16 PHP
PHP面向对象之后期静态绑定功能介绍
May 18 PHP
php生成过去100年下拉列表的方法
Jul 20 PHP
php实现json编码的方法
Jul 30 PHP
Zend Framework自定义Helper类相关注意事项总结
Mar 14 PHP
PHP魔术方法之__call与__callStatic使用方法
Jul 23 PHP
360搜索引擎自动收录php改写方案
Apr 28 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用户登录之cookie信息安全分析
2016/05/13 PHP
jquery中获得$.ajax()事件返回的值并添加事件的方法
2010/04/15 Javascript
JavaScript的事件绑定(方便不支持js的时候)
2013/10/01 Javascript
我的Node.js学习之路(一)
2014/07/06 Javascript
浅谈javascript 函数属性和方法
2015/01/21 Javascript
JavaScript用select实现日期控件
2015/07/17 Javascript
Bootstrap每天必学之工具提示(Tooltip)插件
2016/04/26 Javascript
JavaScript sort数组排序方法和自我实现排序方法小结
2016/06/06 Javascript
JS获取中文拼音首字母并通过拼音首字母快速查找页面内对应中文内容的方法【附demo源码】
2016/08/19 Javascript
jquery 属性选择器(匹配具有指定属性的元素)
2016/09/06 Javascript
AngularJS通过ng-route实现基本的路由功能实例详解
2016/12/13 Javascript
Bootstrap一款超好用的前端框架
2017/09/25 Javascript
react+ant design实现Table的增、删、改的示例代码
2018/12/27 Javascript
大转盘抽奖小程序版 转盘抽奖网页版
2020/04/16 Javascript
vue实现浏览器全屏展示功能
2019/11/27 Javascript
[47:36]Optic vs Newbee 2018国际邀请赛小组赛BO2 第二场 8.17
2018/08/18 DOTA
Python新手实现2048小游戏
2015/03/31 Python
在Python中使用Neo4j数据库的教程
2015/04/16 Python
bpython 功能强大的Python shell
2016/02/16 Python
Python文本相似性计算之编辑距离详解
2016/11/28 Python
Python模拟登陆实现代码
2017/06/14 Python
Python实现嵌套列表去重方法示例
2017/12/28 Python
kafka-python 获取topic lag值方式
2019/12/23 Python
Python restful框架接口开发实现
2020/04/13 Python
解决python Jupyter不能导入外部包问题
2020/04/15 Python
CSS3常用的几种颜色渐变模式总结
2016/11/18 HTML / CSS
Canvas中设置width与height的问题浅析
2018/11/01 HTML / CSS
技校毕业生的自我评价
2013/12/27 职场文书
秦兵马俑教学反思
2014/02/07 职场文书
纠风工作实施方案
2014/03/15 职场文书
环保口号大全
2014/06/12 职场文书
2014年辅导员工作总结
2014/11/18 职场文书
病人写给医生的感谢信
2015/01/23 职场文书
Nginx反爬虫策略,防止UA抓取网站
2021/03/31 Servers
Python加密与解密模块hashlib与hmac
2022/06/05 Python
win10如何更改appdata文件夹的默认位置?
2022/07/15 数码科技