spellfix1
Package spellfix1 implements a fuzzy-text-matching virtual table inspired by SQLite’s spellfix1 extension. Build a vocabulary by inserting words, then query with WHERE word MATCH ? to retrieve the closest matches by edit distance, grouped phonetically.
CREATE VIRTUAL TABLE vocab USING spellfix1;INSERT INTO vocab(word) VALUES ('apple'), ('banana'), ('cherry'), ('aple');SELECT word, distance FROM vocab WHERE word MATCH 'aple' LIMIT 3;-- aple 0-- apple 1-- ... etc.For a typed Go handle over this vtab — Create / Add / AddMany / Size / Correct / CorrectSQL / Drop, mirroring vec.Table and fts.Index — see Vocab. The raw SQL above and the typed API are interchangeable.
Schema
Section titled “Schema”word TEXT -- the candidate vocabulary entryrank INTEGER -- caller-supplied frequency score (0 if not given)distance INTEGER -- Damerau-Levenshtein distance from the queryscore INTEGER -- distance*1024 - rank (lower is better)matchlen INTEGER -- length of the query prefix consumedphonetic HIDDEN -- the Soundex key (4-char A-Z)top HIDDEN -- caller-supplied LIMIT overridescope HIDDEN -- max distance bound (default 4)srchcnt HIDDEN -- how many candidates the planner examinedsoundslike HIDDEN -- alternative search-spelling (rarely used)What’s NOT included from upstream spellfix1
Section titled “What’s NOT included from upstream spellfix1”- Cyrillic / Greek / other non-Latin transliteration tables.
- Caller-tunable cost matrices (the
editdist3cost-matrix API). - The Russian-language phonetic encoder.
- The
editdist1SQL function (use(SELECT distance FROM vocab WHERE word MATCH ? LIMIT 1)to get the distance directly).
We use Soundex for phonetic grouping (well-defined, simple, language-agnostic) and Damerau-Levenshtein for distance (covers transpositions in addition to insert/delete/substitute).
Persistence
Section titled “Persistence”The vocabulary lives in a shadow table named <vtab>_storage on the same schema. Vocabulary survives db.Close() / reconnect.
Ported in spirit from SQLite spellfix1 — same SQL surface, simpler algorithm.
Full API: pkg.go.dev/gosqlite.org/ext/spellfix1