389
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
1 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
2 |
// first test
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
3 |
|
424
|
4 |
import M2._
|
389
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
5 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
6 |
def urban_groupById(ratings: List[(String, String)]) =
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
7 |
ratings.groupBy(_._1).view.mapValues(_.map(_._2)).toMap
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
8 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
9 |
def urban_get_csv_file(name: String) : List[String] = {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
10 |
import io.Source
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
11 |
import scala.util._
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
12 |
val csv = Source.fromFile(name)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
13 |
csv.mkString.split("\n").toList.drop(1)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
14 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
15 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
16 |
def urban_process_ratings(lines: List[String]) : List[(String, String)] = {
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
17 |
for (cols <- lines.map(_.split(",").toList);
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
18 |
if (cols(2).toFloat >= 4)) yield (cols(0), cols(1))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
19 |
}
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
20 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
21 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
22 |
val urban_good_ratings = process_ratings(urban_get_csv_file("ratings.csv"))
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
23 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
24 |
val urban_ratings_map = urban_groupById(urban_good_ratings)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
25 |
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
26 |
assert(favourites(urban_ratings_map, "912").length == 80)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
27 |
assert(favourites(urban_ratings_map, "858").length == 158)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
28 |
assert(favourites(urban_ratings_map, "260").length == 201)
|
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
29 |
|