0 | module Text.Distance.Levenshtein
6 | import Data.List.Extra
11 | spec : String -> String -> Nat
12 | spec a b = loop (unpack a) (unpack b) where
14 | loop : List Char -> List Char -> Nat
15 | loop [] ys = length ys
16 | loop xs [] = length xs
17 | loop (x :: xs) (y :: ys)
18 | = if x == y then loop xs ys
19 | else 1 + minimum [ loop (x :: xs) ys
26 | compute : HasIO io => String -> String -> io Nat
34 | mat <- new (w+1) (h+1)
40 | for_ [0..w] $
\ i => write mat i 0 i
41 | for_ [0..h] $
\ j => write mat 0 j j
44 | let get = \i, j => case !(read {io} mat i j) of
45 | Nothing => assert_total $
46 | idris_crash "INTERNAL ERROR: compute -> Badly initialised matrix"
51 | for_ [1..h] $
\ j => do
52 | for_ [1..w] $
\ i => do
56 | let cost = let c = assert_total $
strIndex a (i-1)
57 | d = assert_total $
strIndex b (j-1)
58 | in if c == d then 0 else
59 | if isAlpha c && isAlpha d then 1 else
60 | if isDigit c && isDigit d then 1 else 2
62 | minimum [ 1 + !(get i (j-1))
63 | , 1 + !(get (i-1) j)
64 | , cost + !(get (i-1) (j-1))
68 | integerToNat . cast <$> get w h