标题:利用PHP扩展trie_filter做中文敏感词过滤 出处:沧海一粟 时间:Sat, 22 Oct 2016 20:28:25 +0000 作者:jed 地址:http://www.dzhope.com/post/1025/ 内容: 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) 网友 囚蝶i 反馈现在这个网站的download 已经不能下载了。 可以使用svn 来下载 checkout 地址 :http://Linux.thai.NET/svn/software/datrie/tags/r_0_2_4 谢谢 囚蝶i 的反馈 wget http://linux.thai.net/pub/ThaiLinux/software/libthai/libdatrie-0.2.4.tar.gz 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 我使用5.2.17版本时候:trie_filter_search_all 这个函数会有错误 Generated by Bo-blog 2.1.1 Release