Your IP : 216.73.217.13


Current Path : /home/deltalab/PMS/recommendations/user_profiling/components/
Upload File :
Current File : //home/deltalab/PMS/recommendations/user_profiling/components/ProfilingUsers.py

from collections import defaultdict

from _library import profiling_utils

class ProfilingUsers:
    def __init__(self, orders):
        self.orders = orders
        self.orders.sort_values(by = 'customer_id', inplace = True)
        
    def mine_orders(self):
        
        # Initialize user profiles
        self.user_profiles = defaultdict(lambda: defaultdict(set))
        
        # Mine the orders
        miner_func = lambda df_row: profiling_utils.retrive_userCategories(df_row, self.user_profiles)
        self.orders.apply(miner_func, axis = 1)

        customers_consents = self.orders.drop_duplicates(subset=['customer_id'])
        customers_consents = customers_consents[['customer_id','consenso_dati_particolari']]
        
        for user_profile_key in self.user_profiles:
            # ATTRIBUTE: Weight
            min_weight = int(min(self.user_profiles[user_profile_key]['product_weights']))
            max_weight = int(max(self.user_profiles[user_profile_key]['product_weights']))
            self.user_profiles[user_profile_key]['product_weights'] = {'min': min_weight, 'max': max_weight}                              

            


            user_products_info = self.orders[self.orders['SKU'].isin(self.user_profiles[user_profile_key]['unique_products'])].drop_duplicates(subset=['SKU'])

            # ATTRIBUTE: is adult

            self.user_profiles[user_profile_key]['adult'] = 1 if any(p == 1 for p in user_products_info['consenso_adult'].values) else 0

            # ATTRIBUTE: purchased biologic, vegan, biodinamic products as percentage

            #-1 = no consenso per dati particolari
            self.user_profiles[user_profile_key]['bio_percentage'] = -1
            self.user_profiles[user_profile_key]['biodinamic_percentage'] = -1
            self.user_profiles[user_profile_key]['vegan_percentage'] = -1
            self.user_profiles[user_profile_key]['gluten_free_percentage'] = -1

            if(customers_consents.loc[customers_consents['customer_id'] == 1]['consenso_dati_particolari'].values[0] is not None):
                user_purchased_bio_percentage = sum(user_products_info['biologic']) / len(user_products_info['biologic'])
                user_purchased_vegan_percentage = sum(user_products_info['vegan']) / len(user_products_info['vegan'])
                user_purchased_biodinamic_percentage = sum(user_products_info['biodinamic']) / len(user_products_info['biodinamic'])
                user_purchased_gluten_free = sum(user_products_info['gluten_free']) / len(user_products_info['gluten_free'])

                self.user_profiles[user_profile_key]['bio_percentage'] = user_purchased_bio_percentage
                self.user_profiles[user_profile_key]['biodinamic_percentage'] = user_purchased_biodinamic_percentage
                self.user_profiles[user_profile_key]['vegan_percentage'] = user_purchased_vegan_percentage
                self.user_profiles[user_profile_key]['gluten_free_percentage'] = user_purchased_gluten_free

        # Unknown customer id --> no user linked to the order
        if -1 in self.user_profiles.keys():
            self.user_profiles.pop(-1)
        return self.user_profiles