由于国内互联网管制政策,强大的敏感词过滤系统成为了每一个网站必须具备的基本功能,尤其是那些涉及UGC内容的站点。对敏感词过滤流行的方案是基于Double-array trie tree,更复杂的采用了“多模式匹配算法”,只不过Double-array trie tree性能已经足以满足项目需要,因此找了一个基于Double-array trie tree算法的开源项目满足项目目前需要。大部分的Double-array trie tree项目都采用了 libdatrie http://linux.thai.net/~thep/datrie/datrie.html
操作系统:CentOS
1. 安装libevent http://monkey.org/~provos/libevent/
yum install libevent.x86_64 libevent-devel.x86_64 libevent.i686 libevent-devel.i686
2、安装libiconv
由于libdatrie依赖于libiconv来实现编码转换,因此必须安装libiconv
wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz
tar zxvf libiconv-1.14.tar.gz
cd libiconv-1.14
./configure
make
make install
2.安装libdatrie http://linux.thai.net/~thep/datrie/datrie.html
wget ftp://linux.thai.net/pub/ThaiLinux/software/libthai/libdatrie-0.2.4.tar.gz
tar zxvf libdatrie-0.2.4.tar.gz
cd libdatrie-0.2.4
./configure CFLAGS=-liconv
make
make install
3、安装libra-server
mkdir libra-server
cd libra-server
wget https://libra-server.googlecode.com/files/libra-server-v1.1.zip
unzip libra-server-v1.1.zip
make
echo "/usr/local/lib" >>/etc/ld.so.conf
/sbin/ldconfig -v
启动matcher server:
./matcher
4、测试
将matcher.php拷贝到web服务器对应目录下,并在目录下创建以下测试代码。文件采用UTF-8无BOM编码格式存储
a、创建初始化过滤关键词库的代码 initdb.php
<?php
include(‘matcher.php’);
$matcher = new Matcher(‘127.0.0.1’);
$matcher->set(‘法轮功’);
$matcher->set(‘共产党’);
$matcher->set(‘江泽民’);
$matcher->set(‘美女’);
$matcher->set(‘测试’);
$matcher->set(‘fuck’);
?>
b、创建搜索测试代码 search.php
<?php
include(‘matcher.php’);
$matcher = new Matcher(‘127.0.0.1’);
$matchs = $matcher->gets(‘测试代码,法轮功。fuck’);
if (!empty($matchs)) {
print_r($matchs);
}
?>
5、其他需要进一步优化的地方
《如何写一个完美的违禁字过滤系统》http://is.gd/btBGua 的思路值得参考
汉字拆分(*): 把汉字拆分后, 逃避检查, 比如“明教” ==》” 日月孝文“。
跳字处理(引入编辑距离的概念): 中间插入特殊的字符==》"共-产——党"。
严格 HTML 过滤(*) : 通过 HTML 的标签, 实现逃避检查 <font color="red">国
</font> <font color="red">民</font> <font color="red">党</font> (显示成: 国民党)
简体/繁体转换: 通过写入繁体字逃避检查。
组合判断(*): 对于特殊的人物名字, 需要通过组合方式判定, 毛泽东|大越进|独
裁 这样的文本同时出现才能判断违禁。
过滤评分体系(Bayers): 建立一个违禁评分题体系。
故意逃避检查的惩罚系统:包括插字,插入 tag,汉字拆分,繁简体混合等种种伪装系
统。
高亮方法:能够高亮“氵去车仑功”“法 —轮—功”“<font color="red">国
</font> <font color="red">民</font> <font color="red">党</font> (显示成: 国民党)”等种种特殊伪装关键字。