marking2/danube_test6.scala
author Christian Urban <urbanc@in.tum.de>
Mon, 28 Jan 2019 12:49:19 +0000
changeset 259 43995ea34fe7
child 284 9a04eb6a2291
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
259
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
// first test 
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
def urban_groupById(ratings: List[(String, String)]) = 
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
  ratings.groupBy(_._1).mapValues(_.map(_._2)) 
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
def urban_get_csv_file(name: String) : List[String] = {
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
  import io.Source
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
  import scala.util._
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
  val csv = Source.fromFile(name)
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
  csv.mkString.split("\n").toList.drop(1)
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
}
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
def urban_process_ratings(lines: List[String]) : List[(String, String)] = {
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
  for (cols <- lines.map(_.split(",").toList); 
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
       if (cols(2).toFloat >= 4)) yield (cols(0), cols(1))  
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
}
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
def urban_process_movies(lines: List[String]) : List[(String, String)] = {
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
  for (cols <- lines.map(_.split(",").toList)) yield (cols(0), cols(1))  
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
}
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
val urban_good_ratings = process_ratings(urban_get_csv_file("ratings.csv"))
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
val urban_movie_names = process_movies(urban_get_csv_file("movies.csv")).toMap
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
val urban_ratings_map = urban_groupById(urban_good_ratings)
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
assert((for (n <- List("1", "2", "3", "4", "5")) yield {
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
  recommendations(urban_ratings_map, urban_movie_names, n).length
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
}) == List(2, 2, 2, 0, 2))
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33