Пример #1
0
    def obtain_history_decay_factor(self, product_id):
        """ Produces a history decay factor according to some mathematical function of
            the number of previous recommendations of a certain product to a certain user.

            :param product_id: The id of the product.
        """
        if self.history_decay_function_name is None:
            return 1

        if self.user_impressions_summary is not None:
            latest_impressions_count = self.user_impressions_summary.get(product_id, (0, None))[0]
        else:
            latest_impressions_count = 0

        result = 1

        if self.history_decay_function_name == "linear":
            ttl = self.history_decay_linear_function_ttl
            result = df.linear(latest_impressions_count, ttl)
        elif self.history_decay_function_name == "rational":
            result = df.rational(latest_impressions_count)
        elif self.history_decay_function_name == "exponential":
            halflife = self.history_decay_exponential_function_halflife
            result = df.exponential(latest_impressions_count, halflife)
        elif self.history_decay_function_name == "step":
            ttl = self.history_decay_step_function_ttl
            result = df.step(latest_impressions_count, 1, -1, ttl)

        return result
Пример #2
0
    def obtain_product_age_decay_factor(self, product_date, present_date):
        """ Produces an age decay factor according to some mathematical function of the
            number of units of time (days, weeks) since the product was added to the system.

            :param product_date: The date of the product.
            :param present_date: The current date.
        """
        if self.product_age_decay_function_name is None:
            return 1

        product_age_in_days = None
        if product_date is not None:
            try:
                utc_product_date = pytz.utc.localize(product_date)
            except Exception:
                utc_product_date = product_date
            product_age = present_date - utc_product_date
            product_age_in_days = product_age.days

        result = 1

        if self.product_age_decay_function_name == "linear":
            ttl = self.product_age_decay_linear_function_ttl
            result = df.linear(product_age_in_days, ttl)
        elif self.product_age_decay_function_name == "rational":
            result = df.rational(product_age_in_days)
        elif self.product_age_decay_function_name == "exponential":
            halflife = self.product_age_decay_exponential_function_halflife
            result = df.exponential(product_age_in_days, halflife)
        elif self.product_age_decay_function_name == "step":
            ttl = self.product_age_decay_step_function_ttl
            result = df.step(product_age_in_days, 1, -1, ttl)

        return result