Skip to content

Commit

Permalink
fix: count ingredients with specified percent (#9272)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephanegigandet authored Nov 16, 2023
1 parent 675225d commit 65ef06c
Show file tree
Hide file tree
Showing 56 changed files with 195 additions and 0 deletions.
67 changes: 67 additions & 0 deletions lib/ProductOpener/Ingredients.pm
Original file line number Diff line number Diff line change
Expand Up @@ -2968,6 +2968,7 @@ sub estimate_ingredients_percent_service ($product_ref, $updated_product_fields_
# Add a percent_max value for salt and sugar ingredients, based on the nutrition facts.
add_percent_max_for_ingredients_from_nutrition_facts($product_ref);

# Compute the min and max range for each ingredient
if (compute_ingredients_percent_min_max_values(100, 100, $product_ref->{ingredients}) < 0) {

# The computation yielded seemingly impossible values, delete the values
Expand All @@ -2978,6 +2979,23 @@ sub estimate_ingredients_percent_service ($product_ref, $updated_product_fields_
$product_ref->{ingredients_percent_analysis} = 1;
}

# Count ingredients with specified percent
my ($ingredients_n, $ingredients_with_specified_percent_n, $total_specified_percent)
= count_ingredients_with_specified_percent($product_ref->{ingredients});
if ($ingredients_with_specified_percent_n > 0) {
add_tag($product_ref, "misc", "en:some-ingredients-with-specified-percent");
if ($ingredients_with_specified_percent_n == $ingredients_n) {
add_tag($product_ref, "misc", "en:all-ingredients-with-specified-percent");
}
if ($ingredients_with_specified_percent_n >= 5) {
add_tag($product_ref, "misc", "en:at-least-5-ingredients-with-specified-percent");
if ($ingredients_with_specified_percent_n >= 10) {
add_tag($product_ref, "misc", "en:at-least-10-ingredients-with-specified-percent");
}
}
}

# Estimate the percent values for each ingredient for which we don't have a specified percent
compute_ingredients_percent_estimates(100, $product_ref->{ingredients});

# Indicate which fields were created or updated
Expand All @@ -2987,6 +3005,55 @@ sub estimate_ingredients_percent_service ($product_ref, $updated_product_fields_
return;
}

=head2 count_ingredients_with_specified_percent($product_ref)
Count ingredients with specified percent, including sub-ingredients.
=head3 Return values
=head4 $ingredients_n
Number of ingredients.
=head4 $ingredients_with_specified_percent_n
Number of ingredients with a specified percent value.
=head4 $total_specified_percent
Sum of the specified percent values.
Note: this can be greater than 100 if percent values are specified for ingredients and their sub ingredients.
=cut

sub count_ingredients_with_specified_percent ($ingredients_ref) {

my ($ingredients_n, $ingredients_with_specified_percent_n, $total_specified_percent) = (0, 0, 0);

if (defined $ingredients_ref) {
foreach my $ingredient_ref (@{$ingredients_ref}) {
$ingredients_n++;
if (defined $ingredient_ref->{percent}) {
$ingredients_with_specified_percent_n++;
$total_specified_percent += $ingredient_ref->{percent};
}
if (defined $ingredient_ref->{ingredients}) {
my (
$sub_ingredients_n,
$sub_ingredients_with_specified_percent_n,
$sub_ingredients_total_specified_percent
) = count_ingredients_with_specified_percent($ingredient_ref->{ingredients});
$ingredients_n += $sub_ingredients_n;
$ingredients_with_specified_percent_n += $sub_ingredients_with_specified_percent_n;
$total_specified_percent += $sub_ingredients_total_specified_percent;
}
}
}

return ($ingredients_n, $ingredients_with_specified_percent_n, $total_specified_percent);
}

=head2 delete_ingredients_percent_values ( ingredients_ref )
This function deletes the percent_min and percent_max values of all ingredients.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -842,6 +842,8 @@
"lc" : "en",
"minerals_tags" : [],
"misc_tags" : [
"en:some-ingredients-with-specified-percent",
"en:all-ingredients-with-specified-percent",
"en:nutriscore-computed",
"en:nutrition-fruits-vegetables-nuts-estimate-from-ingredients",
"en:nutrition-all-nutriscore-values-known",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,7 @@
],
"lc" : "fr",
"misc_tags" : [
"en:some-ingredients-with-specified-percent",
"en:packagings-number-of-components-0",
"en:packagings-not-complete",
"en:packagings-empty",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,8 @@
],
"lc" : "fr",
"misc_tags" : [
"en:some-ingredients-with-specified-percent",
"en:all-ingredients-with-specified-percent",
"en:packagings-number-of-components-3",
"en:packagings-not-complete",
"en:packagings-not-empty-but-not-complete",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,8 @@
],
"lc" : "fr",
"misc_tags" : [
"en:some-ingredients-with-specified-percent",
"en:all-ingredients-with-specified-percent",
"en:packagings-number-of-components-3",
"en:packagings-not-complete",
"en:packagings-not-empty-but-not-complete",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,7 @@
"known_ingredients_n" : 10,
"lc" : "en",
"misc_tags" : [
"en:some-ingredients-with-specified-percent",
"en:packagings-number-of-components-0",
"en:packagings-not-complete",
"en:packagings-empty",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,7 @@
"known_ingredients_n" : 10,
"lc" : "en",
"misc_tags" : [
"en:some-ingredients-with-specified-percent",
"en:packagings-number-of-components-0",
"en:packagings-not-complete",
"en:packagings-empty",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,7 @@
"known_ingredients_n" : 10,
"lc" : "en",
"misc_tags" : [
"en:some-ingredients-with-specified-percent",
"en:packagings-number-of-components-0",
"en:packagings-not-complete",
"en:packagings-empty",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,7 @@
"known_ingredients_n" : 10,
"lc" : "en",
"misc_tags" : [
"en:some-ingredients-with-specified-percent",
"en:packagings-number-of-components-0",
"en:packagings-not-complete",
"en:packagings-empty",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,7 @@
"known_ingredients_n" : 10,
"lc" : "en",
"misc_tags" : [
"en:some-ingredients-with-specified-percent",
"en:packagings-number-of-components-0",
"en:packagings-not-complete",
"en:packagings-empty",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,7 @@
"known_ingredients_n" : 10,
"lc" : "en",
"misc_tags" : [
"en:some-ingredients-with-specified-percent",
"en:packagings-number-of-components-0",
"en:packagings-not-complete",
"en:packagings-empty",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@
"ingredients_without_ciqual_codes_n" : 3,
"known_ingredients_n" : 10,
"lc" : "en",
"misc_tags" : [
"en:some-ingredients-with-specified-percent"
],
"nutriments" : {
"fruits-vegetables-legumes-estimate-from-ingredients_100g" : 50,
"fruits-vegetables-legumes-estimate-from-ingredients_serving" : 50,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@
"ingredients_without_ciqual_codes_n" : 1,
"known_ingredients_n" : 10,
"lc" : "en",
"misc_tags" : [
"en:some-ingredients-with-specified-percent"
],
"nutriments" : {
"fruits-vegetables-legumes-estimate-from-ingredients_100g" : 85,
"fruits-vegetables-legumes-estimate-from-ingredients_serving" : 85,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@
"ingredients_without_ciqual_codes_n" : 1,
"known_ingredients_n" : 5,
"lc" : "en",
"misc_tags" : [
"en:some-ingredients-with-specified-percent",
"en:all-ingredients-with-specified-percent"
],
"nutriments" : {
"fruits-vegetables-legumes-estimate-from-ingredients_100g" : 42.5287356321839,
"fruits-vegetables-legumes-estimate-from-ingredients_serving" : 42.5287356321839,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,10 @@
"ingredients_without_ciqual_codes_n" : 2,
"known_ingredients_n" : 24,
"lc" : "es",
"misc_tags" : [
"en:some-ingredients-with-specified-percent",
"en:at-least-5-ingredients-with-specified-percent"
],
"nutriments" : {
"fruits-vegetables-legumes-estimate-from-ingredients_100g" : 77.125,
"fruits-vegetables-legumes-estimate-from-ingredients_serving" : 77.125,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@
"ingredients_without_ciqual_codes_n" : 4,
"known_ingredients_n" : 13,
"lc" : "fi",
"misc_tags" : [
"en:some-ingredients-with-specified-percent"
],
"nutriments" : {
"fruits-vegetables-legumes-estimate-from-ingredients_100g" : 0,
"fruits-vegetables-legumes-estimate-from-ingredients_serving" : 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,9 @@
"ingredients_without_ciqual_codes_n" : 12,
"known_ingredients_n" : 26,
"lc" : "fi",
"misc_tags" : [
"en:some-ingredients-with-specified-percent"
],
"nutriments" : {
"fruits-vegetables-legumes-estimate-from-ingredients_100g" : 0,
"fruits-vegetables-legumes-estimate-from-ingredients_serving" : 0,
Expand Down
4 changes: 4 additions & 0 deletions tests/unit/expected_test_results/ingredients/fi-percents.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@
"ingredients_without_ciqual_codes_n" : 1,
"known_ingredients_n" : 7,
"lc" : "fi",
"misc_tags" : [
"en:some-ingredients-with-specified-percent",
"en:all-ingredients-with-specified-percent"
],
"nutriments" : {
"fruits-vegetables-legumes-estimate-from-ingredients_100g" : 100,
"fruits-vegetables-legumes-estimate-from-ingredients_serving" : 100,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,9 @@
"ingredients_without_ciqual_codes_n" : 12,
"known_ingredients_n" : 25,
"lc" : "fr",
"misc_tags" : [
"en:some-ingredients-with-specified-percent"
],
"nutriments" : {
"fruits-vegetables-legumes-estimate-from-ingredients_100g" : 0,
"fruits-vegetables-legumes-estimate-from-ingredients_serving" : 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,10 @@
"ingredients_without_ciqual_codes_n" : 19,
"known_ingredients_n" : 71,
"lc" : "fr",
"misc_tags" : [
"en:some-ingredients-with-specified-percent",
"en:at-least-5-ingredients-with-specified-percent"
],
"nutriments" : {
"fruits-vegetables-legumes-estimate-from-ingredients_100g" : 6.5,
"fruits-vegetables-legumes-estimate-from-ingredients_serving" : 6.5,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,12 @@
"ingredients_without_ciqual_codes_n" : 4,
"known_ingredients_n" : 24,
"lc" : "fr",
"misc_tags" : [
"en:some-ingredients-with-specified-percent",
"en:all-ingredients-with-specified-percent",
"en:at-least-5-ingredients-with-specified-percent",
"en:at-least-10-ingredients-with-specified-percent"
],
"nutriments" : {
"fruits-vegetables-legumes-estimate-from-ingredients_100g" : 0,
"fruits-vegetables-legumes-estimate-from-ingredients_serving" : 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@
"ingredients_without_ciqual_codes_n" : 5,
"known_ingredients_n" : 18,
"lc" : "fr",
"misc_tags" : [
"en:some-ingredients-with-specified-percent"
],
"nutriments" : {
"fruits-vegetables-legumes-estimate-from-ingredients_100g" : 0,
"fruits-vegetables-legumes-estimate-from-ingredients_serving" : 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@
"ingredients_without_ciqual_codes_n" : 2,
"known_ingredients_n" : 12,
"lc" : "fr",
"misc_tags" : [
"en:some-ingredients-with-specified-percent"
],
"nutriments" : {
"fruits-vegetables-legumes-estimate-from-ingredients_100g" : 100,
"fruits-vegetables-legumes-estimate-from-ingredients_serving" : 100,
Expand Down
4 changes: 4 additions & 0 deletions tests/unit/expected_test_results/ingredients/fr-percents.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@
"ingredients_without_ciqual_codes_n" : 1,
"known_ingredients_n" : 7,
"lc" : "fr",
"misc_tags" : [
"en:some-ingredients-with-specified-percent",
"en:all-ingredients-with-specified-percent"
],
"nutriments" : {
"fruits-vegetables-legumes-estimate-from-ingredients_100g" : 100,
"fruits-vegetables-legumes-estimate-from-ingredients_serving" : 100,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@
"ingredients_without_ciqual_codes_n" : 0,
"known_ingredients_n" : 11,
"lc" : "fr",
"misc_tags" : [
"en:some-ingredients-with-specified-percent"
],
"nutriments" : {
"fruits-vegetables-legumes-estimate-from-ingredients_100g" : 85,
"fruits-vegetables-legumes-estimate-from-ingredients_serving" : 85,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@
"ingredients_without_ciqual_codes_n" : 1,
"known_ingredients_n" : 5,
"lc" : "fr",
"misc_tags" : [
"en:some-ingredients-with-specified-percent",
"en:all-ingredients-with-specified-percent"
],
"nutriments" : {
"fruits-vegetables-legumes-estimate-from-ingredients_100g" : 42.5287356321839,
"fruits-vegetables-legumes-estimate-from-ingredients_serving" : 42.5287356321839,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@
"ingredients_without_ciqual_codes_n" : 2,
"known_ingredients_n" : 8,
"lc" : "fr",
"misc_tags" : [
"en:some-ingredients-with-specified-percent"
],
"nutriments" : {
"fruits-vegetables-legumes-estimate-from-ingredients_100g" : 0,
"fruits-vegetables-legumes-estimate-from-ingredients_serving" : 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@
"ingredients_without_ciqual_codes_n" : 3,
"known_ingredients_n" : 10,
"lc" : "fr",
"misc_tags" : [
"en:some-ingredients-with-specified-percent",
"en:all-ingredients-with-specified-percent"
],
"nutriments" : {
"fruits-vegetables-legumes-estimate-from-ingredients_100g" : 46.8503937007874,
"fruits-vegetables-legumes-estimate-from-ingredients_serving" : 46.8503937007874,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@
"en:french-pork"
],
"lc" : "fr",
"misc_tags" : [
"en:some-ingredients-with-specified-percent"
],
"nutriments" : {
"fruits-vegetables-legumes-estimate-from-ingredients_100g" : 40,
"fruits-vegetables-legumes-estimate-from-ingredients_serving" : 40,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@
"ingredients_without_ciqual_codes_n" : 1,
"known_ingredients_n" : 10,
"lc" : "en",
"misc_tags" : [
"en:some-ingredients-with-specified-percent",
"en:all-ingredients-with-specified-percent",
"en:at-least-5-ingredients-with-specified-percent"
],
"nutriments" : {
"fruits-vegetables-legumes-estimate-from-ingredients_100g" : 90,
"fruits-vegetables-legumes-estimate-from-ingredients_serving" : 90,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@
"ingredients_without_ciqual_codes_n" : 2,
"known_ingredients_n" : 7,
"lc" : "en",
"misc_tags" : [
"en:some-ingredients-with-specified-percent",
"en:all-ingredients-with-specified-percent"
],
"nutriments" : {
"fruits-vegetables-legumes-estimate-from-ingredients_100g" : 0,
"fruits-vegetables-legumes-estimate-from-ingredients_serving" : 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@
"lc" : "en",
"minerals_tags" : [],
"misc_tags" : [
"en:some-ingredients-with-specified-percent",
"en:nutrition-not-enough-data-to-compute-nutrition-score",
"en:nutriscore-missing-nutrition-data",
"en:nutriscore-missing-nutrition-data-energy",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
"lc" : "en",
"minerals_tags" : [],
"misc_tags" : [
"en:some-ingredients-with-specified-percent",
"en:nutriscore-computed",
"en:nutrition-fruits-vegetables-nuts-estimate-from-ingredients",
"en:nutrition-all-nutriscore-values-known",
Expand Down
Loading

0 comments on commit 65ef06c

Please sign in to comment.