# -*- coding: utf-8 -*-
"""
desc : build the ac tree
"""
class Node(object):
"""
define node class
"""
def __init__(self):
"""
initialize the ac node
"""
self.next = {}
self.fail = None
self.isWord = False
class Ahocorasick(object):
"""
define ac class
"""
def __init__(self):
"""
initialize the ac model
"""
self.__root = Node()
def addWord(self, word):
"""
add words to ac model
"""
tmp = self.__root
for i in word:
tmp = tmp.next.setdefault(i, Node())
tmp.isWord = True
def make(self):
"""
build the ac model, especialily the failure node
"""
tmpQueue = []
tmpQueue.append(self.__root)
while (len(tmpQueue) > 0):
temp = tmpQueue.pop()
p = None
for k, v in temp.next.items():
if temp == self.__root:
temp.next[k].fail = self.__root
else:
p = temp.fail
while p is not None:
if k in p.next:
temp.next[k].fail = p.next[k]
break
p = p.fail
if p is None:
temp.next[k].fail = self.__root
tmpQueue.append(temp.next[k])
def search(self, content):
"""
search any sentence in the ac model
"""
result = []
for currentPosition in range(len(content)):
word = content[currentPosition]
endWordIndex = currentPosition
p = self.__root
while word in p.next:
if p == self.__root:
startWordIndex = currentPosition
p = p.next[word]
if p.isWord:
result.append((startWordIndex, endWordIndex))
if p.next and endWordIndex + 1 < len(content):
endWordIndex += 1
word = content[endWordIndex]
else:
break
while (word not in p.next) and (p != self.__root):
p = p.fail
return result
AhModel = Ahocorasick()
AhModel = ahocorasick_fast.Ahocorasick()
for keyword in all_keyword_map:
AhModel.addWord(keyword)
AhModel.make()
# 匹配关键词
for pair in AhModel.search(query):
start, end = pair
keyword = query[start:end+1]
if keyword in all_keyword_map:
full_name = all_keyword_map[keyword]
print(full_name, keyword)