Local recipe app ingredient parsing
A local recipe app parsed 500g and 2 cups, but mixed units kept breaking it.
I built a local recipe app so I could stop scrolling through messy recipe pages on my phone. The first version handled simple ingredients fine: 500g flour, 2 cups milk, even awkward brand names. Then it hit “1 1/2 tbsp” and “a pinch,” and the parser fell apart.
I started with a plain ruleset, then added a small model pass to normalize quantities before saving them. That worked until mixed units showed up in the same line, like “200g plus 1 cup” or “1 tbsp oil, divided.” The app would either merge them wrong or drop the second amount entirely.
The boring part was spent in test cases. I made about 120 ingredient strings by hand, including some ugly pantry stuff, and compared parsed output against what I’d actually cook with. The worst bug was decimal conversion: it quietly turned fractions into floats and rounded too early, so 1/3 became 0.3 and drifted in scaling.
Right now it’s usable for my own recipes, but not something I’d trust for imported recipe dumps. I still don’t have a clean answer for vague amounts like “a handful” or “season to taste,” and I’m not sure I want one. Those are still basically human judgments.
What surprised me most was that parsing wasn’t hardest on the weird ingredients; it was hardest on the normal ones that got combined in one sentence. I’d probably add a stricter intermediate schema earlier next time, instead of trying to fix everything at the display layer.
9 replies
Did you normalize everything to grams in the end?
Mostly, yes, but “a pinch” and “to taste” stay as non-numeric tokens.
I had the same issue with “divided” ingredients; it always breaks naive parsers.
Keeping fractions as rational numbers longer usually fixes the 1/3 to 0.3 drift.
If the app is local-only, is all this parsing even worth the complexity?
For me yes, because the import step was the part I kept avoiding.
“1 1/2 tbsp”最好先词法切分,再进单位归一化,不然空格会把解析器搞乱。
你有处理“2-3 cloves”这种范围吗?我每次都卡在这类表达上。
有,但现在只是存成 range,没法自动换算成一个固定值,菜单缩放时还是得人工看一眼。