python 敏感词过滤

敏感词过滤的经典算法DFA ,做了评估实验

先上代码

[python]  view plain  copy
  1. #!/usr/bin/python2.6    
  2. # -*- coding: utf-8 -*-  
  3. import time  
  4. class Node(object):  
  5.     def __init__(self):  
  6.         self.children = None  
  7.   
  8. # The encode of word is UTF-8  
  9. def add_word(root,word):  
  10.     node = root  
  11.     for i in range(len(word)):  
  12.         if node.children == None:  
  13.             node.children = {}  
  14.             node.children[word[i]] = Node()  
  15.   
  16.         elif word[i] not in node.children:  
  17.             node.children[word[i]] = Node()  
  18.   
  19.         node = node.children[word[i]]  
  20.   
  21. def init(path):  
  22.     root = Node()  
  23.     fp = open(path,'r')  
  24.     for line in fp:  
  25.         line = line[0:-1]  
  26.         #print len(line)  
  27.         #print line  
  28.         #print type(line)  
  29.         add_word(root,line)  
  30.     fp.close()  
  31.     return root  
  32.   
  33. # The encode of word is UTF-8  
  34. # The encode of message is UTF-8  
  35. def is_contain(message, root):  
  36.     for i in range(len(message)):  
  37.         p = root  
  38.         j = i  
  39.         while (j<len(message) and p.children!=None and message[j] in p.children):  
  40.             p = p.children[message[j]]  
  41.             j = j + 1  
  42.   
  43.         if p.children==None:  
  44.             #print '---word---',message[i:j]  
  45.             return True  
  46.       
  47.     return False  
  48.   
  49.   
  50.   
  51. def dfa():  
  52.     print '----------------dfa-----------'  
  53.     root = init('/tmp/word.txt')  
  54.   
  55.     message = '四处乱咬乱吠,吓得家中11岁的女儿躲在屋里不敢出来,直到辖区派出所民警赶到后,才将孩子从屋中救出。最后在征得主人同意后,民警和村民合力将这只发疯的狗打死'  
  56.     #message = '不顾'  
  57.     print '***message***',len(message)  
  58.     start_time = time.time()  
  59.     for i in range(1000):  
  60.         res = is_contain(message,root)  
  61.         #print res  
  62.     end_time = time.time()  
  63.     print (end_time - start_time)   
  64.   
  65. def is_contain2(message,word_list):  
  66.     for item in word_list:  
  67.         if message.find(item)!=-1:  
  68.             return True  
  69.     return False  
  70.   
  71. def normal():  
  72.     print '------------normal--------------'  
  73.     path = '/tmp/word.txt'  
  74.     fp = open(path,'r')  
  75.     word_list = []  
  76.     message = '四处乱咬乱吠,吓得家中11岁的女儿躲在屋里不敢出来,直到辖区派出所民警赶到后,才将孩子从屋中救出。最后在征得主人同意后,民警和村民合力将这只发疯的狗打死'  
  77.     print '***message***',len(message)  
  78.     for line in fp:  
  79.         line = line[0:-1]  
  80.         word_list.append(line)  
  81.     fp.close()  
  82.     print 'The count of word:',len(word_list)  
  83.     start_time = time.time()  
  84.     for i in range(1000):  
  85.         res = is_contain2(message,word_list)  
  86.         #print res  
  87.     end_time = time.time()  
  88.     print (end_time - start_time)   
  89.   
  90.   
  91. if __name__ == '__main__':  
  92.     dfa()  
  93.     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 算法的时间复杂度基本上是与敏感词的个数无关的。


  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值