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