Skip to content

Instantly share code, notes, and snippets.

@jottinger
Created October 16, 2014 18:07
Show Gist options
  • Save jottinger/d7a19afb947c3c93e7b8 to your computer and use it in GitHub Desktop.
Save jottinger/d7a19afb947c3c93e7b8 to your computer and use it in GitHub Desktop.
package chapter2
import scala.math._
object Recommendations {
def sim_distance(preferences: Map[String, Map[String, Double]],
person1: String, person2: String)= {
val p1=preferences(person1)
val p2=preferences(person2)
val si: Set[String] = p1.keySet.intersect(p2.keySet)
if (si.isEmpty) {
0.0
}
var sum:Double=0
//sum1=si map ((k)=>pow(preferences(person1)(k) - preferences(person2)(k), 2)) reduceLeft( _ + _ )
si foreach ((k) => sum += pow(p1(k) - p2(k), 2))
1.0/(1.0+sum)
}
def main(args: Array[String]) = {
val critics: Map[String, Map[String, Double]] =
Map("Lisa Rose" -> Map(
"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(
"Lady in the Water" -> 3.0,
"Snakes on a Plane" -> 3.5,
"Just My Luck" -> 1.5,
"Superman Returns" -> 5.0,
"You, Me, and Dupree" -> 3.5,
"The Night Listener" -> 3.0
),
"Michael Phillips" -> Map(
"Lady in the Water" -> 2.5,
"Snakes on a Plane" -> 3.0,
"Superman Returns" -> 3.5,
"The Night Listener" -> 4.0
),
"Claudia Puig" -> Map(
"Snakes on a Plane" -> 3.5,
"Just My Luck" -> 3.0,
"Superman Returns" -> 4.0,
"You, Me, and Dupree" -> 2.5,
"The Night Listener" -> 4.5
),
"Mick Lasalle" -> Map(
"Lady in the Water" -> 3.0,
"Snakes on a Plane" -> 4.0,
"Just My Luck" -> 2.0,
"Superman Returns" -> 3.0,
"You, Me, and Dupree" -> 2.0,
"The Night Listener" -> 3.0
),
"Jack Matthews" -> Map(
"Lady in the Water" -> 3.0,
"Snakes on a Plane" -> 4.0,
"Superman Returns" -> 5.0,
"You, Me, and Dupree" -> 3.5,
"The Night Listener" -> 3.0
),
"Toby" -> Map(
"Snakes on a Plane" -> 4.5,
"Superman Returns" -> 4.0,
"You, Me, and Dupree" -> 1.0
)
)
critics foreach ((t) => println(t._1 + "-->" + t._2))
println(critics("Toby"))
println(sim_distance(critics, "Lisa Rose", "Gene Seymour"))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment