Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: solve quantity false positives issue #2037 #2038

Merged
merged 7 commits into from
May 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 9 additions & 3 deletions lib/ProductOpener/DataQualityFood.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1203,12 +1203,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();