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