From d9660c6e713bd2b578d6d8c6dab232d75add5994 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Bournhonesque?= Date: Wed, 31 Jan 2024 10:38:37 +0100 Subject: [PATCH] fix: fix extreme weight detection for multi-packaging (#1298) --- robotoff/prediction/ocr/product_weight.py | 5 +++ .../prediction/ocr/test_product_weight.py | 36 +++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/robotoff/prediction/ocr/product_weight.py b/robotoff/prediction/ocr/product_weight.py index 9d2edc6d26..65a5d37fc9 100644 --- a/robotoff/prediction/ocr/product_weight.py +++ b/robotoff/prediction/ocr/product_weight.py @@ -198,6 +198,11 @@ def process_multi_packaging(match) -> Optional[dict]: return None normalized_value, normalized_unit = normalize_weight(value, unit) + + # Check that the weight is not extreme + if is_extreme_weight(normalized_value, normalized_unit): + return None + text = f"{count} x {value} {unit}" result = { "text": text, diff --git a/tests/unit/prediction/ocr/test_product_weight.py b/tests/unit/prediction/ocr/test_product_weight.py index 47796b92e0..96df0b6615 100644 --- a/tests/unit/prediction/ocr/test_product_weight.py +++ b/tests/unit/prediction/ocr/test_product_weight.py @@ -164,6 +164,42 @@ def test_is_suspicious_weight(value: float, unit: str, expected: bool): ), ], ), + ( + "10 x 60g", + [ + Prediction( + type=PredictionType.product_weight, + data={ + "raw": "10 x 60g", + "unit": "g", + "count": "10", + "value": "60", + "notify": False, + "priority": 2, + "matcher_type": "multi_packaging", + "normalized_unit": "g", + "normalized_value": 60, + "automatic_processing": True, + }, + value_tag=None, + value="10 x 60 g", + automatic_processing=True, + predictor="regex", + predictor_version="1", + barcode=None, + timestamp=None, + source_image=None, + id=None, + confidence=None, + server_type=ServerType.off, + ), + ], + ), + # Extreme weight should not trigger a prediction + ( + "50 x 50kg", + [], + ), ], ) def test_find_product_weight(text: str, expected: list[dict]):