Created
November 21, 2022 01:54
-
-
Save jdh30/b0e6986821bb8427d8927b16cabbd106 to your computer and use it in GitHub Desktop.
Levenshtein edit distance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let distance s1 s2 = | |
let indexable s = | |
let a = Array.ofString s in | |
Array.length a, Array.get a in | |
let (m, u), (n, v) = indexable s1, indexable s2 in | |
let d1 = Array.init n id in | |
let d0 = Array.init n [_ → 0] in | |
let () = for 1 1 (m-1) () [(), i → | |
let () = Array.Unsafe.set d0 0 i in | |
let ui = u i in | |
let () = for 1 1 (n-1) () [(), j → | |
let a = Array.get d1 j in | |
let b = Array.get d0 (j-1) in | |
let c = Array.get d1 (j-1) + if ui = v j then -1 else 0 in | |
1 + min (min a b) c | |
@ Array.Unsafe.set d0 j ] in | |
Array.blit d0 0 d1 0 n ] in | |
Array.get d0 (n-1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment