Skip to content

Commit

Permalink
fix: NLP Recipe parsing of composite ingredients (#618)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomBursch authored Jan 2, 2025
1 parent 6e83ec3 commit a2017f6
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions backend/app/service/ingredient_parsing.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from ingredient_parser import parse_ingredient
import ingredient_parser
import ingredient_parser.dataclasses
from litellm import completion
import json
import os
Expand All @@ -24,10 +25,15 @@ def __str__(self):


def parseNLP(ingredients: list[str]) -> list[IngredientParsingResult]:
def parseNLPSingle(ingredient):
parsed = parse_ingredient(ingredient)
def nlpAmountToDescription(amount: ingredient_parser.dataclasses.IngredientAmount) -> str:
if isinstance(amount, ingredient_parser.dataclasses.CompositeIngredientAmount):
return amount.text
return f"{amount.quantity} {amount.unit}"

def parseNLPSingle(ingredient: str) -> IngredientParsingResult:
parsed = ingredient_parser.parse_ingredient(ingredient)
name = parsed.name.text if parsed.name else None
description = f"{parsed.amount[0].quantity if len(parsed.amount) > 0 else ''} {parsed.amount[0].unit if len(parsed.amount) > 0 else ''}"
description = nlpAmountToDescription(parsed.amount[0]) if len(parsed.amount) > 0 else ''
# description = description + (" " if description else "") + (parsed.comment.text if parsed.comment else "") # Usually cooking instructions
return IngredientParsingResult(ingredient, name, description)

Expand Down

0 comments on commit a2017f6

Please sign in to comment.