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