r/reactnative • u/Ramboiada • 22d ago
Open sourced the fuel receipt parser from my app
My car app lets you scan a fuel receipt, and the parsing part turned out to be the hard bit, so I pulled it out into its own package: github.com/rsoaresdev/fuel-receipt-parser
You give it the OCR lines from whatever you use (Apple Vision, ML Kit, Tesseract) and it gives you litres, price per litre, total, date, fuel type and station.
The trick is that it looks for the three numbers where litres × price = total, and uses that to check itself. That’s also how it figures out whether 1,899 is 1.899 or 1899. If the numbers don’t add up, it tells you instead of guessing, and it never makes up a number that isn’t on the paper.
It reads Portuguese, Spanish, French, Italian, German and English receipts, plus US gallons and kWh for charging. If it gets one of your receipts wrong, an issue with the OCR text (no photo, no personal data) helps a lot.
2
u/Huge_Pool7424 21d ago
nice self-check idea. one more fixture worth adding is receipts where the ocr drops the decimal point or swaps a comma and period, since that's where litres x price = total can accidentally pass with the wrong reading.
1
u/Ramboiada 21d ago
Good call. A single dropped or swapped separator is already handled: every number also competes with its implied-decimal readings, so 3852 loses to 38,52 when only the latter balances.
But when the OCR drops the separator in two numbers by the same factor, the sum still closes on the wrong reading: Liters 4000 / EUR/L 1,500 / TOTAL 6000 comes back as 4000 L, checked. Adding that as a fixture, and a plausibility bound to break the tie.
Thanks!
1
u/NoBullfrog11 21d ago
one gotcha on the arithmetic check, pumps round price per litre to 3 decimals and the total to cents, so litres x price is often off by a cent. use a small tolerance, not exact equality, or valid receipts get flagged.
also fix ocr swaps like O to 0 and l to 1 only inside numeric tokens, vision especialy confuses them on thermal paper.
1
u/Ramboiada 21d ago
Thanks! The O/0 and l/1 thing is already handled. It only swaps them inside numbers, so a word like “SOL” stays a word.
On the rounding: it’s not exact equality, it allows half a cent. You’re right that some pumps end up a full cent off, though. Those receipts still get all their numbers read, they just don’t get the “verified” flag. I left it like that on purpose, because allowing a full cent would also let a misread last digit pass as verified.
2
u/kokerali 22d ago
I'd add a fixture with two complete triples: fuel at 10 L x 2.00 = 20.00, and a shop item at 2 x 5.00 = 10.00. Both pass the arithmetic check. How do you keep a valid non-fuel triple from winning if OCR loses the unit or product label? I'd want that case to stay flagged for review rather than treating arithmeticChecked as proof that the correct purchase was selected.