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

news/2024/10/6 20:47:38 标签: 面试, 算法, python
  • 自己写的分组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



http://www.niftyadmin.cn/n/5692150.html

相关文章

华为OD机试 - 约瑟夫问题(Python/JS/C/C++ 2024 E卷 200分)

华为OD机试 2024E卷题库疯狂收录中,刷题点这里 专栏导读 本专栏收录于《华为OD机试真题(Python/JS/C/C)》。 刷的越多,抽中的概率越大,私信哪吒,备注华为OD,加入华为OD刷题交流群,…

Vue3 动态路由实现的一种方法

动态路由 目的: 根据服务器传回来的数据动态的注册路由信息,登录用户的角色不同生成的菜单不同 使用插件做动态路由的好处: 路由页面增加或者减少时,只需要增加或减少相关的路由文件,不需要再修改代码 服务器返回的信…

《计算机原理与系统结构》学习系列——计算机的算数运算(上)

系列文章目录 目录 ALU行波进位加法器超前进位加法器整数运算加减法乘法无符号数相乘N位乘法数的工作流程N位乘法器改进:硬件资源更快速的乘法 MIPS中的乘法除法 32位除法器流程除法器改进 更快速的除法 MIPS中的除法总结 ALU ALU功能:对a,…

Pikachu-SSRF(curl / file_get_content)

SSRF SSRF是Server-side Request Forge的缩写,中文翻译为服务端请求伪造。产生的原因是由于服务端提供了从其他服务器应用获取数据的功能且没有对地址和协议等做过滤和限制。常见的一个场景就是,通过用户输入的URL来获取图片。这个功能如果被恶意使用&am…

【STM32】TCP/IP通信协议(2)--LwIP内存管理

五、LWIP内存管理 1.什么是内存管理? (1)内存管理,是指软件运行时对计算机内存资源的分配的使用的技术,其主要目的是如何高效、快速的分配,并且在适当的时候释放和回收内存资源(就比如C语言当…

【鼠鼠学AI代码合集#7】概率

机器学习与概率——理解预测的核心 1. 机器学习的本质:基于数据做预测 机器学习的核心目标是基于已有的数据来进行预测和推断。在不同领域,机器学习的应用场景广泛,但背后的逻辑都是基于概率模型来估计某种事件发生的可能性。以下是几个典型…

Vue入门-指令学习-v-on

v-on 作用:注册事件 添加监听 提供处理逻辑 语法: v-on:事件名"内联语句" v-on:事件名"methods中的函数名" 注意:" v-on:"可以替换为" " v-on:click"XXX" --> cli…

html+css+js实现step进度条效果

实现效果 代码实现 HTML部分 <div class"box"><ul class"step"><li class"circle actives ">1</li><li class"circle">2</li><li class"circle">3</li><li class&quo…