main_testing2/danube.scala
author Christian Urban <christian.urban@kcl.ac.uk>
Fri, 15 Jan 2021 02:40:57 +0000
changeset 384 6e1237691307
parent 379 5616b45d656f
child 403 ffce7b61b446
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
384
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
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
384
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
     3
//===========================================
379
5616b45d656f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
     4
384
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
     5
object CW7b {
211
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
import io.Source
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
import scala.util._
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
329
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
    10
211
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
// (1) Implement the function get_csv_url which takes an url-string
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
//     as argument and requests the corresponding file. The two urls
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
//     of interest are ratings_url and movies_url, which correspond 
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
//     to CSV-files.
384
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
    15
//
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
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
384
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
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] = {
384
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
    22
  val site = Source.fromURL(url, "ISO-8859-1")
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
    23
  val site_string = site.mkString
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
    24
  val output = (site_string.split("\n")).toList
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
    25
  output.tail
211
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
}
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
384
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
    28
  // get_csv_url("https://nms.kcl.ac.uk/christian.urban/ratings.csv")
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
    29
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
    30
//val ratings_url = """https://nms.kcl.ac.uk/christian.urban/ratings.csv"""
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
    31
//val movies_url = """https://nms.kcl.ac.uk/christian.urban/movies.csv"""
211
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
384
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
    33
// testcases
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
    34
//-----------
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
    35
//:
329
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
    36
//val movies = get_csv_url(movies_url)
384
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
    37
  // val ratings = get_csv_url(ratings_url)
211
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
//ratings.length  // 87313
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
//movies.length   // 9742
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
384
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
    42
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
    43
// (2) Implement two functions that process the CSV-files from (1). The ratings
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
    44
//     function filters out all ratings below 4 and ReTurns a list of 
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
    45
//     (userID, movieID) pairs. The movies function just ReTurns a list 
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
    46
//     of (movieID, title) pairs. Note the input to these functions, that is
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
    47
//     the argument lines, will be the output of the function get_csv_url.
211
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
def process_ratings(lines: List[String]) : List[(String, String)] = {
384
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
    51
  val filter = lines.filter(_.last.asDigit >=4)
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
    52
  val output = (for(i <- 0 until filter.length) yield ((filter(i).split(",").toList)(0), (filter(i).split(",").toList)(1))).toList
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
    53
  output
211
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
}
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    55
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    56
def process_movies(lines: List[String]) : List[(String, String)] = {
384
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
    57
  val output = (for(i <- 0 until lines.length) yield ((lines(i).split(",").toList)(0), (lines(i).split(",").toList)(1))).toList
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
    58
  output
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
    59
}
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
    60
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
    61
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
    62
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
    63
def process_ratings2(lines: List[String]) : List[(String, String)] = {
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
    64
  for (cols <- lines.map(_.split(",").toList); 
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
    65
       if (cols(2).toFloat >= 4)) yield (cols(0), cols(1))  
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
    66
}
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
    67
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
    68
def process_movies2(lines: List[String]) : List[(String, String)] = {
329
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
    69
  for (cols <- lines.map(_.split(",").toList)) yield (cols(0), cols(1))  
211
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    70
}
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    71
384
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
    72
// testcases
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
    73
//-----------
329
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
    74
//val good_ratings = process_ratings(ratings)
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
    75
//val movie_names = process_movies(movies)
211
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    76
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    77
//good_ratings.length   //48580
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    78
//movie_names.length    // 9742
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    79
384
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
    80
329
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
    81
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
    82
384
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
    83
// (3) Implement a grouping function that calculates a Map
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
    84
//     containing the userIDs and all the corresponding recommendations 
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
    85
//     (list of movieIDs). This  should be implemented in a tail
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
    86
//     recursive fashion, using a Map m as accumulator. This Map m
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
    87
//     is set to Map() at the beginning of the calculation.
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
    88
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
    89
val ratings_url = """https://nms.kcl.ac.uk/christian.urban/ratings.csv"""
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
    90
val ratings = get_csv_url(ratings_url)
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
    91
val good_ratings = process_ratings(ratings)
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
    92
val v515 = good_ratings.filter(_._1 == "515")
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
    93
val v515_2 = v515.map(_._2)
329
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
    94
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
    95
def groupById(ratings: List[(String, String)], 
384
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
    96
              m: Map[String, List[String]]) : Map[String, List[String]] = {
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
    97
val users = (for((k,v) <- ratings) yield k).distinct
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
    98
val movie_ids = (for(i <- 1 to users.length) yield
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
    99
  (for ((k,v) <- ratings if(i.toString == k)) yield v).toList).toList
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   100
  val out_map = (users zip movie_ids).toMap
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   101
out_map
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   102
}
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   103
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   104
def groupById2(ratings: List[(String, String)], 
329
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
   105
              m: Map[String, List[String]]) : Map[String, List[String]] = ratings match {
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
   106
  case Nil => m
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
   107
  case (id, mov) :: rest => {
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
   108
    val old_ratings = m.getOrElse (id, Nil)
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
   109
    val new_ratings = m + (id -> (mov :: old_ratings))
384
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   110
    groupById2(rest, new_ratings)
329
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
   111
  }
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
   112
}
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
   113
384
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   114
val ls0_urban = 
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   115
  List(("1", "a"), ("1", "c"), ("1", "c"))
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   116
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   117
groupById(ls0_urban, Map())
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   118
groupById2(ls0_urban, Map())
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   119
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   120
val ls00_urban = 
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   121
  List(("3", "a"), ("3", "c"), ("3", "c"))
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   122
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   123
groupById(ls00_urban, Map())
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   124
groupById2(ls00_urban, Map())
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   125
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   126
groupById(good_ratings, Map()).getOrElse("515", Nil)
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   127
groupById2(good_ratings, Map()).getOrElse("515", Nil)
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   128
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   129
val ls1_urban = 
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   130
  List(("1", "a"), ("2", "a"), 
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   131
       ("1", "c"), ("2", "a"), ("1", "c"))
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   132
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   133
groupById(ls1_urban, Map())
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   134
groupById2(ls1_urban, Map())
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   135
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   136
val ls2_urban = 
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   137
  List(("1", "a"), ("1", "b"), ("2", "x"), 
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   138
       ("3", "a"), ("2", "y"), ("3", "c"))
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   139
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   140
groupById(ls2_urban, Map())
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   141
groupById2(ls2_urban, Map())
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   142
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   143
val ls3_urban = (1 to 1000 by 10).map(_.toString).toList
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   144
val ls4_urban = ls3_urban zip ls3_urban.tail
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   145
val ls5_urban = ls4_urban ::: ls4_urban.reverse
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   146
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   147
groupById(ls5_urban, Map()) == groupById2(ls5_urban, Map())
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   148
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   149
groupById(ls5_urban, Map())
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   150
groupById2(ls5_urban, Map())
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   151
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   152
groupById(v515, Map())
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   153
groupById2(v515, Map())
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   154
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   155
groupById(v515.take(1), Map())
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   156
groupById2(v515.take(2), Map())
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   157
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   158
// testcases
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   159
//-----------
329
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
   160
//val ratings_map = groupById(good_ratings, Map())
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
   161
//val movies_map = movie_names.toMap
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
   162
384
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   163
//ratings_map.get("414").get.map(movies_map.get(_)).length
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   164
//    => most prolific recommender with 1227 positive ratings
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   165
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   166
//ratings_map.get("475").get.map(movies_map.get(_)).length
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   167
//    => second-most prolific recommender with 787 positive ratings
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   168
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   169
//ratings_map.get("214").get.map(movies_map.get(_)).length 
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   170
//    => least prolific recommender with only 1 positive rating
211
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   171
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   172
384
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   173
// (4) Implement a function that takes a ratings map and a movie_name as argument.
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   174
//     The function calculates all suggestions containing
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   175
//     the movie in its recommendations. It ReTurns a list of all these
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   176
//     recommendations (each of them is a list and needs to have the movie deleted, 
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   177
//     otherwise it might happen we recommend the same movie).
211
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   178
384
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   179
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   180
def favourites(m: Map[String, List[String]], mov: String) : List[List[String]] = {
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   181
 (for((k,v) <- m if (v.contains(mov))) yield v.filter(_!=mov).toList).toList
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   182
}
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   183
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   184
def favourites2(m: Map[String, List[String]], mov: String) : List[List[String]] = 
329
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
   185
  (for (id <- m.keys.toList;
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
   186
        if m(id).contains(mov)) yield m(id).filter(_ != mov))
211
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   187
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   188
384
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   189
// testcases
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   190
//-----------
211
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   191
// movie ID "912" -> Casablanca (1942)
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   192
//          "858" -> Godfather
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   193
//          "260" -> Star Wars: Episode IV - A New Hope (1977)
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   194
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   195
//favourites(ratings_map, "912").length  // => 80
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   196
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   197
// That means there are 80 users that recommend the movie with ID 912.
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   198
// Of these 80  users, 55 gave a good rating to movie 858 and
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   199
// 52 a good rating to movies 260, 318, 593.
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   200
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   201
384
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   202
329
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
   203
// (5) Implement a suggestions function which takes a rating
384
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   204
//     map and a movie_name as arguments. It calculates all the recommended
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   205
//     movies sorted according to the most frequently suggested movie(s) first.
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   206
211
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   207
def suggestions(recs: Map[String, List[String]], 
384
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   208
                mov_name: String) : List[String] = {
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   209
  val flat = favourites(recs, mov_name).flatten.groupMapReduce(identity)(_ => 1)(_ + _)
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   210
  val sorted = flat.toList.sortWith(_._2 > _._2).map(_._1)
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   211
  sorted
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   212
}
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   213
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   214
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   215
def mapValues[S, T, R](m: Map[S, T], f: T => R) =
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   216
  m.map { case (x, y) => (x, f(y)) }
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   217
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   218
def suggestions2(recs: Map[String, List[String]], 
329
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
   219
                    mov_name: String) : List[String] = {
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
   220
  val favs = favourites(recs, mov_name).flatten
384
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   221
  val favs_counted = mapValues(favs.groupBy(identity), (v:List[String]) => v.size).toList
329
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
   222
  val favs_sorted = favs_counted.sortBy(_._2).reverse
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
   223
  favs_sorted.map(_._1)
211
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   224
}
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   225
384
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   226
// testcases
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   227
//-----------
211
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   228
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   229
//suggestions(ratings_map, "912")
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   230
//suggestions(ratings_map, "912").length  
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   231
// => 4110 suggestions with List(858, 260, 318, 593, ...)
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   232
//    being the most frequently suggested movies
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   233
384
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   234
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   235
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   236
// (6) Implement a recommendations function which generates at most
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   237
//     *two* of the most frequently suggested movies. It ReTurns the 
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   238
//     actual movie names, not the movieIDs.
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   239
211
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   240
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   241
def recommendations(recs: Map[String, List[String]],
384
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   242
                    movs: Map[String, String],
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   243
                    mov_name: String) : List[String] = {
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   244
  val sugg = suggestions(recs, mov_name)
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   245
  val movies = (for (i <- 0 until 2 if (i < sugg.length)) yield movs(sugg(i))).toList
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   246
  movies
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   247
}
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   248
211
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   249
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   250
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   251
// testcases
384
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   252
//-----------
211
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   253
// recommendations(ratings_map, movies_map, "912")
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   254
//   => List(Godfather, Star Wars: Episode IV - A NewHope (1977))
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   255
384
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   256
//recommendations(ratings_map, movies_map, "260")
211
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   257
//   => List(Star Wars: Episode V - The Empire Strikes Back (1980), 
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   258
//           Star Wars: Episode VI - Return of the Jedi (1983))
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   259
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   260
// recommendations(ratings_map, movies_map, "2")
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   261
//   => List(Lion King, Jurassic Park (1993))
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   262
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   263
// recommendations(ratings_map, movies_map, "0")
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   264
//   => Nil
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   265
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   266
// recommendations(ratings_map, movies_map, "1")
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   267
//   => List(Shawshank Redemption, Forrest Gump (1994))
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   268
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   269
// recommendations(ratings_map, movies_map, "4")
379
5616b45d656f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
   270
//   => 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
   271
384
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   272
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   273
379
5616b45d656f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
   274
// (7) Calculate the recommendations for all movies according to
5616b45d656f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
   275
// what the recommendations function in (6) produces (this
5616b45d656f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
   276
// can take a few seconds). Put all recommendations into a list 
5616b45d656f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
   277
// (of strings) and count how often the strings occur in
5616b45d656f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
   278
// this list. This produces a list of string-int pairs,
5616b45d656f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
   279
// where the first component is the movie name and the second
384
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   280
// is the number of how many times the movie was recommended. 
379
5616b45d656f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
   281
// Sort all the pairs according to the number
5616b45d656f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
   282
// of times they were recommended (most recommended movie name 
5616b45d656f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
   283
// first).
5616b45d656f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
   284
5616b45d656f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
   285
def most_recommended(recs: Map[String, List[String]],
5616b45d656f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
   286
                     movs: Map[String, String]) : List[(String, Int)] = {
384
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   287
  val movies = (((for((k,v) <- movs) yield recommendations(recs, movs, k)).toList).flatten).groupMapReduce(identity)(_ => 1)(_ + _)
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   288
  val sorted = movies.toList.sortWith(_._2 > _._2)
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   289
  sorted
379
5616b45d656f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
   290
}
211
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   291
384
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   292
// testcase
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   293
//
379
5616b45d656f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
   294
//most_recommended(ratings_map, movies_map).take(3)
5616b45d656f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
   295
// =>
5616b45d656f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
   296
// List((Matrix,698), 
5616b45d656f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
   297
//      (Star Wars: Episode IV - A New Hope (1977),402), 
5616b45d656f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
   298
//      (Jerry Maguire (1996),382))
211
092e0879a5ae updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   299
379
5616b45d656f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
   300
5616b45d656f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
   301
384
6e1237691307 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
   302
}