main_testing2/danube_test5.scala
author Christian Urban <christian.urban@kcl.ac.uk>
Tue, 24 Nov 2020 23:36:21 +0000
changeset 367 e6ae724255a8
parent 347 4de31fdc0d67
child 379 5616b45d656f
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
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
// first test 
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
import CW7b._
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
def urban_groupById(ratings: List[(String, String)]) = 
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
  ratings.groupBy(_._1).view.mapValues(_.map(_._2)).toMap 
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
def urban_get_csv_file(name: String) : List[String] = {
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
  import io.Source
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
  import scala.util._
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
  val csv = Source.fromFile(name)
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
  csv.mkString.split("\n").toList.drop(1)
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
}
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
def urban_process_ratings(lines: List[String]) : List[(String, String)] = {
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
  for (cols <- lines.map(_.split(",").toList); 
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
       if (cols(2).toFloat >= 4)) yield (cols(0), cols(1))  
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
}
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
val urban_good_ratings = process_ratings(urban_get_csv_file("ratings.csv"))
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
val urban_ratings_map = urban_groupById(urban_good_ratings)
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
assert(suggestions(urban_ratings_map, "912").length  == 4110)
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
assert(suggestions(urban_ratings_map, "858").length  == 4883)
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
assert(suggestions(urban_ratings_map, "260").length  == 4970)
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29