marking2/danube.scala
changeset 486 9c03b5e89a2a
parent 485 19b75e899d37
child 487 efad9725dfd8
equal deleted inserted replaced
485:19b75e899d37 486:9c03b5e89a2a
     1 // Part 2 and 3 about Movie Recommendations 
       
     2 // at Danube.co.uk
       
     3 //===========================================
       
     4 
       
     5 import io.Source
       
     6 import scala.util._
       
     7 
       
     8 object CW7b { // for purposes of generating a jar
       
     9 
       
    10 // (1) Implement the function get_csv_url which takes an url-string
       
    11 //     as argument and requests the corresponding file. The two urls
       
    12 //     of interest are ratings_url and movies_url, which correspond 
       
    13 //     to CSV-files.
       
    14 //     The function should ReTurn the CSV file appropriately broken
       
    15 //     up into lines, and the first line should be dropped (that is without
       
    16 //     the header of the CSV file). The result is a list of strings (lines
       
    17 //     in the file).
       
    18 
       
    19 def get_csv_url(url: String) : List[String] = {
       
    20   val csv = Source.fromURL(url)("ISO-8859-1")
       
    21   csv.mkString.split("\n").toList.drop(1)
       
    22 }
       
    23 
       
    24 val ratings_url = """https://nms.kcl.ac.uk/christian.urban/ratings.csv"""
       
    25 val movies_url = """https://nms.kcl.ac.uk/christian.urban/movies.csv"""
       
    26 
       
    27 // test cases
       
    28 
       
    29 
       
    30 //val ratings = get_csv_url(ratings_url)
       
    31 //val movies = get_csv_url(movies_url)
       
    32 
       
    33 //ratings.length  // 87313
       
    34 //movies.length   // 9742
       
    35 
       
    36 // (2) Implement two functions that process the CSV files. The ratings
       
    37 //     function filters out all ratings below 4 and ReTurns a list of 
       
    38 //     (userID, movieID) pairs. The movies function just ReTurns a list 
       
    39 //     of (movieId, title) pairs.
       
    40 
       
    41 
       
    42 def process_ratings(lines: List[String]) : List[(String, String)] = {
       
    43   for (cols <- lines.map(_.split(",").toList); 
       
    44        if (cols(2).toFloat >= 4)) yield (cols(0), cols(1))  
       
    45 }
       
    46 
       
    47 def process_movies(lines: List[String]) : List[(String, String)] = {
       
    48   for (cols <- lines.map(_.split(",").toList)) yield (cols(0), cols(1))  
       
    49 }
       
    50 
       
    51 // test cases
       
    52 
       
    53 //val good_ratings = process_ratings(ratings)
       
    54 //val movie_names = process_movies(movies)
       
    55 
       
    56 //good_ratings.length   //48580
       
    57 //movie_names.length    // 9742
       
    58 
       
    59 //==============================================
       
    60 // Do not change anything below, unless you want 
       
    61 // to submit the file for the advanced part 3!
       
    62 //==============================================
       
    63 
       
    64 
       
    65 // (3) Implement a grouping function that calulates a map
       
    66 //     containing the userIds and all the corresponding recommendations 
       
    67 //     (list of movieIds). This  should be implemented in a tail
       
    68 //     recursive fashion, using a map m as accumulator. This map
       
    69 //     is set to Map() at the beginning of the claculation.
       
    70 
       
    71 def groupById(ratings: List[(String, String)], 
       
    72               m: Map[String, List[String]]) : Map[String, List[String]] = ratings match {
       
    73   case Nil => m
       
    74   case (id, mov) :: rest => {
       
    75     val old_ratings = m.getOrElse (id, Nil)
       
    76     val new_ratings = m + (id -> (mov :: old_ratings))
       
    77     groupById(rest, new_ratings)
       
    78   }
       
    79 }
       
    80 
       
    81 // test cases
       
    82 //val ratings_map = groupById(good_ratings, Map())
       
    83 //val movies_map = movie_names.toMap
       
    84 
       
    85 //ratings_map.get("414").get.map(movies_map.get(_)) // most prolific recommender with 1227 positive ratings
       
    86 //ratings_map.get("474").get.map(movies_map.get(_)) // second-most prolific recommender with 787 positive ratings
       
    87 //ratings_map.get("214").get.map(movies_map.get(_)) // least prolific recommender with only 1 positive rating
       
    88 
       
    89 
       
    90 
       
    91 //(4) Implement a function that takes a ratings map and a movie_name as argument.
       
    92 // The function calculates all suggestions containing
       
    93 // the movie mov in its recommendations. It ReTurns a list of all these
       
    94 // recommendations (each of them is a list and needs to have mov deleted, 
       
    95 // otherwise it might happen we recommend the same movie).
       
    96 
       
    97 def favourites(m: Map[String, List[String]], mov: String) : List[List[String]] = 
       
    98   (for (id <- m.keys.toList;
       
    99         if m(id).contains(mov)) yield m(id).filter(_ != mov))
       
   100 
       
   101 
       
   102 
       
   103 // test cases
       
   104 // movie ID "912" -> Casablanca (1942)
       
   105 //          "858" -> Godfather
       
   106 //          "260" -> Star Wars: Episode IV - A New Hope (1977)
       
   107 
       
   108 //favourites(ratings_map, "912").length  // => 80
       
   109 
       
   110 // That means there are 80 users that recommend the movie with ID 912.
       
   111 // Of these 80  users, 55 gave a good rating to movie 858 and
       
   112 // 52 a good rating to movies 260, 318, 593.
       
   113 
       
   114 
       
   115 // (5) Implement a suggestions function which takes a rating
       
   116 // map and a movie_name as arguments. It calculates all the recommended
       
   117 // movies sorted according to the most frequently suggested movie(s) first.
       
   118 def suggestions(recs: Map[String, List[String]], 
       
   119                     mov_name: String) : List[String] = {
       
   120   val favs = favourites(recs, mov_name).flatten
       
   121   val favs_counted = favs.groupBy(identity).view.mapValues(_.size).toList
       
   122   val favs_sorted = favs_counted.sortBy(_._2).reverse
       
   123   favs_sorted.map(_._1)
       
   124 }
       
   125 
       
   126 // test cases
       
   127 
       
   128 //suggestions(ratings_map, "912")
       
   129 //suggestions(ratings_map, "912").length  
       
   130 // => 4110 suggestions with List(858, 260, 318, 593, ...)
       
   131 //    being the most frequently suggested movies
       
   132 
       
   133 // (6) Implement recommendations functions which generates at most
       
   134 // *two* of the most frequently suggested movies. It Returns the 
       
   135 // actual movie names, not the movieIDs.
       
   136 
       
   137 def recommendations(recs: Map[String, List[String]],
       
   138                    movs: Map[String, String],
       
   139                    mov_name: String) : List[String] =
       
   140   suggestions(recs, mov_name).take(2).map(movs.get(_).get)                 
       
   141 
       
   142 
       
   143 // testcases
       
   144 
       
   145 // recommendations(ratings_map, movies_map, "912")
       
   146 //   => List(Godfather, Star Wars: Episode IV - A NewHope (1977))
       
   147 
       
   148 //recommendations(ratings_map, movies_map, "260")
       
   149 //   => List(Star Wars: Episode V - The Empire Strikes Back (1980), 
       
   150 //           Star Wars: Episode VI - Return of the Jedi (1983))
       
   151 
       
   152 // recommendations(ratings_map, movies_map, "2")
       
   153 //   => List(Lion King, Jurassic Park (1993))
       
   154 
       
   155 // recommendations(ratings_map, movies_map, "0")
       
   156 //   => Nil
       
   157 
       
   158 // recommendations(ratings_map, movies_map, "1")
       
   159 //   => List(Shawshank Redemption, Forrest Gump (1994))
       
   160 
       
   161 // recommendations(ratings_map, movies_map, "4")
       
   162 //   => Nil  (there are three ratings fro this movie in ratings.csv but they are not positive)     
       
   163 
       
   164 
       
   165 // If you want to calculate the recomendations for all movies.
       
   166 // Will take a few seconds calculation time.
       
   167 
       
   168 //val all = for (name <- movie_names.map(_._1)) yield {
       
   169 //  recommendations(ratings_map, movies_map, name)
       
   170 //}
       
   171 
       
   172 // helper functions
       
   173 //List().take(2)
       
   174 //List(1).take(2)
       
   175 //List(1,2).take(2)
       
   176 //List(1,2,3).take(2)
       
   177 
       
   178 }
       
   179 
       
   180