templates2/danube.scala
author Christian Urban <urbanc@in.tum.de>
Tue, 05 Nov 2019 00:41:02 +0000
changeset 313 dea46bdfd648
parent 284 9a04eb6a2291
child 333 24bc76d97db2
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
284
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents: 203
diff changeset
     1
// Core Part 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
284
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents: 203
diff changeset
     5
object CW7b {
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
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
//def get_csv_url(url: String) : List[String] = ...
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
//-----------
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
//val ratings = get_csv_url(ratings_url)
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 
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
//     of (movieID, title) pairs.
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
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_ratings(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
//def process_movies(lines: List[String]) : List[(String, String)] = ...
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
// testcases
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
//-----------
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
//val good_ratings = process_ratings(ratings)
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
//val movie_names = process_movies(movies)
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    52
//good_ratings.length   //48580
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    53
//movie_names.length    // 9742
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
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    57
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    58
// (3) Implement a grouping function that calculates a Map
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    59
//     containing the userIDs and all the corresponding recommendations 
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    60
//     (list of movieIDs). This  should be implemented in a tail
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    61
//     recursive fashion, using a Map m as accumulator. This Map m
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    62
//     is set to Map() at the beginning of the calculation.
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    63
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
//def groupById(ratings: List[(String, String)], 
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
//              m: Map[String, List[String]]) : Map[String, List[String]] = ...
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    66
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    67
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    68
// testcases
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    69
//-----------
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    70
//val ratings_map = groupById(good_ratings, Map())
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    71
//val movies_map = movie_names.toMap
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    72
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    73
//ratings_map.get("414").get.map(movies_map.get(_)) 
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    74
//    => most prolific recommender with 1227 positive ratings
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    75
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    76
//ratings_map.get("474").get.map(movies_map.get(_)) 
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    77
//    => second-most prolific recommender with 787 positive ratings
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    78
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    79
//ratings_map.get("214").get.map(movies_map.get(_)) 
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    80
//    => least prolific recommender with only 1 positive rating
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    81
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
// (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
    85
//     The function calculates all suggestions containing
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    86
//     the movie in its recommendations. It ReTurns a list of all these
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    87
//     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
    88
//     otherwise it might happen we recommend the same movie).
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    89
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    90
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    91
//def favourites(m: Map[String, List[String]], mov: String) : List[List[String]] = ...
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    92
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    93
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    94
// testcases
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    95
//-----------
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    96
// movie ID "912" -> Casablanca (1942)
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    97
//          "858" -> Godfather
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    98
//          "260" -> Star Wars: Episode IV - A New Hope (1977)
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    99
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   100
//favourites(ratings_map, "912").length  // => 80
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   101
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   102
// That means there are 80 users that recommend the movie with ID 912.
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   103
// Of these 80  users, 55 gave a good rating to movie 858 and
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   104
// 52 a good rating to movies 260, 318, 593.
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   105
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
// (5) Implement a suggestions function which takes a rating
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   109
//     map and a movie_name as arguments. It calculates all the recommended
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   110
//     movies sorted according to the most frequently suggested movie(s) first.
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   111
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   112
//def suggestions(recs: Map[String, List[String]], 
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   113
//                mov_name: String) : List[String] = ...
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   114
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   115
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   116
// testcases
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   117
//-----------
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   118
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   119
//suggestions(ratings_map, "912")
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   120
//suggestions(ratings_map, "912").length  
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   121
// => 4110 suggestions with List(858, 260, 318, 593, ...)
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   122
//    being the most frequently suggested movies
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   123
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
// (6) Implement a recommendations function which generates at most
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   127
//     *two* of the most frequently suggested movies. It ReTurns the 
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   128
//     actual movie names, not the movieIDs.
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   129
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   130
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   131
//def recommendations(recs: Map[String, List[String]],
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   132
//                    movs: Map[String, String],
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   133
//                    mov_name: String) : List[String] = ...
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   134
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
// testcases
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   138
//-----------
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   139
// recommendations(ratings_map, movies_map, "912")
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   140
//   => List(Godfather, Star Wars: Episode IV - A NewHope (1977))
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   141
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   142
//recommendations(ratings_map, movies_map, "260")
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   143
//   => List(Star Wars: Episode V - The Empire Strikes Back (1980), 
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   144
//           Star Wars: Episode VI - Return of the Jedi (1983))
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, "2")
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   147
//   => List(Lion King, Jurassic Park (1993))
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   148
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   149
// recommendations(ratings_map, movies_map, "0")
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   150
//   => Nil
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   151
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   152
// recommendations(ratings_map, movies_map, "1")
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   153
//   => List(Shawshank Redemption, Forrest Gump (1994))
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   154
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   155
// recommendations(ratings_map, movies_map, "4")
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   156
//   => 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
   157
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   158
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   159
// If you want to calculate the recommendations for all movies,
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   160
// then use this code (it will take a few seconds calculation time).
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   161
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   162
//val all = for (name <- movie_names.map(_._1)) yield {
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   163
//  recommendations(ratings_map, movies_map, name)
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   164
//}
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   165
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   166
// helper functions
284
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents: 203
diff changeset
   167
//List().take(2)
203
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   168
//List(1).take(2)
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   169
//List(1,2).take(2)
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   170
//List(1,2,3).take(2)
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   171
eb188f9ac038 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   172
284
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents: 203
diff changeset
   173
}