敏感词汇过滤

WBOY
Release: 2016-06-23 14:39:13
Original
762 people have browsed it

假如用户输入了 

$str="输入内容123";
Copy after login

下了个abc.txt,内容为,大概一千条
坏蛋|1火枪|1超级大坏蛋|1....
Copy after login


如何用这个txt文件检查过滤$str1;要效率,求支招


回复讨论(解决方案)

效率 分批读取过滤..

从 abc.txt 构造出 trie 树,然后匹配即可
算法在精华区中

从 abc.txt 构造出 trie 树,然后匹配即可
算法在精华区中 版主你好,看到贴子了是个类,怎么调用呀?
不会运用~悲剧啊,求再详细一点,万分感谢
另外我下的文件为啥每一行尾都有"|1",干嘛用的

替换掉所有的 |1 吧,人家的东西人家自有用处。

替换掉所有的 |1 吧,人家的东西人家自有用处。
谢谢回答,如何匹配呢,还是没摸到头脑,提问只前我也知道是一行行读了来匹配
麻烦给个鱼吧,渔多数都是概念,俺还没到一点通地步~

以 http://bbs.csdn.net/topics/390221822 的 ttrie 类为例

trie = new TTrie;foreach(file('abc.txt') as $r) $trie->set(strtok($r, '|'));$s =<<< TXT输入内容坏蛋123TXT;//仅识别print_r($trie->match($s));/* 得Array(    [0] => 输入内容    [1] => 坏蛋    [2] => 123)*///去掉字典字$trie->savematch = 0;print_r($trie->match($s));/* 得Array(    [0] => 输入内容    [1] => 123)*/
Copy after login

显然已经满足你的需要了
输出结果时连接数组为串就可以了

如果是想加亮关键字,可以这样
class mytrie extends ttrie {  function b() {    $this->buffer[] = '<b>' . array_pop($this->buffer) . '<b/>';  }}    $trie = new mytrie;foreach(file('abc.txt') as $r) $trie->set(strtok($r, '|'), 'b');$s =<<< TXT输入内容坏蛋123TXT;print_r($trie->match($s));/* 得Array(    [0] => 输入内容    [1] => <b>坏蛋<b/> 《== 这个就被突出显示了    [2] => 123)*/
Copy after login

其实渔已经给你了,鱼还是自己动手的好

为了使实例化的对象得以复用,还应在 match 方法开始处加入
    $this->buffer = array();    $this->input = 0;    $this->backtracking = 0;
Copy after login
以重新初始化数据指针

版主太威武了。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!