def predict_category(client, name: str, lang: str) -> Optional[Tuple[str, float]]: if lang not in SUPPORTED_LANG: return None preprocessed_name = preprocess_name(name, lang) results = match(client, preprocessed_name, lang) hits = results["hits"]["hits"] if hits: hit = hits[0] return hit["_source"]["id"], hit["_score"] return None
def predict_category(client, name: str, lang: str) -> Optional[Tuple[str, float]]: """Predict category from product name using ES. Lang is not used to filter category index, but to stem the name in the right way. :param elasticsearch.Elasticsearch client: ES client :param name: name of the product :param lang: language of the name :return: None if language not supported or no guess """ if lang not in SUPPORTED_LANG: return None preprocessed_name = preprocess_name(name, lang) results = match(client, preprocessed_name, lang) hits = results["hits"]["hits"] if hits: hit = hits[0] return hit["_source"]["id"], hit["_score"] return None
def test_preprocess_name(name: str, lang: str, expected: str): assert preprocess_name(name, lang) == expected