Пример #1
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
Пример #3
0
    def get_rating_on_context(self, user, item, context, threshold):

        neighbour_context = self.user_dictionary[user].item_contexts[item]
        context_similarity = context_utils.get_context_similarity(
            context, neighbour_context, self.topic_indices)

        if context_similarity < threshold:
            return None

        return self.user_dictionary[user].item_ratings[item]
Пример #4
0
    def get_rating_on_context(self, user, item, context, threshold):

        neighbour_context = self.user_dictionary[user].item_contexts[item]
        context_similarity = context_utils.get_context_similarity(
            context, neighbour_context, self.topic_indices)

        if context_similarity < threshold:
            return None

        return self.user_dictionary[user].item_ratings[item]
    def calculate_user_similarity(self, user1, user2, threshold):

        common_items = extractor.get_common_items(
            self.user_dictionary, user1, user2)

        if not common_items:
            return None

        filtered_items = {}

        for item in common_items:
            context1 = self.user_dictionary[user1].item_contexts[item]
            context2 = self.user_dictionary[user2].item_contexts[item]
            context_similarity = context_utils.get_context_similarity(
                context1, context2, self.context_rich_topics)
            if context_similarity > threshold:
                filtered_items[item] = context_similarity

        numerator = 0
        denominator1 = 0
        denominator2 = 0
        denominator3 = 0
        user1_average = self.user_dictionary[user1].average_overall_rating
        user2_average = self.user_dictionary[user2].average_overall_rating

        for item in filtered_items.keys():
            context_similarity = filtered_items[item]
            user1_rating = self.user_dictionary[user1].item_ratings[item]
            user2_rating = self.user_dictionary[user2].item_ratings[item]

            numerator +=\
                (user1_rating - user1_average) *\
                (user2_rating - user2_average) *\
                context_similarity
            denominator1 += (user1_rating - user1_average) ** 2
            denominator2 += (user2_rating - user2_average) ** 2
            denominator3 += context_similarity ** 2

        denominator = math.sqrt(denominator1 * denominator2 * denominator3)

        if denominator == 0:
            return None

        return numerator / denominator
Пример #6
0
    def calculate_user_similarity(self, user1, user2, threshold):

        common_items = extractor.get_common_items(self.user_dictionary, user1,
                                                  user2)

        if not common_items:
            return None

        filtered_items = {}

        for item in common_items:
            context1 = self.user_dictionary[user1].item_contexts[item]
            context2 = self.user_dictionary[user2].item_contexts[item]
            context_similarity = context_utils.get_context_similarity(
                context1, context2, self.context_rich_topics)
            if context_similarity > threshold:
                filtered_items[item] = context_similarity

        numerator = 0
        denominator1 = 0
        denominator2 = 0
        denominator3 = 0
        user1_average = self.user_dictionary[user1].average_overall_rating
        user2_average = self.user_dictionary[user2].average_overall_rating

        for item in filtered_items.keys():
            context_similarity = filtered_items[item]
            user1_rating = self.user_dictionary[user1].item_ratings[item]
            user2_rating = self.user_dictionary[user2].item_ratings[item]

            numerator +=\
                (user1_rating - user1_average) *\
                (user2_rating - user2_average) *\
                context_similarity
            denominator1 += (user1_rating - user1_average)**2
            denominator2 += (user2_rating - user2_average)**2
            denominator3 += context_similarity**2

        denominator = math.sqrt(denominator1 * denominator2 * denominator3)

        if denominator == 0:
            return None

        return numerator / denominator
Пример #7
0
    def calculate_neighbour_contribution(
            self, neighbour_id, item_id, context, threshold):

        neighbour_rating = self.user_baseline_calculator.get_rating_on_context(
            neighbour_id, item_id, context, threshold)
        neighbor_average =\
            self.user_baseline_calculator.calculate_user_baseline(
                neighbour_id, context, threshold)
        neighbour_context =\
            self.user_baseline_calculator.user_dictionary[neighbour_id].item_contexts[item_id]
        context_similarity = context_utils.get_context_similarity(
            context, neighbour_context, self.user_baseline_calculator.topic_indices)

        if not neighbour_rating:
            return None
        if not neighbor_average:
            return None

        return (neighbour_rating - neighbor_average) * context_similarity
Пример #8
0
    def calculate_neighbour_contribution(self, neighbour_id, item_id, context,
                                         threshold):

        neighbour_rating = self.user_baseline_calculator.get_rating_on_context(
            neighbour_id, item_id, context, threshold)
        neighbor_average =\
            self.user_baseline_calculator.calculate_user_baseline(
                neighbour_id, context, threshold)
        neighbour_context =\
            self.user_baseline_calculator.user_dictionary[neighbour_id].item_contexts[item_id]
        context_similarity = context_utils.get_context_similarity(
            context, neighbour_context,
            self.user_baseline_calculator.topic_indices)

        if not neighbour_rating:
            return None
        if not neighbor_average:
            return None

        return (neighbour_rating - neighbor_average) * context_similarity