diff -r b5b6ed38c2f2 -r 682611a0fb89 main_solution2/danube.scala --- a/main_solution2/danube.scala Mon Nov 02 13:10:02 2020 +0000 +++ b/main_solution2/danube.scala Wed Nov 04 14:46:03 2020 +0000 @@ -160,20 +160,37 @@ // => List(Shawshank Redemption, Forrest Gump (1994)) // recommendations(ratings_map, movies_map, "4") -// => Nil (there are three ratings fro this movie in ratings.csv but they are not positive) +// => Nil (there are three ratings for this movie in ratings.csv but they are not positive) + +// (7) Calculate the recommendations for all movies according to +// what the recommendations function in (6) produces (this +// can take a few seconds). Put all recommendations into a list +// (of strings) and count how often the strings occur in +// this list. This produces a list of string-int pairs, +// where the first component is the movie name and the second +// is the number of how many times they were recommended. +// Sort all the pairs according to the number +// of times they were recommended (most recommended movie name +// first). + +def occurrences(xs: List[String]): List[(String, Int)] = + for (x <- xs.distinct) yield (x, xs.count(_ == x)) + +def most_recommended(recs: Map[String, List[String]], + movs: Map[String, String]) : List[(String, Int)] = { + val all = (for (name <- movs.toList.map(_._1)) yield { + recommendations(recs, movs, name) + }).flatten + val occs = occurrences(all) + occs.sortBy(_._2).reverse +} -// If you want to calculate the recomendations for all movies. -// Will take a few seconds calculation time. +//most_recommended(ratings_map, movies_map).take(3) +// => +// List((Matrix,698), +// (Star Wars: Episode IV - A New Hope (1977),402), +// (Jerry Maguire (1996),382)) -//val all = for (name <- movie_names.map(_._1)) yield { -// recommendations(ratings_map, movies_map, name) -//} - -// helper functions -//List().take(2 -//List(1).take(2) -//List(1,2).take(2) -//List(1,2,3).take(2) }