Skip to content

Commit

Permalink
feat: data quality warning for 3 identical values in nutrition tables (
Browse files Browse the repository at this point in the history
…#8109)

* data quality warning for 3 identical values in nutrition tables for major nutriments

* include fiber and sodium

* apply corrections suggested by Stephane
  • Loading branch information
benbenben2 authored Feb 24, 2023
1 parent 6fabb9d commit 70a4926
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 0 deletions.
4 changes: 4 additions & 0 deletions docs/how-to-guides/use-gitpod.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ on http://openfoodfacts.localhost just as documented in the quickstart guide!

**Remark:** for some Linux distributions, the port 80 is reserved. A workaround is to switch to port 8080: in gitpod, open the .env file and replace the line PRODUCT_OPENER_PORT=80 by PRODUCT_OPENER_PORT=8080, then replace -L 80:localhost:80 by -L 8080:localhost:8080. **Rollback the changes on .env before to make a pull request!***

**Remark:** the address to connect with ssh can change after few days. If you get a ```Connection closed by ... port 22``` simply go back to https://gitpod.io/workspaces and copy the new address.

**Remark:** if you load the page after some changes but get a ```502 Bad Gateway``` check again your code. Something may be wrong with it. Eventually, try to comment the part you just coded to see if it works.

Create an account to be able to edit products.

## Some commands
Expand Down
39 changes: 39 additions & 0 deletions lib/ProductOpener/DataQualityFood.pm
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,9 @@ sub check_nutrition_data ($product_ref) {
my $nid_non_zero = 0;

my $total = 0;
# variables to check if there are 3 or more duplicates in nutriments
my @major_nutriments_values = ();
my %nutriments_values_occurences = ();

if ( (defined $product_ref->{nutriments}{"energy-kcal_value"})
and (defined $product_ref->{nutriments}{"energy-kj_value"}))
Expand Down Expand Up @@ -758,6 +761,42 @@ sub check_nutrition_data ($product_ref) {
if (($nid eq 'fat') or ($nid eq 'carbohydrates') or ($nid eq 'proteins') or ($nid eq 'salt')) {
$total += $product_ref->{nutriments}{$nid . "_100g"};
}

# variables to check if there are 3 or more duplicates in nutriments
if (
(
($nid eq 'fat_100g')
or ($nid eq 'saturated-fat_100g')
or ($nid eq 'carbohydrates_100g')
or ($nid eq 'sugars_100g')
or ($nid eq 'fiber_100g')
or ($nid eq 'proteins_100g')
or ($nid eq 'salt_100g')
or ($nid eq 'sodium_100g')
)
and ($product_ref->{nutriments}{$nid} > 1)
)
{
push(@major_nutriments_values, $product_ref->{nutriments}{$nid});
}

}

# create a hash key: nutriment value, value: number of occurence
foreach my $nutriment_value (@major_nutriments_values) {
if (exists($nutriments_values_occurences{$nutriment_value})) {
$nutriments_values_occurences{$nutriment_value}++;
}
else {
$nutriments_values_occurences{$nutriment_value} = 1;
}
}
# raise warning if there are 3 or more duplicates in nutriments
foreach my $keys (keys %nutriments_values_occurences) {
if ($nutriments_values_occurences{$keys} > 2) {
push @{$product_ref->{data_quality_warnings_tags}}, "en:nutrition-3-or-more-values-are-identical";
last;
}
}

if ($total > 105) {
Expand Down
4 changes: 4 additions & 0 deletions taxonomies/data_quality.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2647,6 +2647,10 @@ show_on_producers_platform:en:yes
en:Nutri-Score score from producer does not match calculated score
description:en:This might be due among other things to incorrect nutrition values, incomplete nutrition values (missing fiber, fruits and vegetables), incorrect fruits and vegetables estimate, incorrect input of diluted/reconstituted values.

<en:Nutrition warnings
en:Nutrition 3 or more values are identical
description:en:Someone may have entered wrong data because there are 3 identical values in nutrition table and those values are above 1

<en:Nutrition warnings
en:Nutrition all values zero
#description:en:
Expand Down
40 changes: 40 additions & 0 deletions tests/unit/dataqualityfood.t
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,45 @@ check_quality_and_test_product_has_quality_tag(
'en:ingredients-extracted-ingredient-from-picture-with-more-than-100-percent',
'percentage should not be above 100, error when extracting the ingredients from the picture', 0
);

# en:nutrition-3-or-more-values-are-identical
$product_ref = {
nutriments => {
"carbohydrates_100g" => 0,
"fat_100g" => 0,
"proteins_100g" => 0,
}
};
check_quality_and_test_product_has_quality_tag(
$product_ref,
'en:nutrition-3-or-more-values-are-identical',
'3 or more identical values and above 1 in the nutrition table', 0
);
$product_ref = {
nutriments => {
"carbohydrates_100g" => 1,
"fat_100g" => 2,
"proteins_100g" => 3,
}
};
check_quality_and_test_product_has_quality_tag(
$product_ref,
'en:nutrition-3-or-more-values-are-identical',
'3 or more identical values and above 1 in the nutrition table', 0
);
$product_ref = {
nutriments => {
"carbohydrates_100g" => 3,
"fat_100g" => 3,
"proteins_100g" => 3,
}
};
check_quality_and_test_product_has_quality_tag(
$product_ref,
'en:nutrition-3-or-more-values-are-identical',
'3 or more identical values and above 1 in the nutrition table', 1
);

# sum of fructose plus glucose plus maltose plus lactose plus sucrose cannot be greater than sugars
$product_ref = {nutriments => {}};
ProductOpener::DataQuality::check_quality($product_ref);
Expand Down Expand Up @@ -667,4 +706,5 @@ check_quality_and_test_product_has_quality_tag(
'en:nutrition-fructose-plus-glucose-plus-maltose-plus-lactose-plus-sucrose-greater-than-sugars',
'sum of fructose plus glucose plus maltose plus lactose plus sucrose cannot be greater than sugars', 0
);

done_testing();

0 comments on commit 70a4926

Please sign in to comment.