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