diff -r 0e591f806290 -r 8a34b2ebc8cc testing2/danube.scala --- a/testing2/danube.scala Tue Dec 03 11:07:09 2019 +0000 +++ b/testing2/danube.scala Mon Jan 27 10:18:13 2020 +0000 @@ -2,118 +2,114 @@ // at Danube.co.uk //=========================================== -object CW7b { - import io.Source import scala.util._ +object CW7b { // for purposes of generating a jar + // (1) Implement the function get_csv_url which takes an url-string // as argument and requests the corresponding file. The two urls // of interest are ratings_url and movies_url, which correspond // to CSV-files. -// -// The function should ReTurn the CSV-file appropriately broken +// The function should ReTurn the CSV file appropriately broken // up into lines, and the first line should be dropped (that is without -// the header of the CSV-file). The result is a list of strings (lines +// the header of the CSV file). The result is a list of strings (lines // in the file). def get_csv_url(url: String) : List[String] = { - Try(Source.fromURL(url)("UTF-8").mkString.split("\n").toList.tail).getOrElse(List()) + val csv = Source.fromURL(url)("ISO-8859-1") + csv.mkString.split("\n").toList.drop(1) } - val ratings_url = """https://nms.kcl.ac.uk/christian.urban/ratings.csv""" val movies_url = """https://nms.kcl.ac.uk/christian.urban/movies.csv""" -// testcases -//----------- -// val ratings = get_csv_url(ratings_url) -// val movies = get_csv_url(movies_url) +// test cases + +//val ratings = get_csv_url(ratings_url) +//val movies = get_csv_url(movies_url) //ratings.length // 87313 //movies.length // 9742 - - -// (2) Implement two functions that process the CSV-files from (1). The ratings +// (2) Implement two functions that process the CSV files. The ratings // function filters out all ratings below 4 and ReTurns a list of // (userID, movieID) pairs. The movies function just ReTurns a list -// of (movieID, title) pairs. +// of (movieId, title) pairs. def process_ratings(lines: List[String]) : List[(String, String)] = { - val filteredLines = lines.filter(line => line.split(",")(2).toInt >= 4) - filteredLines.map(line => (line.split(",")(0), line.split(",")(1))) + for (cols <- lines.map(_.split(",").toList); + if (cols(2).toFloat >= 4)) yield (cols(0), cols(1)) } def process_movies(lines: List[String]) : List[(String, String)] = { - lines.map(line => (line.split(",")(0), line.split(",")(1))) + for (cols <- lines.map(_.split(",").toList)) yield (cols(0), cols(1)) } +// test cases -// testcases -//----------- -// val good_ratings = process_ratings(ratings) -// val movie_names = process_movies(movies) +//val good_ratings = process_ratings(ratings) +//val movie_names = process_movies(movies) //good_ratings.length //48580 //movie_names.length // 9742 +//============================================== +// Do not change anything below, unless you want +// to submit the file for the advanced part 3! +//============================================== + + +// (3) Implement a grouping function that calulates a map +// containing the userIds and all the corresponding recommendations +// (list of movieIds). This should be implemented in a tail +// recursive fashion, using a map m as accumulator. This map +// is set to Map() at the beginning of the claculation. + +def groupById(ratings: List[(String, String)], + m: Map[String, List[String]]) : Map[String, List[String]] = ratings match { + case Nil => m + case (id, mov) :: rest => { + val old_ratings = m.getOrElse (id, Nil) + val new_ratings = m + (id -> (mov :: old_ratings)) + groupById(rest, new_ratings) + } +} + +// +//val ls = List(("1", "a"), ("2", "a"), ("1", "c"), ("2", "a"), ("1", "c")) +// +//val m = groupById(ls, Map()) +// +//m.getOrElse("1", Nil).count(_ == "c") // => 2 +//m.getOrElse("1", Nil).count(_ == "a") // => 1 + +// test cases +//val ratings_map = groupById(good_ratings, Map()) +//groupById(good_ratings, Map()).get("214") +//groupById(good_ratings, Map()).toList.minBy(_._2.length) +//val movies_map = movie_names.toMap + +//ratings_map.get("414").get.map(movies_map.get(_)) // most prolific recommender with 1227 positive ratings +//ratings_map.get("474").get.map(movies_map.get(_)) // second-most prolific recommender with 787 positive ratings +//ratings_map.get("214").get.map(movies_map.get(_)) // least prolific recommender with only 1 positive rating -// (3) Implement a grouping function that calculates a Map -// containing the userIDs and all the corresponding recommendations -// (list of movieIDs). This should be implemented in a tail -// recursive fashion, using a Map m as accumulator. This Map m -// is set to Map() at the beginning of the calculation. +//(4) Implement a function that takes a ratings map and a movie_name as argument. +// The function calculates all suggestions containing +// the movie mov in its recommendations. It ReTurns a list of all these +// recommendations (each of them is a list and needs to have mov deleted, +// otherwise it might happen we recommend the same movie). -def groupById(ratings: List[(String, String)], - m: Map[String, List[String]]) : Map[String, List[String]] = { - if (ratings.length == 0) m - else { - val firstUser = ratings(0)._1 - val userRatings = ratings.filter(r => r._1 == firstUser) - val movieIds = userRatings.map(r => r._2) - val newMap = m + (firstUser -> movieIds) - groupById(ratings.filter(r => r._1 != firstUser), newMap) - } -} - - -// testcases -//----------- -//val ratings_map = groupById(good_ratings, Map()) -//val movies_map = movie_names.toMap - -//ratings_map.get("414").get.map(movies_map.get(_)) -// => most prolific recommender with 1227 positive ratings - -//ratings_map.get("474").get.map(movies_map.get(_)) -// => second-most prolific recommender with 787 positive ratings - -//ratings_map.get("214").get.map(movies_map.get(_)) -// => least prolific recommender with only 1 positive rating +def favourites(m: Map[String, List[String]], mov: String) : List[List[String]] = + (for (id <- m.keys.toList; + if m(id).contains(mov)) yield m(id).filter(_ != mov)) -// (4) Implement a function that takes a ratings map and a movie_name as argument. -// The function calculates all suggestions containing -// the movie in its recommendations. It ReTurns a list of all these -// recommendations (each of them is a list and needs to have the movie deleted, -// otherwise it might happen we recommend the same movie). - - -def favourites(m: Map[String, List[String]], mov: String) : List[List[String]] = { - val movieLists = m.map(r => r._2).toList.filter(_.contains(mov)) - for (movieList <- movieLists) yield { - movieList.filter(_!=mov) - } -} - - -// testcases -//----------- +// test cases // movie ID "912" -> Casablanca (1942) // "858" -> Godfather // "260" -> Star Wars: Episode IV - A New Hope (1977) @@ -125,53 +121,45 @@ // 52 a good rating to movies 260, 318, 593. +// (5) Implement a suggestions function which takes a rating +// map and a movie_name as arguments. It calculates all the recommended +// movies sorted according to the most frequently suggested movie(s) first. -// (5) Implement a suggestions function which takes a rating -// map and a movie_name as arguments. It calculates all the recommended -// movies sorted according to the most frequently suggested movie(s) first. - +// needed in Scala 2.13. + +def mapValues[S, T, R](m: Map[S, T], f: T => R) = + m.map { case (x, y) => (x, f(y)) } def suggestions(recs: Map[String, List[String]], - mov_name: String) : List[String] = { - val favs = favourites(recs, mov_name).flatten - favs.map(x => (x, favs.count(_==x))) - .sortBy(_._1) - .reverse - .sortBy(_._2) - .reverse - .distinct - .map(_._1) + mov_name: String) : List[String] = { + val favs = favourites(recs, mov_name).flatten + val favs_counted = mapValues(favs.groupBy(identity), (v:List[String]) => v.size).toList + val favs_sorted = favs_counted.sortBy(_._2).reverse + favs_sorted.map(_._1) } +// check +// groupMap is equivalent to groupBy(key).mapValues(_.map(f)) -// testcases -//----------- +// test cases //suggestions(ratings_map, "912") //suggestions(ratings_map, "912").length // => 4110 suggestions with List(858, 260, 318, 593, ...) // being the most frequently suggested movies - - -// (6) Implement a recommendations function which generates at most -// *two* of the most frequently suggested movies. It ReTurns the -// actual movie names, not the movieIDs. - +// (6) Implement recommendations functions which generates at most +// *two* of the most frequently suggested movies. It Returns the +// actual movie names, not the movieIDs. def recommendations(recs: Map[String, List[String]], - movs: Map[String, String], - mov_name: String) : List[String] = { - val sug = suggestions(recs, mov_name) - val toptwo = sug.take(2) - if (toptwo.length == 0) Nil - else toptwo.map(movs(_)) -} - + movs: Map[String, String], + mov_name: String) : List[String] = + suggestions(recs, mov_name).take(2).map(movs.get(_).get) // testcases -//----------- + // recommendations(ratings_map, movies_map, "912") // => List(Godfather, Star Wars: Episode IV - A NewHope (1977)) @@ -189,11 +177,11 @@ // => List(Shawshank Redemption, Forrest Gump (1994)) // recommendations(ratings_map, movies_map, "4") -// => Nil (there are three ratings for this movie in ratings.csv but they are not positive) +// => Nil (there are three ratings fro this movie in ratings.csv but they are not positive) -// If you want to calculate the recommendations for all movies, -// then use this code (it will take a few seconds calculation time). +// If you want to calculate the recomendations for all movies. +// Will take a few seconds calculation time. //val all = for (name <- movie_names.map(_._1)) yield { // recommendations(ratings_map, movies_map, name) @@ -205,32 +193,4 @@ //List(1,2).take(2) //List(1,2,3).take(2) - - } - -// val ratings_url = """https://nms.kcl.ac.uk/christian.urban/ratings.csv""" -// val movies_url = """https://nms.kcl.ac.uk/christian.urban/movies.csv""" - -// val ratings = CW7b.get_csv_url(ratings_url) -// val movies = CW7b.get_csv_url(movies_url) - -// println(movies.length) -// val good_ratings = CW7b.process_ratings(ratings) -// val movie_names = CW7b.process_movies(movies) - -// val ratings_map = CW7b.groupById(good_ratings, Map()) -// val movies_map = movie_names.toMap - - - -//println(CW7b.recommendations(ratings_map, movies_map, "912")) -/* -val ratings_url = """https://nms.kcl.ac.uk/christian.urban/ratings.csv""" - -val ratings = CW7b.get_csv_url(ratings_url) - -val good_ratings = CW7b.process_ratings(ratings) -val ratings_map = CW7b.groupById(good_ratings, Map()) - -println(CW7b.suggestions(ratings_map, "912").length)*/