fix: NLP Ingredient Parser Misses Some Fractions (#3618)

This commit is contained in:
Michael Genson 2024-05-20 05:18:11 -05:00 committed by GitHub
parent 3d3279738b
commit a4c3b0da71
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -29,24 +29,24 @@ class CRFIngredient(BaseModel):
input: str = ""
name: str = ""
other: str = ""
qty: str = ""
qty: Annotated[str, Field(validate_default=True)] = ""
comment: str = ""
unit: str = ""
confidence: CRFConfidence
@field_validator("qty", mode="before")
def validate_qty(qty, info: ValidationInfo): # sourcery skip: merge-nested-ifs
if qty is None or qty == "":
# Check if other contains a fraction
try:
if info.data["other"] is not None and info.data["other"].find("/") != -1:
return round(float(Fraction(info.data["other"])), 3)
else:
return 1
except Exception:
pass
def validate_qty(cls, qty, info: ValidationInfo):
if qty is not None and qty != "":
return qty
return qty
# Check if other contains a fraction
try:
if info.data["other"] is not None and info.data["other"].find("/") != -1:
return str(round(float(Fraction(info.data["other"])), 3))
else:
return "0"
except Exception:
return ""
def _exec_crf_test(input_text):