Skip to main content

Crate fuzzy_aho_corasick

Crate fuzzy_aho_corasick 

Source
Expand description

Unicode-aware Aho–Corasick automaton with fuzzy matching: substitutions, insertions, deletions, and transpositions, over grapheme clusters, with optional case-insensitive folding.

Build an immutable engine with FuzzyAhoCorasickBuilder, then query it. Similarity for a candidate match against a length-N pattern is (N - penalties) / N * weight, and a match is kept when it meets the caller’s threshold. Edit limits can be set globally on the builder or per Pattern, and edit costs are tuned via FuzzyPenalties.

§Example

use fuzzy_aho_corasick::{FuzzyAhoCorasickBuilder, FuzzyLimits, SearchOptions};

let engine = FuzzyAhoCorasickBuilder::new()
    .fuzzy(FuzzyLimits::new().edits(1))
    .case_insensitive(true)
    .build(["hello", "world"]);

// Two typos: an extra 'l' (insertion) and swapped 'lr' (transposition).
let matches = engine.search("helllo wolrd", &SearchOptions::new().threshold(0.8).sorted().non_overlapping()).unwrap();
let found: Vec<&str> = matches.iter().map(|m| m.pattern.as_str()).collect();
assert!(found.contains(&"hello") && found.contains(&"world"));

§Bounding worst-case work

The search is exact by default. When a high edit budget is combined with a low threshold the state space can explode; FuzzyAhoCorasickBuilder::beam_width caps the active frontier, and FuzzyAhoCorasickBuilder::auto_beam stays exact until a state budget is exceeded and only then engages a beam — leaving ordinary searches unaffected.

§Bit-parallel pre-filter

For large, mostly-non-matching inputs, FuzzyAhoCorasick::with_prefilter returns a Prefiltered wrapper that runs a bit-parallel approximate scan to locate candidate regions and only re-searches those with the full engine. Results are identical to FuzzyAhoCorasick::search; it falls back to a plain full search when the configuration can’t be reduced to the bit model.

See the README for a full guide.

Re-exports§

pub use structs::*;

Modules§

structs
The crate’s public data types (patterns, limits, penalties, matches, segments, …). Everything here is also re-exported at the crate root, so use fuzzy_aho_corasick::Pattern and use fuzzy_aho_corasick::structs::Pattern are equivalent.

Structs§

FuzzyAhoCorasickBuilder
Builder for FuzzyAhoCorasick.
FuzzyReplacer
A turnkey fuzzy find-and-replace built from (pattern → replacement) pairs.
Prefiltered
A FuzzyAhoCorasick wrapped with an optional bit-parallel pre-filter.
SearchOptions
Configuration for a search: the similarity threshold, the ranking order, and the overlap resolution. Construct with SearchOptions::new (all defaults) and refine with the chainable setters; or build one literally.
StreamMatch
A match found by the streaming API, with absolute byte offsets into the whole stream.
StreamMatches
Iterator returned by FuzzyAhoCorasick::stream_matches.

Enums§

Order
How the raw matches are ranked before they’re returned (and before overlap resolution).
Overlap
How overlapping matches are resolved after ranking.
SearchError
An error from a search call.

Constants§

DEFAULT_THRESHOLD
Default similarity threshold used when SearchOptions doesn’t set one. 0.0 keeps every match the configured edit limits allow — the limits are the real quality gate, so an unset threshold adds no extra similarity filter. Set SearchOptions::threshold to filter more aggressively.

Type Aliases§

PatternIndex
Index of a pattern within the automaton’s pattern list — the pattern_index on a FuzzyMatch, and the position of a pattern in the slice passed to build.