mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
fix bug in levenshtein distance & add tests
This commit is contained in:
parent
b6a62cf20e
commit
75eebb516f
@ -4,6 +4,9 @@ module strings
|
|||||||
// the distance between between two strings (lower is closer)
|
// the distance between between two strings (lower is closer)
|
||||||
pub fn levenshtein_distance(a, b string) int {
|
pub fn levenshtein_distance(a, b string) int {
|
||||||
mut f := [0].repeat(b.len + 1)
|
mut f := [0].repeat(b.len + 1)
|
||||||
|
for j in 0..f.len {
|
||||||
|
f[j] = j
|
||||||
|
}
|
||||||
for ca in a {
|
for ca in a {
|
||||||
mut j := 1
|
mut j := 1
|
||||||
mut fj1 := f[0]
|
mut fj1 := f[0]
|
||||||
|
13
vlib/strings/similarity_test.v
Normal file
13
vlib/strings/similarity_test.v
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
import strings
|
||||||
|
|
||||||
|
fn test_levenshtein_distance() {
|
||||||
|
assert strings.levenshtein_distance('', '') == 0
|
||||||
|
assert strings.levenshtein_distance('one', 'one') == 0
|
||||||
|
assert strings.levenshtein_distance('', 'two') == 3
|
||||||
|
assert strings.levenshtein_distance('three', '') == 5
|
||||||
|
assert strings.levenshtein_distance('bananna', '') == 7
|
||||||
|
assert strings.levenshtein_distance('cats', 'hats') == 1
|
||||||
|
assert strings.levenshtein_distance('hugs', 'shrugs') == 2
|
||||||
|
assert strings.levenshtein_distance('broom', 'shroom') == 2
|
||||||
|
assert strings.levenshtein_distance('flomax', 'volmax') == 3
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user