Use variable-length threshold for levenshtein distance

Fixes #348
This commit is contained in:
KtorZ 2023-02-22 13:29:39 +01:00
parent 00e9dabe82
commit bdee5e7995
No known key found for this signature in database
GPG Key ID: 33173CB6F77F4277
1 changed files with 2 additions and 1 deletions

View File

@ -875,11 +875,12 @@ fn suggest_neighbor<'a>(
items: impl Iterator<Item = &'a String>,
default: &'a str,
) -> String {
let threshold = (name.len() as f64).sqrt().round() as usize;
items
.map(|s| (s, levenshtein::distance(name, s)))
.min_by(|(_, a), (_, b)| a.cmp(b))
.and_then(|(suggestion, distance)| {
if distance <= 4 {
if distance <= threshold {
Some(format!("Did you mean '{}'?", suggestion.yellow()))
} else {
None