您当前的位置: 首页 > 网站编程 > PHP教程 > php权重计算方法代码分享

php权重计算方法代码分享

作者:不详 来源:网络 发布时间: 2014-08-11 08:54 点击:
权重计算,稍加修改亦可用于分词,词频统计,全文和spam检测等 代码如下: ?php /* vim: set expandtab tabstop=4 shiftwidth=4: */ // +------------------------------------------------------------------------ // Name : 权重计算 // Description: 稍加修改,亦可

php权重计算方法代码分享

  权重计算,稍加修改亦可用于分词,词频统计,全文和spam检测等

  代码如下:

  <?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 .= "
分享到:
本文"php权重计算方法代码分享"由远航站长收集整理而来,仅供大家学习与参考使用。更多网站制作教程尽在远航站长站。
顶一下
(0)
0%
踩一下
(0)
0%
[点击 次] [返回上一页] [打印]
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 密码: 验证码:
关于本站 - 联系我们 - 网站声明 - 友情连接- 网站地图 - 站点地图 - 返回顶部
Copyright © 2007-2013 www.yhzhan.com(远航站长). All Rights Reserved .
远航站长:为中小站长提供最佳的学习与交流平台,提供网页制作与网站编程等各类网站制作教程.
官方QQ:445490277 网站群:26680406 网站备案号:豫ICP备07500620号-4