Skip to content

Commit

Permalink
taxonomy: taking into account the ingredient origin when retrieving t…
Browse files Browse the repository at this point in the history
…he corresponding Ecobalyse code (#11108)

### What

Goal : being able to take into account the origin when retrieving the
corresponding ecobalyse code for a given ingredient

---------

Co-authored-by: Stéphane Gigandet <stephane@openfoodfacts.org>
Co-authored-by: Open Food Facts Bot <contact@openfoodfacts.org>
Co-authored-by: Alex Garel <alex@openfoodfacts.org>
  • Loading branch information
4 people authored Dec 12, 2024
1 parent 455f552 commit 83eee67
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 8 deletions.
57 changes: 56 additions & 1 deletion lib/ProductOpener/Ingredients.pm
Original file line number Diff line number Diff line change
Expand Up @@ -3458,7 +3458,7 @@ sub get_missing_ciqual_codes ($ingredients_ref) {

=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)
Assign a ecobalyse_code or a ecobalyse_proxy_code to ingredients and sub ingredients. (NOTE : this is a first version that'll soon be improved)
=head3 Arguments
Expand Down Expand Up @@ -3495,10 +3495,36 @@ sub get_missing_ecobalyse_ids ($ingredients_ref) {

# List of suffixes
my @suffixes = ();
# If the ingredient is both organic and French...
if ( (defined $ingredient_ref->{labels})
and ($ingredient_ref->{labels} =~ /\ben:organic\b/)
and (defined $ingredient_ref->{origins})
and (get_geographical_area($ingredient_ref->{origins}) eq "fr"))
{
push @suffixes, "_labels_en_organic_origins_en_france";
push @suffixes, "_labels_en_organic_origins_en_european_union";
}
# If the ingredient is both organic and European...
if ( (defined $ingredient_ref->{labels})
and ($ingredient_ref->{labels} =~ /\ben:organic\b/)
and (defined $ingredient_ref->{origins})
and (get_geographical_area($ingredient_ref->{origins}) eq "eu"))
{
push @suffixes, "_labels_en_organic_origins_en_european_union";
}
# If the ingredient is organic...
if ((defined $ingredient_ref->{labels}) and ($ingredient_ref->{labels} =~ /\ben:organic\b/)) {
push @suffixes, "_labels_en_organic";
}
# If the ingredient is French...
if ((defined $ingredient_ref->{origins}) and (get_geographical_area($ingredient_ref->{origins}) eq "fr")) {
push @suffixes, "_origins_en_france";
push @suffixes, "_origins_en_european_union";
}
# If the ingredient is European...
if ((defined $ingredient_ref->{origins}) and (get_geographical_area($ingredient_ref->{origins}) eq "eu")) {
push @suffixes, "_origins_en_european_union";
}
push @suffixes, '';

# First try an exact match, and then a proxy match
Expand Down Expand Up @@ -3535,6 +3561,35 @@ sub get_missing_ecobalyse_ids ($ingredients_ref) {
return @ingredients_without_ecobalyse_ids;
}

=head2 get_geographical_area ($originid)
Retrieve the geographical area for ecobalyse. (NOTE : this is a first version that'll soon be improved)
=head3 Arguments
=head4 $originid
reference to the name of the country
=head3 Return values
=head4 $ecobalyse_area
=cut

sub get_geographical_area ($originid) {
# Getting information about the country
my $ecobalyse_area = "";
if (get_inherited_property("countries", $originid, "ecobalyse_is_part_of_eu") eq "yes") {
$ecobalyse_area = "eu";
}
if ($originid eq "en:france") {
$ecobalyse_area = "fr";
}

return $ecobalyse_area;
}

=head2 estimate_ingredients_percent_service ( $product_ref, $updated_product_fields_ref, $errors_ref )
Compute minimum and maximum percent ranges and percent estimates for each ingredient and sub ingredient.
Expand Down
1 change: 1 addition & 0 deletions stop_words.txt
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ dont
dropdown
du
ecoscore
ecobalyse
Ecobalyse
EAN
EANs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@
"ingredients" : [
{
"ciqual_food_code" : "20009",
"ecobalyse_code" : "carrot-non-eu",
"ecobalyse_code" : "carrot-fr",
"id" : "en:carrot",
"is_in_taxonomy" : 1,
"origins" : "en:france",
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/expected_test_results/ecoscore/carrots.json
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@
"ingredients" : [
{
"ciqual_food_code" : "20009",
"ecobalyse_code" : "carrot-non-eu",
"ecobalyse_code" : "carrot-fr",
"id" : "en:carrot",
"is_in_taxonomy" : 1,
"origins" : "en:france",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@
},
{
"ciqual_proxy_food_code" : "28205",
"ecobalyse_code" : "cooked-ham",
"ecobalyse_code" : "cooked-ham-fr",
"id" : "en:cooked-ham",
"is_in_taxonomy" : 1,
"origins" : "en:france",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@
"ingredients" : [
{
"ciqual_food_code" : "13000",
"ecobalyse_code" : "apricot-eu",
"ecobalyse_code" : "apricot-fr",
"id" : "en:apricot",
"is_in_taxonomy" : 1,
"origins" : "en:france",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@
"ingredients" : [
{
"ciqual_food_code" : "13000",
"ecobalyse_code" : "apricot-eu",
"ecobalyse_code" : "apricot-fr",
"id" : "en:apricot",
"is_in_taxonomy" : 1,
"origins" : "en:france",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"ingredients" : [
{
"ciqual_food_code" : "13014",
"ecobalyse_code" : "strawberry-non-eu",
"ecobalyse_code" : "strawberry-fr-offseason",
"id" : "en:strawberry",
"is_in_taxonomy" : 1,
"origins" : "en:france",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
},
{
"ciqual_proxy_food_code" : "28205",
"ecobalyse_code" : "cooked-ham",
"ecobalyse_code" : "cooked-ham-fr",
"id" : "en:cooked-ham",
"is_in_taxonomy" : 1,
"origins" : "en:france",
Expand Down

0 comments on commit 83eee67

Please sign in to comment.