main_solution2/danube.scala
changeset 349 682611a0fb89
parent 347 4de31fdc0d67
child 384 6e1237691307
equal deleted inserted replaced
348:b5b6ed38c2f2 349:682611a0fb89
   158 
   158 
   159 // recommendations(ratings_map, movies_map, "1")
   159 // recommendations(ratings_map, movies_map, "1")
   160 //   => List(Shawshank Redemption, Forrest Gump (1994))
   160 //   => List(Shawshank Redemption, Forrest Gump (1994))
   161 
   161 
   162 // recommendations(ratings_map, movies_map, "4")
   162 // recommendations(ratings_map, movies_map, "4")
   163 //   => Nil  (there are three ratings fro this movie in ratings.csv but they are not positive)     
   163 //   => Nil  (there are three ratings for this movie in ratings.csv but they are not positive)     
       
   164 
       
   165 // (7) Calculate the recommendations for all movies according to
       
   166 // what the recommendations function in (6) produces (this
       
   167 // can take a few seconds). Put all recommendations into a list 
       
   168 // (of strings) and count how often the strings occur in
       
   169 // this list. This produces a list of string-int pairs,
       
   170 // where the first component is the movie name and the second
       
   171 // is the number of how many times they were recommended. 
       
   172 // Sort all the pairs according to the number
       
   173 // of times they were recommended (most recommended movie name 
       
   174 // first).
       
   175 
       
   176 def occurrences(xs: List[String]): List[(String, Int)] =
       
   177   for (x <- xs.distinct) yield (x, xs.count(_ == x))
       
   178 
       
   179 def most_recommended(recs: Map[String, List[String]],
       
   180                      movs: Map[String, String]) : List[(String, Int)] = {
       
   181    val all =  (for (name <- movs.toList.map(_._1)) yield {
       
   182      recommendations(recs, movs, name)                     
       
   183    }).flatten
       
   184    val occs = occurrences(all)
       
   185    occs.sortBy(_._2).reverse
       
   186 }
   164 
   187 
   165 
   188 
   166 // If you want to calculate the recomendations for all movies.
   189 //most_recommended(ratings_map, movies_map).take(3)
   167 // Will take a few seconds calculation time.
   190 // =>
       
   191 // List((Matrix,698), 
       
   192 //      (Star Wars: Episode IV - A New Hope (1977),402), 
       
   193 //      (Jerry Maguire (1996),382))
   168 
   194 
   169 //val all = for (name <- movie_names.map(_._1)) yield {
       
   170 //  recommendations(ratings_map, movies_map, name)
       
   171 //}
       
   172 
       
   173 // helper functions
       
   174 //List().take(2
       
   175 //List(1).take(2)
       
   176 //List(1,2).take(2)
       
   177 //List(1,2,3).take(2)
       
   178 
   195 
   179 }
   196 }