面试系列-分组Tire树匹配算法

  • 自己写的分组Tire树匹配算法,该算法用于云南省人工智能重点实验室与云南电网合作项目(云南电网敏感信息识别系统),用于快速匹配文本将项目中数据算法抽离出来,特此分享!!!
  • 可以实现动态的插入、删除操作
python 复制代码
# 自己写的组Tire树筛选算法
# 该算法用于本实验室和云南电网敏感信息项目,用于快速匹配文本
# 将项目中数据算法抽离出来,特此分享

class TireNode:
    def __init__(self):
        self.children = {}  # 字典类型,类似与JAVA中的map
        self.group_ids = set()  # 初始化组ID为-1,表示未分配

# Tire树
class Tire:
    def __init__(self):
        self.root = TireNode()
    # 插入
    def insert(self, word, group_id):
        node = self.root
        for char in word:
            if char not in node.children:
                node.children[char] = TireNode()
            node = node.children[char]

        if group_id  not in node.group_ids:
            node.group_ids.add(group_id)  # 标记单词所属的组ID
            return True
        else:
            return False  # 代表当前Tire树中已经存在
    # 搜索
    def search(self, word):
        node = self.root
        for char in word:
            if char not in node.children:
                return None, word
            node = node.children[char]
        if len(node.group_ids) != 0:  # 如果group_ids不为空,说明已经到达结尾
            return node.group_ids,word
        return None, word
    # 删除
    def delete(self, group_id, word):
        node = self.root
        for char in word:
            if char not in node.children:
                return False  # 删除失败
            node = node.children[char]

        if group_id not in node.group_ids:
            return False
        else:
            node.group_ids.remove(group_id) # 移除集合中的group_id
            return True

# 基于TireTree算法的组关键词筛选
class KeyWords(object):

    def __init__(self):
        # 创建Tire树
        self.tire = Tire()
        # 记录每个group_id 所对应的关键词个数
        self.tire_group_ids = {}

        # 从数据库获取数据
        self.gjc_lists = [["电网信息", "电网"], []]

        # 将关键词插入Tire树,并记录每个组的关键词数量
        for group_id, keywords in enumerate(self.gjc_lists):
            for keyword in keywords:
                # 向Tire树中插入
                success = self.tire.insert(keyword, group_id)
                if success:   # 如果插入成功才进行更新
                    if group_id not in self.tire_group_ids:
                        self.tire_group_ids[group_id] = 1
                    else:
                        self.tire_group_ids[group_id] += self.tire_group_ids[group_id]

        print()
    # 文本匹配,必须匹配上某个组中所有关键词才算是匹配上
    def match(self, text):
        # 遍历文本,检查关键词
        group_dict = {}
        for i in range(len(text)):
            for j in range(i + 1, len(text) + 1):
                group_ids, group_word = self.tire.search(text[i:j])

                if group_ids is not None:
                    # 如果存在,可能有多个,因为不同组可能具有相同的关键词
                    for group_id in group_ids:
                        if group_id not in group_dict:  # 将查到的group_ids都记录下来
                            group_dict[group_id] = 1
                        else:
                            group_dict[group_id] += 1
                        # 如果发现某个组个数已经匹配上,则匹配成功
                        if group_dict[group_id] == self.tire_group_ids[group_id]:
                            return True

        # 如果都没有匹配上,说明没有匹配成功
        return False

    # 传入一个组以及word 来实现删除
    def delete(self, group_id, word):
        success = self.tire.delete(group_id, word)
        if success: # 如果删除成功,更新tire_group_ids
            if group_id in self.tire_group_ids:
                self.tire_group_ids[group_id] -= 1
        return success

    def insert(self, group_id, word):
        success = self.tire.insert(word, group_id)
        if success:
            if group_id not in self.tire_group_ids:
                self.tire_group_ids[group_id] = 1
            else:
                self.tire_group_ids[group_id] += 1
相关推荐
ada7_4 小时前
LeetCode(python)230.二叉搜索树中第k小的元素
python·算法·leetcode·链表
江上鹤.1484 小时前
Day36官方文档的阅读
python
嗝o゚4 小时前
Flutter 无障碍功能开发最佳实践
python·flutter·华为
芝麻开门-新起点4 小时前
第13-1章 Python地理空间开发
开发语言·python
TL滕4 小时前
从0开始学算法——第十五天(滑动窗口练习)
笔记·学习·算法
DuHz4 小时前
milliLoc 论文精读:把商用毫米波 FMCW 的绝对测距从“厘米栅格”推进到“毫米级连续值”,并顺带修正 AoA 的系统相位偏差
论文阅读·物联网·算法·信息与通信·毫米波雷达
UCoding4 小时前
新能源技术面试 -- 给出一套mysql备份容灾方案
mysql·面试·主从
qq_401700415 小时前
Linux文件锁解决多进程并发
linux·服务器·算法
秋刀鱼 ..5 小时前
2026年电力电子与电能变换国际学术会议 (ICPEPC 2026)
大数据·python·计算机网络·数学建模·制造
长安er5 小时前
LeetCode 83/237/82 链表删除问题-盒子模型
数据结构·算法·leetcode·链表·力扣