Skip to content

Instantly share code, notes, and snippets.

@paulogeyer
Created September 11, 2015 18:45
Show Gist options
  • Save paulogeyer/2edabd7841015b877554 to your computer and use it in GitHub Desktop.
Save paulogeyer/2edabd7841015b877554 to your computer and use it in GitHub Desktop.
import Data.List (intersect)
import qualified Data.Map as Map
prefs = Map.fromList
[("Lisa Rose", Map.fromList [("Lady in the Water", 2.5)
, ("Snakes on a Plane", 3.5)
, ("Just My Luck", 3.0)
, ("Superman Returns", 3.5)
, ("You, Me and Dupree", 2.5)
, ("The Night Listener", 3.0)])
, ("Gene Seymour", Map.fromList [("Lady in the Water", 3.0)
, ("Snakes on a Plane", 3.5)
, ("Just My Luck", 1.5)
, ("Superman Returns", 5.0)
, ("The Night Listener", 3.0)
, ("You, Me and Dupree", 3.5)])
]
sim_pearson :: String -> String -> Double
sim_pearson prefs p1 p2 =
if siLength > 0
then si
else []
where p1Movies = snd (prefs !! p1)
p2Movies = snd (prefs !! p2)
si = Map.keys p1Movies `intersect` Map.keys p2Movies
siLength = length si
sum movies = foldl (\acc movie -> case (Map.lookup movie movies) of
Just val -> acc + val
Nothing -> acc) 0 si
sum1 = sum p1Movies
sum2 = sum p2Movies
sumsq movies = foldl (\acc movie -> case (Map.lookup movie movies) of
Just val -> acc + (val**2)
Nothing -> acc) 0 si
sum1sq = sumsq p1Movies
sum2sq = sumsq p2Movies
pSum = foldl (\acc movie -> acc + ((Map.lookup movie p1Movies) * (Map.lookup movie p2Movies))) 0 si
num = pSum - ((sum1 * sum2) / siLength)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment