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: illegal division by zero in percent estimate #9007

Merged
merged 1 commit into from
Sep 19, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/ProductOpener/Ingredients.pm
Original file line number Diff line number Diff line change
Expand Up @@ -2878,10 +2878,11 @@ sub init_percent_values ($total_min, $total_max, $ingredients_ref) {
# This is the case in particular for recipes that can be specified in grams with a total greater than 100g
# So we start supposing it's grams (as if it's percent it will also work).

if (($total_min == $total_max) and $all_ingredients_have_a_set_percent) {
# In scale_percents or scale_grams mode, the percent/quantity sum must be greater than 0
if (($total_min == $total_max) and ($all_ingredients_have_a_set_percent) and ($percent_sum > 0)) {
$percent_mode = "scale_percents";
}
elsif (($total_min == $total_max) and $all_ingredients_have_a_set_quantity) {
elsif (($total_min == $total_max) and ($all_ingredients_have_a_set_quantity) and ($quantity_sum > 0)) {
$percent_mode = "scale_grams";
}
elsif ($percent_sum > $total_max) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
{
"ingredients" : [
{
"id" : "en:each-capsule-contains",
"ingredients" : [
{
"id" : "en:paracetamol-500-m-5-060198-790",
"percent_estimate" : 100,
"percent_max" : 100,
"percent_min" : 100,
"quantity" : "0 mg",
"quantity_g" : 0,
"text" : "paracetamol 500 m 5 060198 790"
}
],
"percent_estimate" : 100,
"percent_max" : 100,
"percent_min" : 100,
"text" : "each capsule contains"
}
],
"ingredients_analysis" : {
"en:palm-oil-content-unknown" : [
"en:each-capsule-contains",
"en:paracetamol-500-m-5-060198-790"
],
"en:vegan-status-unknown" : [
"en:each-capsule-contains",
"en:paracetamol-500-m-5-060198-790"
],
"en:vegetarian-status-unknown" : [
"en:each-capsule-contains",
"en:paracetamol-500-m-5-060198-790"
]
},
"ingredients_analysis_tags" : [
"en:palm-oil-content-unknown",
"en:vegan-status-unknown",
"en:vegetarian-status-unknown"
],
"ingredients_hierarchy" : [
"en:each-capsule-contains",
"en:paracetamol-500-m-5-060198-790"
],
"ingredients_n" : 2,
"ingredients_n_tags" : [
"2",
"1-10"
],
"ingredients_original_tags" : [
"en:each-capsule-contains",
"en:paracetamol-500-m-5-060198-790"
],
"ingredients_percent_analysis" : 1,
"ingredients_tags" : [
"en:each-capsule-contains",
"en:paracetamol-500-m-5-060198-790"
],
"ingredients_text" : "each capsule contains: paracetamol 500 m 5 060198 790 0 mg.",
"ingredients_with_specified_percent_n" : 0,
"ingredients_with_specified_percent_sum" : 0,
"ingredients_with_unspecified_percent_n" : 1,
"ingredients_with_unspecified_percent_sum" : 100,
"ingredients_without_ciqual_codes" : [
"en:paracetamol-500-m-5-060198-790"
],
"ingredients_without_ciqual_codes_n" : 1,
"known_ingredients_n" : 0,
"lc" : "en",
"nutriments" : {
"fruits-vegetables-legumes-estimate-from-ingredients_100g" : 0,
"fruits-vegetables-legumes-estimate-from-ingredients_serving" : 0,
"fruits-vegetables-nuts-estimate-from-ingredients_100g" : 0,
"fruits-vegetables-nuts-estimate-from-ingredients_serving" : 0
},
"unknown_ingredients_n" : 2
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
{
"ingredients" : [
{
"id" : "fr:analyse-moyenne-pour-1",
"ingredients" : [
{
"id" : "fr:1472-kj",
"percent_estimate" : 100,
"percent_max" : 100,
"percent_min" : 100,
"text" : "1472 kJ"
}
],
"percent_estimate" : 100,
"percent_max" : 100,
"percent_min" : 100,
"quantity" : "00 g",
"quantity_g" : 0,
"text" : "Analyse moyenne pour 1"
}
],
"ingredients_analysis" : {
"en:palm-oil-content-unknown" : [
"fr:analyse-moyenne-pour-1",
"fr:1472-kj"
],
"en:vegan-status-unknown" : [
"fr:analyse-moyenne-pour-1",
"fr:1472-kj"
],
"en:vegetarian-status-unknown" : [
"fr:analyse-moyenne-pour-1",
"fr:1472-kj"
]
},
"ingredients_analysis_tags" : [
"en:palm-oil-content-unknown",
"en:vegan-status-unknown",
"en:vegetarian-status-unknown"
],
"ingredients_hierarchy" : [
"fr:analyse-moyenne-pour-1",
"fr:1472-kj"
],
"ingredients_n" : 2,
"ingredients_n_tags" : [
"2",
"1-10"
],
"ingredients_original_tags" : [
"fr:analyse-moyenne-pour-1",
"fr:1472-kj"
],
"ingredients_percent_analysis" : 1,
"ingredients_tags" : [
"fr:analyse-moyenne-pour-1",
"fr:1472-kj"
],
"ingredients_text" : "Analyse moyenne pour 1 00 g: 1472 kJ,",
"ingredients_with_specified_percent_n" : 0,
"ingredients_with_specified_percent_sum" : 0,
"ingredients_with_unspecified_percent_n" : 1,
"ingredients_with_unspecified_percent_sum" : 100,
"ingredients_without_ciqual_codes" : [
"fr:1472-kj"
],
"ingredients_without_ciqual_codes_n" : 1,
"known_ingredients_n" : 0,
"lc" : "fr",
"nutriments" : {
"fruits-vegetables-legumes-estimate-from-ingredients_100g" : 0,
"fruits-vegetables-legumes-estimate-from-ingredients_serving" : 0,
"fruits-vegetables-nuts-estimate-from-ingredients_100g" : 0,
"fruits-vegetables-nuts-estimate-from-ingredients_serving" : 0
},
"unknown_ingredients_n" : 2
}
15 changes: 15 additions & 0 deletions tests/unit/ingredients.t
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,21 @@ Origin of peaches: Spain. Origin of some unknown ingredient: France. origin of A
ingredients_text => "farine demi-complète de riz, farine de blé demi complet",
}
],
# illegal division by zero
[
"fr-illegal-division-by-zero",
{
lc => "fr",
ingredients_text => "Analyse moyenne pour 1 00 g: 1472 kJ,",
}
],
[
"en-illegal-division-by-zero",
{
lc => "en",
ingredients_text => "each capsule contains: paracetamol 500 m 5 060198 790 0 mg.",
}
],
);

foreach my $test_ref (@tests) {
Expand Down
Loading