Skip to content

Instantly share code, notes, and snippets.

View mattpodwysocki's full-sized avatar

Matthew Podwysocki mattpodwysocki

View GitHub Profile
'Superman Returns': 3.5, 'The Night Listener': 4.0},
'Claudia Puig': {'Snakes on a Plane': 3.5, 'Just My Luck': 3.0,
'The Night Listener': 4.5, 'Superman Returns': 4.0,
'You, Me and Dupree': 2.5},
'Mick LaSalle': {'Lady in the Water': 3.0, 'Snakes on a Plane': 4.0,
'Just My Luck': 2.0, 'Superman Returns': 3.0, 'The Night Listener': 3.0,
'You, Me and Dupree': 2.0},
'Jack Matthews': {'Lady in the Water': 3.0, 'Snakes on a Plane': 4.0,
'The Night Listener': 3.0, 'Superman Returns': 5.0, 'You, Me and Dupree': 3.5},
'Toby': {'Snakes on a Plane':4.5,'You, Me and Dupree':1.0,'Superman Returns':4.0}}
#light
let critics =
Map.of_list [("Lisa Rose",
Map.of_list [("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.of_list [("Lady in the Water", 3.0);("Snakes on a Plane", 3.5);
("Just My Luck", 1.5);("Superman Returns", 5.0);
#light
let critics =
Map.of_list [("Lisa Rose",
Map.of_list [("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.of_list [("Lady in the Water", 3.0);("Snakes on a Plane", 3.5);
("Just My Luck", 1.5);("Superman Returns", 5.0);
# Gets recommendations for a person by using a weighted average
# of every other user's rankings
def getRecommendations(prefs,person,similarity=sim_pearson):
totals={}
simSums={}
for other in prefs:
# don't compare me to myself
if other==person: continue
sim=similarity(prefs,person,other)
#light
module Map =
let insertWith (f:'a -> 'a -> 'a) (k:'tkey) (v:'a) (m:Map<'tkey, 'a>) =
match Map.tryfind k m with
| None -> Map.add k v m
| Some x -> let res = f v x
Map.add k res m
// Gets recommendations for a person by using a weighted average
# prefs = Map String (Map String Double)
def transformPrefs(prefs):
result={}
for person in prefs:
for item in prefs[person]:
result.setdefault(item,{})
# Flip item and person
result[item][person]=prefs[person][item]
// F#
let transformPrefs
(prefs:Map<string, Map<string, float>>) =
let add k1 k2 v m =
let curInner =
match Map.tryfind k1 m with
| None -> Map.empty
| Some x -> x
m |> Map.add k1 (Map.add k2 v curInner)
#light
module Option =
let some (n:'b) (f:'a -> 'b) (opt:'a option) : 'b =
match n, f, opt with
| n, _, None -> n
| _, f, Some x -> f x
module Map =
/// Inserts into the Map with the given function if already exists, else adds
#light
module Option =
let some (n:'b) (f:'a -> 'b) (opt:'a option) : 'b =
match n, f, opt with
| n, _, None -> n
| _, f, Some x -> f x
module Map =
let insertWith (f:'a -> 'a -> 'a) (k:'tkey) (v:'a) (m:Map<'tkey, 'a>) =
#light
module SeqMonad =
let bindS l f = Seq.concat (Seq.map f l)
let returnS l = seq { yield l }
type SeqBuilder() =
member x.Delay(f) = f()
member x.Bind(l, f) = bindS l f
member x.Return(l) = returnS l