Offline voice dictation with fuzzy matching against a catalog
speech-to-textfuzzy-matchingjaro-winklerlexerflutterofflinelocal-first
The plumb Android app (Flutter, local-first) lets a tradesman say "two three quarter elbows, labor two hours, wax ring" and get priced invoice lines without any AI or network: on-device speech to text, then a small lexer, a dictation parser, and a fuzzy matcher against his own pricebook. AI was kept only as an optional extra (reading receipt photos). The design generalizes to any "speak items from a known catalog" problem: groceries, inventory, parts orders.
Pipeline
- Speech: the
speech_to_textplugin, auto-restarted after each pause so a long dictation is not cut off, with an optional on-device-only mode. (Verified to initialize on an emulator; accuracy with trade vocabulary on a real phone was still unmeasured.) - Lexer (
core/lexer.dart): classifies tokens as quantity, pipe size, unit or word. This is where "two three quarter elbows" (2 x 3/4" elbow) differs from "two inch PVC" (one item, 2" size). A separatenameModelexes catalog names. - Dictation parser (
core/dictation.dart): splits the transcript into items on pauses and commas, "and", numbers, and "a/an"; then rejoins splits when the whole phrase is itself a catalog item ("tub and shower valve", "install a toilet"). Moves a duration ("two hours") onto the per-hour labor item even when it appears elsewhere in the phrase. - Matcher (
core/matcher.dart): scores each phrase against every item name and alias.
Scoring
- Text normalization helpers (
core/text.dart): a phonetic sound key, Jaro-Winkler similarity, plural stemming, and awordSimilaritythat deliberately scores weak resemblances as 0, so noise does not add up to a false match. - Sizes must match exactly. Fuzzy text, never fuzzy dimensions: "3/4 copper elbow" can never match a 1/2" one.
- Thresholds:
matchedat score 0.7 or higher;ambiguouswhen the top two candidates are within 0.06 of each other; otherwiseunmatched. - Named
ItemMatcher(notMatcher) to avoid clashing with the test library'sMatcher.
Never guess
- Unmatched: the line is stored unpriced with a "Not in pricebook" flag.
- Ambiguous (e.g. size missing): ask "Which one?" with the tied items as chips.
- Flagged lines block sending the document.
Learning from corrections, carefully
When the user fixes an unmatched line, the phrase-to-item mapping is saved (table learned),
so the same words match next time. When the user resolves an ambiguous line, nothing is
learned on purpose: "copper pipe" should keep asking which size instead of silently becoming
whatever was picked last.
Surrounding app decisions
- Everything in SQLite on the phone (
sqlite3package; schema versioned withPRAGMA user_version); no account, no server. Documents leave as PDFs through the Android share sheet. - Autosave everywhere; no Save button in the editor.
- A
--dart-define=DEMO=trueflag seeds sample data into an empty database for demos, never for release builds. - Unit tests cover the lexer, matcher, dictation parser, store, PDF and receipt rules; pure-Dart logic like this is the cheapest part of the app to test thoroughly.
See also
- Browser dictation with Web Speech and MediaRecorder
- Designing a quoting and invoicing app for a solo tradesman
- Driving the Android emulator headlessly for QA
- Android app signing keys and why losing one is permanent