Your IP : 216.73.217.13


Current Path : /home/deltalab/PMS/recommendations/recommender-system-batch/components/
Upload File :
Current File : //home/deltalab/PMS/recommendations/recommender-system-batch/components/coPurchase_RS.py

from _library.recom_utils import assRules_utils
from _library import toolkit
from _library.data_utils import data_loader

class associationRules_RS:
    
    def __init__(self, platform_products, coPurchase_versionName, product_identifier, load_remotely):
        
        # Load association rules
        association_rules_df, enhanced_association_rules_df = data_loader.load_coPurchases(coPurchase_versionName, product_identifier,load_remotely=load_remotely)
        # Pre-process the loaded data
        if coPurchase_versionName == 'OLDinTrentino':
            name_mapping = {'Linked regions': 'production_areas'}
        else: 
            name_mapping = None
        association_rules_df = assRules_utils.preProcessing_coPurchases(association_rules_df, name_mapping)
        enhanced_association_rules_df = assRules_utils.preProcessing_coPurchases(enhanced_association_rules_df, name_mapping)
        
        # Save the association rules
        self.association_rules_df = association_rules_df
        self.enhanced_association_rules_df = enhanced_association_rules_df
         
        # Save the products of inTrentino
        self.platform_products = platform_products
        
        # Save the product identifier
        self.product_identifier = product_identifier
        self.category_based = True if self.product_identifier == 'Product Type' else False
        
        # Flag to check whether the parameters have been set
        self.flag_params = False
        
    def set_params(self, unique_product_identifier, excluded_products = [], excluded_link_types = [], force_perfect_match = False,
                   hide_raw_scores = True, filter_source_platform = True, verbose = False):
        
        # Set the parameters
        # A list of unique product identifiers.
        self.unique_product_identifier = unique_product_identifier
        self.excluded_products = excluded_products
        self.force_perfect_match = True if self.category_based else force_perfect_match 
        self.excluded_link_types = excluded_link_types
        self.hide_raw_scores = hide_raw_scores
        self.filter_source_platform = filter_source_platform
        self.verbose = verbose
        
        self.flag_params = True
        
            
    def generate_associationRules(self, reference_items, flag_enhanced_assRules = False):
        
        if flag_enhanced_assRules:
            assRules_df = self.enhanced_association_rules_df
        else:
            assRules_df = self.association_rules_df
        
        # Discover the recommendations
        recommended_items = assRules_utils.find_recommendations(assRules_df, 
                                                                self.platform_products, 
                                                                self.product_identifier,
                                                                reference_items, 
                                                                self.excluded_products,
                                                                self.excluded_link_types,
                                                                self.force_perfect_match,
                                                                self.hide_raw_scores, 
                                                                self.filter_source_platform,
                                                                self.verbose)
        
        # Add the name of this method to the recommendations
        recommended_items = toolkit.add_recommendationSource(recommended_items, self.rs_name)
        
        # [IF CATEGORY BASED] If the recommendations are based on categories
        if self.category_based and len(recommended_items) > 0:

            # Fill recommended categories with products 
            recommended_items = assRules_utils.addItems_byCategory(
                recommendations = recommended_items,
                reference_items = reference_items,
                all_products = self.platform_products,
                unique_product_identifier = self.unique_product_identifier,
                filter_source_platform = self.filter_source_platform,
                single_item = self.filter_source_platform)
        
        return recommended_items
   
    def itemWise_assRulesBased_recommendations(self, reference_items, flag_enhanced_assRules):
        
        # Slight artefact
        if (not isinstance(reference_items, list)) or (not isinstance(reference_items, set)):
            reference_items = [reference_items]
        
        # Generate the recommendations
        recommended_items = self.generate_associationRules(reference_items, flag_enhanced_assRules)
        
        return recommended_items
    
    def generate_assRulesBased_recommendations(self, flag_enhanced_assRules):
        
        if not self.flag_params:
            self.set_params()
        
        self.rs_name = 'enhanced'  if flag_enhanced_assRules else 'simple'
        self.rs_name += '_assRules'
        if self.category_based: 
            self.rs_name += '_cat'

        # Pre-processing the products
        reference_products = self.platform_products.apply(
            func = lambda df_row: toolkit.extract_referenceProduct(df_row, self.product_identifier), 
            axis = 1)
        
        # Compute the recommendation for each products
        recommendations = reference_products.apply(
            func = lambda product: self.itemWise_assRulesBased_recommendations(product, flag_enhanced_assRules))
        
        # Improve the data representation
        identifier = 'item_name'
        if self.category_based:
            if self.unique_product_identifier == 'Title':
                identifier = 'product_name'
            elif self.unique_product_identifier == 'SKU':
                identifier = 'sku'
        recommendations.index = reference_products.apply(lambda product: product[identifier])
        recommendations = recommendations.to_dict()

        return recommendations