Skip to content

Commit

Permalink
fix: error and warning for vegan label with non-vegan ingredients (#9063
Browse files Browse the repository at this point in the history
)
  • Loading branch information
benbenben2 committed Sep 29, 2023
1 parent 495c58f commit 5627ce3
Show file tree
Hide file tree
Showing 3 changed files with 212 additions and 1 deletion.
68 changes: 68 additions & 0 deletions lib/ProductOpener/DataQualityFood.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1373,6 +1373,73 @@ sub check_categories ($product_ref) {
return;
}

=head2 check_labels( PRODUCT_REF )
Checks related to specific product labels.
Vegan label: check that there is no non-vegan ingredient.
Vegetarian label: check that there is no non-vegetarian ingredient.
=cut

sub check_labels ($product_ref) {
# this also include en:vegan that is a child of en:vegetarian
if (defined $product_ref->{labels_tags} && has_tag($product_ref, "labels", "en:vegetarian")) {
if (defined $product_ref->{ingredients}) {
my @ingredients = @{$product_ref->{ingredients}};

while (@ingredients) {

# Remove and process the first ingredient
my $ingredient_ref = shift @ingredients;
my $ingredientid = $ingredient_ref->{id};

# Add sub-ingredients at the beginning of the ingredients array
if (defined $ingredient_ref->{ingredients}) {

unshift @ingredients, @{$ingredient_ref->{ingredients}};
}

# some additives_classes (like thickener, for example) do not have the key-value vegan and vegetarian
# it can be additives_classes that contain only vegan/vegetarian additives.
# to avoid false-positive - instead of raising a warning (else below) we ignore additives_classes
if (!exists_taxonomy_tag("additives_classes", $ingredientid)) {
if (has_tag($product_ref, "labels", "en:vegan")) {
# vegan
if (defined $ingredient_ref->{"vegan"}) {
if ($ingredient_ref->{"vegan"} eq 'no') {
push @{$product_ref->{data_quality_errors_tags}},
"en:vegan-label-but-non-vegan-ingredient";
}
# else 'yes', 'maybe'
}
else {
push @{$product_ref->{data_quality_warnings_tags}},
"en:vegan-label-but-could-not-confirm-for-all-ingredients";
}
}

# vegetarian
if (defined $ingredient_ref->{"vegetarian"}) {
if ($ingredient_ref->{"vegetarian"} eq 'no') {
push @{$product_ref->{data_quality_errors_tags}},
"en:vegetarian-label-but-non-vegetarian-ingredient";
}
# else 'yes', 'maybe'
}
else {
push @{$product_ref->{data_quality_warnings_tags}},
"en:vegetarian-label-but-could-not-confirm-for-all-ingredients";
}
}
}
}
}

return;
}

sub compare_nutriscore_with_value_from_producer ($product_ref) {

if (
Expand Down Expand Up @@ -1572,6 +1639,7 @@ sub check_quality_food ($product_ref) {
check_quantity($product_ref);
detect_categories($product_ref);
check_categories($product_ref);
check_labels($product_ref);
compare_nutriscore_with_value_from_producer($product_ref);
check_ecoscore_data($product_ref);
check_food_groups($product_ref);
Expand Down
20 changes: 20 additions & 0 deletions taxonomies/data_quality.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2857,3 +2857,23 @@ en:Carbon footprint from known ingredients less than from meat or fish
<en:Carbon footprint warnings
en:Carbon footprint from known ingredients more than from meat or fish
#description:en:



### Labels

<en:Data quality errors
en:vegan-label-but-non-vegan-ingredient
description:en:This product has a vegan label but contains non-vegan ingredients.

<en:Data quality errors
en:vegetarian-label-but-non-vegetarian-ingredient
description:en:This product has a vegetarian label but contains non-vegetarian ingredients.

<en:Data quality warnings
en:vegan-label-but-could-not-confirm-for-all-ingredients
description:en:This product has a vegan label, however some ingredients are not known as non-vegan in the taxonomy (could be unknown ingredients or missing vegan true/false in the taxonomy).

<en:Data quality warnings
en:vegetarian-label-but-could-not-confirm-for-all-ingredients
description:en:This product has a vegetarian label, however some ingredients are not known as non-vegetarian in the taxonomy (could be unknown ingredients or missing vegetarian true/false in the taxonomy).
125 changes: 124 additions & 1 deletion tests/unit/dataqualityfood.t
Original file line number Diff line number Diff line change
Expand Up @@ -1094,5 +1094,128 @@ check_quality_and_test_product_has_quality_tag(
'en:ingredients-single-ingredient-from-category-does-not-match-actual-ingredients',
'We expect the ingredient given in the taxonomy for this product', 0
);

# vegan label but non-vegan ingredients
# unknown ingredient -> warnings
$product_ref = {
labels_tags => ["en:vegetarian", "en:vegan",],
ingredients => [
{
id => "en:lentils",
vegan => "yes",
vegetarian => "yes"
},
{
id => "en:green-bell-pepper",
vegan => "yes",
vegetarian => "yes"
},
{
id => "en:totoro",
}
],
};
ProductOpener::DataQuality::check_quality($product_ref);
check_quality_and_test_product_has_quality_tag(
$product_ref,
'en:vegan-label-but-non-vegan-ingredient',
'raise error only when vegan is no and label is vegan', 0
);
check_quality_and_test_product_has_quality_tag(
$product_ref,
'en:vegetarian-label-but-non-vegetarian-ingredient',
'raise error only when vegetarian is no and label is vegan', 0
);
check_quality_and_test_product_has_quality_tag(
$product_ref,
'en:vegan-label-but-could-not-confirm-for-all-ingredients',
'raise warning because vegan or non-vegan is unknown for an ingredient', 1
);
check_quality_and_test_product_has_quality_tag(
$product_ref,
'en:vegetarian-label-but-could-not-confirm-for-all-ingredients',
'raise warning because vegetarian or non-vegetarian is unknown for an ingredient', 1
);
# non-vegan/non-vegetarian ingredient -> error
$product_ref = {
labels_tags => ["en:vegetarian", "en:vegan",],
ingredients => [
{
id => "en:lentils",
vegan => "yes",
vegetarian => "yes"
},
{
id => "en:green-bell-pepper",
vegan => "yes",
vegetarian => "yes"
},
{
id => "en:chicken",
vegan => "no",
vegetarian => "no"
}
],
};
ProductOpener::DataQuality::check_quality($product_ref);
check_quality_and_test_product_has_quality_tag(
$product_ref,
'en:vegan-label-but-non-vegan-ingredient',
'raise error only when vegan is no and label is vegan', 1
);
check_quality_and_test_product_has_quality_tag(
$product_ref,
'en:vegetarian-label-but-non-vegetarian-ingredient',
'raise error only when vegetarian is no and label is vegan', 1
);
check_quality_and_test_product_has_quality_tag(
$product_ref,
'en:vegan-label-but-could-not-confirm-for-all-ingredients',
'raise warning because vegan or non-vegan is unknown for an ingredient', 0
);
check_quality_and_test_product_has_quality_tag(
$product_ref,
'en:vegetarian-label-but-could-not-confirm-for-all-ingredients',
'raise warning because vegetarian or non-vegetarian is unknown for an ingredient', 0
);
# non-vegan/vegatarian ingredient -> error
$product_ref = {
labels_tags => ["en:vegetarian", "en:vegan",],
ingredients => [
{
id => "en:lentils",
vegan => "yes",
vegetarian => "yes"
},
{
id => "en:green-bell-pepper",
vegan => "yes",
vegetarian => "yes"
},
{
id => "en:honey",
vegan => "no",
vegetarian => "yes"
}
],
};
check_quality_and_test_product_has_quality_tag(
$product_ref,
'en:vegan-label-but-non-vegan-ingredient',
'raise error only when vegan is no and label is vegan', 1
);
check_quality_and_test_product_has_quality_tag(
$product_ref,
'en:vegetarian-label-but-non-vegetarian-ingredient',
'raise error only when vegetarian is no and label is vegan', 0
);
check_quality_and_test_product_has_quality_tag(
$product_ref,
'en:vegan-label-but-could-not-confirm-for-all-ingredients',
'raise warning because vegan or non-vegan is unknown for an ingredient', 0
);
check_quality_and_test_product_has_quality_tag(
$product_ref,
'en:vegetarian-label-but-could-not-confirm-for-all-ingredients',
'raise warning because vegetarian or non-vegetarian is unknown for an ingredient', 0
);
done_testing();

0 comments on commit 5627ce3

Please sign in to comment.