1.replace过滤
最简单也是最直接的就是直接循环敏感词,然后使用replace过滤关键词,文章和敏感词少的时候还可以,多的时候效率就真的很一般了。
2.使用正则过滤
有两个技术要点,
1.使用Python正则表达式的re的sub()函数;
2.在正则表达式语法中,竖线“|”表示二选一或多选一。
代码参考
import re
def check_filter(keywords, text):
return re.sub("|".join(keywords), "***", text)
keywords = ("暴力", "色情", "其他关键字")
text = "这句话里不包含暴力,也不包含色情,但是可能包含其他关键字"
print(check_filter(keywords, text))
返回结果
这句话里不包含***,也不包含***,但是可能包含***
3.DFA过滤敏感词算法
在网上查了下敏感词过滤方案,找到了一种名为DFA的算法,即Deterministic Finite Automaton算法,翻译成中文就是确定有穷自动机算法。它的基本思想是基于状态转移来检索敏感词,只需要扫描一次待检测文本,就能对所有敏感词进行检测,所以效率比方案一高不少。
假设我们有以下5个敏感词需要检测:傻逼、傻子、傻大个、坏蛋、坏人。那么我们可以先把敏感词中有相同前缀的词组合成一个树形结构,不同前缀的词分属不同树形分支,以上述5个敏感词为例,可以初始化成如下2棵树:
把敏感词组成成树形结构有什么好处呢?最大的好处就是可以减少检索次数,我们只需要遍历一次待检测文本,然后在敏感词库中检索出有没有该字符对应的子树就行了,如果没有相应的子树,说明当前检测的字符不在敏感词库中,则直接跳过继续检测下一个字符;如果有相应的子树,则接着检查下一个字符是不是前一个字符对应的子树的子节点,这样迭代下去,就能找出待检测文本中是否包含敏感词了。
我们以文本“你是不是傻逼”为例,我们依次检测每个字符,因为前4个字符都不在敏感词库里,找不到相应的子树,所以直接跳过。当检测到“傻”字时,发现敏感词库中有相应的子树,我们把他记为tree-1,接着再搜索下一个字符“逼”是不是子树tree-1的子节点,发现恰好是,接下来再判断“逼”这个字符是不是叶子节点,如果是,则说明匹配到了一个敏感词了,在这里“逼”这个字符刚好是tree-1的叶子节点,所以成功检索到了敏感词:“傻逼”。大家发现了没有,在我们的搜索过程中,我们只需要扫描一次被检测文本就行了,而且对于被检测文本中不存在的敏感词,如这个例子中的“坏蛋”和“坏人”,我们完全不会扫描到,因此相比方案一效率大大提升了。
在python中,我们可以用dict来存储上述的树形结构,还是以上述敏感词为例,我们把每个敏感词字符串拆散成字符,再存储到dict中,可以这样存:
{
'傻': {
'逼': {
'x00': 0
},
'子': {
'x00': 0
},
'大': {
'个': {
'x00': 0
}
}
},
'坏': {
'蛋': {
'x00': 0
},
'人': {
'x00': 0}
}
}
首先将每个词的第一个字符作为key,value则是另一个dict,value对应的dict的key为第二个字符,如果还有第三个字符,则存储到以第二个字符为key的value中,当然这个value还是一个dict,以此类推下去,直到最后一个字符,当然最后一个字符对应的value也是dict,只不过这个dict只需要存储一个结束标志就行了,像上述的例子中,我们就存了一个{'x00': 0}的dict,来表示这个value对应的key是敏感词的最后一个字符。
同理,“坏人”和“坏蛋”这2个敏感词也是按这样的方式存储起来,这里就不罗列出来了。
用dict存储有什么好处呢?我们知道dict在理想情况下可以以O(1)的时间复杂度进行查询,所以我们在遍历待检测字符串的过程中,可以以O(1)的时间复杂度检索出当前字符是否在敏感词库中,效率比方案一提升太多了。
接下来上代码。
# -*- coding:utf-8 -*-
import time
time1 = time.time()
# DFA算法
class DFAFilter(object):
def init(self):
self.keyword_chains = {} # 关键词链表
self.delimit = 'x00' # 限定<span class="token keyword">def</span> <span class="token method-definition"><span class="token function">add</span></span><span class="token punctuation">(</span><span class="token keyword">self</span><span class="token punctuation">,</span> keyword<span class="token punctuation">)</span><span class="token punctuation">:</span> keyword <span class="token operator">=</span> keyword<span class="token punctuation">.</span>lower<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment"># 关键词英文变为小写</span> chars <span class="token operator">=</span> keyword<span class="token punctuation">.</span>strip<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment"># 关键字去除首尾空格和换行</span> <span class="token keyword">if</span> <span class="token keyword">not</span> chars<span class="token punctuation">:</span> <span class="token comment"># 如果关键词为空直接返回</span> <span class="token keyword">return</span> level <span class="token operator">=</span> <span class="token keyword">self</span><span class="token punctuation">.</span>keyword_chains <span class="token comment"># 遍历关键字的每个字</span> <span class="token keyword">for</span> i <span class="token keyword">in</span> range<span class="token punctuation">(</span>len<span class="token punctuation">(</span>chars<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token comment"># 如果这个字已经存在字符链的key中就进入其子字典</span> <span class="token keyword">if</span> chars<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token keyword">in</span> level<span class="token punctuation">:</span> level <span class="token operator">=</span> level<span class="token punctuation">[</span>chars<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">]</span> <span class="token keyword">else</span><span class="token punctuation">:</span> <span class="token keyword">if</span> <span class="token keyword">not</span> isinstance<span class="token punctuation">(</span>level<span class="token punctuation">,</span> dict<span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword">break</span> <span class="token keyword">for</span> j <span class="token keyword">in</span> range<span class="token punctuation">(</span>i<span class="token punctuation">,</span> len<span class="token punctuation">(</span>chars<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">:</span> level<span class="token punctuation">[</span>chars<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token punctuation">}</span> last_level<span class="token punctuation">,</span> last_char <span class="token operator">=</span> level<span class="token punctuation">,</span> chars<span class="token punctuation">[</span>j<span class="token punctuation">]</span> level <span class="token operator">=</span> level<span class="token punctuation">[</span>chars<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">]</span> last_level<span class="token punctuation">[</span>last_char<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token keyword">self</span><span class="token punctuation">.</span>delimit<span class="token punctuation">:</span> <span class="token number">0</span><span class="token punctuation">}</span> <span class="token keyword">break</span> <span class="token keyword">if</span> i <span class="token operator">==</span> len<span class="token punctuation">(</span>chars<span class="token punctuation">)</span> <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">:</span> level<span class="token punctuation">[</span><span class="token keyword">self</span><span class="token punctuation">.</span>delimit<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0</span> <span class="token keyword">def</span> <span class="token method-definition"><span class="token function">parse</span></span><span class="token punctuation">(</span><span class="token keyword">self</span><span class="token punctuation">,</span> path<span class="token punctuation">)</span><span class="token punctuation">:</span> with open<span class="token punctuation">(</span>path<span class="token punctuation">,</span> encoding<span class="token operator">=</span><span class="token string">'utf-8'</span><span class="token punctuation">)</span> as f<span class="token punctuation">:</span> <span class="token keyword">for</span> keyword <span class="token keyword">in</span> f<span class="token punctuation">:</span> <span class="token keyword">self</span><span class="token punctuation">.</span>add<span class="token punctuation">(</span>str<span class="token punctuation">(</span>keyword<span class="token punctuation">)</span><span class="token punctuation">.</span>strip<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> print<span class="token punctuation">(</span><span class="token keyword">self</span><span class="token punctuation">.</span>keyword_chains<span class="token punctuation">)</span> <span class="token keyword">def</span> <span class="token method-definition"><span class="token function">filter</span></span><span class="token punctuation">(</span><span class="token keyword">self</span><span class="token punctuation">,</span> message<span class="token punctuation">,</span> repl<span class="token operator">=</span><span class="token string">"*"</span><span class="token punctuation">)</span><span class="token punctuation">:</span> message <span class="token operator">=</span> message<span class="token punctuation">.</span>lower<span class="token punctuation">(</span><span class="token punctuation">)</span> ret <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span> start <span class="token operator">=</span> <span class="token number">0</span> <span class="token keyword">while</span> start <span class="token operator"><</span> len<span class="token punctuation">(</span>message<span class="token punctuation">)</span><span class="token punctuation">:</span> level <span class="token operator">=</span> <span class="token keyword">self</span><span class="token punctuation">.</span>keyword_chains step_ins <span class="token operator">=</span> <span class="token number">0</span> <span class="token keyword">for</span> char <span class="token keyword">in</span> message<span class="token punctuation">[</span>start<span class="token punctuation">:</span><span class="token punctuation">]</span><span class="token punctuation">:</span> <span class="token keyword">if</span> char <span class="token keyword">in</span> level<span class="token punctuation">:</span> step_ins <span class="token operator">+</span><span class="token operator">=</span> <span class="token number">1</span> <span class="token keyword">if</span> <span class="token keyword">self</span><span class="token punctuation">.</span>delimit <span class="token keyword">not</span> <span class="token keyword">in</span> level<span class="token punctuation">[</span>char<span class="token punctuation">]</span><span class="token punctuation">:</span> level <span class="token operator">=</span> level<span class="token punctuation">[</span>char<span class="token punctuation">]</span> <span class="token keyword">else</span><span class="token punctuation">:</span> ret<span class="token punctuation">.</span>append<span class="token punctuation">(</span>repl <span class="token operator">*</span> step_ins<span class="token punctuation">)</span> start <span class="token operator">+</span><span class="token operator">=</span> step_ins <span class="token operator">-</span> <span class="token number">1</span> <span class="token keyword">break</span> <span class="token keyword">else</span><span class="token punctuation">:</span> ret<span class="token punctuation">.</span>append<span class="token punctuation">(</span>message<span class="token punctuation">[</span>start<span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token keyword">break</span> <span class="token keyword">else</span><span class="token punctuation">:</span> ret<span class="token punctuation">.</span>append<span class="token punctuation">(</span>message<span class="token punctuation">[</span>start<span class="token punctuation">]</span><span class="token punctuation">)</span> start <span class="token operator">+</span><span class="token operator">=</span> <span class="token number">1</span> <span class="token keyword">return</span> <span class="token string">''</span><span class="token punctuation">.</span>join<span class="token punctuation">(</span>ret<span class="token punctuation">)</span>
if name == "main":
gfw = DFAFilter()
path = "E:/lyh/test/sensitive_words.txt"
gfw.parse(path)
text = "你真是个大傻逼,大傻子,傻大个,大坏蛋,坏人。"
result = gfw.filter(text)print<span class="token punctuation">(</span>text<span class="token punctuation">)</span> print<span class="token punctuation">(</span>result<span class="token punctuation">)</span> time2 <span class="token operator">=</span> time<span class="token punctuation">.</span>time<span class="token punctuation">(</span><span class="token punctuation">)</span> print<span class="token punctuation">(</span><span class="token string">'总共耗时:'</span> <span class="token operator">+</span> str<span class="token punctuation">(</span>time2 <span class="token operator">-</span> time1<span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">'s'</span><span class="token punctuation">)</span>
sensitive_words.txt
傻逼
傻子
傻大个
坏蛋
坏人
运行结果
你真是个大傻逼,大傻子,傻大个,大坏蛋,坏人。
你真是个大**,大**,***,大**,**。
总共耗时:0.0009999275207519531s
4.AC自动机过滤敏感词算法
AC自动机:一个常见的例子就是给出n个单词,再给出一段包含m个字符的文章,让你找出有多少个单词在文章里出现过。
简单地讲,AC自动机就是字典树+kmp算法+失配指针
# -*- coding:utf-8 -*-
import time
time1=time.time()# AC自动机算法
class node(object):
def init(self):
self.next = {}
self.fail = None
self.isWord = False
self.word = ""class ac_automation(object):
<span class="token keyword">def</span> <span class="token method-definition"><span class="token function">__init__</span></span><span class="token punctuation">(</span><span class="token keyword">self</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword">self</span><span class="token punctuation">.</span>root <span class="token operator">=</span> node<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment"># 添加敏感词函数</span> <span class="token keyword">def</span> <span class="token method-definition"><span class="token function">addword</span></span><span class="token punctuation">(</span><span class="token keyword">self</span><span class="token punctuation">,</span> word<span class="token punctuation">)</span><span class="token punctuation">:</span> temp_root <span class="token operator">=</span> <span class="token keyword">self</span><span class="token punctuation">.</span>root <span class="token keyword">for</span> char <span class="token keyword">in</span> word<span class="token punctuation">:</span> <span class="token keyword">if</span> char <span class="token keyword">not</span> <span class="token keyword">in</span> temp_root<span class="token punctuation">.</span><span class="token keyword">next</span><span class="token punctuation">:</span> temp_root<span class="token punctuation">.</span><span class="token keyword">next</span><span class="token punctuation">[</span>char<span class="token punctuation">]</span> <span class="token operator">=</span> node<span class="token punctuation">(</span><span class="token punctuation">)</span> temp_root <span class="token operator">=</span> temp_root<span class="token punctuation">.</span><span class="token keyword">next</span><span class="token punctuation">[</span>char<span class="token punctuation">]</span> temp_root<span class="token punctuation">.</span>isWord <span class="token operator">=</span> <span class="token constant">True</span> temp_root<span class="token punctuation">.</span>word <span class="token operator">=</span> word <span class="token comment"># 失败指针函数</span> <span class="token keyword">def</span> <span class="token method-definition"><span class="token function">make_fail</span></span><span class="token punctuation">(</span><span class="token keyword">self</span><span class="token punctuation">)</span><span class="token punctuation">:</span> temp_que <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span> temp_que<span class="token punctuation">.</span>append<span class="token punctuation">(</span><span class="token keyword">self</span><span class="token punctuation">.</span>root<span class="token punctuation">)</span> <span class="token keyword">while</span> len<span class="token punctuation">(</span>temp_que<span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token number">0</span><span class="token punctuation">:</span> temp <span class="token operator">=</span> temp_que<span class="token punctuation">.</span>pop<span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span> p <span class="token operator">=</span> <span class="token constant">None</span> <span class="token keyword">for</span> key<span class="token punctuation">,</span>value <span class="token keyword">in</span> temp<span class="token punctuation">.</span><span class="token keyword">next</span><span class="token punctuation">.</span>item<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword">if</span> temp <span class="token operator">==</span> <span class="token keyword">self</span><span class="token punctuation">.</span>root<span class="token punctuation">:</span> temp<span class="token punctuation">.</span><span class="token keyword">next</span><span class="token punctuation">[</span>key<span class="token punctuation">]</span><span class="token punctuation">.</span>fail <span class="token operator">=</span> <span class="token keyword">self</span><span class="token punctuation">.</span>root <span class="token keyword">else</span><span class="token punctuation">:</span> p <span class="token operator">=</span> temp<span class="token punctuation">.</span>fail <span class="token keyword">while</span> p is <span class="token keyword">not</span> <span class="token constant">None</span><span class="token punctuation">:</span> <span class="token keyword">if</span> key <span class="token keyword">in</span> p<span class="token punctuation">.</span><span class="token keyword">next</span><span class="token punctuation">:</span> temp<span class="token punctuation">.</span><span class="token keyword">next</span><span class="token punctuation">[</span>key<span class="token punctuation">]</span><span class="token punctuation">.</span>fail <span class="token operator">=</span> p<span class="token punctuation">.</span>fail <span class="token keyword">break</span> p <span class="token operator">=</span> p<span class="token punctuation">.</span>fail <span class="token keyword">if</span> p is <span class="token constant">None</span><span class="token punctuation">:</span> temp<span class="token punctuation">.</span><span class="token keyword">next</span><span class="token punctuation">[</span>key<span class="token punctuation">]</span><span class="token punctuation">.</span>fail <span class="token operator">=</span> <span class="token keyword">self</span><span class="token punctuation">.</span>root temp_que<span class="token punctuation">.</span>append<span class="token punctuation">(</span>temp<span class="token punctuation">.</span><span class="token keyword">next</span><span class="token punctuation">[</span>key<span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token comment"># 查找敏感词函数</span> <span class="token keyword">def</span> <span class="token method-definition"><span class="token function">search</span></span><span class="token punctuation">(</span><span class="token keyword">self</span><span class="token punctuation">,</span> content<span class="token punctuation">)</span><span class="token punctuation">:</span> p <span class="token operator">=</span> <span class="token keyword">self</span><span class="token punctuation">.</span>root result <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span> currentposition <span class="token operator">=</span> <span class="token number">0</span> <span class="token keyword">while</span> currentposition <span class="token operator"><</span> len<span class="token punctuation">(</span>content<span class="token punctuation">)</span><span class="token punctuation">:</span> word <span class="token operator">=</span> content<span class="token punctuation">[</span>currentposition<span class="token punctuation">]</span> <span class="token keyword">while</span> word <span class="token keyword">in</span> p<span class="token punctuation">.</span><span class="token keyword">next</span> <span class="token operator">==</span> <span class="token constant">False</span> <span class="token keyword">and</span> p <span class="token operator">!=</span> <span class="token keyword">self</span><span class="token punctuation">.</span>root<span class="token punctuation">:</span> p <span class="token operator">=</span> p<span class="token punctuation">.</span>fail <span class="token keyword">if</span> word <span class="token keyword">in</span> p<span class="token punctuation">.</span><span class="token keyword">next</span><span class="token punctuation">:</span> p <span class="token operator">=</span> p<span class="token punctuation">.</span><span class="token keyword">next</span><span class="token punctuation">[</span>word<span class="token punctuation">]</span> <span class="token keyword">else</span><span class="token punctuation">:</span> p <span class="token operator">=</span> <span class="token keyword">self</span><span class="token punctuation">.</span>root <span class="token keyword">if</span> p<span class="token punctuation">.</span>isWord<span class="token punctuation">:</span> result<span class="token punctuation">.</span>append<span class="token punctuation">(</span>p<span class="token punctuation">.</span>word<span class="token punctuation">)</span> p <span class="token operator">=</span> <span class="token keyword">self</span><span class="token punctuation">.</span>root currentposition <span class="token operator">+</span><span class="token operator">=</span> <span class="token number">1</span> <span class="token keyword">return</span> result <span class="token comment"># 加载敏感词库函数</span> <span class="token keyword">def</span> <span class="token method-definition"><span class="token function">parse</span></span><span class="token punctuation">(</span><span class="token keyword">self</span><span class="token punctuation">,</span> path<span class="token punctuation">)</span><span class="token punctuation">:</span> with open<span class="token punctuation">(</span>path<span class="token punctuation">,</span>encoding<span class="token operator">=</span><span class="token string">'utf-8'</span><span class="token punctuation">)</span> as f<span class="token punctuation">:</span> <span class="token keyword">for</span> keyword <span class="token keyword">in</span> f<span class="token punctuation">:</span> <span class="token keyword">self</span><span class="token punctuation">.</span>addword<span class="token punctuation">(</span>str<span class="token punctuation">(</span>keyword<span class="token punctuation">)</span><span class="token punctuation">.</span>strip<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment"># 敏感词替换函数</span> <span class="token keyword">def</span> <span class="token method-definition"><span class="token function">words_replace</span></span><span class="token punctuation">(</span><span class="token keyword">self</span><span class="token punctuation">,</span> text<span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token string">""</span>" <span class="token symbol">:param</span> ah<span class="token punctuation">:</span> <span class="token constant">AC</span>自动机 <span class="token symbol">:param</span> text<span class="token punctuation">:</span> 文本 <span class="token symbol">:return</span><span class="token punctuation">:</span> 过滤敏感词之后的文本 <span class="token string">""</span>" result <span class="token operator">=</span> list<span class="token punctuation">(</span>set<span class="token punctuation">(</span><span class="token keyword">self</span><span class="token punctuation">.</span>search<span class="token punctuation">(</span>text<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token keyword">for</span> x <span class="token keyword">in</span> result<span class="token punctuation">:</span> m <span class="token operator">=</span> text<span class="token punctuation">.</span>replace<span class="token punctuation">(</span>x<span class="token punctuation">,</span> <span class="token string">'*'</span> <span class="token operator">*</span> len<span class="token punctuation">(</span>x<span class="token punctuation">)</span><span class="token punctuation">)</span> text <span class="token operator">=</span> m <span class="token keyword">return</span> text
if name == 'main':
ah <span class="token operator">=</span> ac_automation<span class="token punctuation">(</span><span class="token punctuation">)</span> path<span class="token operator">=</span><span class="token string">'F:/文本反垃圾算法/sensitive_words.txt'</span> ah<span class="token punctuation">.</span>parse<span class="token punctuation">(</span>path<span class="token punctuation">)</span> text1<span class="token operator">=</span><span class="token string">"新疆骚乱苹果新品发布会雞八"</span> text2<span class="token operator">=</span>ah<span class="token punctuation">.</span>words_replace<span class="token punctuation">(</span>text1<span class="token punctuation">)</span> print<span class="token punctuation">(</span>text1<span class="token punctuation">)</span> print<span class="token punctuation">(</span>text2<span class="token punctuation">)</span> time2 <span class="token operator">=</span> time<span class="token punctuation">.</span>time<span class="token punctuation">(</span><span class="token punctuation">)</span> print<span class="token punctuation">(</span><span class="token string">'总共耗时:'</span> <span class="token operator">+</span> str<span class="token punctuation">(</span>time2 <span class="token operator">-</span> time1<span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">'s'</span><span class="token punctuation">)</span>
运行结果
新疆骚乱苹果新品发布会雞八
****苹果新品发布会**
总共耗时:0.0010304450988769531s