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: increased float rounding precision for CRF parser #1369

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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)
michael-genson marked this conversation as resolved.
Show resolved Hide resolved
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", ""),
Comment on lines +29 to +34
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added two test cases that pass on my machine and update the other two to work with the new significant digits we're using now.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that looks good to me, and the tests make sense so if they're passing I don't see anything wrong with it!

]


Expand Down