Skip to content

Commit

Permalink
fix: increased float rounding precision for CRF parser (#1369)
Browse files Browse the repository at this point in the history
* increased float rounding precision for crf parser

* limited fractions to a max denominator of 32 to prevent weirdly specific values

* add test cases for 1/8 and 1/32

* add rounding to avoid more digits than necessary

Co-authored-by: Hayden <64056131+hay-kot@users.noreply.github.com>
  • Loading branch information
michael-genson and hay-kot committed Jun 11, 2022
1 parent 504bf41 commit b904b16
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 5 deletions.
2 changes: 1 addition & 1 deletion mealie/schema/recipe/recipe_ingredient.py
Expand Up @@ -74,7 +74,7 @@ def validate_quantity(cls, value, values) -> NoneFloat:
empty string.
"""
if isinstance(value, float):
return value
return round(value, 3)
if value is None or value == "":
return None
return value
Expand Down
2 changes: 1 addition & 1 deletion mealie/services/parser_services/crfpp/processor.py
Expand Up @@ -37,7 +37,7 @@ def validate_qty(qty, values): # sourcery skip: merge-nested-ifs
# Check if other contains a fraction
try:
if values["other"] is not None and values["other"].find("/") != -1:
return float(Fraction(values["other"])).__round__(1)
return round(float(Fraction(values["other"])), 3)
else:
return 1
except Exception:
Expand Down
2 changes: 1 addition & 1 deletion mealie/services/parser_services/ingredient_parser.py
Expand Up @@ -74,7 +74,7 @@ def _crf_to_ingredient(self, crf_model: crfpp.CRFIngredient) -> ParsedIngredient
unit=CreateIngredientUnit(name=crf_model.unit),
food=CreateIngredientFood(name=crf_model.name),
disable_amount=False,
quantity=float(sum(Fraction(s) for s in crf_model.qty.split())),
quantity=float(sum(Fraction(s).limit_denominator(32) for s in crf_model.qty.split())),
)
except Exception as e:
logger.error(f"Failed to parse ingredient: {crf_model}: {e}")
Expand Down
8 changes: 6 additions & 2 deletions tests/unit_tests/test_ingredient_parser.py
Expand Up @@ -26,8 +26,12 @@ def crf_exists() -> bool:
test_ingredients = [
TestIngredient("½ cup all-purpose flour", 0.5, "cup", "all-purpose flour", ""),
TestIngredient("1 ½ teaspoons ground black pepper", 1.5, "teaspoon", "black pepper", "ground"),
TestIngredient("⅔ cup unsweetened flaked coconut", 0.7, "cup", "coconut", "unsweetened flaked"),
TestIngredient("⅓ cup panko bread crumbs", 0.3, "cup", "panko bread crumbs", ""),
TestIngredient("⅔ cup unsweetened flaked coconut", 0.667, "cup", "coconut", "unsweetened flaked"),
TestIngredient("⅓ cup panko bread crumbs", 0.333, "cup", "panko bread crumbs", ""),
# Small Fraction Tests - PR #1369
# Reported error is was for 1/8 - new lowest expected threshold is 1/32
TestIngredient("1/8 cup all-purpose flour", 0.125, "cup", "all-purpose flour", ""),
TestIngredient("1/32 cup all-purpose flour", 0.03125, "cup", "all-purpose flour", ""),
]


Expand Down

0 comments on commit b904b16

Please sign in to comment.