(一)pyahocorasick和marisa_trie,字符串快速查找的python包,自然语言处理,命名实体识别可用的高效包...

news/2025/1/20 3:01:06/

Pyahocorasick

Pyahocorasick是一个基于AC自动机算法的字符串匹配工具。它可以用于快速查找多个短字符串在一个长字符串中的所有出现位置。Pyahocorasick可以在构建状态机时使用多线程,从而大大加快构建速度。

安装Pyahocorasick

Pyahocorasick可以使用pip命令进行安装:

pip install pyahocorasick

使用Pyahocorasick

以下是使用Pyahocorasick进行字符串匹配的示例代码:

import ahocorasick# 构建模式匹配自动机
patterns = ['he', 'she', 'his', 'hers']
automaton = ahocorasick.Automaton()
for pattern in patterns:automaton.add_word(pattern, pattern)
automaton.make_automaton()# 在文本中查找匹配
text = 'ushershewashis'
matches = []
for end_index, matched_pattern in automaton.iter(text):start_index = end_index - len(matched_pattern) + 1matches.append((matched_pattern, start_index, end_index))
print(matches)

  输出:

[('she', 1, 3), ('he', 2, 3), ('hers', 2, 5), ('she', 5, 7), ('he', 6, 7), ('his', 11, 13)]

Marisa_trie

Marisa_trie是一个高效的Trie树实现,可以用于存储和查找大量字符串。它能够压缩存储空间,并提供快速的前缀匹配和近似匹配功能。Marisa_trie还支持多种不同的序列化格式,可以在不同的程序和平台之间共享。

安装Marisa_trie

Marisa_trie可以使用pip命令进行安装:

pip install marisa-trie

使用Marisa_trie

以下是使用Marisa_trie进行字符串匹配的示例代码:

import marisa_trie# 构建trie
short_strings = ['hello', 'world', 'python','py']
trie = marisa_trie.Trie(short_strings)# 匹配长字符串
long_string = 'this is a hello world example using python hello'results = []
for i in range(len(long_string)):matches = trie.prefixes(long_string[i:])# 输出匹配结果if matches:for matche in matches:results.append((matche,i,i+len(matche)))print(results)

结果:

[('hello', 10, 15), ('world', 16, 21), ('py', 36, 38), ('python', 36, 42), ('hello', 43, 48)]

在以上示例代码中,我们首先构建了一个包含多个短字符串的Trie树。然后我们遍历文本中的所有前缀,并在Trie树中查找匹配的前缀。一旦找到匹配的前缀,我们可以计算匹配的起始和结束位置,并将它们添加到匹配列表中。


http://www.ppmy.cn/news/52050.html

相关文章

Linux设备驱动开发详解

Linux 内核系列文章 Linux 内核设计与实现 深入理解 Linux 内核 Linux 设备驱动程序 Linux设备驱动开发详解 文章目录 Linux 内核系列文章前言一、pandas是什么?二、使用步骤1、引入库2、Linux3、C4、Java5、Spring6、Qt7、Android8、Docker9、Web10、Windows11、Py…

【MATLAB数据处理实用案例详解(13)】——利用Elman网络实现上证股市开盘价预测

目录 一、问题描述二、Elman网络预测上证股市开盘价原理三、算法步骤3.1 加载数据3.2 构造样本集3.3 划分训练集和测试集3.4 创建Elman神经网络3.5 网络训练3.6 测试 四、结果展示 一、问题描述 选择2005年6月30日至2006年12月1日的上证开盘价进行预测分析。数据保存在elm_sto…

110页智慧农业解决方案(农业信息化解决方案)(ppt可编辑)

本资料来源公开网络,仅供个人学习,请勿商用,如有侵权请联系删除。 第一部分 智慧农业概述 智慧农业以农业资源为基础、市场为导向、效益为中心、产业化为抓手,面向农业管理部门、农技推广部门、农业企业、农业园区和基地、农业专…

4月19日驱动开发

点亮3个led灯和蜂鸣器和风扇 #ifndef __HEAD_H_ #define __HEAD_H_ typedef struct { unsigned int moder; unsigned int otyper; unsigned int ospeedr; unsigned int pupdr; unsigned int idr; unsigned int odr; }gpio_t; #define PHY_LED1 0x50006000 #define PHY_LED2…

Android 自动亮度

config_autoBrightnessLcdBacklightValues config_autoBrightnessLevels 自动背光开关 <bool name="config_automatic_brightness_available">true</bool><integer name="config_autoBrightnessBrighteningLightDebounce">2000</inte…

反射-Class类分析

反射相关的主要类 java.lang.Class&#xff1a;代表一个类&#xff0c;Class对象表示某个类加载后在堆中的对象java.lang.reflect.Method&#xff1a;代表类的方法&#xff0c;Method对象表示某个类的方法java.lang.reflect.Field&#xff1a;代表类的成员变量&#xff0c;Fie…

深度学习 - 45.MMOE Gate 简单实现 By Keras

目录 一.引言 二.MMoE 模型分析 三.MMoE 逻辑实现 • Input • Expert Output • Gate Output • Weighted Sum • Sigmoid Output • 完整代码 四.总结 一.引言 上一篇文章介绍了 MMoE 借鉴 MoE 的思路&#xff0c;为每一类输出构建一个 Gate 并最终加权多个 Exper…

alibaba arthas的新人上手教程

背景 Arthas 是Alibaba开源的Java诊断工具。 github开源地址&#xff1a;GitHub - alibaba/arthas: Alibaba Java Diagnostic Tool Arthas/Alibaba Java诊断利器Arthas 上手教程 1.下载arthas&#xff0c;并测试运行demo curl -O https://arthas.aliyun.com/arthas-boot.j…