Created
July 19, 2011 09:48
-
-
Save micrypt/1091860 to your computer and use it in GitHub Desktop.
Scala translation (Collective Intelligence)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| val critics = 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, | |
| "The Night Listener" -> 3.0, | |
| "You,Me and Dupree" -> 3.5), | |
| "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, | |
| "The Night Listener" -> 4.5, | |
| "You,Me and Dupree" -> 2.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, | |
| "The Night Listener" -> 3.0, | |
| "You,Me and Dupree" -> 2.0), | |
| "Jack Matthews" -> Map( | |
| "Lady in the Water" -> 3.0, | |
| "Snakes on a Plane" -> 4.0, | |
| "Superman Returns" -> 3.0, | |
| "The Night Listener" -> 3.0, | |
| "You,Me and Dupree" -> 2.0), | |
| "Toby" -> Map( | |
| "Snakes on a Plane" -> 4.5, | |
| "You,Me and Dupree" -> 1.0, | |
| "Superman Returns" -> 4.0) ) | |
| import scala.math.{sqrt,pow} | |
| def simDistance(prefs: Map[String,Map[String,Double]],person1: String,person2: String): Double = { | |
| // Get the set of shared items | |
| val si = (prefs(person1).keys.toSet intersect prefs(person2).keys.toSet).toList | |
| // Add up the squares of all the differences | |
| val sumOfSquares = si.map(item => pow(prefs(person1)(item)-prefs(person2)(item),2)).sum | |
| // If they have no ratings in common,return 0 | |
| 1/(1+sqrt(sumOfSquares)) | |
| } | |
| def simPearson(prefs: Map[String,Map[String,Double]],person1: String,person2: String): Double = { | |
| // Get the list of shared items | |
| val si = (prefs(person1).keys.toSet intersect prefs(person2).keys.toSet).toList | |
| val n = si.length | |
| // Add up all the preferences | |
| val sum1 = si.map(item => prefs(person1)(item)).sum | |
| val sum2 = si.map(item => prefs(person2)(item)).sum | |
| // Add up the squares | |
| val sum1Sq = si.map(item => pow(prefs(person1)(item),2)).sum | |
| val sum2Sq = si.map(item => pow(prefs(person2)(item),2)).sum | |
| // Sum up the products | |
| val pSum = si.map(item => prefs(person1)(item)*prefs(person2)(item)).sum | |
| // Calculate the Pearson score | |
| val num = pSum - (sum1*sum2/n) | |
| val den = sqrt((sum1Sq-pow(sum1,2)/n) * (sum2Sq-pow(sum2,2)/n)) | |
| if (den != 0) num/den else 0 | |
| } | |
| def topMatches(prefs: Map[String,Map[String,Double]],person: String,n:Int=5,similarity: (Map[String, Map[String,Double]], String, String) => Double =simPearson) = { | |
| val scores = prefs.filter((other) => other._1 != person).map { (other) => | |
| (similarity(prefs,person,other._1),other._1) | |
| }.toList | |
| scores.sortBy(_._1).reverse slice(0,n) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment