Jaro similarity: Difference between revisions

Content added Content deleted
(→‎{{header|Haskell}}: Reduced a case analysis to a `>>=` bind)
m (→‎{{header|Haskell}}: white space and layout)
Line 1,063: Line 1,063:
import Text.Printf (printf)
import Text.Printf (printf)
import Data.Maybe (mapMaybe)
import Data.Maybe (mapMaybe)

jaro :: String -> String -> Float
jaro :: String -> String -> Float
jaro x y =
jaro x y =
Line 1,072: Line 1,072:
0 -> 0
0 -> 0
_ -> (1 / 3) * ((m / s1) + (m / s2) + ((m - t) / m))
_ -> (1 / 3) * ((m / s1) + (m / s2) + ((m - t) / m))

matches :: String -> String -> [(Int, Char)]
matches :: String -> String -> [(Int, Char)]
matches s1 s2 =
matches s1 s2 =
Line 1,081: Line 1,081:
(\(c, n) ->
(\(c, n) ->
let offset = max 0 (n - (r + 1)) -- initial chars out of range ?
let offset = max 0 (n - (r + 1)) -- initial chars out of range ?
-- Any index for this char within range ?
-- Any offset for this char within range.
in elemIndex c (drop offset (take (n + r) ys)) >>= (\i -> Just (offset + i, c)))
in elemIndex c (drop offset (take (n + r) ys)) >>=
(\i -> Just (offset + i, c)))
(zip xs [1 ..])
(zip xs [1 ..])

transpositions :: [(Int, Char)] -> Int
transpositions :: [(Int, Char)] -> Int
transpositions = length . filter (uncurry (>)) . (zip <*> tail)
transpositions = length . filter (uncurry (>)) . (zip <*> tail)

-- TEST ----------------------------------------------------------------------
-- TEST ----------------------------------------------------------------------
main :: IO ()
main :: IO ()