Example #1
0
def test_similarity_date():
    """ Tests the calculation of the similarity of two products based on a 'date' attribute.
    """
    date1 = dt.datetime(2000, 11, 24, 10, 0)
    date2 = dt.datetime(2000, 11, 26, 10, 0)
    similarity = pm.compute_similarity_for_date(date1, date2, halflife=2)
    nose.tools.ok_(abs(similarity - 0.5) < tests.FLOAT_DELTA, "Wrong date similarity")
def _process_non_text_attributes_contributions(context, products, strengths):
    """ Adds the contribution of the non-text product attributes to the informed map of strengths.

        :param context: The customer context.
        :param products: A map {product_id: record} where record is a dict {attribute: value}.
        :param strengths: A map {(product, template_product): strength_value} with partially computed strengths
                       (possibly resulting from the contributions of other product attributes). The contribution
                       of the current attribute (whose map of terms by product is given as *tfidf_map*) will be added
                       to this same *strengths* map.
    """
    for (product_id, template_id) in strengths:
        for attr_type, weight_by_attribute in context.similarity_weights_by_type.items():
            if attr_type == pm.TEXT:
                continue

            for attribute, weight in weight_by_attribute.items():
                product_attr_value = products[product_id].get_attribute(attribute)
                template_attr_value = products[template_id].get_attribute(attribute)

                if product_attr_value is None or template_attr_value is None:
                    log.warn("Missing atribute [{0}] value product == {1}, template == {2}".format(
                        attribute, product_attr_value, template_attr_value))
                    continue

                contribution = 0
                if attr_type == pm.NUMERIC:
                    contribution = pm.compute_similarity_for_numeric(product_attr_value, template_attr_value)
                elif attr_type == pm.FIXED:
                    contribution = pm.compute_similarity_for_fixed(product_attr_value, template_attr_value)
                elif attr_type == pm.LIST:
                    contribution = pm.compute_similarity_for_list(product_attr_value, template_attr_value)
                elif attr_type == pm.DATE:
                    contribution = pm.compute_similarity_for_date(product_attr_value, template_attr_value,
                                                                  context.date_similarity_halflife)

                strengths[(product_id, template_id)] += contribution * weight