@@ -4,10 +4,9 @@ use std::cmp::Ordering;
4
4
// Internal
5
5
use crate :: builder:: Command ;
6
6
7
- /// Produces multiple strings from a given list of possible values which are similar
8
- /// to the passed in value `v` within a certain confidence by least confidence.
9
- /// Thus in a list of possible values like ["foo", "bar"], the value "fop" will yield
10
- /// `Some("foo")`, whereas "blark" would yield `None`.
7
+ /// Find strings from an iterable of `possible_values` similar to a given value `v`
8
+ /// Returns a Vec of all possible values that exceed a similarity threshold
9
+ /// sorted by ascending similarity, most similar comes last
11
10
#[ cfg( feature = "suggestions" ) ]
12
11
pub ( crate ) fn did_you_mean < T , I > ( v : & str , possible_values : I ) -> Vec < String >
13
12
where
16
15
{
17
16
let mut candidates: Vec < ( f64 , String ) > = possible_values
18
17
. into_iter ( )
19
- . map ( |pv| ( strsim:: jaro_winkler ( v, pv. as_ref ( ) ) , pv. as_ref ( ) . to_owned ( ) ) )
20
- . filter ( |( confidence, _) | * confidence > 0.8 )
18
+ // GH #4660: using `jaro` because `jaro_winkler` implementation in `strsim-rs` is wrong
19
+ // causing strings with common prefix >=10 to be considered perfectly similar
20
+ . map ( |pv| ( strsim:: jaro ( v, pv. as_ref ( ) ) , pv. as_ref ( ) . to_owned ( ) ) )
21
+ // Confidence of 0.7 so that bar -> baz is suggested
22
+ . filter ( |( confidence, _) | * confidence > 0.7 )
21
23
. collect ( ) ;
22
24
candidates. sort_by ( |a, b| a. 0 . partial_cmp ( & b. 0 ) . unwrap_or ( Ordering :: Equal ) ) ;
23
25
candidates. into_iter ( ) . map ( |( _, pv) | pv) . collect ( )
@@ -112,6 +114,15 @@ mod test {
112
114
) ;
113
115
}
114
116
117
+ #[ test]
118
+ fn best_fit_long_common_prefix_issue_4660 ( ) {
119
+ let p_vals = [ "alignmentScore" , "alignmentStart" ] ;
120
+ assert_eq ! (
121
+ did_you_mean( "alignmentScorr" , p_vals. iter( ) ) ,
122
+ vec![ "alignmentStart" , "alignmentScore" ]
123
+ ) ;
124
+ }
125
+
115
126
#[ test]
116
127
fn flag_missing_letter ( ) {
117
128
let p_vals = [ "test" , "possible" , "values" ] ;
0 commit comments