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
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值