|
1 // Part 2 and 3 about Movie Recommendations |
|
2 // at Danube.co.uk |
|
3 //=========================================== |
|
4 |
|
5 import io.Source |
|
6 import scala.util._ |
|
7 |
|
8 // (1) Implement the function get_csv_url which takes an url-string |
|
9 // as argument and requests the corresponding file. The two urls |
|
10 // of interest are ratings_url and movies_url, which correspond |
|
11 // to CSV-files. |
|
12 // |
|
13 // The function should ReTurn the CSV-file appropriately broken |
|
14 // up into lines, and the first line should be dropped (that is without |
|
15 // the header of the CSV-file). The result is a list of strings (lines |
|
16 // in the file). |
|
17 |
|
18 //def get_csv_url(url: String) : List[String] = ... |
|
19 |
|
20 |
|
21 val ratings_url = """https://nms.kcl.ac.uk/christian.urban/ratings.csv""" |
|
22 val movies_url = """https://nms.kcl.ac.uk/christian.urban/movies.csv""" |
|
23 |
|
24 // testcases |
|
25 //----------- |
|
26 //val ratings = get_csv_url(ratings_url) |
|
27 //val movies = get_csv_url(movies_url) |
|
28 |
|
29 //ratings.length // 87313 |
|
30 //movies.length // 9742 |
|
31 |
|
32 |
|
33 |
|
34 // (2) Implement two functions that process the CSV-files from (1). The ratings |
|
35 // function filters out all ratings below 4 and ReTurns a list of |
|
36 // (userID, movieID) pairs. The movies function just ReTurns a list |
|
37 // of (movieID, title) pairs. |
|
38 |
|
39 |
|
40 //def process_ratings(lines: List[String]) : List[(String, String)] = ... |
|
41 |
|
42 //def process_movies(lines: List[String]) : List[(String, String)] = ... |
|
43 |
|
44 |
|
45 // testcases |
|
46 //----------- |
|
47 //val good_ratings = process_ratings(ratings) |
|
48 //val movie_names = process_movies(movies) |
|
49 |
|
50 //good_ratings.length //48580 |
|
51 //movie_names.length // 9742 |
|
52 |
|
53 |
|
54 |
|
55 //============================================== |
|
56 // Do not change anything below, unless you want |
|
57 // to submit the file for the advanced part 3! |
|
58 //============================================== |
|
59 |
|
60 |
|
61 |
|
62 // (3) Implement a grouping function that calculates a Map |
|
63 // containing the userIDs and all the corresponding recommendations |
|
64 // (list of movieIDs). This should be implemented in a tail |
|
65 // recursive fashion, using a Map m as accumulator. This Map m |
|
66 // is set to Map() at the beginning of the calculation. |
|
67 |
|
68 //def groupById(ratings: List[(String, String)], |
|
69 // m: Map[String, List[String]]) : Map[String, List[String]] = ... |
|
70 |
|
71 |
|
72 // testcases |
|
73 //----------- |
|
74 //val ratings_map = groupById(good_ratings, Map()) |
|
75 //val movies_map = movie_names.toMap |
|
76 |
|
77 //ratings_map.get("414").get.map(movies_map.get(_)) |
|
78 // => most prolific recommender with 1227 positive ratings |
|
79 |
|
80 //ratings_map.get("474").get.map(movies_map.get(_)) |
|
81 // => second-most prolific recommender with 787 positive ratings |
|
82 |
|
83 //ratings_map.get("214").get.map(movies_map.get(_)) |
|
84 // => least prolific recommender with only 1 positive rating |
|
85 |
|
86 |
|
87 |
|
88 // (4) Implement a function that takes a ratings map and a movie_name as argument. |
|
89 // The function calculates all suggestions containing |
|
90 // the movie in its recommendations. It ReTurns a list of all these |
|
91 // recommendations (each of them is a list and needs to have the movie deleted, |
|
92 // otherwise it might happen we recommend the same movie). |
|
93 |
|
94 |
|
95 //def favourites(m: Map[String, List[String]], mov: String) : List[List[String]] = ... |
|
96 |
|
97 |
|
98 // testcases |
|
99 //----------- |
|
100 // movie ID "912" -> Casablanca (1942) |
|
101 // "858" -> Godfather |
|
102 // "260" -> Star Wars: Episode IV - A New Hope (1977) |
|
103 |
|
104 //favourites(ratings_map, "912").length // => 80 |
|
105 |
|
106 // That means there are 80 users that recommend the movie with ID 912. |
|
107 // Of these 80 users, 55 gave a good rating to movie 858 and |
|
108 // 52 a good rating to movies 260, 318, 593. |
|
109 |
|
110 |
|
111 |
|
112 // (5) Implement a suggestions function which takes a rating |
|
113 // map and a movie_name as arguments. It calculates all the recommended |
|
114 // movies sorted according to the most frequently suggested movie(s) first. |
|
115 |
|
116 //def suggestions(recs: Map[String, List[String]], |
|
117 // mov_name: String) : List[String] = ... |
|
118 |
|
119 |
|
120 // testcases |
|
121 //----------- |
|
122 |
|
123 //suggestions(ratings_map, "912") |
|
124 //suggestions(ratings_map, "912").length |
|
125 // => 4110 suggestions with List(858, 260, 318, 593, ...) |
|
126 // being the most frequently suggested movies |
|
127 |
|
128 |
|
129 |
|
130 // (6) Implement a recommendations function which generates at most |
|
131 // *two* of the most frequently suggested movies. It ReTurns the |
|
132 // actual movie names, not the movieIDs. |
|
133 |
|
134 |
|
135 //def recommendations(recs: Map[String, List[String]], |
|
136 // movs: Map[String, String], |
|
137 // mov_name: String) : List[String] = ... |
|
138 |
|
139 |
|
140 |
|
141 // testcases |
|
142 //----------- |
|
143 // recommendations(ratings_map, movies_map, "912") |
|
144 // => List(Godfather, Star Wars: Episode IV - A NewHope (1977)) |
|
145 |
|
146 //recommendations(ratings_map, movies_map, "260") |
|
147 // => List(Star Wars: Episode V - The Empire Strikes Back (1980), |
|
148 // Star Wars: Episode VI - Return of the Jedi (1983)) |
|
149 |
|
150 // recommendations(ratings_map, movies_map, "2") |
|
151 // => List(Lion King, Jurassic Park (1993)) |
|
152 |
|
153 // recommendations(ratings_map, movies_map, "0") |
|
154 // => Nil |
|
155 |
|
156 // recommendations(ratings_map, movies_map, "1") |
|
157 // => List(Shawshank Redemption, Forrest Gump (1994)) |
|
158 |
|
159 // recommendations(ratings_map, movies_map, "4") |
|
160 // => Nil (there are three ratings for this movie in ratings.csv but they are not positive) |
|
161 |
|
162 |
|
163 // If you want to calculate the recommendations for all movies, |
|
164 // then use this code (it will take a few seconds calculation time). |
|
165 |
|
166 //val all = for (name <- movie_names.map(_._1)) yield { |
|
167 // recommendations(ratings_map, movies_map, name) |
|
168 //} |
|
169 |
|
170 // helper functions |
|
171 //List().take(2 |
|
172 //List(1).take(2) |
|
173 //List(1,2).take(2) |
|
174 //List(1,2,3).take(2) |
|
175 |
|
176 |