Пример #1
0
    def get_user_neighbours(self, user, item):

        sim_users_matrix = self.similarity_matrix[user].copy()
        sim_users_matrix.pop(user, None)

        # We remove the users who have not rated the given item
        sim_users_matrix = {
            k: v for k, v in sim_users_matrix.items()
            if item in self.ratings_matrix[k]}

        # We remove neighbours that don't have a similarity with user
        sim_users_matrix = {
            k: v for k, v in sim_users_matrix.items()
            if v}

        # print(sim_users_matrix)

        # Sort the users by similarity
        neighbourhood = dictionary_utils.sort_dictionary_keys(
            sim_users_matrix)  # [:self.num_neighbors]

        if self.num_neighbors:
            neighbourhood = neighbourhood[:self.num_neighbors]

        # print('neighbourhood', neighbourhood)
        # print('neighbourhood size:', len(neighbourhood))

        return neighbourhood
Пример #2
0
    def get_neighbourhood(self, user, item, context, threshold):

        all_users = self.user_ids[:]
        all_users.remove(user)
        neighbours = []

        # We remove the users who have not rated the given item
        for neighbour in all_users:
            if item in self.user_dictionary[neighbour].item_ratings:
                neighbours.append(neighbour)

        neighbour_similarity_map = {}
        for neighbour in neighbours:
            neighbour_context =\
                self.user_dictionary[neighbour].item_contexts[item]
            context_similarity = context_utils.get_context_similarity(
                context, neighbour_context, self.topic_indices)
            if context_similarity > threshold:
                neighbour_similarity_map[neighbour] = context_similarity

        # Sort the users by similarity
        neighbourhood = dictionary_utils.sort_dictionary_keys(
            neighbour_similarity_map)  # [:self.num_neighbors]

        if self.num_neighbours is None:
            return neighbourhood

        return neighbourhood
    def get_neighbourhood(self, user, item, context, threshold):

        all_users = self.user_ids[:]
        all_users.remove(user)
        neighbours = []

        # We remove the users who have not rated the given item
        for neighbour in all_users:
            if item in self.user_dictionary[neighbour].item_ratings:
                neighbours.append(neighbour)

        neighbour_similarity_map = {}
        for neighbour in neighbours:
            neighbour_context =\
                self.user_dictionary[neighbour].item_contexts[item]
            context_similarity = context_utils.get_context_similarity(
                context, neighbour_context, self.topic_indices)
            if context_similarity > threshold:
                neighbour_similarity_map[neighbour] = context_similarity

        # Sort the users by similarity
        neighbourhood = dictionary_utils.sort_dictionary_keys(
            neighbour_similarity_map)  # [:self.num_neighbors]

        if self.num_neighbours is None:
            return neighbourhood

        return neighbourhood
    def get_neighbourhood(self, user_id):

        cluster_name = self.user_dictionary[user_id].cluster
        cluster_users = list(self.user_cluster_dictionary[cluster_name])
        cluster_users.remove(user_id)

        # We remove the given user from the cluster in order to avoid bias
        if self._num_neighbors is None:
            return cluster_users

        similarity_matrix = self.user_similarity_matrix[user_id].copy()
        similarity_matrix.pop(user_id, None)
        ordered_similar_users = dictionary_utils.sort_dictionary_keys(similarity_matrix)

        intersection_set = set.intersection(set(ordered_similar_users), set(cluster_users))
        intersection_lst = [t for t in ordered_similar_users if t in intersection_set]

        return intersection_lst  # [:self._num_neighbors]
Пример #5
0
    def get_neighbourhood(self, user_id, item_id):

        # if self._num_neighbors is None:
        #     neighbourhood = list(self.user_ids)
        #     neighbourhood.remove(user_id)
        #     return neighbourhood

        similarity_matrix = self.user_similarity_matrix[user_id].copy()
        similarity_matrix.pop(user_id, None)

        # We remove the users who have not rated the given item
        similarity_matrix = {
            k: v for k, v in similarity_matrix.items()
            if item_id in self.user_dictionary[k].item_ratings}

        # Sort the users by similarity
        neighbourhood = dictionary_utils.sort_dictionary_keys(
            similarity_matrix)  # [:self._num_neighbors]

        return neighbourhood
Пример #6
0
    def get_neighbourhood2(self, user, item, context, threshold):

        sim_users_matrix = self.similarity_matrix[user].copy()
        sim_users_matrix.pop(user, None)

        # We remove the users who have not rated the given item
        sim_users_matrix = {
            k: v
            for k, v in sim_users_matrix.items()
            if item in self.ratings_matrix[k]
        }

        # We remove neighbours that don't have a similarity with user
        sim_users_matrix = {k: v for k, v in sim_users_matrix.items() if v}

        # Sort the users by similarity
        neighbourhood = dictionary_utils.sort_dictionary_keys(sim_users_matrix)
        if self.num_neighbors:
            return neighbourhood[:self.num_neighbors]

        return neighbourhood
Пример #7
0
    def get_neighbourhood(self, user_id, item_id):

        # if self._num_neighbors is None:
        #     neighbourhood = list(self.user_ids)
        #     neighbourhood.remove(user_id)
        #     return neighbourhood

        similarity_matrix = self.user_similarity_matrix[user_id].copy()
        similarity_matrix.pop(user_id, None)

        # We remove the users who have not rated the given item
        similarity_matrix = {
            k: v
            for k, v in similarity_matrix.items()
            if item_id in self.user_dictionary[k].item_ratings
        }

        # Sort the users by similarity
        neighbourhood = dictionary_utils.sort_dictionary_keys(
            similarity_matrix)  # [:self._num_neighbors]

        return neighbourhood
Пример #8
0
    def get_neighbourhood(self, user_id):

        cluster_name = self.user_dictionary[user_id].cluster
        cluster_users = list(self.user_cluster_dictionary[cluster_name])
        cluster_users.remove(user_id)

        # We remove the given user from the cluster in order to avoid bias
        if self._num_neighbors is None:
            return cluster_users

        similarity_matrix = self.user_similarity_matrix[user_id].copy()
        similarity_matrix.pop(user_id, None)
        ordered_similar_users = dictionary_utils.sort_dictionary_keys(
            similarity_matrix)

        intersection_set = set.intersection(set(ordered_similar_users),
                                            set(cluster_users))
        intersection_lst = [
            t for t in ordered_similar_users if t in intersection_set
        ]

        return intersection_lst  # [:self._num_neighbors]
Пример #9
0
    def get_neighbourhood2(self, user, item, context, threshold):

        sim_users_matrix = self.similarity_matrix[user].copy()
        sim_users_matrix.pop(user, None)

        # We remove the users who have not rated the given item
        sim_users_matrix = {
            k: v for k, v in sim_users_matrix.items()
            if item in self.ratings_matrix[k]}

        # We remove neighbours that don't have a similarity with user
        sim_users_matrix = {
            k: v for k, v in sim_users_matrix.items()
            if v}

        # Sort the users by similarity
        neighbourhood = dictionary_utils.sort_dictionary_keys(
            sim_users_matrix)
        if self.num_neighbors:
            return neighbourhood[:self.num_neighbors]

        return neighbourhood
Пример #10
0
    def get_neighbourhood(self, user, item, context, threshold):

        all_users = self.user_ids[:]
        all_users.remove(user)
        neighbours = []

        # We remove the users who have not rated the given item
        for neighbour in all_users:
            if item in self.reviews_matrix[neighbour]:
                neighbours.append(neighbour)

        neighbour_similarity_map = {}
        for neighbour in neighbours:
            neighbour_context = self.context_matrix[neighbour][item]
            context_similarity = get_context_similarity(
                context, neighbour_context, self.context_rich_topics)
            if context_similarity > threshold:
                neighbour_similarity_map[neighbour] = context_similarity

        # Sort the users by similarity
        neighbourhood = dictionary_utils.sort_dictionary_keys(
            neighbour_similarity_map)  # [:self.num_neighbors]

        return neighbourhood
Пример #11
0
    def get_neighbourhood(self, user, item, context, threshold):

        all_users = self.user_ids[:]
        all_users.remove(user)
        neighbours = []

        # We remove the users who have not rated the given item
        for neighbour in all_users:
            if item in self.reviews_matrix[neighbour]:
                neighbours.append(neighbour)

        neighbour_similarity_map = {}
        for neighbour in neighbours:
            neighbour_context = self.context_matrix[neighbour][item]
            context_similarity = get_context_similarity(
                context, neighbour_context, self.context_rich_topics)
            if context_similarity > threshold:
                neighbour_similarity_map[neighbour] = context_similarity

        # Sort the users by similarity
        neighbourhood = dictionary_utils.sort_dictionary_keys(
            neighbour_similarity_map)  # [:self.num_neighbors]

        return neighbourhood