예제 #1
0
def print_recommend(target_rating: Rating, movies: MovieDict,
                    ratings: UserRatingDict,
                    movie_users: MovieUserDict) -> None:
    """Print recommendations from movies for a user with target_rating, using
    data from ratings and movie_users.
    """
    results = student.recommend_movies(target_rating, movies, ratings,
                                       movie_users, 5)
    print("Watched:")
    for movie_id in target_rating:
        print(movie_id, movies[movie_id][0])
    print("Recommend:")
    for movie_id in results:
        print(movie_id, movies[movie_id][0])
    print()
예제 #2
0
assert isinstance(result, dict), \
    TYPECHECK_FEEBACK.format('get_similar_users', 'dict', type(result))
for key in result:
    assert isinstance(key, int), \
        TYPECHECK_FEEBACK.format('get_similar_users', 'dict with int keys', 'dict with ' + type(key) + 'keys')
    assert isinstance(result[key], float), \
        TYPECHECK_FEEBACK.format('get_similar_users', 'dict with float values', 'dict with ' + type(result[key]) + ' values')

assert USER_RATING_DICT_SMALL_COPY == rf.USER_RATING_DICT_SMALL, \
       'get_similar_users should not mutate the parameter'
assert MOVIE_USER_DICT_SMALL_COPY == rf.MOVIE_USER_DICT_SMALL, \
       'get_similar_users should not mutate the parameter'

# Type check recommender_functions.recommend_movies
result = rf.recommend_movies({302156: 4.5}, rf.MOVIE_DICT_SMALL,
                             rf.USER_RATING_DICT_SMALL,
                             rf.MOVIE_USER_DICT_SMALL, 2)
assert isinstance(result, list), \
    TYPECHECK_FEEBACK.format('recommend_movies', 'list', type(result))
for item in result:
    assert isinstance(item, int), \
        TYPECHECK_FEEBACK.format('recommend_movies', 'list of int', 'list of ' + type(item))
assert MOVIE_DICT_SMALL_COPY == rf.MOVIE_DICT_SMALL, \
       'recommend_movies should not mutate the parameter'
assert MOVIE_USER_DICT_SMALL_COPY == rf.MOVIE_USER_DICT_SMALL, \
       'recommend_movies should not mutate the parameter'

builtins.print = our_print
builtins.input = our_input

print(
예제 #3
0
def recommend_interactive(movies: MovieDict,
                          ratings: UserRatingDict,
                          movie_users: MovieUserDict) -> None:
    """Recommend movies based on user input using data from movies, rating,
    and movie_users.
    """

    print("This script uses the code you wrote to recommend movies.")
    print("To begin, rate at least one movie using the 'rate' command.")
    print("You will need to know the movie ID in order to rate a movie.")
    print("You can use the 'search' command to search for a movie's ID.")
    
    command = None # user input command
    while command != 'no':
        user_ratings = {}
        while command != 'recommend':
            print()
            print("Type 'search', 'rate' or 'recommend' to search for a " +
                  "movie, rate a movie, or see recommendations.")
            command = input("Command: ")
            if command == 'search':
                print("What movie would you like to search for?")
                query = input("Search: ")
                query = query.strip()
                results = search_movie(query, movies)
                if len(results) > 0:
                    print("Search results:")
                    for k, v in results:
                        print("Movie Id: " + str(k) + ", Title: " + v)
                else:
                    print("There are no movies with that title.")
            if command == 'rate':
                print("What movie ID do you wish to rate?")
                mid = input("Movie ID: ")
                mid = int(mid)
                if mid not in movies:
                    print("That movie id does not exist.")
                else:
                    title = movies[mid][0]
                    print("What is your rating (0.5 - 5.0) for " + title + "?")
                    rating = input("Rating: ")
                    rating = float(rating)
                    if 0.5 <= rating <= 5:
                        user_ratings[mid] = rating
                    else:
                        print("Rating should be a decimal number between " +
                              "0.5 and 5.0")
            if command == 'recommend':
                if len(user_ratings) == 0:
                    print("You must rate at least one movie to use the " +
                          "recommender.")
                    command = None # to not exit the while loop
        
        print()
        print("You have rated the following movies:")
        for movie, rating in user_ratings.items():
            print(movies[movie][0] + "    -- " + str(rating))
        print()
        print("Here are your top 5 recommendations:")
        results = student.recommend_movies(user_ratings,
                                           ratings, 
                                           movie_users,
                                           5)
        for movie_id in results:
            print(movie_id, movies[movie_id][0])

        print()
        print("Would you like to run another recommendation?")
        command = input("Type 'no' to exit: ")