Python实现敏感词过滤的4种方法
在我们生活中的一些场合经常会有一些不该出现的敏感词,我们通常会使用*去屏蔽它,例如:尼玛->**,一些骂人的敏感词和一些政治敏感词都不应该出现在一些公共场合中,这个时候我们就需要一定的手段去屏蔽这些敏感词。下面我来介绍一些简单版本的敏感词屏蔽的方法。
(我已经尽量把脏话做成图片的形式了,要不然文章发不出去)
方法一:replace过滤
replace就是最简单的字符串替换,当一串字符串中有可能会出现的敏感词时,我们直接使用相应的replace方法用*替换出敏感词即可。
缺点:
文本和敏感词少的时候还可以,多的时候效率就比较差了
importdatetime now=datetime.datetime.now() print(filter_sentence,"|",now)
如果是多个敏感词可以用列表进行逐一替换
foriindirty: speak=speak.replace(i,'*') print(speak,"|",now)
方法二:正则表达式过滤
正则表达式算是一个不错的匹配方法了,日常的查询中,机会都会用到正则表达式,包括我们的爬虫,也都是经常会使用到正则表达式的,在这里我们主要是使用“|”来进行匹配,“|”的意思是从多个目标字符串中选择一个进行匹配。写个简单的例子:
importre
defsentence_filter(keywords,text):
returnre.sub("|".join(keywords),"***",text)
print(sentence_filter(dirty,speak))
方法三:DFA过滤算法
DFA的算法,即DeterministicFiniteAutomaton算法,翻译成中文就是确定有穷自动机算法。它的基本思想是基于状态转移来检索敏感词,只需要扫描一次待检测文本,就能对所有敏感词进行检测。(实现见代码注释)
#!/usr/bin/envpython
#-*-coding:utf-8-*-
#@Time:2020/4/1511:40
#@Software:PyCharm
#article_add:https://www.cnblogs.com/JentZhang/p/12718092.html
__author__="JentZhang"
importjson
MinMatchType=1#最小匹配规则
MaxMatchType=2#最大匹配规则
classDFAUtils(object):
"""
DFA算法
"""
def__init__(self,word_warehouse):
"""
算法初始化
:paramword_warehouse:词库
"""
#词库
self.root=dict()
#无意义词库,在检测中需要跳过的(这种无意义的词最后有个专门的地方维护,保存到数据库或者其他存储介质中)
self.skip_root=['','&','!','!','@','#','$','¥','*','^','%','?','?','<','>',"《",'》']
#初始化词库
forwordinword_warehouse:
self.add_word(word)
defadd_word(self,word):
"""
添加词库
:paramword:
:return:
"""
now_node=self.root
word_count=len(word)
foriinrange(word_count):
char_str=word[i]
ifchar_strinnow_node.keys():
#如果存在该key,直接赋值,用于下一个循环获取
now_node=now_node.get(word[i])
now_node['is_end']=False
else:
#不存在则构建一个dict
new_node=dict()
ifi==word_count-1:#最后一个
new_node['is_end']=True
else:#不是最后一个
new_node['is_end']=False
now_node[char_str]=new_node
now_node=new_node
defcheck_match_word(self,txt,begin_index,match_type=MinMatchType):
"""
检查文字中是否包含匹配的字符
:paramtxt:待检测的文本
:parambegin_index:调用getSensitiveWord时输入的参数,获取词语的上边界index
:parammatch_type:匹配规则1:最小匹配规则,2:最大匹配规则
:return:如果存在,则返回匹配字符的长度,不存在返回0
"""
flag=False
match_flag_length=0#匹配字符的长度
now_map=self.root
tmp_flag=0#包括特殊字符的敏感词的长度
foriinrange(begin_index,len(txt)):
word=txt[i]
#检测是否是特殊字符"
ifwordinself.skip_rootandlen(now_map)<100:
#len(nowMap)<100保证已经找到这个词的开头之后出现的特殊字符
tmp_flag+=1
continue
#获取指定key
now_map=now_map.get(word)
ifnow_map:#存在,则判断是否为最后一个
#找到相应key,匹配标识+1
match_flag_length+=1
tmp_flag+=1
#如果为最后一个匹配规则,结束循环,返回匹配标识数
ifnow_map.get("is_end"):
#结束标志位为true
flag=True
#最小规则,直接返回,最大规则还需继续查找
ifmatch_type==MinMatchType:
break
else:#不存在,直接返回
break
iftmp_flag<2ornotflag:#长度必须大于等于1,为词
tmp_flag=0
returntmp_flag
defget_match_word(self,txt,match_type=MinMatchType):
"""
获取匹配到的词语
:paramtxt:待检测的文本
:parammatch_type:匹配规则1:最小匹配规则,2:最大匹配规则
:return:文字中的相匹配词
"""
matched_word_list=list()
foriinrange(len(txt)):#0---11
length=self.check_match_word(txt,i,match_type)
iflength>0:
word=txt[i:i+length]
matched_word_list.append(word)
#i=i+length-1
returnmatched_word_list
defis_contain(self,txt,match_type=MinMatchType):
"""
判断文字是否包含敏感字符
:paramtxt:待检测的文本
:parammatch_type:匹配规则1:最小匹配规则,2:最大匹配规则
:return:若包含返回true,否则返回false
"""
flag=False
foriinrange(len(txt)):
match_flag=self.check_match_word(txt,i,match_type)
ifmatch_flag>0:
flag=True
returnflag
defreplace_match_word(self,txt,replace_char='*',match_type=MinMatchType):
"""
替换匹配字符
:paramtxt:待检测的文本
:paramreplace_char:用于替换的字符,匹配的敏感词以字符逐个替换,如"你是大王八",敏感词"王八",替换字符*,替换结果"你是大**"
:parammatch_type:匹配规则1:最小匹配规则,2:最大匹配规则
:return:替换敏感字字符后的文本
"""
tuple_set=self.get_match_word(txt,match_type)
word_set=[iforiintuple_set]
result_txt=""
iflen(word_set)>0:#如果检测出了敏感词,则返回替换后的文本
forwordinword_set:
replace_string=len(word)*replace_char
txt=txt.replace(word,replace_string)
result_txt=txt
else:#没有检测出敏感词,则返回原文本
result_txt=txt
returnresult_txt
if__name__=='__main__':
dfa=DFAUtils(word_warehouse=word_warehouse)
print('词库结构:',json.dumps(dfa.root,ensure_ascii=False))
#待检测的文本
msg=msg
print('是否包含:',dfa.is_contain(msg))
print('相匹配的词:',dfa.get_match_word(msg))
print('替换包含的词:',dfa.replace_match_word(msg))
方法四:AC自动机
AC自动机需要有前置知识:Trie树(简单介绍:又称前缀树,字典树,是用于快速处理字符串的问题,能做到快速查找到一些字符串上的信息。)
详细参考:
https://www.luogu.com.cn/blog/juruohyfhaha/trie-xue-xi-zong-jie
ac自动机,就是在tire树的基础上,增加一个fail指针,如果当前点匹配失败,则将指针转移到fail指针指向的地方,这样就不用回溯,而可以路匹配下去了。
详细匹配机制我在这里不过多赘述,关于AC自动机可以参考一下这篇文章:
https://www.nhooo.com/article/128711.htm
python可以利用ahocorasick模块快速实现:
#python3-mpipinstallpyahocorasick
importahocorasick
defbuild_actree(wordlist):
actree=ahocorasick.Automaton()
forindex,wordinenumerate(wordlist):
actree.add_word(word,(index,word))
actree.make_automaton()
returnactree
if__name__=='__main__':
actree=build_actree(wordlist=wordlist)
sent_cp=sent
foriinactree.iter(sent):
sent_cp=sent_cp.replace(i[1][1],"**")
print("屏蔽词:",i[1][1])
print("屏蔽结果:",sent_cp)
当然,我们也可以手写一份AC自动机,具体参考:
classTrieNode(object):
__slots__=['value','next','fail','emit']
def__init__(self,value):
self.value=value
self.next=dict()
self.fail=None
self.emit=None
classAhoCorasic(object):
__slots__=['_root']
def__init__(self,words):
self._root=AhoCorasic._build_trie(words)
@staticmethod
def_build_trie(words):
assertisinstance(words,list)andwords
root=TrieNode('root')
forwordinwords:
node=root
forcinword:
ifcnotinnode.next:
node.next[c]=TrieNode(c)
node=node.next[c]
ifnotnode.emit:
node.emit={word}
else:
node.emit.add(word)
queue=[]
queue.insert(0,(root,None))
whilelen(queue)>0:
node_parent=queue.pop()
curr,parent=node_parent[0],node_parent[1]
forsubincurr.next.itervalues():
queue.insert(0,(sub,curr))
ifparentisNone:
continue
elifparentisroot:
curr.fail=root
else:
fail=parent.fail
whilefailandcurr.valuenotinfail.next:
fail=fail.fail
iffail:
curr.fail=fail.next[curr.value]
else:
curr.fail=root
returnroot
defsearch(self,s):
seq_list=[]
node=self._root
fori,cinenumerate(s):
matched=True
whilecnotinnode.next:
ifnotnode.fail:
matched=False
node=self._root
break
node=node.fail
ifnotmatched:
continue
node=node.next[c]
ifnode.emit:
for_innode.emit:
from_index=i+1-len(_)
match_info=(from_index,_)
seq_list.append(match_info)
node=self._root
returnseq_list
if__name__=='__main__':
aho=AhoCorasic(['foo','bar'])
printaho.search('barfoothefoobarman')
以上便是使用Python实现敏感词过滤的四种方法,前面两种方法比较简单,后面两种偏向算法,需要先了解算法具体实现的原理,之后代码就好懂了。(DFA作为比较常用的过滤手段,建议大家掌握一下~)
最后附上敏感词词库:
https://github.com/qloog/sensitive_words
以上就是Python实现敏感词过滤的4种方法的详细内容,更多关于python敏感词过滤的资料请关注毛票票其它相关文章!
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。