mirror of
https://github.com/nushell/nushell.git
synced 2024-11-30 04:14:17 +01:00
did_you_mean: case-insensitive edit distance, tests (#4999)
This commit is contained in:
parent
911fba8a8a
commit
5586d4a0a0
@ -360,19 +360,10 @@ impl From<Box<dyn std::error::Error + Send + Sync>> for ShellError {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn did_you_mean(possibilities: &[String], tried: &str) -> Option<String> {
|
pub fn did_you_mean(possibilities: &[String], tried: &str) -> Option<String> {
|
||||||
// First see if we can find the same string just cased differently
|
|
||||||
let great_match_index = possibilities
|
|
||||||
.iter()
|
|
||||||
.position(|word| word.to_lowercase() == tried.to_lowercase());
|
|
||||||
|
|
||||||
if let Some(index) = great_match_index {
|
|
||||||
return Some(possibilities[index].to_owned());
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut possible_matches: Vec<_> = possibilities
|
let mut possible_matches: Vec<_> = possibilities
|
||||||
.iter()
|
.iter()
|
||||||
.map(|word| {
|
.map(|word| {
|
||||||
let edit_distance = levenshtein_distance(word, tried);
|
let edit_distance = levenshtein_distance(&word.to_lowercase(), &tried.to_lowercase());
|
||||||
(edit_distance, word.to_owned())
|
(edit_distance, word.to_owned())
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
@ -446,3 +437,32 @@ pub fn levenshtein_distance(a: &str, b: &str) -> usize {
|
|||||||
|
|
||||||
result
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::did_you_mean;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn did_you_mean_works_with_wrong_case() {
|
||||||
|
let possibilities = &["OS".into(), "PWD".into()];
|
||||||
|
let actual = did_you_mean(possibilities, "pwd");
|
||||||
|
let expected = Some(String::from("PWD"));
|
||||||
|
assert_eq!(actual, expected)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn did_you_mean_works_with_typo() {
|
||||||
|
let possibilities = &["OS".into(), "PWD".into()];
|
||||||
|
let actual = did_you_mean(possibilities, "PWF");
|
||||||
|
let expected = Some(String::from("PWD"));
|
||||||
|
assert_eq!(actual, expected)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn did_you_mean_works_with_wrong_case_and_typo() {
|
||||||
|
let possibilities = &["OS".into(), "PWD".into()];
|
||||||
|
let actual = did_you_mean(possibilities, "pwf");
|
||||||
|
let expected = Some(String::from("PWD"));
|
||||||
|
assert_eq!(actual, expected)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user