main_testing2/danube_test6.scala
changeset 379 5616b45d656f
parent 347 4de31fdc0d67
child 403 ffce7b61b446
equal deleted inserted replaced
378:7a5ad01a85b5 379:5616b45d656f
     1 
     1 
     2 import CW7b._
     2 import CW7b._
     3 
     3 
     4 // first test 
     4 // first test 
     5 
     5 
       
     6 def urban_groupById(ratings: List[(String, String)], 
       
     7               m: Map[String, List[String]]) : Map[String, List[String]] = ratings match {
       
     8   case Nil => m
       
     9   case (id, mov) :: rest => {
       
    10     val old_ratings = m.getOrElse (id, Nil)
       
    11     val new_ratings = m + (id -> (mov :: old_ratings))
       
    12     urban_groupById(rest, new_ratings)
       
    13   }
       
    14 }
     6 
    15 
     7 def urban_groupById(ratings: List[(String, String)]) = 
    16 //def urban_groupById(ratings: List[(String, String)]) = 
     8   ratings.groupBy(_._1).view.mapValues(_.map(_._2)).toMap 
    17 //  ratings.groupBy(_._1).view.mapValues(_.map(_._2)).toMap 
     9 
    18 
    10 def urban_get_csv_file(name: String) : List[String] = {
    19 def urban_get_csv_file(name: String) : List[String] = {
    11   import io.Source
    20   import io.Source
    12   import scala.util._
    21   import scala.util._
    13   val csv = Source.fromFile(name)
    22   val csv = Source.fromFile(name)("ISO-8859-1")
    14   csv.mkString.split("\n").toList.drop(1)
    23   csv.mkString.split("\n").toList.drop(1)
    15 }
    24 }
    16 
    25 
    17 def urban_process_ratings(lines: List[String]) : List[(String, String)] = {
    26 def urban_process_ratings(lines: List[String]) : List[(String, String)] = {
    18   for (cols <- lines.map(_.split(",").toList); 
    27   for (cols <- lines.map(_.split(",").toList); 
    19        if (cols(2).toFloat >= 4)) yield (cols(0), cols(1))  
    28        if (cols(2).toInt >= 4)) yield (cols(0), cols(1))  
    20 }
    29 }
    21 
    30 
    22 def urban_process_movies(lines: List[String]) : List[(String, String)] = {
    31 def urban_process_movies(lines: List[String]) : List[(String, String)] = {
    23   for (cols <- lines.map(_.split(",").toList)) yield (cols(0), cols(1))  
    32   for (cols <- lines.map(_.split(",").toList)) yield (cols(0), cols(1))  
    24 }
    33 }
    25 
    34 
    26 
    35 
    27 val urban_good_ratings = process_ratings(urban_get_csv_file("ratings.csv"))
    36 val urban_good_ratings = urban_process_ratings(urban_get_csv_file("ratings.csv"))
    28 val urban_movie_names = process_movies(urban_get_csv_file("movies.csv")).toMap
    37 val urban_movie_names = urban_process_movies(urban_get_csv_file("movies.csv")).toMap
    29 
    38 
    30 val urban_ratings_map = urban_groupById(urban_good_ratings)
    39 val urban_ratings_map = urban_groupById(urban_good_ratings, Map())
    31 
    40 
    32 assert((for (n <- List("1", "2", "3", "4", "5")) yield {
    41 assert((for (n <- List("1", "2", "3", "4", "5")) yield {
    33   recommendations(urban_ratings_map, urban_movie_names, n).length
    42   recommendations(urban_ratings_map, urban_movie_names, n).length
    34 }) == List(2, 2, 2, 0, 2))
    43 }) == List(2, 2, 2, 0, 2))
    35 
    44