main_marking2/danube_test7b.scala
author Christian Urban <christian.urban@kcl.ac.uk>
Fri, 05 Nov 2021 17:20:53 +0000
changeset 398 7d9b765d4012
parent 389 4113d4d8cf62
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
import CW7b._
4113d4d8cf62 updated marking 2
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     3
4113d4d8cf62 updated marking 2
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     4
def urban_groupById(ratings: List[(String, String)]) = 
4113d4d8cf62 updated marking 2
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     5
  ratings.groupBy(_._1).view.mapValues(_.map(_._2)).toMap 
4113d4d8cf62 updated marking 2
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     6
4113d4d8cf62 updated marking 2
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     7
def urban_get_csv_file(name: String) : List[String] = {
4113d4d8cf62 updated marking 2
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     8
  import io.Source
4113d4d8cf62 updated marking 2
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     9
  import scala.util._
4113d4d8cf62 updated marking 2
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    10
  val csv = Source.fromFile(name)
4113d4d8cf62 updated marking 2
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    11
  csv.mkString.split("\n").toList.drop(1)
4113d4d8cf62 updated marking 2
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    12
}
4113d4d8cf62 updated marking 2
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    13
4113d4d8cf62 updated marking 2
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    14
def urban_process_ratings(lines: List[String]) : List[(String, String)] = {
4113d4d8cf62 updated marking 2
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    15
  for (cols <- lines.map(_.split(",").toList); 
4113d4d8cf62 updated marking 2
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    16
       if (cols(2).toFloat >= 4)) yield (cols(0), cols(1))  
4113d4d8cf62 updated marking 2
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    17
}
4113d4d8cf62 updated marking 2
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    18
4113d4d8cf62 updated marking 2
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    19
def urban_process_movies(lines: List[String]) : List[(String, String)] = {
4113d4d8cf62 updated marking 2
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    20
  for (cols <- lines.map(_.split(",").toList)) yield (cols(0), cols(1))  
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
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_good_ratings = process_ratings(urban_get_csv_file("ratings.csv"))
4113d4d8cf62 updated marking 2
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    25
val urban_movie_names = process_movies(urban_get_csv_file("movies.csv")).toMap
4113d4d8cf62 updated marking 2
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    26
4113d4d8cf62 updated marking 2
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    27
val urban_ratings_map = urban_groupById(urban_good_ratings)
4113d4d8cf62 updated marking 2
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    28
4113d4d8cf62 updated marking 2
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    29
def urban_test() = 
4113d4d8cf62 updated marking 2
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    30
  { most_recommended(urban_ratings_map, urban_movie_names) ; true }
4113d4d8cf62 updated marking 2
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    31
4113d4d8cf62 updated marking 2
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    32
assert(urban_test())
4113d4d8cf62 updated marking 2
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    33
4113d4d8cf62 updated marking 2
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    34