| 
396
 | 
     1  | 
// Main Part 2 about Movie Recommendations 
  | 
| 
203
 | 
     2  | 
// at Danube.co.uk
  | 
| 
 | 
     3  | 
//===========================================
  | 
| 
 | 
     4  | 
  | 
| 
396
 | 
     5  | 
object M2 {
 | 
| 
284
 | 
     6  | 
  | 
| 
203
 | 
     7  | 
import io.Source
  | 
| 
 | 
     8  | 
import scala.util._
  | 
| 
 | 
     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  | 
//
  | 
| 
 | 
    15  | 
//     The function should ReTurn the CSV-file appropriately broken
  | 
| 
 | 
    16  | 
//     up into lines, and the first line should be dropped (that is without
  | 
| 
 | 
    17  | 
//     the header of the CSV-file). The result is a list of strings (lines
  | 
| 
 | 
    18  | 
//     in the file).
  | 
| 
 | 
    19  | 
  | 
| 
347
 | 
    20  | 
def get_csv_url(url: String) : List[String] = ???
  | 
| 
203
 | 
    21  | 
  | 
| 
 | 
    22  | 
  | 
| 
 | 
    23  | 
val ratings_url = """https://nms.kcl.ac.uk/christian.urban/ratings.csv"""
  | 
| 
 | 
    24  | 
val movies_url = """https://nms.kcl.ac.uk/christian.urban/movies.csv"""
  | 
| 
 | 
    25  | 
  | 
| 
 | 
    26  | 
// testcases
  | 
| 
 | 
    27  | 
//-----------
  | 
| 
333
 | 
    28  | 
//:
  | 
| 
203
 | 
    29  | 
//val movies = get_csv_url(movies_url)
  | 
| 
 | 
    30  | 
  | 
| 
 | 
    31  | 
//ratings.length  // 87313
  | 
| 
 | 
    32  | 
//movies.length   // 9742
  | 
| 
 | 
    33  | 
  | 
| 
 | 
    34  | 
  | 
| 
 | 
    35  | 
  | 
| 
 | 
    36  | 
// (2) Implement two functions that process the CSV-files from (1). 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 
  | 
| 
333
 | 
    39  | 
//     of (movieID, title) pairs. Note the input to these functions, that is
  | 
| 
 | 
    40  | 
//     the argument lines, will be the output of the function get_csv_url.
  | 
| 
203
 | 
    41  | 
  | 
| 
 | 
    42  | 
  | 
| 
347
 | 
    43  | 
def process_ratings(lines: List[String]) : List[(String, String)] = ???
  | 
| 
203
 | 
    44  | 
  | 
| 
347
 | 
    45  | 
def process_movies(lines: List[String]) : List[(String, String)] = ???
  | 
| 
203
 | 
    46  | 
  | 
| 
 | 
    47  | 
  | 
| 
 | 
    48  | 
// testcases
  | 
| 
 | 
    49  | 
//-----------
  | 
| 
 | 
    50  | 
//val good_ratings = process_ratings(ratings)
  | 
| 
 | 
    51  | 
//val movie_names = process_movies(movies)
  | 
| 
 | 
    52  | 
  | 
| 
 | 
    53  | 
//good_ratings.length   //48580
  | 
| 
 | 
    54  | 
//movie_names.length    // 9742
  | 
| 
 | 
    55  | 
  | 
| 
 | 
    56  | 
  | 
| 
 | 
    57  | 
  | 
| 
 | 
    58  | 
  | 
| 
 | 
    59  | 
// (3) Implement a grouping function that calculates a Map
  | 
| 
 | 
    60  | 
//     containing the userIDs and all the corresponding recommendations 
  | 
| 
 | 
    61  | 
//     (list of movieIDs). This  should be implemented in a tail
  | 
| 
 | 
    62  | 
//     recursive fashion, using a Map m as accumulator. This Map m
  | 
| 
 | 
    63  | 
//     is set to Map() at the beginning of the calculation.
  | 
| 
 | 
    64  | 
  | 
| 
347
 | 
    65  | 
def groupById(ratings: List[(String, String)], 
  | 
| 
 | 
    66  | 
              m: Map[String, List[String]]) : Map[String, List[String]] = ???
  | 
| 
203
 | 
    67  | 
  | 
| 
 | 
    68  | 
  | 
| 
 | 
    69  | 
// testcases
  | 
| 
 | 
    70  | 
//-----------
  | 
| 
 | 
    71  | 
//val ratings_map = groupById(good_ratings, Map())
  | 
| 
 | 
    72  | 
//val movies_map = movie_names.toMap
  | 
| 
 | 
    73  | 
  | 
| 
 | 
    74  | 
//ratings_map.get("414").get.map(movies_map.get(_)) 
 | 
| 
 | 
    75  | 
//    => most prolific recommender with 1227 positive ratings
  | 
| 
 | 
    76  | 
  | 
| 
 | 
    77  | 
//ratings_map.get("474").get.map(movies_map.get(_)) 
 | 
| 
 | 
    78  | 
//    => second-most prolific recommender with 787 positive ratings
  | 
| 
 | 
    79  | 
  | 
| 
 | 
    80  | 
//ratings_map.get("214").get.map(movies_map.get(_)) 
 | 
| 
 | 
    81  | 
//    => least prolific recommender with only 1 positive rating
  | 
| 
 | 
    82  | 
  | 
| 
 | 
    83  | 
  | 
| 
 | 
    84  | 
  | 
| 
 | 
    85  | 
// (4) Implement a function that takes a ratings map and a movie_name as argument.
  | 
| 
 | 
    86  | 
//     The function calculates all suggestions containing
  | 
| 
 | 
    87  | 
//     the movie in its recommendations. It ReTurns a list of all these
  | 
| 
 | 
    88  | 
//     recommendations (each of them is a list and needs to have the movie deleted, 
  | 
| 
 | 
    89  | 
//     otherwise it might happen we recommend the same movie).
  | 
| 
 | 
    90  | 
  | 
| 
 | 
    91  | 
  | 
| 
347
 | 
    92  | 
def favourites(m: Map[String, List[String]], mov: String) : List[List[String]] = ???
  | 
| 
203
 | 
    93  | 
  | 
| 
 | 
    94  | 
  | 
| 
 | 
    95  | 
// testcases
  | 
| 
 | 
    96  | 
//-----------
  | 
| 
 | 
    97  | 
// movie ID "912" -> Casablanca (1942)
  | 
| 
 | 
    98  | 
//          "858" -> Godfather
  | 
| 
 | 
    99  | 
//          "260" -> Star Wars: Episode IV - A New Hope (1977)
  | 
| 
 | 
   100  | 
  | 
| 
 | 
   101  | 
//favourites(ratings_map, "912").length  // => 80
  | 
| 
 | 
   102  | 
  | 
| 
 | 
   103  | 
// That means there are 80 users that recommend the movie with ID 912.
  | 
| 
 | 
   104  | 
// Of these 80  users, 55 gave a good rating to movie 858 and
  | 
| 
 | 
   105  | 
// 52 a good rating to movies 260, 318, 593.
  | 
| 
 | 
   106  | 
  | 
| 
 | 
   107  | 
  | 
| 
 | 
   108  | 
  | 
| 
 | 
   109  | 
// (5) Implement a suggestions function which takes a rating
  | 
| 
 | 
   110  | 
//     map and a movie_name as arguments. It calculates all the recommended
  | 
| 
 | 
   111  | 
//     movies sorted according to the most frequently suggested movie(s) first.
  | 
| 
 | 
   112  | 
  | 
| 
347
 | 
   113  | 
def suggestions(recs: Map[String, List[String]], 
  | 
| 
 | 
   114  | 
                mov_name: String) : List[String] = ???
  | 
| 
203
 | 
   115  | 
  | 
| 
 | 
   116  | 
  | 
| 
 | 
   117  | 
// testcases
  | 
| 
 | 
   118  | 
//-----------
  | 
| 
 | 
   119  | 
  | 
| 
 | 
   120  | 
//suggestions(ratings_map, "912")
  | 
| 
 | 
   121  | 
//suggestions(ratings_map, "912").length  
  | 
| 
 | 
   122  | 
// => 4110 suggestions with List(858, 260, 318, 593, ...)
  | 
| 
 | 
   123  | 
//    being the most frequently suggested movies
  | 
| 
 | 
   124  | 
  | 
| 
 | 
   125  | 
  | 
| 
 | 
   126  | 
  | 
| 
 | 
   127  | 
// (6) Implement a recommendations function which generates at most
  | 
| 
 | 
   128  | 
//     *two* of the most frequently suggested movies. It ReTurns the 
  | 
| 
 | 
   129  | 
//     actual movie names, not the movieIDs.
  | 
| 
 | 
   130  | 
  | 
| 
 | 
   131  | 
  | 
| 
347
 | 
   132  | 
def recommendations(recs: Map[String, List[String]],
  | 
| 
 | 
   133  | 
                    movs: Map[String, String],
  | 
| 
 | 
   134  | 
                    mov_name: String) : List[String] = ???
  | 
| 
203
 | 
   135  | 
  | 
| 
 | 
   136  | 
  | 
| 
 | 
   137  | 
  | 
| 
 | 
   138  | 
// testcases
  | 
| 
 | 
   139  | 
//-----------
  | 
| 
 | 
   140  | 
// recommendations(ratings_map, movies_map, "912")
  | 
| 
 | 
   141  | 
//   => List(Godfather, Star Wars: Episode IV - A NewHope (1977))
  | 
| 
 | 
   142  | 
  | 
| 
 | 
   143  | 
//recommendations(ratings_map, movies_map, "260")
  | 
| 
 | 
   144  | 
//   => List(Star Wars: Episode V - The Empire Strikes Back (1980), 
  | 
| 
 | 
   145  | 
//           Star Wars: Episode VI - Return of the Jedi (1983))
  | 
| 
 | 
   146  | 
  | 
| 
 | 
   147  | 
// recommendations(ratings_map, movies_map, "2")
  | 
| 
 | 
   148  | 
//   => List(Lion King, Jurassic Park (1993))
  | 
| 
 | 
   149  | 
  | 
| 
 | 
   150  | 
// recommendations(ratings_map, movies_map, "0")
  | 
| 
 | 
   151  | 
//   => Nil
  | 
| 
 | 
   152  | 
  | 
| 
 | 
   153  | 
// recommendations(ratings_map, movies_map, "1")
  | 
| 
 | 
   154  | 
//   => List(Shawshank Redemption, Forrest Gump (1994))
  | 
| 
 | 
   155  | 
  | 
| 
 | 
   156  | 
// recommendations(ratings_map, movies_map, "4")
  | 
| 
 | 
   157  | 
//   => Nil  (there are three ratings for this movie in ratings.csv but they are not positive)     
  | 
| 
 | 
   158  | 
  | 
| 
 | 
   159  | 
  | 
| 
349
 | 
   160  | 
  | 
| 
284
 | 
   161  | 
}
  |