Python Trie樹實現最長前綴字符串提取

在文本解析項目中,經常會碰到提取品牌、商家名等需求。如給定一個手機型號字符串,要求從中提取出品牌。Trie可以很好滿足此類需求。

Tire,也叫前綴樹字典樹,是一種數據結構,可以用來快速檢索字符串是否存在以及在字符串開始處抽取預定義的子字符串。搜索時間複雜度爲O(M)  M爲字符串長度。

Tire

代碼實現

Python中無指針,使用Dict實現樹結構。

# -*- coding: utf-8 -*-
"""
Trie for prefix search, a data structure that quickly matches and extracts predefined substrings
at the beginning of a given text (if they can be found).

We can also skip certain characters and still succeed in a match.
"""

default_ignored_chars = u' _-/'


class Trie(object):
    def __init__(self, items, ignored_chars=default_ignored_chars):
        """ Stores all given items into this trie. """
        self.ignored_chars = ignored_chars

        self.trie = {}
        for item in items:
            assert item, 'Empty/none item passed in'
            item = item.strip()
            assert item, 'Empty item given'
            curr_dict = self.trie
            for c in item.upper():
                if c not in self.ignored_chars:
                    curr_dict = curr_dict.setdefault(c, {})
            curr_dict['end'] = item

    def is_item(self, text):
        """ Return True if text is a valid item stored in this trie. """
        if not text:
            return False
        curr_dict = self.trie
        for c in text.upper():
            if c not in self.ignored_chars:
                if c not in curr_dict:
                    return False
                curr_dict = curr_dict[c]
        return 'end' in curr_dict

    def extract_longest_item(self, text):
        """ Return longest item-name found at beginning of the text. Also returns the
            offset where the item ends in case the caller wants to chop the string. """
        curr_dict, longest, offset = self.trie, None, 0

        if not text:
            return longest, offset

        for i, c in enumerate(text.upper()):
            if c not in self.ignored_chars:
                if c not in curr_dict:
                    return longest, offset
                curr_dict = curr_dict[c]
                if 'end' in curr_dict:
                    longest, offset = curr_dict['end'], i + 1

        return longest, offset


# tester
if __name__ == '__main__':

    brands = ['Huawei', 'OPPO', 'VIVO', 'Xiaomi', 'Xiao', 'HTC', 'Oneplus']
    model_name = 'xiaomi mix3'
    brand_lookup = Trie(brands)

    brand, offset = brand_lookup.extract_longest_item(model_name)
    print(brand, offset)

通過自己寫代碼理解Trie之後,也可以使用Google的開源實現  https://github.com/google/pygtrie

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章