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: bug_ingredients_wrongly_specific_ingredients #9624

Merged
merged 3 commits into from
Jan 9, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions lib/ProductOpener/Ingredients.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1182,16 +1182,25 @@ sub parse_specific_ingredients_from_text ($product_ref, $text, $percent_or_quant
# - prepared with 60g of fruits
# - prepared with 40g of fruits per 100g of finished product
elsif (
(defined $percent_or_quantity_regexp)
(
(defined $percent_or_quantity_regexp)
and (defined $of)
and ($of ne "")
and (defined $per_100g_regexp)
and ($per_100g_regexp ne "")
)
and (
# if the string does not start with "prepared with", it needs to finish with "per 100g",
# otherwise we will match items that could be part of the ingredients list such as "75% of tomatoes

# prepared with, percent, ingredient, optional per 100g, separator
# $of needs to be first in (?:$of|\s|:) so that " of " is matched by it, instead of the ingredient capturing group
(
$text
=~ /((?:^|;|,|\.| - )\s*)$prepared_with(?:$of|\s|:)+$percent_or_quantity_regexp(?:$of|\s|:)+\s*([^,.;]+?)\s*(?:$per_100g_regexp)?(?:;|,|\.| - |$)/i
(defined $prepared_with)
and ($prepared_with ne "")
and ($text
=~ /((?:^|;|,|\.| - )\s*)$prepared_with(?:$of|\s|:)+$percent_or_quantity_regexp(?:$of|\s|:)+\s*([^,.;]+?)\s*(?:$per_100g_regexp)?(?:;|,|\.| - |$)/i
)
)
or
# percent, ingredient, per 100g, separator
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
[
{
"id" : "en:apple",
"percent" : 84,
"percent_estimate" : 84,
"percent_max" : 84,
"percent_min" : 84,
"text" : "jabłka"
},
{
"id" : "en:mango",
"percent" : 10,
"percent_estimate" : 10,
"percent_max" : 10,
"percent_min" : 10,
"processing" : "en:pureed",
"text" : "mango"
},
{
"id" : "en:concentrated-apple-juice",
"percent" : 3,
"percent_estimate" : 3,
"percent_max" : 3,
"percent_min" : 3,
"text" : "zagęszczony sok jabłkowy"
},
{
"id" : "pl:zagęszczony-sok-z-marakuji",
"percent" : 1.5,
"percent_estimate" : 1.5,
"percent_max" : 1.5,
"percent_min" : 1.5,
"text" : "zagęszczony sok z marakuji"
},
{
"id" : "en:acerola-juice",
"percent_estimate" : 1.5,
"percent_max" : 1.5,
"percent_min" : 1.5,
"processing" : "en:concentrated",
"text" : "sok z aceroli"
}
]
8 changes: 8 additions & 0 deletions tests/unit/ingredients_percent.t
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,14 @@ my @tests = (
"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é."
},
],
[
'percentage-range-negative-pl',
{
lc => "pl",
ingredients_text =>
"84% jabłka, 10% przecier z mango, 3% zagęszczony sok jabłkowy, 1,5% zagęszczony sok z marakuji, zagęszczony sok z aceroli."
},
],
# max sugar and salt from nutrition facts
[
'max-sugar-salt-nutrition-facts',
Expand Down
Loading