Skip to content

Commit

Permalink
fix: use origins property from categories for Eco-Score (#9402)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephanegigandet committed Nov 24, 2023
1 parent a956004 commit ae8fd7d
Show file tree
Hide file tree
Showing 138 changed files with 1,096 additions and 12 deletions.
72 changes: 65 additions & 7 deletions lib/ProductOpener/Ecoscore.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1113,6 +1113,12 @@ my @production_system_labels = (
["en:responsible-aquaculture-asc", 10],
);

foreach my $label_ref (@production_system_labels) {

# Canonicalize the label ids in case the normalized id changed
$label_ref->[0] = canonicalize_taxonomy_tag("en", "labels", $label_ref->[0]);
}

sub compute_ecoscore_production_system_adjustment ($product_ref) {

$product_ref->{ecoscore_data}{adjustments}{production_system} = {value => 0, labels => []};
Expand Down Expand Up @@ -1296,6 +1302,28 @@ sub aggregate_origins_of_ingredients ($default_origins_ref, $aggregated_origins_
return;
}

=head2 get_country_origin_from_origins ( $origins_ref )
Given a list of origins, return the country for the first origin that is a country or a child of a country.
=cut

sub get_country_origin_from_origins ($origins_ref) {

foreach my $origin_id (@$origins_ref) {

# If the origin is a child of a country, use the country
my $country_code = get_inherited_property("origins", $origin_id, "country_code_2:en");

if ( (defined $country_code)
and (defined $ecoscore_data{origins}{canonicalize_taxonomy_tag("en", "origins", $country_code)}))
{
return canonicalize_taxonomy_tag("en", "origins", $country_code);
}
}
return;
}

=head2 compute_ecoscore_origins_of_ingredients_adjustment ( $product_ref )
Computes adjustments(bonus or malus for transportation + EPI / Environmental Performance Index)
Expand Down Expand Up @@ -1336,6 +1364,22 @@ sub compute_ecoscore_origins_of_ingredients_adjustment ($product_ref) {
}
}

# Check if we have categories with an origins:en property (e.g. French wines -> origins:en:france)
my @origins_from_categories = ();

if (defined $product_ref->{categories_tags}) {
foreach my $category (@{$product_ref->{categories_tags}}) {
my $origin_id = get_property("categories", $category, "origins:en");
if (defined $origin_id) {
push @origins_from_categories, split(',', $origin_id);
}
}
}
my $origin_from_categories = get_country_origin_from_origins(\@origins_from_categories);
if (defined $origin_from_categories) {
@origins_from_categories = ($origin_from_categories);
}

# If we don't have ingredients, check if we have an origin for a specific ingredient
# (e.g. we have the label "French eggs" even though we don't have ingredients)
if ( (scalar @origins_from_origins_field == 0)
Expand All @@ -1347,28 +1391,41 @@ sub compute_ecoscore_origins_of_ingredients_adjustment ($product_ref) {
}
}

# If we have origins from the origins field and from the categories, we will use the origins from the origins field
my $default_origins_ref = \@origins_from_categories;

if (scalar @origins_from_origins_field == 0) {
@origins_from_origins_field = ("en:unknown");
}
else {
$default_origins_ref = \@origins_from_origins_field;
}

$log->debug("compute_ecoscore_origins_of_ingredients_adjustment - origins field",
{origins_tags => $product_ref->{origins_tags}, origins_from_origins_field => \@origins_from_origins_field})
if $log->is_debug();
if (scalar @origins_from_categories == 0) {
@origins_from_categories = ("en:unknown");
}

$log->debug(
"compute_ecoscore_origins_of_ingredients_adjustment - origins field",
{
origins_tags => $product_ref->{origins_tags},
origins_from_origins_field => \@origins_from_origins_field,
origins_from_categories => \@origins_from_categories
}
) if $log->is_debug();

# Sum the % values/estimates of all ingredients by origins

my %aggregated_origins = ();

if ((defined $product_ref->{ingredients}) and (scalar @{$product_ref->{ingredients}} > 0)) {
aggregate_origins_of_ingredients(\@origins_from_origins_field,
\%aggregated_origins, $product_ref->{ingredients});
aggregate_origins_of_ingredients($default_origins_ref, \%aggregated_origins, $product_ref->{ingredients});
}
else {
# If we don't have ingredients listed, apply the origins from the origins field
# using a dummy ingredient

aggregate_origins_of_ingredients(\@origins_from_origins_field,
\%aggregated_origins, [{percent_estimate => 100}]);
aggregate_origins_of_ingredients($default_origins_ref, \%aggregated_origins, [{percent_estimate => 100}]);
}

# Compute the transportation and EPI values and a sorted list of aggregated origins
Expand Down Expand Up @@ -1412,6 +1469,7 @@ sub compute_ecoscore_origins_of_ingredients_adjustment ($product_ref) {

$product_ref->{ecoscore_data}{adjustments}{origins_of_ingredients} = {
origins_from_origins_field => \@origins_from_origins_field,
origins_from_categories => \@origins_from_categories,
aggregated_origins => \@aggregated_origins,
epi_score => 0 + $epi_score,
epi_value => round($epi_value),
Expand Down
9 changes: 7 additions & 2 deletions taxonomies/categories.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2154,7 +2154,7 @@ bg:Френска бира
de:Französische Biere
es:Cervezas francesas, Cervezas de Francia
fi:ranskalaiset oluet
fr:Bières françaises, Bières de France
fr:Bières françaises, Bières de France, bière française, bière de France
hu:Francia sörök
it:Birre francesi, birre dalla Francia
lt:Prancūziškas alus
Expand Down Expand Up @@ -3791,6 +3791,7 @@ agribalyse_food_code:en:1023
ciqual_food_code:en:1023
ciqual_food_name:en:Spirit made from wine, armagnac or cognac type
ciqual_food_name:fr:Eau de vie de vin, type armagnac, cognac
origins:en: en:France

<en:Armagnac
en:Armagnac-Ténarèze
Expand Down Expand Up @@ -3865,6 +3866,7 @@ agribalyse_food_code:en:1023
ciqual_food_code:en:1023
ciqual_food_name:en:Spirit made from wine, armagnac or cognac type
ciqual_food_name:fr:Eau de vie de vin, type armagnac, cognac
origins:en: en:France

<en:Eaux de vie
en:Swiss Eaux-de-vie
Expand All @@ -3873,6 +3875,7 @@ fr:Eaux-de-vie suisses, Eau-de-vie suisse, Eaux-de-vie de Suisse, Eau-de-vie de
it:Acquaviti svizzere, Acquaviti della Svizzera
nl:Zwitserse eau-de-vies
#wikidata:en:
origins:en: en:Switzerland

<en:Eaux de vie
en:French Eaux-de-vie
Expand All @@ -3881,6 +3884,7 @@ fr:Eaux-de-vie françaises, Eau-de-vie française, Eaux-de-vie de France, Eau-de
it:Acquaviti francesi, Acquaviti della Francia
nl:Franse eau-de-vies
#wikidata:en:
origins:en: en:France

<en:French Eaux-de-vie
en:French cider eau-de-vies
Expand Down Expand Up @@ -10503,7 +10507,7 @@ ca:Vi francès
de:französische Weine
es:Vinos de Francia, vinos franceses
fi:ranskalaiset viinit
fr:Vins français, vin français
fr:Vins français, vin français, vins de France, vin de France
he:יינות מצרפת
hu:Francia borok
it:Vini francesi
Expand All @@ -10517,6 +10521,7 @@ wikidata:en:Q630531
agribalyse_proxy_food_code:en:5215
agribalyse_proxy_food_name:en:Wine, white, dry
agribalyse_proxy_food_name:fr:Vin blanc sec
origins:en: en:France

<en:Wines from France
fr:Agenais
Expand Down
6 changes: 3 additions & 3 deletions taxonomies/nutrient_levels.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1776,7 +1776,7 @@ hu:Só / kis mennyiségben
hy:Salt in low quantity
hz:Salt in low quantity
ia:Salt in low quantity
id:Garam dapur di kuantitas rendah
id:Garam di kuantitas rendah
ie:Salt in low quantity
ig:Ńnú in low quantity
ii:Salt in low quantity
Expand Down Expand Up @@ -1966,7 +1966,7 @@ hu:Só / mérsékelt mennyiségben
hy:Salt in moderate quantity
hz:Salt in moderate quantity
ia:Salt in moderate quantity
id:Garam dapur di kuantitas sedang
id:Garam di kuantitas sedang
ie:Salt in moderate quantity
ig:Ńnú in moderate quantity
ii:Salt in moderate quantity
Expand Down Expand Up @@ -2156,7 +2156,7 @@ hu:Só / nagy mennyiségben
hy:Salt in high quantity
hz:Salt in high quantity
ia:Salt in high quantity
id:Garam dapur di kuantitas tinggi
id:Garam di kuantitas tinggi
ie:Salt in high quantity
ig:Ńnú in high quantity
ii:Salt in high quantity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@
],
"epi_score" : 0,
"epi_value" : -5,
"origins_from_categories" : [
"en:unknown"
],
"origins_from_origins_field" : [
"en:unknown"
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@
],
"epi_score" : 0,
"epi_value" : -5,
"origins_from_categories" : [
"en:unknown"
],
"origins_from_origins_field" : [
"en:unknown"
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@
],
"epi_score" : 0,
"epi_value" : -5,
"origins_from_categories" : [
"en:unknown"
],
"origins_from_origins_field" : [
"en:unknown"
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -758,6 +758,9 @@
],
"epi_score" : 0,
"epi_value" : -5,
"origins_from_categories" : [
"en:unknown"
],
"origins_from_origins_field" : [
"en:unknown"
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@
],
"epi_score" : 0,
"epi_value" : -5,
"origins_from_categories" : [
"en:unknown"
],
"origins_from_origins_field" : [
"en:unknown"
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@
],
"epi_score" : 0,
"epi_value" : -5,
"origins_from_categories" : [
"en:unknown"
],
"origins_from_origins_field" : [
"en:unknown"
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@
],
"epi_score" : 0,
"epi_value" : -5,
"origins_from_categories" : [
"en:unknown"
],
"origins_from_origins_field" : [
"en:unknown"
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@
],
"epi_score" : 0,
"epi_value" : -5,
"origins_from_categories" : [
"en:unknown"
],
"origins_from_origins_field" : [
"en:unknown"
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@
],
"epi_score" : 0,
"epi_value" : -5,
"origins_from_categories" : [
"en:unknown"
],
"origins_from_origins_field" : [
"en:unknown"
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@
],
"epi_score" : 0,
"epi_value" : -5,
"origins_from_categories" : [
"en:unknown"
],
"origins_from_origins_field" : [
"en:unknown"
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@
],
"epi_score" : 0,
"epi_value" : -5,
"origins_from_categories" : [
"en:unknown"
],
"origins_from_origins_field" : [
"en:unknown"
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@
],
"epi_score" : 0,
"epi_value" : -5,
"origins_from_categories" : [
"en:unknown"
],
"origins_from_origins_field" : [
"en:unknown"
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@
],
"epi_score" : 0,
"epi_value" : -5,
"origins_from_categories" : [
"en:unknown"
],
"origins_from_origins_field" : [
"en:unknown"
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@
],
"epi_score" : 0,
"epi_value" : -5,
"origins_from_categories" : [
"en:unknown"
],
"origins_from_origins_field" : [
"en:unknown"
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@
],
"epi_score" : 0,
"epi_value" : -5,
"origins_from_categories" : [
"en:unknown"
],
"origins_from_origins_field" : [
"en:unknown"
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@
],
"epi_score" : 0,
"epi_value" : -5,
"origins_from_categories" : [
"en:unknown"
],
"origins_from_origins_field" : [
"en:unknown"
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -760,6 +760,9 @@
],
"epi_score" : 0,
"epi_value" : -5,
"origins_from_categories" : [
"en:unknown"
],
"origins_from_origins_field" : [
"en:unknown"
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@
],
"epi_score" : 0,
"epi_value" : -5,
"origins_from_categories" : [
"en:unknown"
],
"origins_from_origins_field" : [
"en:unknown"
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@
],
"epi_score" : 0,
"epi_value" : -5,
"origins_from_categories" : [
"en:unknown"
],
"origins_from_origins_field" : [
"en:unknown"
],
Expand Down
Loading

0 comments on commit ae8fd7d

Please sign in to comment.