main_testing2/danube_test5.scala
changeset 379 5616b45d656f
parent 347 4de31fdc0d67
child 403 ffce7b61b446
equal deleted inserted replaced
378:7a5ad01a85b5 379:5616b45d656f
     1 
     1 
     2 // first test 
     2 // first test 
     3 
     3 
     4 import CW7b._
     4 import CW7b._
     5 
     5 
     6 def urban_groupById(ratings: List[(String, String)]) = 
     6 def urban_groupById(ratings: List[(String, String)], 
     7   ratings.groupBy(_._1).view.mapValues(_.map(_._2)).toMap 
     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 }
       
    15 
       
    16 //def urban_groupById(ratings: List[(String, String)]) = 
       
    17 //  ratings.groupBy(_._1).view.mapValues(_.map(_._2)).toMap 
     8 
    18 
     9 def urban_get_csv_file(name: String) : List[String] = {
    19 def urban_get_csv_file(name: String) : List[String] = {
    10   import io.Source
    20   import io.Source
    11   import scala.util._
    21   import scala.util._
    12   val csv = Source.fromFile(name)
    22   val csv = Source.fromFile(name)("ISO-8859-1")
    13   csv.mkString.split("\n").toList.drop(1)
    23   csv.mkString.split("\n").toList.drop(1)
    14 }
    24 }
    15 
    25 
    16 def urban_process_ratings(lines: List[String]) : List[(String, String)] = {
    26 def urban_process_ratings(lines: List[String]) : List[(String, String)] = {
    17   for (cols <- lines.map(_.split(",").toList); 
    27   for (cols <- lines.map(_.split(",").toList); 
    18        if (cols(2).toFloat >= 4)) yield (cols(0), cols(1))  
    28        if (cols(2).toInt >= 4)) yield (cols(0), cols(1))  
    19 }
    29 }
    20 
    30 
    21 
    31 
    22 val urban_good_ratings = process_ratings(urban_get_csv_file("ratings.csv"))
    32 val urban_good_ratings = urban_process_ratings(urban_get_csv_file("ratings.csv"))
    23 
    33 
    24 val urban_ratings_map = urban_groupById(urban_good_ratings)
    34 val urban_ratings_map = urban_groupById(urban_good_ratings, Map())
    25 
    35 
    26 assert(suggestions(urban_ratings_map, "912").length  == 4110)
    36 assert(suggestions(urban_ratings_map, "912").length  == 4110)
    27 assert(suggestions(urban_ratings_map, "858").length  == 4883)
    37 assert(suggestions(urban_ratings_map, "858").length  == 4883)
    28 assert(suggestions(urban_ratings_map, "260").length  == 4970)
    38 assert(suggestions(urban_ratings_map, "260").length  == 4970)
    29 
    39