marking2/danube_test4.scala
author Christian Urban <urbanc@in.tum.de>
Tue, 29 Oct 2019 14:12:24 +0000
changeset 281 87b9e3e2c1a7
parent 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
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
val urban_good_ratings = process_ratings(urban_get_csv_file("ratings.csv"))
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
val urban_ratings_map = urban_groupById(urban_good_ratings)
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
assert(favourites(urban_ratings_map, "912").length  == 80)
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
assert(favourites(urban_ratings_map, "858").length  == 158)
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
assert(favourites(urban_ratings_map, "260").length  == 201)
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28