You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Polars的replace_many函数中配置Aho-Corasick的MatchKind

如何在Polars的replace_many中指定Aho-Corasick的MatchKind?

我查看Polars源码后发现,其内部构建Aho-Corasick自动机的函数默认使用MatchKind::Standard,但这不符合我的使用场景:

fn build_ac(patterns: &StringChunked, ascii_case_insensitive: bool) -> PolarsResult<AhoCorasick> {
    AhoCorasickBuilder::new()
        .ascii_case_insensitive(ascii_case_insensitive)
        .build(patterns.downcast_iter().flatten().flatten())
        .map_err(|e| polars_err!(ComputeError: "could not build aho corasick automaton {}", e))
}

我的测试场景代码如下:

let patterns = &["abcd", "abc", "ab"];
let haystack = "asd abcd asd abc asd";

let ac = AhoCorasick::builder()
    .ascii_case_insensitive(true)
    .match_kind(MatchKind::Standard)
    .build(patterns)
    .unwrap();

let result = ac.replace_all(&haystack, &["1", "2", "3"]);
println!("{}", result)

使用MatchKind::Standard时得到的结果是:

asd 3cd asd 3c asd

但我期望匹配最长/最左优先的模式,得到结果:

asd 1 asd 2 asd

这需要使用MatchKind::LeftmostFirst或MatchKind::LeftmostLongest。请问有没有办法直接在Polars的replace_many函数中指定MatchKind,而不用自行编写函数并通过map来实现?

内容的提问来源于stack exchange,提问作者congrom

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 05:06:01