python 实现敏感词过滤的方法

yipeiwu_com6年前Python基础

如下所示:

#!/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 算法的时间复杂度基本上是与敏感词的个数无关的。

以上这篇python 实现敏感词过滤的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Numpy之文件存取的示例代码

上一篇中我们简要带过了Numpy的数据持久化,在这一篇中将要具体说明Numpy提供的文件存取功能。Numpy可以将数组保存至二进制文件、文本文件,同时支持将多个数组保存至一个文件中。 1...

解读Python编程中的命名空间与作用域

解读Python编程中的命名空间与作用域

变量是拥有匹配对象的名字(标识符)。命名空间是一个包含了变量名称们(键)和它们各自相应的对象们(值)的字典。 一个Python表达式可以访问局部命名空间和全局命名空间里的变量。如果一个局...

Python设计模式之抽象工厂模式

Python设计模式之抽象工厂模式

python面向对象编程入门,我们需要不断学习进步 """抽象工厂模式的实现""" import random class PetShop: """宠物商店""" d...

python抽取指定url页面的title方法

今天简单使用了一下python的re模块和lxml模块,分别利用的它们提供的正则表达式和xpath来解析页面源码从中提取所需的title,xpath在完成这样的小任务上效率非常好,在这里...

使用Python实现下载网易云音乐的高清MV

Python下载网易云音乐的高清MV,没有从首页进去解析,直接循环了.... downPage1.py 复制代码 代码如下: #coding=utf-8 import urllib im...