| 284 |      1 | 
 | 
| 403 |      2 | import M2._
 | 
| 284 |      3 | 
 | 
|  |      4 | // first test 
 | 
|  |      5 | 
 | 
| 379 |      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 | }
 | 
| 284 |     15 | 
 | 
| 379 |     16 | //def urban_groupById(ratings: List[(String, String)]) = 
 | 
|  |     17 | //  ratings.groupBy(_._1).view.mapValues(_.map(_._2)).toMap 
 | 
| 284 |     18 | 
 | 
|  |     19 | def urban_get_csv_file(name: String) : List[String] = {
 | 
|  |     20 |   import io.Source
 | 
|  |     21 |   import scala.util._
 | 
| 379 |     22 |   val csv = Source.fromFile(name)("ISO-8859-1")
 | 
| 284 |     23 |   csv.mkString.split("\n").toList.drop(1)
 | 
|  |     24 | }
 | 
|  |     25 | 
 | 
|  |     26 | def urban_process_ratings(lines: List[String]) : List[(String, String)] = {
 | 
|  |     27 |   for (cols <- lines.map(_.split(",").toList); 
 | 
| 379 |     28 |        if (cols(2).toInt >= 4)) yield (cols(0), cols(1))  
 | 
| 284 |     29 | }
 | 
|  |     30 | 
 | 
|  |     31 | def urban_process_movies(lines: List[String]) : List[(String, String)] = {
 | 
|  |     32 |   for (cols <- lines.map(_.split(",").toList)) yield (cols(0), cols(1))  
 | 
|  |     33 | }
 | 
|  |     34 | 
 | 
|  |     35 | 
 | 
| 379 |     36 | val urban_good_ratings = urban_process_ratings(urban_get_csv_file("ratings.csv"))
 | 
|  |     37 | val urban_movie_names = urban_process_movies(urban_get_csv_file("movies.csv")).toMap
 | 
| 284 |     38 | 
 | 
| 379 |     39 | val urban_ratings_map = urban_groupById(urban_good_ratings, Map())
 | 
| 284 |     40 | 
 | 
|  |     41 | assert((for (n <- List("1", "2", "3", "4", "5")) yield {
 | 
|  |     42 |   recommendations(urban_ratings_map, urban_movie_names, n).length
 | 
|  |     43 | }) == List(2, 2, 2, 0, 2))
 | 
|  |     44 | 
 |