python 實(shí)現(xiàn)敏感詞過濾的方法
如下所示:
#!/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 = '不顧'
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 = []
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()
測試結(jié)果:
1) 敏感詞 100個(gè)
----------------dfa----------- ***message*** 224 0.325479984283 ------------normal-------------- ***message*** 224 The count of word: 100 0.107350111008
2) 敏感詞 1000 個(gè)
----------------dfa----------- ***message*** 224 0.324251890182 ------------normal-------------- ***message*** 224 The count of word: 1000 1.05939006805
從上面的實(shí)驗(yàn)我們可以看出,在DFA 算法只有在敏感詞較多的情況下,才有意義。在百來個(gè)敏感詞的情況下,甚至不如普通算法
下面從理論上推導(dǎo)時(shí)間復(fù)雜度,為了方便分析,首先假定消息文本是等長的,長度為lenA;每個(gè)敏感詞的長度相同,長度為lenB,敏感詞的個(gè)數(shù)是m。
1) DFA算法的核心是構(gòu)建一棵多叉樹,由于我們已經(jīng)假設(shè),敏感詞的長度相同,所以樹的最大深度為lenB,那么我們可以說從消息文本的某個(gè)位置(字節(jié))開始的某個(gè)子串是否在敏感詞樹中,最多只用經(jīng)過lenB次匹配.也就是說判斷一個(gè)消息文本中是否有敏感詞的時(shí)間復(fù)雜度是lenA * lenB
2) 再來看看普通做法,是使用for循環(huán),對(duì)每一個(gè)敏感詞,依次在消息文本中進(jìn)行查找,假定字符串是使用KMP算法,KMP算法的時(shí)間復(fù)雜度是O(lenA + lenB)
那么對(duì)m個(gè)敏感詞查找的時(shí)間復(fù)雜度是 (lenA + lenB ) * m
綜上所述,DFA 算法的時(shí)間復(fù)雜度基本上是與敏感詞的個(gè)數(shù)無關(guān)的。
以上這篇python 實(shí)現(xiàn)敏感詞過濾的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python多線程threading join和守護(hù)線程setDeamon原理詳解
這篇文章主要介紹了Python多線程threading join和守護(hù)線程setDeamon原理詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03
Python回調(diào)函數(shù)用法實(shí)例詳解
這篇文章主要介紹了Python回調(diào)函數(shù)用法,以實(shí)例形式較為詳細(xì)的分析了Python回調(diào)函數(shù)的定義、功能及相關(guān)使用技巧,需要的朋友可以參考下2015-07-07
python 中文件輸入輸出及os模塊對(duì)文件系統(tǒng)的操作方法
這篇文章主要介紹了python 中文件輸入輸出及os模塊對(duì)文件系統(tǒng)的操作方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-08-08
Keras實(shí)現(xiàn)將兩個(gè)模型連接到一起
這篇文章主要介紹了Keras實(shí)現(xiàn)將兩個(gè)模型連接到一起,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-05-05
python中使用iterrows()對(duì)dataframe進(jìn)行遍歷的實(shí)例
今天小編就為大家分享一篇python中使用iterrows()對(duì)dataframe進(jìn)行遍歷的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-06-06
使用llama?Index幫你訓(xùn)練pdf的示例詳解
這篇文章主要為大家介紹了使用llama?Index?幫你訓(xùn)練pdf,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
圖文詳解感知機(jī)算法原理及Python實(shí)現(xiàn)
感知機(jī)是二類分類的線性分類模型,其輸入為實(shí)例的特征向量,輸出為實(shí)例的類別(取+1和-1二值)。本文將為大家詳細(xì)講講感知機(jī)算法的原理及實(shí)現(xiàn),需要的可以參考一下2022-08-08
Python Selenium 之?dāng)?shù)據(jù)驅(qū)動(dòng)測試的實(shí)現(xiàn)
這篇文章主要介紹了Python Selenium 之?dāng)?shù)據(jù)驅(qū)動(dòng)測試的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
Python自動(dòng)化構(gòu)建工具scons使用入門筆記
這篇文章主要介紹了Python自動(dòng)化構(gòu)建工具scons使用入門筆記,本文講解了安裝scons、scons常用命令、scons使用示例等內(nèi)容,需要的朋友可以參考下2015-03-03

