Skip to content

Commit

Permalink
fix: remove warnings for empty/undef quantities (#9573)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephanegigandet authored Jan 5, 2024
1 parent 7ed1019 commit 6250c9f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 25 deletions.
44 changes: 28 additions & 16 deletions lib/ProductOpener/Units.pm
Original file line number Diff line number Diff line change
Expand Up @@ -248,17 +248,19 @@ sub parse_quantity_unit ($quantity, $standard_unit_bool = undef) {
# 6 bricks de 1 l
# 10 unités, 170 g
# 4 bouteilles en verre de 20cl
if ($quantity
=~ /(?<number>\d+)(\s(\p{Letter}| )+)?(\s)?( de | of |x|\*)(\s)?(?<quantity>$number_regexp)(\s)?(?<unit>$units_regexp)\b/i
)
{
$m = $+{number};
$q = lc($+{quantity});
$u = $+{unit};
}
elsif ($quantity =~ /(?<quantity>$number_regexp)(\s)?(?<unit>$units_regexp)\s*\b/i) {
$q = lc($+{quantity});
$u = $+{unit};
if (defined $quantity) {
if ($quantity
=~ /(?<number>\d+)(\s(\p{Letter}| )+)?(\s)?( de | of |x|\*)(\s)?(?<quantity>$number_regexp)(\s)?(?<unit>$units_regexp)\b/i
)
{
$m = $+{number};
$q = lc($+{quantity});
$u = $+{unit};
}
elsif ($quantity =~ /(?<quantity>$number_regexp)(\s)?(?<unit>$units_regexp)\s*\b/i) {
$q = lc($+{quantity});
$u = $+{unit};
}
}

return ($q, $m, $u);
Expand Down Expand Up @@ -303,13 +305,23 @@ Returns undef if no unit was detected.
=cut

sub extract_standard_unit ($quantity_field) {

my $standard_unit = undef;

my (undef, undef, $unit) = parse_quantity_unit($quantity_field);

# search in the map of all synonyms in all languages ($units_names)
$unit = lc($unit);
my $unit_id = $units_names{$unit}; # $unit_id can be undefined
if (defined $unit) {

# search in the map of all synonyms in all languages ($units_names)
$unit = lc($unit);
my $unit_id = $units_names{$unit}; # $unit_id can be undefined
if (defined $unit_id) {

$standard_unit = $units{$unit_id}{standard_unit}; # standard_unit can be undefined
}
}

return $units{$unit_id}{standard_unit}; # standard_unit can be undefined
return $standard_unit;
}

=head2 normalize_serving_size($serving)
Expand All @@ -324,7 +336,7 @@ sub normalize_serving_size ($serving) {

# Regex captures any <number>( )?<unit-identifier> group, but leaves allowances for a preceding
# token to allow for patterns like "One bag (32g)", "1 small bottle (180ml)" etc
if ($serving =~ /^(.*[ \(])?(?<quantity>$number_regexp)( )?(?<unit>$units_regexp)\b/i) {
if ((defined $serving) and ($serving =~ /^(.*[ \(])?(?<quantity>$number_regexp)( )?(?<unit>$units_regexp)\b/i)) {
my $q = $+{quantity};
my $u = $+{unit};
$q = convert_string_to_number($q);
Expand Down
21 changes: 12 additions & 9 deletions tests/unit/units.t
Original file line number Diff line number Diff line change
Expand Up @@ -151,19 +151,22 @@ is(normalize_serving_size("5 bottles .33L"), 330);
is(normalize_serving_size("5 bottles2.33L"), undef); # Broken string, missing word separator before number

my @serving_sizes = (
["100g", "100"],
["250 g", "250"],
["1.5kg", "1500"],
["2,5g", "2.5"],
["1 plate (25g)", "25"],
["1 grilled link (82g)", "82"],
["2 buns = 20g", "20"],
["43 someinvalidunit (430g)", "430"],
["1500ml", "1500"],
[undef, undef, undef],
["", undef, undef],
["100g", "100", "g"],
["250 g", "250", "g"],
["1.5kg", "1500", "g"],
["2,5g", "2.5", "g"],
["1 plate (25g)", "25", "g"],
["1 grilled link (82g)", "82", "g"],
["2 buns = 20g", "20", "g"],
["43 someinvalidunit (430g)", "430", "g"],
["1500ml", "1500", "ml"],
);

foreach my $test_ref (@serving_sizes) {
is(normalize_serving_size($test_ref->[0]), $test_ref->[1]) or diag explain $test_ref;
is(extract_standard_unit($test_ref->[0]), $test_ref->[2]) or diag explain $test_ref;
}

done_testing();

0 comments on commit 6250c9f

Please sign in to comment.