1 个不稳定版本
0.1.0 | 2023年11月26日 |
---|
#31 in #levenshtein
45KB
1.5K SLoC
fuzzysearchrs
用 Rust 编写的模糊搜索库。基本上是 FuzzySearch.Net 的移植,后者又受到 fuzzysearch 的启发。
这是模糊搜索的在线版本,不使用索引,因此不是最快的模糊搜索。它主要是为了处理包含错误的 OCR 文档而创建的。
如果不需要插入和删除,仅使用替换通常至少快一个数量级。
用法
在字符串中搜索字符串
let pattern = "sometext";
let text = "here is someteext for you";
// Search with maximum distance 3, insertions, substitutions, deletions allowed
let options = FuzzySearchOptions::new(3);
let results = FuzzySearch::find(pattern, &text.chars().collect::<Vec<_>>(), &options)
.collect::<Vec<_>>();
// Search using only substitutions and maximum distance 3
let results = FuzzySearchSubstitutionsOnly::find(pattern, text, 3);
// Search using with more specific options, for example allowing more substitutions than insertions and deletions
let options = FuzzySearchOptions::with_individual_limits(3, 1, 1);
let results = FuzzySearch::find(pattern, &text.chars().collect::<Vec<_>>(), &options)
.collect::<Vec<_>>();
// Check for any matches using Iterator. Using next on the Iterator is more efficient since enumeration will stop after first match.
// This will not necessarily yield the best match though.
let first = FuzzySearch::find(pattern, &text.chars().collect::<Vec<_>>(), &options).next();
assert!(first.is_some());