def calculate_users_similarity(self, user_dictionary, user1, user2): common_items = extractor.get_common_items(user_dictionary, user1, user2) if not common_items: return None if self._min_common_items is not None and len( common_items) < self._min_common_items: return None user1_overall_ratings = user_dictionary[user1].item_ratings user1_multi_ratings = user_dictionary[user1].item_multi_ratings user2_overall_ratings = user_dictionary[user1].item_ratings user2_multi_ratings = user_dictionary[user2].item_multi_ratings similarity_sum = 0. for item in common_items: user1_item_ratings = list(user1_multi_ratings[item]) user1_item_ratings.insert(0, user1_overall_ratings[item]) user2_item_ratings = list(user2_multi_ratings[item]) user2_item_ratings.insert(0, user2_overall_ratings[item]) similarity_sum += similarity_calculator.calculate_similarity( user1_item_ratings, user2_item_ratings, self._similarity_metric) similarity = similarity_sum / len(common_items) return similarity
def calculate_users_similarity(self, user_dictionary, user1, user2): common_items = extractor.get_common_items(user_dictionary, user1, user2) if not common_items: return None if self._min_common_items is not None and len( common_items) < self._min_common_items: return None user1_overall_ratings =\ extractor.get_user_ratings(user_dictionary, user1, common_items) user1_multi_ratings =\ extractor.get_user_multi_ratings(user_dictionary, user1, common_items) user2_overall_ratings =\ extractor.get_user_ratings(user_dictionary, user2, common_items) user2_multi_ratings =\ extractor.get_user_multi_ratings(user_dictionary, user2, common_items) num_criteria = len(user1_multi_ratings[0]) total_similarity = 0. for i in xrange(0, num_criteria): user1_criterion_item_ratings =\ extractor.get_matrix_column(user1_multi_ratings, i) user2_criterion_item_ratings =\ extractor.get_matrix_column(user2_multi_ratings, i) total_similarity += similarity_calculator.calculate_similarity( user1_criterion_item_ratings, user2_criterion_item_ratings, self._similarity_metric) # We also add the overall similarity total_similarity += similarity_calculator.calculate_similarity( user1_overall_ratings, user2_overall_ratings, self._similarity_metric) average_similarity = total_similarity / (num_criteria + 1) return average_similarity
def calculate_users_similarity(self, user_dictionary, user_id1, user_id2): """ Calculates the similarity between two users based on how similar are their ratings in the reviews :param user_id1: the ID of user 1 :param user_id2: the ID of user 2 :return: a float with the similarity between the two users. Since this function is based on euclidean distance to calculate the similarity, a similarity of 0 indicates that the users share exactly the same tastes """ user_weights1 = user_dictionary[user_id1].criteria_weights user_weights2 = user_dictionary[user_id2].criteria_weights return similarity_calculator.calculate_similarity( user_weights1, user_weights2, self._similarity_metric)
def calculate_users_similarity(self, user_dictionary, user1, user2): common_items = extractor.get_common_items(user_dictionary, user1, user2) if not common_items: return None if self._min_common_items is not None and len( common_items) < self._min_common_items: return None user1_ratings =\ extractor.get_user_ratings(user_dictionary, user1, common_items) user2_ratings =\ extractor.get_user_ratings(user_dictionary, user2, common_items) similarity_value = similarity_calculator.calculate_similarity( user1_ratings, user2_ratings, self._similarity_metric) return similarity_value