频道栏目
首页 > 资讯 > php > 正文

利用PHP扩展trie_filter做中文敏感词过滤

14-01-17        来源:[db:作者]  
收藏   我要投稿

1.安装libiconv,这个是libdatrie的依赖项

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#Download)

tar zxf libdatrie-0.2.4.tar.gz   
cd libdatrie-0.2.4   
./configure --prefix=/usr/local   
make   
make install


编译出现错误 trietool.c:125: undefined reference to `libiconv'

解决办法为:./configure LDFLAGS=-L/usr/local/lib LIBS=-liconv


3. 安装 trie_filter 扩展

由于官方trie_filter扩展对中文支持的不是很好,所以在git上找到了一个在官方扩展上面改写的扩展经过测试没有问题

安装方法如下:

https://github.com/wulijun/php-ext-trie-filter 在这里下载源码包

phpize
./configure --with-php-config=/usr/local/bin/php-config 
make
make install

4. 修改 php.ini 文件,添加 trie_filter 扩展:extension=trie_filter.so,重启PHP。

查看phpinfo发现trie_filter 扩展可用,如下图所示:



5、生成用语检测的词典,由于上面下载的源码包中并没有带生成词典的命令 所以还需要下载官方的源码包

(https://code.google.com/p/as3chat/downloads/detail?name=trie_filter-2011-03-21.tar.gz)

tar zxf trie_filter-2011.03.21.tar.gz   
cd trie_filter-2011.03.21    

gcc -o dpp dpp.c -ldatrie // 生成dpp命令用语编译词典

./dpp words.txt words.dic  //将words.txt 编译成trie_filter使用的词典 words.txt中每个词占一行


生成词典的时候 报错:./dpp: error while loading shared libraries: libdatrie.so.1: cannot open shared object file: No such file or directory

解决办法:执行

ldconfig

然后在执行

./dpp words.txt words.dic

就好了


6、测试:

";  
var_dump($res2);
trie_filter_free($file); //最后别忘记调用free

建议使用php 5.3.3以上的版本,我使用的是5.3.3



相关TAG标签
上一篇:delphi中打开图片实例demo——2014005
下一篇:python enumerate用法
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站