0 | module Libraries.Text.Distance.Levenshtein
3 | import Libraries.Data.IOMatrix
8 | spec : String -> String -> Nat
9 | spec a b = loop (fastUnpack a) (fastUnpack b) where
11 | loop : List Char -> List Char -> Nat
12 | loop [] ys = length ys
13 | loop xs [] = length xs
14 | loop (x :: xs) (y :: ys)
15 | = if x == y then loop xs ys
24 | compute : HasIO io => String -> String -> io Nat
32 | mat <- new (w+1) (h+1)
38 | for_ [0..w] $
\ i => write mat i 0 i
39 | for_ [0..h] $
\ j => write mat 0 j j
42 | let get = \i, j => case !(read {io} mat i j) of
43 | Nothing => assert_total $
44 | idris_crash "INTERNAL ERROR: compute -> Badly initialised matrix"
49 | for_ [1..h] $
\ j => do
50 | for_ [1..w] $
\ i => do
54 | let cost = let c = assert_total $
strIndex a (i-1)
55 | d = assert_total $
strIndex b (j-1)
56 | in if c == d then 0 else
57 | if isAlpha c && isAlpha d then 1 else
58 | if isDigit c && isDigit d then 1 else 2
59 | write mat i j $
minimum
60 | [ 1 + !(get i (j-1))
61 | , 1 + !(get (i-1) j)
62 | , cost + !(get (i-1) (j-1))
66 | integerToNat . cast <$> get w h