探秘高效字符串搜索:Alfred-Margaret
2024-05-31 00:46:14作者:魏献源Searcher
Alfred-Margaret是一个由Haskell语言实现的快速Aho-Corasick字符串搜索算法库,它在Channable项目中广泛应用于各种字符串处理操作。该库巧妙地利用了text包的内部UTF-16表示,以提高效率。想知道更多关于Aho-Corasick算法以及这个库的优化秘诀吗?可以查看这篇详细的公告博客文章。
性能优势
在真实数据集上的运行时间比较显示,Alfred-Margaret在与Java和Rust实现的性能较量中表现出色,甚至优于内存复制的速度基准(见下图):

想要了解更多关于这个基准测试的详细信息,包括数据集、测试设置和解读提示,可以查看博客文章。
LLVM优化提示
如果你使用的是LLVM而非GHC后端,请确保对比不同版本。在GHC 8.10.7上,使用LLVM 9相对于LLVM 12可能会显著改善基准测试时长。具体可参考这个问题。
使用示例
简单来说,Alfred-Margaret让你能轻松检查一个字符串是否包含任何指定的子串:
import qualified Data.Text.AhoCorasick.Automaton as Aho
import qualified Data.Text.AhoCorasick.Searcher as Searcher
searcher = Searcher.build Aho.CaseSensitive ["tshirt", "shirts", "shorts"]
-- 查找"short tshirts"中的匹配项
Searcher.containsAny searcher "short tshirts" -- > True
-- 查找"long shirt"中的匹配项
Searcher.containsAny searcher "long shirt" -- > False
-- 不区分大小写的查找
searcher' = Searcher.build Aho.IgnoreCase ["tshirt", "shirts", "shorts"]
Searcher.containsAny searcher' "Short TSHIRTS" -- > True
此外,你还可以进行多子串的顺序替换:
import Data.Text.AhoCorasick.Automaton (CaseSensitivity (..))
import qualified Data.Text.AhoCorasick.Replacer as Replacer
replacer = Replacer.build CaseSensitive [("tshirt", "banana"), ("shirt", "pear")]
-- 替换所有"tshirts for sale"中的"tshirt"和"shirts"
Replacer.run replacer "tshirts for sale" -- > "bananas for sale"
-- 同时替换多个子串
Replacer.run replacer "tshirts and shirts for sale"
-- > "bananas and pears for sale"
-- 处理重叠匹配情况
Replacer.run replacer "sweatshirts and shirtshirts"
-- > "sweabananas and shirbananas"
Replacer.run replacer "sweatshirts and shirttshirts"
-- > "sweabananas and pearbananas"
甚至,你可以获取所有可能重叠的匹配项:
import qualified Data.Text.AhoCorasick.Automaton as Aho
pairNeedleWithSelf text = (Aho.unpackUtf16 text, text)
automaton = Aho.build $ fmap pairNeedleWithSelf ["tshirt", "shirts", "shorts"]
allMatches = Aho.runText [] (\matches match -> Aho.Step (match : matches))
-- 获取"short tshirts"的所有匹配项
allMatches automaton "short tshirts"
> [ Match {matchPos = CodeUnitIndex 13, matchValue = "shirts"}
> , Match {matchPos = CodeUnitIndex 12, matchValue = "tshirt"}
> ]
-- 找到"sweatshirts and shirtshirts"的所有匹配项
allMatches automaton "sweatshirts and shirtshirts"
> [ Match {matchPos = CodeUnitIndex 27, matchValue = "shirts"}
> , Match {matchPos = CodeUnitIndex 26, matchValue = "tshirt"}
> , Match {matchPos = CodeUnitIndex 22, matchValue = "shirts"}
> , Match {matchPos = CodeUnitIndex 11, matchValue = "shirts"}
> , Match {matchPos = CodeUnitIndex 10, matchValue = "tshirt"}}
> ]
许可证
Alfred-Margaret遵循3-clause BSD许可证。
结语
无论是用于日志分析、文本挖掘还是搜索引擎,Alfred-Margaret都是一款强大的工具,它能帮助你在大量字符串数据中快速定位并替换目标子串。凭借其出色性能和易用性,这款库绝对值得你尝试。立即加入Haskell的Aho-Corasick世界,让您的字符串处理任务变得更快更高效!
登录后查看全文
热门项目推荐
atomcodeClaude Code 的开源替代方案。连接任意大模型,编辑代码,运行命令,自动验证 — 全自动执行。用 Rust 构建,极致性能。 | An open-source alternative to Claude Code. Connect any LLM, edit code, run commands, and verify changes — autonomously. Built in Rust for speed. Get StartedRust099- DDeepSeek-V4-ProDeepSeek-V4-Pro(总参数 1.6 万亿,激活 49B)面向复杂推理和高级编程任务,在代码竞赛、数学推理、Agent 工作流等场景表现优异,性能接近国际前沿闭源模型。Python00
MiMo-V2.5-ProMiMo-V2.5-Pro作为旗舰模型,擅⻓处理复杂Agent任务,单次任务可完成近千次⼯具调⽤与⼗余轮上 下⽂压缩。Python00
GLM-5.1GLM-5.1是智谱迄今最智能的旗舰模型,也是目前全球最强的开源模型。GLM-5.1大大提高了代码能力,在完成长程任务方面提升尤为显著。和此前分钟级交互的模型不同,它能够在一次任务中独立、持续工作超过8小时,期间自主规划、执行、自我进化,最终交付完整的工程级成果。Jinja00
Kimi-K2.6Kimi K2.6 是一款开源的原生多模态智能体模型,在长程编码、编码驱动设计、主动自主执行以及群体任务编排等实用能力方面实现了显著提升。Python00
MiniMax-M2.7MiniMax-M2.7 是我们首个深度参与自身进化过程的模型。M2.7 具备构建复杂智能体应用框架的能力,能够借助智能体团队、复杂技能以及动态工具搜索,完成高度精细的生产力任务。Python00
项目优选
收起
deepin linux kernel
C
28
16
Claude Code 的开源替代方案。连接任意大模型,编辑代码,运行命令,自动验证 — 全自动执行。用 Rust 构建,极致性能。 | An open-source alternative to Claude Code. Connect any LLM, edit code, run commands, and verify changes — autonomously. Built in Rust for speed.
Get Started
Rust
570
99
暂无描述
Dockerfile
709
4.51 K
本项目是CANN提供的数学类基础计算算子库,实现网络在NPU上加速计算。
C++
958
955
🎉 (RuoYi)官方仓库 基于SpringBoot,Spring Security,JWT,Vue3 & Vite、Element Plus 的前后端分离权限管理系统
Vue
1.61 K
942
Ascend Extension for PyTorch
Python
572
694
openEuler内核是openEuler操作系统的核心,既是系统性能与稳定性的基石,也是连接处理器、设备与服务的桥梁。
C
413
339
🍒 Cherry Studio 是一款支持多个 LLM 提供商的桌面客户端
TypeScript
1.42 K
116
暂无简介
Dart
952
235
Nop Platform 2.0是基于可逆计算理论实现的采用面向语言编程范式的新一代低代码开发平台,包含基于全新原理从零开始研发的GraphQL引擎、ORM引擎、工作流引擎、报表引擎、规则引擎、批处理引引擎等完整设计。nop-entropy是它的后端部分,采用java语言实现,可选择集成Spring框架或者Quarkus框架。中小企业可以免费商用
Java
12
2