python实现Ahocorasick

# -*- 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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值