-
-
Save rampion/2158429 to your computer and use it in GitHub Desktop.
an optimized haskell Levenshtein distance, using the vector library
This file contains hidden or 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
| import qualified Data.Vector.Unboxed.Safe as V | |
| med :: String -> String -> Int | |
| med s1 = V.last . V.ifoldl' scanS1 costs_i . V.fromList | |
| where vs1 = V.fromList s1 | |
| costs_i = V.enumFromN 1 $ V.length vs1 -- [0.. length s1] | |
| scanS1 costs_W costSW_i c2 = | |
| let v = V.zip vs1 costs_W | |
| v' = V.postscanl' scanVec (costSW_i, costSW_i + 1) v | |
| scanVec (costSW, costS) (c1, costW) = | |
| (costW, min (min costS costW + 1) | |
| (costSW + if c1 == c2 then 0 else 2)) | |
| in snd $ V.unzip v' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment