SPAM、Bayesian跟中文 4 - 在CakePHP中集成贝叶斯算法

php中文网
发布: 2016-06-13 13:25:42
原创
999人浏览过

SPAM、Bayesian和中文 4 - 在CakePHP中集成贝叶斯算法

上文提到了贝叶斯算法的几种开源实现,本文说说如何将其中一种名为b8的开源实现集成进cakephp。

下载b8及安装

  1. b8的站点下载最新版本,将其解压至vendors目录,文件位置如vendors/b8/b8.php;
  2. 用文本编辑器打开vendors/b8/etc/config_b8,修改databaseType为mysql;
  3. 用文本编辑器打开vendors/b8/etc/config_storage,修改tableName为你用来存储关键字的数据表的名字,修改createDB为TRUE,要注意的是,当你第一次运行b8后,它会建立上述数据表,然后你要重新把createDB改为FALSE;
  4. 用文本编辑器打开vendors/b8/lexer/shared_functions.php,将38行的代码(在echoError())注释掉,否则b8会直接把错误信息显示在你的Cake应用中,当然这在调试程序时还是有用的。

为b8写一个wrapper component

为了让你的Cake能够调用到b8,你需要写一个component。在controllers/components/新建一个spam_shield.php,加入如下代码:

class SpamShieldComponent extends Object { ??? /** * b8 instance?*/ ??? var $b8; ??? /** * standard rating * * comments with ratings which are higher than this one will be considered as SPAM?*/ ??? var $standardRating = 0.7; ??? /** * text to be classified */ ??? var $text; ??? /** * rating of the text */ ??? var $rating; ??? /** * Constructor * * @date 2009-1-20 */ ??? function startup(&$controller) { ??????? //register a CommentModel to get the DBO resource link ??????? $comment = ClassRegistry::init('Comment'); //import b8 and create an instance???? ?????? ?App::import('Vendor', 'b8/b8'); ?????? ?$this->b8 = new b8($comment->getDBOResourceLink()); //set standard rating??? ?????? ?$this->standardRating = Configure::read('LT.bayesRating') ? Configure::read('LT.bayesRating') : $this->standardRating; ??? } ? ??? /** * Set the text to be classified * * @param $text String the text to be classified * @date 2009-1-20 */ ??? function set($text) { ??????? $this->text = $text; ??? } ? ??? /** * Get Bayesian rating * * @date 2009-1-20 */ ??? function rate() { ?????? ?//get Bayes rating and return return ?????? ?$this->rating = $this->b8->classify($this->text); ??? } ? ??? /** * Validate a message based on the rating, return true if it's NOT a SPAM * * @date 2009-1-20 */ ??? function validate() { ??????? return $this->rate() standardRating; ??? } ? ??? /** * Learn a SPAM or a HAM * * @date 2009-1-20 */ ??? function learn($mode) { ?????? ?$this->b8->learn($this->text, $mode); ??? } ? ??? /** * Unlearn a SPAM or a HAM * * @date 2009-1-20 */ ??? function unlearn($mode) { ?????? ?$this->b8->unlearn($this->text, $mode); ??? } }

几点说明:

  1. $standardRating是一个临界点。如果贝叶斯概率高于这个值,则此留言被认为是spam,否则是ham。我设置为0.7,你可以根据自己的情况修改;
  2. Configure::read('LT.bayesRating')是从系统运行配置中动态地获取上述临界点的值,这是我的做法,你可能用不到,根据情况稍微修改甚至不修改都行;
  3. Comment指的是评论的model;
  4. 由于b8需要获得数据库句柄以便能够操作数据表,所以在startup()中我写了$this->b8 = new b8($comment->getDBOResourceLink())一句,其中用到的getDBOResourceLink()马上会提及。

为b8传入数据库句柄

在models/comment.php中加入如下代码:

/** * get the resource link of MySQL connection */ public function getDBOResourceLink() { return $this->getDataSource()->connection; }

至此,准备工作全部做完,我们终于可以使用贝叶斯算法来分类留言。

立即学习PHP免费学习笔记(深入)”;

使用b8分类留言

在controllers/comments_controller.php中,首先载入SpamShieldComponent:

var $components = array('SpamShield');

然后在add()方法中,做如下操作:

//set data for Bayesian validation

$this->SpamShield->set($this->data['Comment']['body']); //validate the comment with Bayesian

if(!$this->SpamShield->validate()) { //set the status

??? $this->data['Comment']['status'] = 'spam'; //save

??? $this->Comment->save($this->data); //learn it $this->SpamShield->learn("spam"); //render

??? $this->renderView('unmoderated');

??? return;

}

//it's a normal post

$this->data['Comment']['status'] = 'published'; //save for publish

$this->Comment->save($this->data); //learn it

$this->SpamShield->learn("ham");

如此一来,b8就会在留言到来时自动的分类并学习,你基本上已经与spam绝缘了!

提醒一下:第一次运行后,别忘了把刚才提到的createDB改为FALSE。

http://dingyu.me/blog/spam-bayesian-chinese-4

PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号