python 实现敏感词过滤的方法

作者:isoleo 时间:2022-08-09 10:22:51 

如下所示:


#!/usr/bin/python2.6  
# -*- coding: utf-8 -*-
import time
class Node(object):
 def __init__(self):
   self.children = None

# The encode of word is UTF-8
def add_word(root,word):
 node = root
 for i in range(len(word)):
   if node.children == None:
     node.children = {}
     node.children[word[i]] = Node()

elif word[i] not in node.children:
     node.children[word[i]] = Node()

node = node.children[word[i]]

def init(path):
 root = Node()
 fp = open(path,'r')
 for line in fp:
   line = line[0:-1]
   #print len(line)
   #print line
   #print type(line)
   add_word(root,line)
 fp.close()
 return root

# The encode of word is UTF-8
# The encode of message is UTF-8
def is_contain(message, root):
 for i in range(len(message)):
   p = root
   j = i
   while (j<len(message) and p.children!=None and message[j] in p.children):
     p = p.children[message[j]]
     j = j + 1

if p.children==None:
     #print '---word---',message[i:j]
     return True

return False

def dfa():
 print '----------------dfa-----------'
 root = init('/tmp/word.txt')

message = '四处乱咬乱吠,吓得家中11岁的女儿躲在屋里不敢出来,直到辖区派出所民警赶到后,才将孩子从屋中救出。最后在征得主人同意后,民警和村民合力将这只发疯的狗打死'
 #message = '不顾'
 print '***message***',len(message)
 start_time = time.time()
 for i in range(1000):
   res = is_contain(message,root)
   #print res
 end_time = time.time()
 print (end_time - start_time)  

def is_contain2(message,word_list):
 for item in word_list:
   if message.find(item)!=-1:
     return True
 return False

def normal():
 print '------------normal--------------'
 path = '/tmp/word.txt'
 fp = open(path,'r')
 word_list = []
 message = '四处乱咬乱吠,吓得家中11岁的女儿躲在屋里不敢出来,直到辖区派出所民警赶到后,才将孩子从屋中救出。最后在征得主人同意后,民警和村民合力将这只发疯的狗打死'
 print '***message***',len(message)
 for line in fp:
   line = line[0:-1]
   word_list.append(line)
 fp.close()
 print 'The count of word:',len(word_list)
 start_time = time.time()
 for i in range(1000):
   res = is_contain2(message,word_list)
   #print res
 end_time = time.time()
 print (end_time - start_time)  

if __name__ == '__main__':
 dfa()
 normal()

测试结果:

1) 敏感词 100个


----------------dfa-----------
***message*** 224
0.325479984283
------------normal--------------
***message*** 224
The count of word: 100
0.107350111008

2) 敏感词 1000 个


----------------dfa-----------
***message*** 224
0.324251890182
------------normal--------------
***message*** 224
The count of word: 1000
1.05939006805

从上面的实验我们可以看出,在DFA 算法只有在敏感词较多的情况下,才有意义。在百来个敏感词的情况下,甚至不如普通算法

下面从理论上推导时间复杂度,为了方便分析,首先假定消息文本是等长的,长度为lenA;每个敏感词的长度相同,长度为lenB,敏感词的个数是m。

1) DFA算法的核心是构建一棵多叉树,由于我们已经假设,敏感词的长度相同,所以树的最大深度为lenB,那么我们可以说从消息文本的某个位置(字节)开始的某个子串是否在敏感词树中,最多只用经过lenB次匹配.也就是说判断一个消息文本中是否有敏感词的时间复杂度是lenA * lenB

2) 再来看看普通做法,是使用for循环,对每一个敏感词,依次在消息文本中进行查找,假定字符串是使用KMP算法,KMP算法的时间复杂度是O(lenA + lenB)

那么对m个敏感词查找的时间复杂度是 (lenA + lenB ) * m

综上所述,DFA 算法的时间复杂度基本上是与敏感词的个数无关的。

来源:https://blog.csdn.net/isoleo/article/details/72379829

标签:python,敏感词,过滤
0
投稿

猜你喜欢

  • PHP数组中头部和尾部添加元素的方法(array_unshift,array_push)

    2023-06-06 22:45:50
  • Python基础之常用库常用方法整理

    2022-10-30 10:43:26
  • 完美解决ARIMA模型中plot_acf画不出图的问题

    2023-07-13 14:17:34
  • XML数据查询技术已经成为现今的研究热点

    2008-09-05 17:13:00
  • Python+django实现文件上传

    2022-08-31 20:52:22
  • 利用pyecharts读取csv并进行数据统计可视化的实现

    2023-07-15 07:18:03
  • asp随机提取access数据库记录的几种方法

    2007-09-06 19:42:00
  • python使用隐式循环快速求和的实现示例

    2022-10-09 13:04:05
  • 一个PHP的QRcode类与大家分享

    2023-06-24 05:39:33
  • 如何把外网python虚拟环境迁移到内网

    2021-12-28 14:08:13
  • Sun正式发布MySQL 5.1版 简化数据库应用

    2008-12-11 15:15:00
  • ASP脚本循环语句

    2009-02-19 13:34:00
  • 在notepad++中实现直接运行python代码

    2022-08-19 09:52:00
  • 进一步了解Python中的XML 工具

    2022-06-25 21:49:17
  • 在Python程序和Flask框架中使用SQLAlchemy的教程

    2021-10-28 06:14:21
  • Python批量按比例缩小图片脚本分享

    2022-06-06 11:46:12
  • Python selenium如何设置等待时间

    2023-08-31 18:53:39
  • 正则表达式字面量在ECMAScript5中的变化

    2012-04-26 16:23:16
  • python保存图片的四个常用方法

    2023-10-03 15:06:37
  • sql server not in 语句使程充崩溃

    2012-01-05 19:05:00
  • asp之家 网络编程 m.aspxhome.com