Guest User

Untitled

a guest
Feb 17th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. -- | Code for calculating the levenstein distance between two strings
  2.  
  3. module Data.Levenstein (distance) where
  4.  
  5. import Prelude
  6.  
  7. import Data.Array as Array
  8. import Data.Array ((!!))
  9. import Data.Function.Memoize (memoize2)
  10. import Data.Maybe as Maybe
  11. import Data.Newtype (unwrap)
  12. import Data.String (CodePoint)
  13. import Data.String as String
  14. import Data.Ord.Min (Min(..))
  15. import Partial.Unsafe (unsafePartial)
  16.  
  17. min3 ∷ ∀ a. Ord a ⇒ a → a → a → a
  18. min3 x y z =
  19. unwrap $ Min x <> Min y <> Min z
  20.  
  21. unsafeIndex ∷ ∀ a. Array a → Int → a
  22. unsafeIndex arr idx =
  23. unsafePartial $ Maybe.fromJust $ arr !! idx
  24.  
  25. infixl 8 unsafeIndex as !!!
  26.  
  27. -- | Calculates the distance from the code points
  28. distanceCodePoints ∷ Array CodePoint → Array CodePoint → Int
  29. distanceCodePoints x y =
  30. d (Array.length x) (Array.length y)
  31. where
  32. d = memoize2 $ \m n → dist m n
  33. dist i 0 = i
  34. dist 0 j = j
  35. dist i j =
  36. let
  37. a = (d (i - 1) j) + 1
  38. b = (d i (j - 1)) + 1
  39. c = (d (i - 1) (j - 1)) + if x !!! (i - 1) == y !!! (j - 1) then 0 else 1
  40. in
  41. min3 a b c
  42.  
  43. -- | Calculates the levenshtein distance between two strings - this is the number of changes required to get from one string to the other..
  44. distance ∷ String → String → Int
  45. distance x y =
  46. distanceCodePoints (String.toCodePointArray x) (String.toCodePointArray y)
Add Comment
Please, Sign In to add comment