Skip to content

Commit

Permalink
Merge pull request #2 from Freyskeyd/feat-distance-JaroWinkler
Browse files Browse the repository at this point in the history
feat(Distance/JaroWinkler): implement Jaro Winkler distance algorithm
  • Loading branch information
Simon Paitrault committed Nov 5, 2015
2 parents 497c8a9 + 3abba79 commit 63b207b
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/distance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,30 @@ pub fn jaro(a: &str, b: &str) -> f64 {
((matches - transpositions) / matches))
}
}

/// Like Jaro but gives a boost to strings that have a common prefix.
///
/// Examples:
/// ```
/// assert!((0.911 - jaro_winkler("cheeseburger", "cheese fries")).abs() < 0.001);
/// ```
pub fn jaro_winkler(a: &str, b: &str) -> f64 {
let jaro_distance = jaro(a, b);

let prefrix_length = a.chars()
.zip(b.chars())
.take_while(|&(a_char, b_char)| a_char == b_char)
.count();

let jaro_winkler_distance = jaro_distance + (0.1 * prefrix_length as f64 * (1.0 - jaro_distance));

if jaro_winkler_distance <= 1.0 {
jaro_winkler_distance
} else {
1.0
}
}

/// Calculates the minimum number of insertions, deletions and substitutions
/// required to change on string into the other.
///
Expand Down

0 comments on commit 63b207b

Please sign in to comment.