main_testing2/danube_test6.scala
author Christian Urban <christian.urban@kcl.ac.uk>
Mon, 08 Nov 2021 02:20:35 +0000
changeset 410 5bc7183e865e
parent 403 ffce7b61b446
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:
diff changeset
     1
403
ffce7b61b446 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
     2
import M2._
284
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
// first test 
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
379
5616b45d656f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
     6
def urban_groupById(ratings: List[(String, String)], 
5616b45d656f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
     7
              m: Map[String, List[String]]) : Map[String, List[String]] = ratings match {
5616b45d656f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
     8
  case Nil => m
5616b45d656f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
     9
  case (id, mov) :: rest => {
5616b45d656f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
    10
    val old_ratings = m.getOrElse (id, Nil)
5616b45d656f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
    11
    val new_ratings = m + (id -> (mov :: old_ratings))
5616b45d656f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
    12
    urban_groupById(rest, new_ratings)
5616b45d656f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
    13
  }
5616b45d656f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
    14
}
284
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
379
5616b45d656f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
    16
//def urban_groupById(ratings: List[(String, String)]) = 
5616b45d656f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
    17
//  ratings.groupBy(_._1).view.mapValues(_.map(_._2)).toMap 
284
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
def urban_get_csv_file(name: String) : List[String] = {
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
  import io.Source
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
  import scala.util._
379
5616b45d656f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
    22
  val csv = Source.fromFile(name)("ISO-8859-1")
284
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
  csv.mkString.split("\n").toList.drop(1)
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
}
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
def urban_process_ratings(lines: List[String]) : List[(String, String)] = {
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
  for (cols <- lines.map(_.split(",").toList); 
379
5616b45d656f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
    28
       if (cols(2).toInt >= 4)) yield (cols(0), cols(1))  
284
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
}
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
def urban_process_movies(lines: List[String]) : List[(String, String)] = {
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
  for (cols <- lines.map(_.split(",").toList)) yield (cols(0), cols(1))  
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
}
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
379
5616b45d656f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
    36
val urban_good_ratings = urban_process_ratings(urban_get_csv_file("ratings.csv"))
5616b45d656f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
    37
val urban_movie_names = urban_process_movies(urban_get_csv_file("movies.csv")).toMap
284
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
379
5616b45d656f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
    39
val urban_ratings_map = urban_groupById(urban_good_ratings, Map())
284
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
assert((for (n <- List("1", "2", "3", "4", "5")) yield {
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
  recommendations(urban_ratings_map, urban_movie_names, n).length
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
}) == List(2, 2, 2, 0, 2))
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44