module Options.Applicative.Help.Levenshtein (
editDistance
) where
editDistance :: Eq a => [a] -> [a] -> Int
editDistance a b =
let
mainDiag =
oneDiag a b (head uppers) (-1 : head lowers)
uppers =
eachDiag a b (mainDiag : uppers)
lowers =
eachDiag b a (mainDiag : lowers)
oneDiag a' b' diagAbove diagBelow = thisdiag
where
doDiag [] _ _ _ _ = []
doDiag _ [] _ _ _ = []
doDiag (ach:ach':as) (bch:bch':bs) nw n w
| ach' == bch && ach == bch'
= nw : doDiag (ach' : as) (bch' : bs) nw (tail n) (tail w)
doDiag (ach:as) (bch:bs) nw n w =
let
me =
if ach == bch then
nw
else
1 + min3 (head w) nw (head n)
in
me : doDiag as bs me (tail n) (tail w)
firstelt = 1 + head diagBelow
thisdiag = firstelt : doDiag a' b' firstelt diagAbove (tail diagBelow)
eachDiag _ [] _ = []
eachDiag _ _ [] = []
eachDiag a' (_:bs) (lastDiag:diags) =
let
nextDiag = head (tail diags)
in
oneDiag a' bs nextDiag lastDiag : eachDiag a' bs diags
lab =
length a - length b
min3 x y z =
if x < y then
x
else
min y z
in
last $
if lab == 0 then
mainDiag
else if lab > 0 then
lowers !! (lab - 1)
else
uppers !! (-1 - lab)