Пример #1
0
 def __init__(self):
     """
     Author: Marten Bolin / John Lidquist
     Date: 2017-11-10
     Last update:
     Purpose:
     The constructor of the RetrieveTopTrending class, creates a retrieve trendingscore
     and retrievemovie to fetch movie info and trending info.
     """
     self.trender = RetrieveTrending()
     self.movie_getter = RetrieveMovie()
Пример #2
0
def get_new_users_matrix():
    """
    Author: Gustaf Norberg
    Date: 2017-11-06
    Last update: 2017-11-14 by Alexander Dahl
    Purpose: returns the new users matrix. The matrix is 10 % of the user ratings.
    Is used for showing that model is evolving

    :return: new users matrix in the form of a numpy matrix
    """

    user_list = []
    movie_list = []
    rating_list = []

    ratings = RetrieveRating().retrieve_ratings()

    counter = 0
    # Puts every 10th row (10, 20, 30...) in new_users_matrix
    for rating in ratings:
        if counter % 10 == 0:
            user_list.append(rating.user_id)
            movie_list.append(rating.movie_id)
            rating_list.append(rating.rating)
        counter += 1
    # TODO Change dimensions to greater than 1 if problem with dimensions
    new_users_matrix = coo_matrix(
        (rating_list, (user_list, movie_list)),
        shape=(RetrieveUser().retrieve_largest_user_id() + 1,
               RetrieveMovie().retrieve_largest_movie_id() + 1))

    return new_users_matrix
Пример #3
0
def get_test_matrix():
    """
    Author: Gustaf Norberg / Alexander Dahl
    Date: 2017-11-06
    Last update: 2017-11-14 by Alexander Dahl
    Purpose:
    returns the test matrix. The matrix is 10% of the user ratings at the moment

    :return: test matrix in the form of a numpy matrix
    """
    test_user_list = []
    test_movie_list = []
    test_rating_list = []

    ratings = RetrieveRating().retrieve_ratings()

    counter = 0
    # Puts every 10th row (5, 15, 25...) in test_matrix
    for rating in ratings:
        if counter % 5 == 0 and counter % 2 == 1:
            test_user_list.append(rating.user_id)
            test_movie_list.append(rating.movie_id)
            test_rating_list.append(rating.rating)
        counter += 1
    # TODO Change dimensions to greater than 1 if problem with dimensions
    test_matrix = coo_matrix(
        (test_rating_list, (test_user_list, test_movie_list)),
        shape=(RetrieveUser().retrieve_largest_user_id() + 1,
               RetrieveMovie().retrieve_largest_movie_id() + 1))
    return test_matrix
Пример #4
0
def get_train_matrix():
    """
    Author: Marten Bolin / John Lidquist
    Date: 2017-10-02
    Last update: 2017-11-14 by Alexander Dahl
    Purpose:
    returns the train matrix. The matrix is 80% (4/5) of the user ratings at the moment
    OBS! coo_matrix is a sparse matrix and will (most likely) have the same
    dimensions for train_matrix, test_matrix and new_user_matrix

    :return: training matrix in the form of a numpy matrix
    """

    user_list = []
    movie_list = []
    rating_list = []
    ratings = RetrieveRating().retrieve_ratings()

    counter = 0
    # Puts everything but every 5th row (1, 2, 3, 4, 6, 7, 8, 9, 11...) in train_matrix
    for rating in ratings:
        if counter % 5 != 0:
            user_list.append(rating.user_id)
            movie_list.append(rating.movie_id)
            rating_list.append(rating.rating)
        counter += 1

    # Added +1 because else the matrix will be to small
    # TODO Change dimensions to greater than 1 if problem with dimensions
    train_matrix = coo_matrix(
        (rating_list, (user_list, movie_list)),
        shape=(RetrieveUser().retrieve_largest_user_id() + 1,
               RetrieveMovie().retrieve_largest_movie_id() + 1))
    return train_matrix
Пример #5
0
    def insert_feedback(user_id, movie_id, watched=None, rating=None):
        """
        Author: Alexander Dahl, Marten Bolin
        Date: 2017-11-17
        Last update:
        Purpose: Make Insert of Feedback to the database.
        if a feedback has a rating this will be used to evolve the lightFM model.
        :param user_id : The id of the user that has made the rating or watched
        :type int
        :param movie_id : The id of the movie that has been rated or watched
        :type int
        :param watched : 1 if has watched (optional)
        :type int
        :param rating : the rating that was made, 1-5 (optional)
        :type float
        """
        InsertFeedback().insert_feedback(user_id, movie_id, watched, rating)

        if rating:
            model = generate_model.load_model(generate_model.get_path())
            # Converting to lists because otherwise it is not possible to convert them to
            # coo matrices
            rating_list = [rating]
            user_list = [user_id]
            movie_list = [movie_id]

            # Need to define shape so that it is coherent with the previous model
            # TODO Change dimensions to greater than 1 if problem with dimensions
            user_matrix = sp.coo_matrix(
                (rating_list, (user_list, movie_list)),
                shape=(RetrieveUser().retrieve_largest_user_id() + 1,
                       RetrieveMovie().retrieve_largest_movie_id() + 1))
            generate_model.evolve_model(generate_model.get_path(), model,
                                        user_matrix)
def test_retrieve_movie():
    """
    Author: John Andree Lidquist
    Date: 2017-11-16
    Last Updated:
    Purpose: Assert that a movie, or all movies, are retrieved correctly
    """

    # PRE-CONDITIONS
    movie_id = -1
    movie_title = "dummy"
    movie_year = 1111

    # We create a session and add a dummy movie that we can later retrieve
    session = create_session()
    dummy_movie = Movie(id=movie_id, title=movie_title, year=movie_year)
    session.add(dummy_movie)
    session.commit(
    )  # We need to close the session, else we get an error when trying to delete it
    session.close()
    # EXPECTED OUTPUT
    expected_id = movie_id
    expected_title = movie_title
    expected_year = movie_year

    # OBSERVED OUTPUT
    # We call the method to be tested to get 1) The movie we added above, and 2) All the movies
    # which is done by not setting the parameter "movie_id"
    retrieve_movie = RetrieveMovie()
    observed_one_movie = retrieve_movie.retrieve_movie(movie_id=movie_id)
    observed_all_movies = retrieve_movie.retrieve_movie()

    # After adding the dummy movie  we remove them again.
    session.delete(observed_one_movie)
    session.commit()
    session.close()

    assert observed_one_movie
    assert observed_one_movie.id == expected_id
    assert observed_one_movie.title == expected_title
    assert observed_one_movie.year == expected_year
    assert observed_all_movies
Пример #7
0
def get_movie_title(movie_id):
    """
    Author: Alexander Dahl
    Date: 2017-11-01
    Last update: 2017-11-13
    Purpose: returns the movie title from a movie id input

    :param movie_id:
    :return: movie name as string
    """
    return RetrieveMovie().retrieve_movie(movie_id).title
 def _run(self):
     """
     Author: Marten Bolin
     Date:2017-11-22
     Last update:
     Purpose: The process that is ran, checks for updates and updates model
     """
     # Get current state
     current_number_of_users = len(RetrieveUser().retrieve_all_users())
     current_number_of_movies = len(RetrieveMovie().retrieve_movie())
     current_number_of_ratings = len(RetrieveRating().retrieve_ratings())
     # Checks rating first due to most likely to change
     if (self.number_of_ratings == current_number_of_ratings
             and self.number_of_users == current_number_of_users
             and self.number_of_movies == current_number_of_movies):
         print("Nothing new, no changes made.")
     else:
         print("Changes detected, adjusting model")
         CreateNewModel.create_new_model()
         self.number_of_users = len(RetrieveUser().retrieve_all_users())
         self.number_of_movies = len(RetrieveMovie().retrieve_movie())
         self.number_of_ratings = len(RetrieveRating().retrieve_ratings())
Пример #9
0
class RetrieveTopTrending:
    """
    Author: Marten Bolin / John Lidquist
    Date: 2017-11-10
    Last update: 2017-11-13 by Marten Bolin
    Purpose:
    Its subclasses should be used to retrieve the top trending content
    """
    def __init__(self):
        """
        Author: Marten Bolin / John Lidquist
        Date: 2017-11-10
        Last update:
        Purpose:
        The constructor of the RetrieveTopTrending class, creates a retrieve trendingscore
        and retrievemovie to fetch movie info and trending info.
        """
        self.trender = RetrieveTrending()
        self.movie_getter = RetrieveMovie()

    def get_title_and_score(self, list_of_trending_movies):
        """
        Author: Marten Bolin / John Lidquist
        Date: 2017-11-10
        Last update:
        Purpose:
        Used to take the movie id and retrieve its title and score. This is then put into seperate
        lists and put into the TopTrendingList class.
        :param list_of_trending_movies: the list of movie ids
        :return: a TopTrendingList
        """

        title_list = []
        score_list = []
        for entry in list_of_trending_movies:
            movie = self.movie_getter.retrieve_movie(entry.movie_id)
            title_list.append(movie.title)
            score_list.append(self.get_score(entry))
        return TopTrendingList(title_list, score_list)

    def get_score(self, entry):
        """
        Author: Marten Bolin / John Lidquist
        Date: 2017-11-10
        Last update:
        Purpose:
        This method gets overridden in RetrieveTopTrendingYoutube/Twitter
         so that the right score is added
        """
        return entry.total_score