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