Attic/danube_test5.scala
author Christian Urban <christian.urban@kcl.ac.uk>
Thu, 06 Jun 2024 22:18:15 +0100
changeset 490 4778fefecd0c
parent 463 0315d9983cd0
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
389
4113d4d8cf62 updated marking 2
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     1
4113d4d8cf62 updated marking 2
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     2
// first test 
4113d4d8cf62 updated marking 2
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     3
424
Christian Urban <christian.urban@kcl.ac.uk>
parents: 389
diff changeset
     4
import M2._
389
4113d4d8cf62 updated marking 2
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     5
4113d4d8cf62 updated marking 2
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     6
def urban_groupById(ratings: List[(String, String)]) = 
4113d4d8cf62 updated marking 2
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     7
  ratings.groupBy(_._1).view.mapValues(_.map(_._2)).toMap 
4113d4d8cf62 updated marking 2
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     8
4113d4d8cf62 updated marking 2
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     9
def urban_get_csv_file(name: String) : List[String] = {
4113d4d8cf62 updated marking 2
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    10
  import io.Source
4113d4d8cf62 updated marking 2
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    11
  import scala.util._
4113d4d8cf62 updated marking 2
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    12
  val csv = Source.fromFile(name)
4113d4d8cf62 updated marking 2
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    13
  csv.mkString.split("\n").toList.drop(1)
4113d4d8cf62 updated marking 2
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    14
}
4113d4d8cf62 updated marking 2
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    15
4113d4d8cf62 updated marking 2
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    16
def urban_process_ratings(lines: List[String]) : List[(String, String)] = {
4113d4d8cf62 updated marking 2
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    17
  for (cols <- lines.map(_.split(",").toList); 
4113d4d8cf62 updated marking 2
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    18
       if (cols(2).toFloat >= 4)) yield (cols(0), cols(1))  
4113d4d8cf62 updated marking 2
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    19
}
4113d4d8cf62 updated marking 2
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    20
4113d4d8cf62 updated marking 2
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    21
4113d4d8cf62 updated marking 2
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    22
val urban_good_ratings = process_ratings(urban_get_csv_file("ratings.csv"))
4113d4d8cf62 updated marking 2
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    23
4113d4d8cf62 updated marking 2
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    24
val urban_ratings_map = urban_groupById(urban_good_ratings)
4113d4d8cf62 updated marking 2
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    25
4113d4d8cf62 updated marking 2
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    26
assert(suggestions(urban_ratings_map, "912").length  == 4110)
4113d4d8cf62 updated marking 2
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    27
assert(suggestions(urban_ratings_map, "858").length  == 4883)
4113d4d8cf62 updated marking 2
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    28
assert(suggestions(urban_ratings_map, "260").length  == 4970)
4113d4d8cf62 updated marking 2
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    29