2019-09-13 14:10:24 +03:00
|
|
|
|
module strings
|
2019-12-20 00:29:37 +03:00
|
|
|
|
// #-js
|
2019-09-13 14:10:24 +03:00
|
|
|
|
// use levenshtein distance algorithm to calculate
|
|
|
|
|
// the distance between between two strings (lower is closer)
|
2020-10-15 13:32:28 +03:00
|
|
|
|
pub fn levenshtein_distance(a string, b string) int {
|
2019-12-18 21:56:30 +03:00
|
|
|
|
mut f := [0].repeat(b.len + 1)
|
2020-02-04 06:21:40 +03:00
|
|
|
|
for j in 0..f.len {
|
|
|
|
|
f[j] = j
|
|
|
|
|
}
|
2019-09-13 14:10:24 +03:00
|
|
|
|
for ca in a {
|
|
|
|
|
mut j := 1
|
|
|
|
|
mut fj1 := f[0]
|
|
|
|
|
f[0]++
|
|
|
|
|
for cb in b {
|
2019-12-18 21:56:30 +03:00
|
|
|
|
mut mn := if f[j] + 1 <= f[j - 1] + 1 { f[j] + 1 } else { f[j - 1] + 1 }
|
2019-09-13 14:10:24 +03:00
|
|
|
|
if cb != ca {
|
2019-12-18 21:56:30 +03:00
|
|
|
|
mn = if mn <= fj1 + 1 { mn } else { fj1 + 1 }
|
|
|
|
|
}
|
|
|
|
|
else {
|
2019-09-13 14:10:24 +03:00
|
|
|
|
mn = if mn <= fj1 { mn } else { fj1 }
|
|
|
|
|
}
|
|
|
|
|
fj1 = f[j]
|
|
|
|
|
f[j] = mn
|
|
|
|
|
j++
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-12-18 21:56:30 +03:00
|
|
|
|
return f[f.len - 1]
|
2019-09-13 14:10:24 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// use levenshtein distance algorithm to calculate
|
|
|
|
|
// how similar two strings are as a percentage (higher is closer)
|
2020-10-15 13:32:28 +03:00
|
|
|
|
pub fn levenshtein_distance_percentage(a string, b string) f32 {
|
2019-09-13 14:10:24 +03:00
|
|
|
|
d := levenshtein_distance(a, b)
|
|
|
|
|
l := if a.len >= b.len { a.len } else { b.len }
|
2019-12-18 21:56:30 +03:00
|
|
|
|
return (1.00 - f32(d) / f32(l)) * 100.00
|
2019-09-13 14:10:24 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// implementation of Sørensen–Dice coefficient.
|
|
|
|
|
// find the similarity between two strings.
|
2019-09-21 14:59:06 +03:00
|
|
|
|
// returns coefficient between 0.0 (not similar) and 1.0 (exact match).
|
2020-10-15 13:32:28 +03:00
|
|
|
|
pub fn dice_coefficient(s1 string, s2 string) f32 {
|
2019-12-18 21:56:30 +03:00
|
|
|
|
if s1.len == 0 || s2.len == 0 {
|
|
|
|
|
return 0.0
|
|
|
|
|
}
|
|
|
|
|
if s1 == s2 {
|
|
|
|
|
return 1.0
|
|
|
|
|
}
|
|
|
|
|
if s1.len < 2 || s2.len < 2 {
|
|
|
|
|
return 0.0
|
|
|
|
|
}
|
2019-09-15 20:07:12 +03:00
|
|
|
|
a := if s1.len > s2.len { s1 } else { s2 }
|
|
|
|
|
b := if a == s1 { s2 } else { s1 }
|
2019-09-13 14:10:24 +03:00
|
|
|
|
mut first_bigrams := map[string]int
|
2020-02-24 19:55:16 +03:00
|
|
|
|
for i in 0..a.len - 1 {
|
2019-12-18 21:56:30 +03:00
|
|
|
|
bigram := a[i..i + 2]
|
|
|
|
|
q := if bigram in first_bigrams { first_bigrams[bigram] + 1 } else { 1 }
|
2019-10-07 01:31:01 +03:00
|
|
|
|
first_bigrams[bigram] = q
|
2019-09-13 14:10:24 +03:00
|
|
|
|
}
|
|
|
|
|
mut intersection_size := 0
|
2020-02-24 19:55:16 +03:00
|
|
|
|
for i in 0..b.len - 1 {
|
2019-12-18 21:56:30 +03:00
|
|
|
|
bigram := b[i..i + 2]
|
2019-09-19 05:22:24 +03:00
|
|
|
|
count := if bigram in first_bigrams { first_bigrams[bigram] } else { 0 }
|
2019-09-13 14:10:24 +03:00
|
|
|
|
if count > 0 {
|
2019-09-15 20:07:12 +03:00
|
|
|
|
first_bigrams[bigram] = count - 1
|
2019-09-13 14:10:24 +03:00
|
|
|
|
intersection_size++
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-05-27 06:42:48 +03:00
|
|
|
|
return (2.0 * f32(intersection_size)) / (f32(a.len) + f32(b.len) - 2)
|
2019-09-13 14:10:24 +03:00
|
|
|
|
}
|
2019-12-20 00:29:37 +03:00
|
|
|
|
|