Skip to content

Commit

Permalink
taxonomy: a new function assign_property_to_ingredients has been added (
Browse files Browse the repository at this point in the history
#10873)

taxonomy: a new function assign_property_to_ingredients has been added
to get ecobalyse ids

### What

In order to calculate the Ecobalyse's score ("coût environnemental" in
french : environmental cost), we must be able to get the corresponding
ecobalyse codes for every ingredients. Thus, the function
"assign_ciqual_code" has been remodeled in a new function
"assign_properties_to_ingredients" in order to get both ciqual codes and
ecobalyse codes for every ingredients.

---------

Co-authored-by: Stéphane Gigandet <stephane@openfoodfacts.org>
  • Loading branch information
very-smartin and stephanegigandet authored Oct 17, 2024
1 parent 5f93132 commit 7519987
Show file tree
Hide file tree
Showing 212 changed files with 1,490 additions and 7 deletions.
84 changes: 79 additions & 5 deletions lib/ProductOpener/Ingredients.pm
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ BEGIN {
&match_ingredient_origin
&parse_origins_from_text
&assign_ciqual_codes
&assign_property_to_ingredients
&get_ingredients_with_property_value
); # symbols to export on request
Expand Down Expand Up @@ -3013,7 +3013,7 @@ sub extend_ingredients_service ($product_ref, $updated_product_fields_ref) {

# Add Ciqual codes
# Used in particular for ingredients estimation from nutrients
assign_ciqual_codes($product_ref);
assign_property_to_ingredients($product_ref);

return;
}
Expand Down Expand Up @@ -3225,7 +3225,7 @@ sub extract_ingredients_from_text ($product_ref) {
remove_fields(
$product_ref,
[
# assign_ciqual_codes - may have been introduced in previous version
# assign_property_to_ingredients - may have been introduced in previous version
"ingredients_without_ciqual_codes",
"ingredients_without_ciqual_codes_n",
]
Expand Down Expand Up @@ -3264,13 +3264,30 @@ sub extract_ingredients_from_text ($product_ref) {
return;
}

sub assign_ciqual_codes ($product_ref) {

sub assign_property_to_ingredients ($product_ref) {
# If the ingredient list is not defined, the function immediately returns
return if not defined $product_ref->{ingredients};

# ------------------------------------ PART 1 : Getting CIQUAL codes ------------------------------------ #
# Retrieves a unique and sorted list of ingredients missing Ciqual codes
my @ingredients_without_ciqual_codes = uniq(sort(get_missing_ciqual_codes($product_ref->{ingredients})));

# Stores this list in the product under the key 'ingredients_without_ciqual_codes'
$product_ref->{ingredients_without_ciqual_codes} = \@ingredients_without_ciqual_codes;

# Also stores the total number of ingredients without Ciqual codes
$product_ref->{ingredients_without_ciqual_codes_n} = @ingredients_without_ciqual_codes + 0.0;

# ------------------------------------ PART 2 : Getting Ecobalyse ids ------------------------------------ #
# Retrieves a unique and sorted list of ingredients missing Ecobalyse ids
my @ingredients_without_ecobalyse_ids = uniq(sort(get_missing_ecobalyse_ids($product_ref->{ingredients})));

# Stores this list in the product under the key 'ingredients_without_ecobalyse_ids'
$product_ref->{ingredients_without_ecobalyse_ids} = \@ingredients_without_ecobalyse_ids;

# Also stores the total number of ingredients without Ecobalyse ids
$product_ref->{ingredients_without_ecobalyse_ids_n} = @ingredients_without_ecobalyse_ids + 0.0;

return;
}

Expand Down Expand Up @@ -3322,6 +3339,63 @@ sub get_missing_ciqual_codes ($ingredients_ref) {
return @ingredients_without_ciqual_codes;
}

=head2 get_missing_ecobalyse_ids ($ingredients_ref)
Assign a ecobalyse_id or a ciqual_proxy_food_code to ingredients and sub ingredients. (NOTE : this is a first version that'll soon be improved)
=head3 Arguments
=head4 $ingredients_ref
reference to an array of ingredients
=head3 Return values
=head4 @ingredients_without_ecobalyse_ids
=cut

sub get_missing_ecobalyse_ids ($ingredients_ref) {
my @ingredients_without_ecobalyse_ids = ();
foreach my $ingredient_ref (@{$ingredients_ref}) {

# Also add sub-ingredients
if (defined $ingredient_ref->{ingredients}) {
push(@ingredients_without_ecobalyse_ids, get_missing_ecobalyse_ids($ingredient_ref->{ingredients}));
}

# Assign a ecobalyse_code or a ecoalyse_proxy_code to the ingredient
delete $ingredient_ref->{ecobalyse_code};
delete $ingredient_ref->{ecobalyse_proxy_code};

# Getting properties from product
#my $product_origin = has_specific_ingredient_property()

# Getting the correct ecobalyse comake devde
my $ecobalyse_code = get_inherited_property("ingredients", $ingredient_ref->{id}, "ecobalyse:en");
if (defined $ecobalyse_code) {
$ingredient_ref->{ecobalyse_code} = $ecobalyse_code;
}
else {
my $ecobalyse_proxy_code
= get_inherited_property("ingredients", $ingredient_ref->{id}, "ecobalyse_proxy:en");
if (defined $ecobalyse_proxy_code) {
$ingredient_ref->{ecobalyse_proxy_code} = $ecobalyse_proxy_code;
}
else {
push(@ingredients_without_ecobalyse_ids, $ingredient_ref->{id});
}
}

#ecobalyse:en
#ecobalyse_labels_en_organic:en
#ecobalyse_origins_en_france:en
#ecobalyse_origins_en_european_union:en
#ecobalyse_labels_en_organic_origins_en_france:en
}
return @ingredients_without_ecobalyse_ids;
}

=head2 estimate_ingredients_percent_service ( $product_ref, $updated_product_fields_ref )
Compute minimum and maximum percent ranges and percent estimates for each ingredient and sub ingredient.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,7 @@
"ingredients" : [
{
"ciqual_food_code" : "13050",
"ecobalyse_code" : "apple-fr",
"id" : "en:apple",
"is_in_taxonomy" : 1,
"percent_estimate" : 62.5,
Expand All @@ -603,6 +604,7 @@
},
{
"ciqual_proxy_food_code" : "19051",
"ecobalyse_code" : "milk",
"id" : "en:milk",
"is_in_taxonomy" : 1,
"percent_estimate" : 18.75,
Expand All @@ -614,6 +616,7 @@
},
{
"ciqual_food_code" : "22000",
"ecobalyse_code" : "egg-indoor-code3",
"id" : "en:egg",
"is_in_taxonomy" : 1,
"percent_estimate" : 9.375,
Expand All @@ -625,6 +628,7 @@
},
{
"ciqual_food_code" : "16129",
"ecobalyse_code" : "refined-palm-oil",
"from_palm_oil" : "yes",
"id" : "en:palm-oil",
"is_in_taxonomy" : 1,
Expand Down Expand Up @@ -699,6 +703,8 @@
"ingredients_with_unspecified_percent_sum" : 100,
"ingredients_without_ciqual_codes" : [],
"ingredients_without_ciqual_codes_n" : 0,
"ingredients_without_ecobalyse_ids" : [],
"ingredients_without_ecobalyse_ids_n" : 0,
"interface_version_created" : "20150316.jqm2",
"interface_version_modified" : "20150316.jqm2",
"known_ingredients_n" : 10,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,7 @@
"ingredients" : [
{
"ciqual_food_code" : "13050",
"ecobalyse_code" : "apple-fr",
"id" : "en:apple",
"is_in_taxonomy" : 1,
"percent_estimate" : 62.5,
Expand All @@ -603,6 +604,7 @@
},
{
"ciqual_proxy_food_code" : "19051",
"ecobalyse_code" : "milk",
"id" : "en:milk",
"is_in_taxonomy" : 1,
"percent_estimate" : 18.75,
Expand All @@ -614,6 +616,7 @@
},
{
"ciqual_food_code" : "22000",
"ecobalyse_code" : "egg-indoor-code3",
"id" : "en:egg",
"is_in_taxonomy" : 1,
"percent_estimate" : 9.375,
Expand All @@ -625,6 +628,7 @@
},
{
"ciqual_food_code" : "16129",
"ecobalyse_code" : "refined-palm-oil",
"from_palm_oil" : "yes",
"id" : "en:palm-oil",
"is_in_taxonomy" : 1,
Expand Down Expand Up @@ -699,6 +703,8 @@
"ingredients_with_unspecified_percent_sum" : 100,
"ingredients_without_ciqual_codes" : [],
"ingredients_without_ciqual_codes_n" : 0,
"ingredients_without_ecobalyse_ids" : [],
"ingredients_without_ecobalyse_ids_n" : 0,
"interface_version_created" : "20150316.jqm2",
"interface_version_modified" : "20150316.jqm2",
"knowledge_panels" : {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,7 @@
"ingredients" : [
{
"ciqual_food_code" : "13050",
"ecobalyse_code" : "apple-fr",
"id" : "en:apple",
"is_in_taxonomy" : 1,
"percent_estimate" : 62.5,
Expand All @@ -603,6 +604,7 @@
},
{
"ciqual_proxy_food_code" : "19051",
"ecobalyse_code" : "milk",
"id" : "en:milk",
"is_in_taxonomy" : 1,
"percent_estimate" : 18.75,
Expand All @@ -614,6 +616,7 @@
},
{
"ciqual_food_code" : "22000",
"ecobalyse_code" : "egg-indoor-code3",
"id" : "en:egg",
"is_in_taxonomy" : 1,
"percent_estimate" : 9.375,
Expand All @@ -625,6 +628,7 @@
},
{
"ciqual_food_code" : "16129",
"ecobalyse_code" : "refined-palm-oil",
"from_palm_oil" : "yes",
"id" : "en:palm-oil",
"is_in_taxonomy" : 1,
Expand Down Expand Up @@ -699,6 +703,8 @@
"ingredients_with_unspecified_percent_sum" : 100,
"ingredients_without_ciqual_codes" : [],
"ingredients_without_ciqual_codes_n" : 0,
"ingredients_without_ecobalyse_ids" : [],
"ingredients_without_ecobalyse_ids_n" : 0,
"interface_version_created" : "20150316.jqm2",
"interface_version_modified" : "20150316.jqm2",
"known_ingredients_n" : 10,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1248,6 +1248,7 @@
"ingredients" : [
{
"ciqual_food_code" : "13050",
"ecobalyse_code" : "apple-fr",
"id" : "en:apple",
"is_in_taxonomy" : 1,
"percent_estimate" : 62.5,
Expand All @@ -1259,6 +1260,7 @@
},
{
"ciqual_proxy_food_code" : "19051",
"ecobalyse_code" : "milk",
"id" : "en:milk",
"is_in_taxonomy" : 1,
"percent_estimate" : 18.75,
Expand All @@ -1270,6 +1272,7 @@
},
{
"ciqual_food_code" : "22000",
"ecobalyse_code" : "egg-indoor-code3",
"id" : "en:egg",
"is_in_taxonomy" : 1,
"percent_estimate" : 9.375,
Expand All @@ -1281,6 +1284,7 @@
},
{
"ciqual_food_code" : "16129",
"ecobalyse_code" : "refined-palm-oil",
"from_palm_oil" : "yes",
"id" : "en:palm-oil",
"is_in_taxonomy" : 1,
Expand Down Expand Up @@ -1355,6 +1359,8 @@
"ingredients_with_unspecified_percent_sum" : 100,
"ingredients_without_ciqual_codes" : [],
"ingredients_without_ciqual_codes_n" : 0,
"ingredients_without_ecobalyse_ids" : [],
"ingredients_without_ecobalyse_ids_n" : 0,
"interface_version_created" : "20150316.jqm2",
"interface_version_modified" : "20150316.jqm2",
"knowledge_panels" : {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,7 @@
"ingredients" : [
{
"ciqual_food_code" : "13050",
"ecobalyse_code" : "apple-fr",
"id" : "en:apple",
"is_in_taxonomy" : 1,
"percent_estimate" : 62.5,
Expand All @@ -598,6 +599,7 @@
},
{
"ciqual_proxy_food_code" : "19051",
"ecobalyse_code" : "milk",
"id" : "en:milk",
"is_in_taxonomy" : 1,
"percent_estimate" : 18.75,
Expand All @@ -609,6 +611,7 @@
},
{
"ciqual_food_code" : "22000",
"ecobalyse_code" : "egg-indoor-code3",
"id" : "en:egg",
"is_in_taxonomy" : 1,
"percent_estimate" : 9.375,
Expand All @@ -620,6 +623,7 @@
},
{
"ciqual_food_code" : "16129",
"ecobalyse_code" : "refined-palm-oil",
"from_palm_oil" : "yes",
"id" : "en:palm-oil",
"is_in_taxonomy" : 1,
Expand Down Expand Up @@ -694,6 +698,8 @@
"ingredients_with_unspecified_percent_sum" : 100,
"ingredients_without_ciqual_codes" : [],
"ingredients_without_ciqual_codes_n" : 0,
"ingredients_without_ecobalyse_ids" : [],
"ingredients_without_ecobalyse_ids_n" : 0,
"interface_version_created" : "20150316.jqm2",
"interface_version_modified" : "20150316.jqm2",
"known_ingredients_n" : 10,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,7 @@
"ingredients" : [
{
"ciqual_proxy_food_code" : "9410",
"ecobalyse_code" : "flour",
"id" : "en:wheat-flour",
"is_in_taxonomy" : 1,
"percent_estimate" : 62.5,
Expand All @@ -534,6 +535,7 @@
},
{
"ciqual_food_code" : "18066",
"ecobalyse_code" : "tap-water",
"id" : "en:water",
"is_in_taxonomy" : 1,
"percent_estimate" : 18.75,
Expand All @@ -556,6 +558,7 @@
},
{
"ciqual_proxy_food_code" : "31016",
"ecobalyse_code" : "sugar",
"id" : "en:sugar",
"is_in_taxonomy" : 1,
"percent_estimate" : 9.375,
Expand Down Expand Up @@ -621,6 +624,10 @@
"ingredients_with_unspecified_percent_sum" : 100,
"ingredients_without_ciqual_codes" : [],
"ingredients_without_ciqual_codes_n" : 0,
"ingredients_without_ecobalyse_ids" : [
"en:salt"
],
"ingredients_without_ecobalyse_ids_n" : 1,
"interface_version_created" : "20150316.jqm2",
"interface_version_modified" : "20150316.jqm2",
"known_ingredients_n" : 10,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,11 @@
"ingredients_with_unspecified_percent_sum" : 100,
"ingredients_without_ciqual_codes" : [],
"ingredients_without_ciqual_codes_n" : 0,
"ingredients_without_ecobalyse_ids" : [
"en:pork-meat",
"en:salt"
],
"ingredients_without_ecobalyse_ids_n" : 2,
"interface_version_created" : "20150316.jqm2",
"interface_version_modified" : "20150316.jqm2",
"known_ingredients_n" : 5,
Expand Down
Loading

0 comments on commit 7519987

Please sign in to comment.