Skip to content

Commit

Permalink
feat: set value_tag to canonical label value during prediction import
Browse files Browse the repository at this point in the history
  • Loading branch information
raphael0202 committed Oct 27, 2022
1 parent 43d6078 commit 80cf93c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
18 changes: 17 additions & 1 deletion robotoff/insights/importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@
get_product_store,
is_valid_image,
)
from robotoff.taxonomy import Taxonomy, get_taxonomy
from robotoff.taxonomy import (
Taxonomy,
TaxonomyType,
get_taxonomy,
match_taxonomized_value,
)
from robotoff.utils import get_logger, text_file_iter
from robotoff.utils.cache import CachedStore

Expand Down Expand Up @@ -566,6 +571,17 @@ def generate_candidates(
for prediction in predictions
if cls.is_prediction_valid(product, prediction.value_tag) # type: ignore
]
for candidate in candidates:
if candidate.value_tag:
# we normalize `value_tag` to the canonical taxonomy value
# it helps to deal with internationalization
# (`fr:sans-gluten` and `en:no-gluten` is the same tag)
value_tag = match_taxonomized_value(
candidate.value_tag, TaxonomyType.label.name
)
if value_tag is not None:
candidate.value_tag = value_tag

taxonomy = get_taxonomy(InsightType.label.name)
for candidate in select_deepest_taxonomized_candidates(candidates, taxonomy):
insight = ProductInsight(**candidate.to_dict())
Expand Down
12 changes: 12 additions & 0 deletions tests/unit/insights/test_importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -776,6 +776,18 @@ def test_is_parent_label(self, label, to_check_labels, expected, mocker):
Product({"code": DEFAULT_BARCODE, "labels_tags": ["en:vegan"]}),
[("en:ecoveg", False), ("en:max-havelaar", True)],
),
(
# fr:sans-gluten should be normalized into en:no-gluten
[
Prediction(
PredictionType.label,
value_tag="fr:sans-gluten",
automatic_processing=True,
),
],
Product({"code": DEFAULT_BARCODE}),
[("en:no-gluten", True)],
),
],
)
def test_generate_candidates(self, predictions, product, expected, mocker):
Expand Down

0 comments on commit 80cf93c

Please sign in to comment.