templates2/danube.scala
author Christian Urban <urbanc@in.tum.de>
Thu, 06 Dec 2018 13:52:50 +0000
changeset 230 bebe34c975a8
parent 203 eb188f9ac038
child 284 9a04eb6a2291
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
203
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
// Part 2 and 3 about Movie Recommendations 
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
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
import io.Source
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
import scala.util._
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
// (1) Implement the function get_csv_url which takes an url-string
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
//     as argument and requests the corresponding file. The two urls
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
//     of interest are ratings_url and movies_url, which correspond 
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
//     to CSV-files.
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
//
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
//     The function should ReTurn the CSV-file appropriately broken
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
//     up into lines, and the first line should be dropped (that is without
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
//     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
    16
//     in the file).
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
//def get_csv_url(url: String) : List[String] = ...
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
val ratings_url = """https://nms.kcl.ac.uk/christian.urban/ratings.csv"""
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
val movies_url = """https://nms.kcl.ac.uk/christian.urban/movies.csv"""
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
// testcases
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
//-----------
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
//val ratings = get_csv_url(ratings_url)
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
//val movies = get_csv_url(movies_url)
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
//ratings.length  // 87313
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
//movies.length   // 9742
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
// (2) Implement two functions that process the CSV-files from (1). The ratings
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
//     function filters out all ratings below 4 and ReTurns a list of 
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
//     (userID, movieID) pairs. The movies function just ReTurns a list 
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
//     of (movieID, title) pairs.
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
//def process_ratings(lines: List[String]) : List[(String, String)] = ...
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
//def process_movies(lines: List[String]) : List[(String, String)] = ...
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
// testcases
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
//-----------
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
//val good_ratings = process_ratings(ratings)
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
//val movie_names = process_movies(movies)
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
//good_ratings.length   //48580
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
//movie_names.length    // 9742
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    52
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    53
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    55
//==============================================
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    56
// Do not change anything below, unless you want 
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    57
// to submit the file for the advanced part 3!
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    58
//==============================================
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    59
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    60
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    61
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    62
// (3) Implement a grouping function that calculates a Map
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    63
//     containing the userIDs and all the corresponding recommendations 
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
//     (list of movieIDs). This  should be implemented in a tail
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
//     recursive fashion, using a Map m as accumulator. This Map m
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    66
//     is set to Map() at the beginning of the calculation.
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    67
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    68
//def groupById(ratings: List[(String, String)], 
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    69
//              m: Map[String, List[String]]) : Map[String, List[String]] = ...
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    70
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    71
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    72
// testcases
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    73
//-----------
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    74
//val ratings_map = groupById(good_ratings, Map())
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    75
//val movies_map = movie_names.toMap
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("414").get.map(movies_map.get(_)) 
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    78
//    => most prolific recommender with 1227 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("474").get.map(movies_map.get(_)) 
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    81
//    => second-most prolific recommender with 787 positive ratings
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    82
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    83
//ratings_map.get("214").get.map(movies_map.get(_)) 
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    84
//    => least prolific recommender with only 1 positive rating
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    85
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    86
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    87
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    88
// (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
    89
//     The function calculates all suggestions containing
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    90
//     the movie in its recommendations. It ReTurns a list of all these
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    91
//     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
    92
//     otherwise it might happen we recommend the same movie).
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
//def favourites(m: Map[String, List[String]], mov: String) : List[List[String]] = ...
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    96
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    97
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    98
// testcases
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    99
//-----------
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   100
// movie ID "912" -> Casablanca (1942)
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   101
//          "858" -> Godfather
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   102
//          "260" -> Star Wars: Episode IV - A New Hope (1977)
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   103
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   104
//favourites(ratings_map, "912").length  // => 80
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   105
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   106
// That means there are 80 users that recommend the movie with ID 912.
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   107
// Of these 80  users, 55 gave a good rating to movie 858 and
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   108
// 52 a good rating to movies 260, 318, 593.
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   109
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   110
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   111
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   112
// (5) Implement a suggestions function which takes a rating
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   113
//     map and a movie_name as arguments. It calculates all the recommended
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   114
//     movies sorted according to the most frequently suggested movie(s) first.
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   115
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   116
//def suggestions(recs: Map[String, List[String]], 
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   117
//                mov_name: String) : List[String] = ...
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
// testcases
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   121
//-----------
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   122
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   123
//suggestions(ratings_map, "912")
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   124
//suggestions(ratings_map, "912").length  
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   125
// => 4110 suggestions with List(858, 260, 318, 593, ...)
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   126
//    being the most frequently suggested movies
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   127
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   128
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   129
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   130
// (6) Implement a recommendations function which generates at most
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   131
//     *two* of the most frequently suggested movies. It ReTurns the 
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   132
//     actual movie names, not the movieIDs.
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   133
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   134
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   135
//def recommendations(recs: Map[String, List[String]],
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   136
//                    movs: Map[String, String],
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   137
//                    mov_name: String) : List[String] = ...
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   138
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   139
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   140
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   141
// testcases
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, "912")
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   144
//   => List(Godfather, Star Wars: Episode IV - A NewHope (1977))
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   145
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   146
//recommendations(ratings_map, movies_map, "260")
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   147
//   => List(Star Wars: Episode V - The Empire Strikes Back (1980), 
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   148
//           Star Wars: Episode VI - Return of the Jedi (1983))
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, "2")
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   151
//   => List(Lion King, Jurassic Park (1993))
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, "0")
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   154
//   => Nil
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, "1")
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   157
//   => List(Shawshank Redemption, Forrest Gump (1994))
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   158
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   159
// recommendations(ratings_map, movies_map, "4")
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   160
//   => 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
   161
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   162
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   163
// If you want to calculate the recommendations for all movies,
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   164
// then use this code (it will take a few seconds calculation time).
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   165
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   166
//val all = for (name <- movie_names.map(_._1)) yield {
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   167
//  recommendations(ratings_map, movies_map, name)
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   168
//}
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   169
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   170
// helper functions
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   171
//List().take(2
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   172
//List(1).take(2)
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   173
//List(1,2).take(2)
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   174
//List(1,2,3).take(2)
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   175
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   176