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

feat: support percentage range #9224

Merged
merged 5 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/ProductOpener/Ingredients.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1802,6 +1802,7 @@ sub parse_ingredients_text_service ($product_ref, $updated_product_fields_ref) {
# $percent_or_quantity_regexp has 2 capturing group: one for the number, and one for the % sign or the unit
my $percent_or_quantity_regexp = '(?:' . "(?:$prepared_with )" . ' )?' # optional produced with
. '(?:<|' . $min_regexp . '|\s|\.|:)*' # optional minimum, and separators
. '(?:\d+(?:(?:\,|\.)\d+)?\s*-\s*?)?' # number+hyphens, first part (10-) of "10-12%"
benbenben2 marked this conversation as resolved.
Show resolved Hide resolved
. '(\d+(?:(?:\,|\.)\d+)?)\s*' # number, possibly with a dot or comma
. '(\%|g|gr|mg|kg|ml|cl|dl|l)\s*' # % or unit
. '(?:' . $min_regexp . '|' # optional minimum
Expand Down Expand Up @@ -5601,6 +5602,8 @@ sub preparse_ingredients_text ($ingredients_lc, $text) {
# transform 0,2% into 0.2%
$text =~ s/(\d),(\d+)( )?(\%|g\b)/$1.$2\%/ig;
$text =~ s/—/-/g;
# transform 0,1-0.2% into 0.1-0.2%
$text =~ s/(\d),(\d+( )?-( )?\d)/$1.$2/ig;

# abbreviations, replace language specific abbreviations first
foreach my $abbreviations_lc ($ingredients_lc, "all") {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
[
{
"id" : "en:pasteurised-whole-milk",
"labels" : "en:fair-trade",
"percent" : 74.3,
"percent_estimate" : 74.3,
"text" : "Lait entier pasteurisé"
},
{
"id" : "en:cane-sugar",
"percent" : 10,
"percent_estimate" : 10,
"text" : "sucre de canne"
},
{
"id" : "en:cream",
"percent" : 5.5,
"percent_estimate" : 5.5,
"text" : "crème"
},
{
"id" : "en:corn-starch",
"percent" : 5,
"percent_estimate" : 5,
"text" : "amidon de mais"
},
{
"id" : "en:coffee",
"percent_estimate" : 5.2,
"processing" : "en:freeze-dried",
"text" : "café"
}
]
8 changes: 8 additions & 0 deletions tests/unit/ingredients_percent.t
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,14 @@ my @tests = (
"Lingon 50%*, socker*, vatten, förtjockningsmedel (pektin), surhetsreglerande medel (citronsyra). *KRAV-certifierad ekologisk ingrediens. Fruktmängd: 50g per 100g. Total mängd socker är 35 g per 100 g sylt. Fruktmängd: 52g per 100 g sylt. Bärmängd: 40 g bär per 100g. Total mängd socker: 45g per 100g sylt. Total mängd socker 44 g, varav tillsatt socker 41g per 100g sylt."
},
],
[
'percentage-range',
{
lc => "fr",
ingredients_text =>
"Lait entier pasteurisé équitable (73,4-74,3%), sucre de canne (9 - 10%), crème (5.1-5.5%), amidon de mais 4-5%, café lyophilisé."
},
],
# max sugar and salt from nutrition facts
[
'max-sugar-salt-nutrition-facts',
Expand Down
Loading