Skip to content

Commit

Permalink
fix: solve quantity false positives issue #2037 (#2038)
Browse files Browse the repository at this point in the history
  • Loading branch information
CharlesNepote authored May 2, 2023
1 parent d9d9c87 commit 80f01b6
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
12 changes: 9 additions & 3 deletions lib/ProductOpener/DataQualityFood.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1205,12 +1205,18 @@ Checks related to the quantity and serving quantity.
=cut

# Check quantity values. See https://en.wiki.openfoodfacts.org/Products_quantities
sub check_quantity ($product_ref) {

# quantity contains "e" - might be an indicator that the user might have wanted to use "℮" \N{U+212E}
if ( (defined $product_ref->{quantity})
and ($product_ref->{quantity} =~ /(?:.*e$)|(?:[0-9]+\s*[kmc]?[gl]?\s*e)/i)
and (not($product_ref->{quantity} =~ /\N{U+212E}/i)))
# example: 650 g e
if (
(defined $product_ref->{quantity})
# contains "kg e", or "g e", or "cl e", etc.
and ($product_ref->{quantity} =~ /(?:[0-9]+\s*[kmc]?[gl]\s*e)/i)
# contains the "℮" symbol
and (not($product_ref->{quantity} =~ /\N{U+212E}/i))
)
{
push @{$product_ref->{data_quality_info_tags}}, "en:quantity-contains-e";
}
Expand Down
37 changes: 37 additions & 0 deletions tests/unit/dataqualityfood.t
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use utf8;
use Test::More;

use ProductOpener::DataQuality qw/:all/;
use ProductOpener::DataQualityFood qw/:all/;
use ProductOpener::Tags qw/:all/;
use ProductOpener::Ingredients qw/:all/;

Expand Down Expand Up @@ -707,4 +708,40 @@ check_quality_and_test_product_has_quality_tag(
'sum of fructose plus glucose plus maltose plus lactose plus sucrose cannot be greater than sugars', 0
);

# testing of ProductOpener::DataQualityFood::check_quantity subroutine
$product_ref = {quantity => "300g"};
ProductOpener::DataQuality::check_quality($product_ref);
check_quality_and_test_product_has_quality_tag($product_ref, 'en:quantity-contains-e',
'quantity does not contain e', 0);
$product_ref = {quantity => "1 verre"};
ProductOpener::DataQuality::check_quality($product_ref);
check_quality_and_test_product_has_quality_tag($product_ref, 'en:quantity-contains-e',
'quantity does not contain e', 0);
$product_ref = {quantity => "1 litre"};
ProductOpener::DataQuality::check_quality($product_ref);
check_quality_and_test_product_has_quality_tag($product_ref, 'en:quantity-contains-e',
'quantity does not contain e', 0);
$product_ref = {quantity => "225 g ℮"};
ProductOpener::DataQuality::check_quality($product_ref);
check_quality_and_test_product_has_quality_tag($product_ref, 'en:quantity-contains-e', 'quantity does not contain e',
0);
$product_ref = {quantity => "300ge"};
ProductOpener::DataQuality::check_quality($product_ref);
check_quality_and_test_product_has_quality_tag($product_ref, 'en:quantity-contains-e', 'quantity contains e', 1);
$product_ref = {quantity => "300mge"};
ProductOpener::DataQuality::check_quality($product_ref);
check_quality_and_test_product_has_quality_tag($product_ref, 'en:quantity-contains-e', 'quantity contains e', 1);
$product_ref = {quantity => "300 mg e"};
ProductOpener::DataQuality::check_quality($product_ref);
check_quality_and_test_product_has_quality_tag($product_ref, 'en:quantity-contains-e', 'quantity contains e', 1);
$product_ref = {quantity => "200 g e (2x100g)"};
ProductOpener::DataQuality::check_quality($product_ref);
check_quality_and_test_product_has_quality_tag($product_ref, 'en:quantity-contains-e', 'quantity contains e', 1);
$product_ref = {quantity => "1kge35.27oz"};
ProductOpener::DataQuality::check_quality($product_ref);
check_quality_and_test_product_has_quality_tag($product_ref, 'en:quantity-contains-e', 'quantity contains e', 1);
$product_ref = {quantity => "300 ml e / 342 g"};
ProductOpener::DataQuality::check_quality($product_ref);
check_quality_and_test_product_has_quality_tag($product_ref, 'en:quantity-contains-e', 'quantity contains e', 1);

done_testing();

0 comments on commit 80f01b6

Please sign in to comment.