1
Fork 0

Fix issue when there are multiple candidates for edit_distance_with_substrings

This commit is contained in:
yukang 2023-03-20 22:48:26 +08:00
parent c90eb4825a
commit a860a720ba
3 changed files with 36 additions and 4 deletions

View file

@ -174,10 +174,10 @@ pub fn find_best_match_for_name(
fn find_best_match_for_name_impl(
use_substring_score: bool,
candidates: &[Symbol],
lookup: Symbol,
lookup_symbol: Symbol,
dist: Option<usize>,
) -> Option<Symbol> {
let lookup = lookup.as_str();
let lookup = lookup_symbol.as_str();
let lookup_uppercase = lookup.to_uppercase();
// Priority of matches:
@ -190,6 +190,7 @@ fn find_best_match_for_name_impl(
let mut dist = dist.unwrap_or_else(|| cmp::max(lookup.len(), 3) / 3);
let mut best = None;
let mut next_candidates = vec![];
for c in candidates {
match if use_substring_score {
edit_distance_with_substrings(lookup, c.as_str(), dist)
@ -198,12 +199,27 @@ fn find_best_match_for_name_impl(
} {
Some(0) => return Some(*c),
Some(d) => {
dist = d - 1;
best = Some(*c);
if use_substring_score {
dist = d;
next_candidates.push(*c);
best = Some(*c);
} else {
dist = d - 1;
best = Some(*c);
}
}
None => {}
}
}
if next_candidates.len() > 1 {
best = find_best_match_for_name_impl(
false,
&next_candidates,
lookup_symbol,
Some(lookup.len()),
);
}
if best.is_some() {
return best;
}