Local recipe parser for meal prep
A local parser handled 43 recipes before mixed units broke ingredient extraction.
I built a local recipe parser for a meal-prep side project because I was tired of copying ingredients by hand into a shopping list. The first pass was simple: scrape the page, pull the ingredient lines, normalize quantities, and dump everything into one JSON file. It worked on 43 recipes, which felt good for about a day.
Then I hit the ugly cases: `1 1/2 cups`, `300ml`, `2 tbsp`, and the occasional line that says `salt to taste` like that counts as a unit. My parser started turning half fractions into zeroes and merging milliliters into cups with no warning. One bad example poisoned the rest of the batch.
I spent most of the time on normalization, not parsing. A recipe with a clean structure is easy; a recipe with parentheses, commas, and “about” in front of everything is where the mess starts. I used ChatGPT to generate test cases, but the actual bug fixes were boring regex and a tiny unit conversion table I kept editing by hand.
I also made one bad assumption: that ingredient names and quantities always come in one line. A few sites split them across nested spans, so the scraper would pick up the wrong text node and invent nonsense like “olive oil 0.5 0.5”. I only caught that after comparing the output with a grocery list and noticing I was buying twice as much onions as I needed.
What surprised me most was that the hard part wasn’t NLP, it was the weird human formatting. If I revisit it, I’d rather build a permissive parser with confidence flags than pretend every recipe can be normalized cleanly. I still don’t know how much “good enough” is for something that feeds directly into dinner planning.
8 replies
我也被 1 1/2 卡过,最后直接加了 fraction 预处理。
你是怎么处理“salt to taste”这种没数量的行?
先标成可选项,不进总量,省得把购物清单搞炸。
300ml 和 300 mL 最好先统一成毫升,再做单位换算。
本地解析很香,但不同站点的 HTML 变化会一直追着你。
我后来是把“1 1/2 cup”先拆成数字和单位,再统一进 grams,少踩很多坑。
除了 fraction,连“pinch”“dash”这种也要单独词表,不然后面总量会一直漏。
你现在对复合单位怎么拆,像“1 cup chopped onions”会不会把修饰词也吞掉?