marking2/danube_test5.scala
author Christian Urban <christian.urban@kcl.ac.uk>
Sat, 11 Mar 2023 23:22:05 +0000
changeset 468 0587ef444547
parent 284 9a04eb6a2291
permissions -rw-r--r--
updated and cleaned
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
284
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents: 259
diff changeset
     4
import CW7b._
259
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
def urban_groupById(ratings: List[(String, String)]) = 
284
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents: 259
diff changeset
     7
  ratings.groupBy(_._1).view.mapValues(_.map(_._2)).toMap 
259
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
def urban_get_csv_file(name: String) : List[String] = {
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
  import io.Source
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
  import scala.util._
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
  val csv = Source.fromFile(name)
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
  csv.mkString.split("\n").toList.drop(1)
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
}
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
def urban_process_ratings(lines: List[String]) : List[(String, String)] = {
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
  for (cols <- lines.map(_.split(",").toList); 
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
       if (cols(2).toFloat >= 4)) yield (cols(0), cols(1))  
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
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
val urban_good_ratings = process_ratings(urban_get_csv_file("ratings.csv"))
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
val urban_ratings_map = urban_groupById(urban_good_ratings)
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
assert(suggestions(urban_ratings_map, "912").length  == 4110)
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
assert(suggestions(urban_ratings_map, "858").length  == 4883)
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
assert(suggestions(urban_ratings_map, "260").length  == 4970)
43995ea34fe7 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29