def test_movies_to_users_muti7(self): """ mutiple movie muliple user """ actual = movies_to_users({1: {10: 3.0}, 2: {9: 3.5}}) expected = {10: [1], 9: [2]} self.assertEqual(actual, expected)
def test_movies_to_users(self): """ default test case """ actual = movies_to_users({1: {10: 3.0}, 2: {10: 3.5}}) expected = {10: [1, 2]} self.assertEqual(actual, expected)
def test_movies_to_users_muti6(self): """ mutiple movie and one_user """ actual = movies_to_users({1: {10: 3.0}, 2: {10: 3.5}}) expected = {10: [1, 2]} self.assertEqual(actual, expected)
def test_movies_to_users5(self): """ mutiple movie with no user """ actual = movies_to_users({1: {}, 2: {}}) expected = {} self.assertEqual(actual, expected)
def test_movies_to_users4(self): """ single movie with muliple user """ actual = movies_to_users({1: {10: 3.0, 5: 4.0}}) expected = {10: [1], 5: [1]} self.assertEqual(actual, expected)
def test_movies_to_users3(self): """ single movie one user """ actual = movies_to_users({1: {10: 3.0}}) expected = {10: [1]} self.assertEqual(actual, expected)
def test_movies_to_users2(self): """ single movie no user """ actual = movies_to_users({1: {}}) expected = {} self.assertEqual(actual, expected)
def test_movies_to_users(self): actual = movies_to_users({1: {10: 3.0}, 2: {10: 3.5}}) expected = {10: [1, 2]} self.assertEqual(actual, expected) #test if tere are two movies user_ratings = {1: {11: 3}, 2: {10: 3.5}} expected = {10: [2], 11: [1]} self.assertEqual(movies_to_users(user_ratings), expected) user_ratings = { 1: { 68735: 3.5, 302156: 4.0 }, 2: { 68735: 1.0, 124057: 1.5, 293660: 4.5 }, 3: { 68735: 4.5, 302156: 2.0, 124057: 3.5 }, } actual = movies_to_users(user_ratings) user_set = set() movie_set = set() # test if all movies are included: for ratings in user_ratings.values(): for movie in ratings.keys(): movie_set.add(movie) self.assertEqual(len(actual), len(movie_set)) for movie, user_list in actual.items(): #test if the user list are sorted sort_list = sorted(user_list) self.assertEqual(sort_list, user_list) #test if all users are included: for user in user_list: user_set.add(user) self.assertTrue(movie in user_ratings[user]) self.assertEqual(len(user_ratings), len(user_set))
assert isinstance(key, int), \ TYPECHECK_FEEBACK.format('read_ratings', 'dict with int keys', 'dict with ' + type(key) + ' keys') assert isinstance(result[key], dict), \ TYPECHECK_FEEBACK.format('read_ratings', 'dict with dict values', 'dict with ' + type(result[key]) + ' values') # Type check recommender_functions.remove_unknown_movies small_ratings = {1001: {68735: 5.0, 302156: 3.5, 10: 4.5}, 1002: {11: 3.0}} small_ratings_copy = copy.deepcopy(small_ratings) result = rf.remove_unknown_movies(small_ratings, rf.MOVIE_DICT_SMALL) assert result == None, \ TYPECHECK_FEEBACK.format('remove_unknown_movies', 'None', type(result)) assert small_ratings_copy != small_ratings, \ 'remove_unknown_movies should mutate the parameter' # Type check recommender_functions.movies_to_users result = rf.movies_to_users(rf.USER_RATING_DICT_SMALL) assert isinstance(result, dict), \ TYPECHECK_FEEBACK.format('movies_to_users', 'dict', type(result)) for key in result: assert isinstance(key, int), \ TYPECHECK_FEEBACK.format('movies_to_users', 'dict with int keys', 'dict with ' + type(key) + ' keys') assert isinstance(result[key], list), \ TYPECHECK_FEEBACK.format('movies_to_users', 'dict with list values', 'dict with ' + type(result[key]) + ' values') # Type check recommender_functions.get_users_who_watched result = rf.get_users_who_watched([293660], rf.MOVIE_USER_DICT_SMALL) assert isinstance(result, list), \ TYPECHECK_FEEBACK.format('get_users_who_watched', 'list', type(result)) for item in result: assert isinstance(item, int), \ TYPECHECK_FEEBACK.format('get_users_who_watched', 'list of int', 'list of ' + type(item))
if __name__ == "__main__": print("Reading in a list of movies.") movfile = open('movies.csv', 'r') movies = student.read_movies(movfile) movfile.close() print("Reading in user ratings.") ratingfile = open('ratings_medium.csv', 'r') ratings = student.read_ratings(ratingfile) ratingfile.close() print("Removing ratings for movies we have no data on.") student.remove_unknown_movies(ratings, movies) print("Building movies to users dictionary.") movie_users = student.movies_to_users(ratings) ### You can uncomment these to test recommendations for the following movies print_recommend({663:5, 274:4.5, 745:4.5}, movies, ratings, movie_users) # Should get: [25753, 65, 25769, 74, 82] print_recommend({2109:3, 954:4}, movies, ratings, movie_users) # Should get: [2313, 100, 2049, 2642, 6970] print_recommend({745:5}, movies, ratings, movie_users) # Should get: [25753, 25769, 82, 74, 2348] print_recommend({1262:5}, movies, ratings, movie_users) # Should get: [4515, 2289, 1249, 55687, 2284]
def test_rate_same_movies(self): actual = movies_to_users({1: {2: 2, 10: 3}, 2: {10: 3.5}, 3: {2: 4}}) expected = {2: [1, 3], 10: [1, 2]} self.assertEqual(actual, expected)
def test_same_id(self): actual = movies_to_users({1: {1: 2}, 2: {10: 3.5}}) expected = {10: [2], 1: [1]} self.assertEqual(actual, expected)
def test_two_different_users(self): actual = movies_to_users({1: {2: 2}, 2: {10: 3.5}}) expected = {10: [2], 2: [1]} self.assertEqual(actual, expected)
def test_movies_to_users_empty_sub(self): actual = movies_to_users({1: {}, 2: {10: 3.5}}) expected = {10: [2]} self.assertEqual(actual, expected)
def test_movies_to_users_empty_entire(self): actual = movies_to_users({}) expected = {} self.assertEqual(actual, expected)