main_testing2/danube_test4.scala
author Christian Urban <christian.urban@kcl.ac.uk>
Mon, 08 Nov 2021 01:39:00 +0000
changeset 403 ffce7b61b446
parent 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
403
ffce7b61b446 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 379
diff changeset
     4
import M2._
284
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._
379
5616b45d656f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
    12
  val csv = Source.fromFile(name)("ISO-8859-1")
284
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); 
379
5616b45d656f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
    18
       if (cols(2).toInt >= 4)) yield (cols(0), cols(1))  
284
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
379
5616b45d656f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 347
diff changeset
    22
val urban_good_ratings = urban_process_ratings(urban_get_csv_file("ratings.csv"))
284
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(favourites(urban_ratings_map, "912").length  == 80)
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
assert(favourites(urban_ratings_map, "858").length  == 158)
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
assert(favourites(urban_ratings_map, "260").length  == 201)
9a04eb6a2291 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29