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