def test_similarity_list(): """ Tests the calculation of the similarity of two products based on a 'list' attribute. """ list1 = ["a", "b", "c"] list2 = ["b", "c", "d", "e"] similarity = pm.compute_similarity_for_list(list1, list2) nose.tools.ok_(abs(similarity - 2/3) < tests.FLOAT_DELTA, "Wrong list similarity") similarity = pm.compute_similarity_for_list(list2, list1) # intentionally asymmetric nose.tools.ok_(abs(similarity - 1/2) < tests.FLOAT_DELTA, "Wrong list 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