Skip to content

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.

word TEXT -- the candidate vocabulary entry
rank INTEGER -- caller-supplied frequency score (0 if not given)
distance INTEGER -- Damerau-Levenshtein distance from the query
score INTEGER -- distance*1024 - rank (lower is better)
matchlen INTEGER -- length of the query prefix consumed
phonetic HIDDEN -- the Soundex key (4-char A-Z)
top HIDDEN -- caller-supplied LIMIT override
scope HIDDEN -- max distance bound (default 4)
srchcnt HIDDEN -- how many candidates the planner examined
soundslike 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 editdist3 cost-matrix API).
  • The Russian-language phonetic encoder.
  • The editdist1 SQL 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).

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