The sqlean-fuzzy extension provides fuzzy-matching helpers:
- Measure distance between two strings.
- Compute phonetic string code.
- Transliterate a string.
If you want a ready-to-use mechanism to search a large vocabulary for close matches, see the spellfix extension instead.
Concepts • String distances • Phonetic codes • Transliteration • Acknowledgements • Installation and usage
The string distances, except for fuzzy_editdist, work with Unicode characters, not bytes:
select fuzzy_leven('café', 'cafe');
-- 1fuzzy_editdist and the phonetic codes only support ASCII strings and return NULL for anything else. Use the translit function to convert a string from UTF-8 to plain ASCII first:
select fuzzy_soundex('Müller');
-- NULL
select fuzzy_soundex(fuzzy_translit('Müller'));
-- M460Invalid UTF-8 bytes are treated as separate replacement characters (U+FFFD). A NUL character is an ordinary character, it doesn't end the string.
If any function argument is NULL, the result is NULL.
Like SQLite's own string functions, the extension returns NULL for invalid data instead of raising an error, so one bad row doesn't abort the whole query:
- A BLOB argument:
fuzzy_leven(x'616263', 'abc')isNULL. Numbers are converted to text, sofuzzy_leven(123, 124)is1. - A non-ASCII string passed to
fuzzy_editdistor a phonetic code function:fuzzy_caver('café')isNULL. - Strings of different lengths passed to
fuzzy_hamming:fuzzy_hamming('abc', 'ab')isNULL.
These functions measure the distance between two strings.
damlev • editdist • hamming • jarowin • leven • osadist
fuzzy_damlev(x, y)
Calculates the Damerau-Levenshtein distance: the minimum number of insertions, deletions, substitutions and transpositions of two adjacent characters needed to turn one string into the other.
select fuzzy_damlev('awesome', 'aewsme');
-- 2Unlike fuzzy_osadist, it allows editing a substring more than once:
select fuzzy_damlev('ca', 'abc');
-- 2Aliased as dlevenshtein(x, y).
fuzzy_editdist(x, y)
Calculates the spellcheck edit distance from the spellfix extension. The cost of each edit depends on the characters involved: changing a letter's case is free, replacing a consonant with a similar one is cheaper than with a dissimilar one, and inserting characters at the end of the string is cheap. A smaller number means a closer match.
select fuzzy_editdist('awesome', 'aewsme');
-- 215If x ends with *, it matches any string that starts with the rest of x.
Only supports ASCII strings, returns NULL otherwise.
Aliased as edit_distance(x, y).
fuzzy_hamming(x, y)
Calculates the Hamming distance: the number of positions at which the characters differ. Returns NULL if the strings have a different number of characters.
select fuzzy_hamming('awesome', 'aewsome');
-- 2Aliased as hamming(x, y).
fuzzy_jarowin(x, y)
Calculates the Jaro-Winkler similarity, from 0 (no similarity) to 1 (equal strings). Strings with a common prefix (of up to 4 characters) are considered more similar.
select fuzzy_jarowin('awesome', 'aewsme');
-- 0.907142857142857Aliased as jaro_winkler(x, y).
fuzzy_leven(x, y)
Calculates the Levenshtein distance: the minimum number of insertions, deletions and substitutions needed to turn one string into the other.
select fuzzy_leven('awesome', 'aewsme');
-- 3Aliased as levenshtein(x, y).
fuzzy_osadist(x, y)
Calculates the Optimal String Alignment distance: like fuzzy_damlev, but each substring can be edited at most once.
select fuzzy_osadist('awesome', 'aewsme');
-- 2
select fuzzy_osadist('ca', 'abc');
-- 3Aliased as osa_distance(x, y).
These functions compute phonetic string codes: similar-sounding strings have the same code.
The functions only support ASCII strings, and return NULL otherwise. Use the translit function to convert the input string from UTF-8 to plain ASCII.
caver • phonetic • soundex • rsoundex
fuzzy_caver(x)
Calculates the Caverphone 2.0 code, which is always 10 characters long, or empty for a string without letters.
select fuzzy_caver('awesome');
-- AWSM111111Aliased as caverphone(x).
fuzzy_phonetic(x)
Calculates the spellcheck phonetic code from the spellfix extension. Ignores case.
select fuzzy_phonetic('awesome');
-- ABACAMAAliased as phonetic_hash(x).
fuzzy_soundex(x)
Calculates the Soundex code: the first letter followed by three digits. Leading non-letters are skipped, and a string without letters has an empty code.
select fuzzy_soundex('awesome');
-- A250Aliased as soundex(x).
fuzzy_rsoundex(x)
Calculates the Refined Soundex code: the first letter followed by a variable number of digits. Leading non-letters are skipped, and a string without letters has an empty code.
select fuzzy_rsoundex('awesome');
-- A03080Aliased as rsoundex(x).
fuzzy_translit(x)
Converts the string from UTF-8 into plain ASCII by replacing each non-ASCII character with some combination of ASCII characters.
The phonetic functions and fuzzy_editdist are ASCII only, so to work with a Unicode string, you should first transliterate it:
select fuzzy_translit('sí señor');
-- si senor
select fuzzy_translit('привет');
-- privetCharacters without a transliteration (and invalid UTF-8 bytes) become ?:
select fuzzy_translit('oh my 😅');
-- oh my ?Aliased as translit(x).
fuzzy_script(x)
Returns the ISO 15924 numeric code of the script the string is written in:
215for Latin,220for Cyrillic,200for Greek,125for Hebrew,160for Arabic,998for a mix of these,999for none of them.
Characters that belong to no script (digits, punctuation, symbols, spaces) are ignored, except that a string with digits but no letters is considered Latin. The Latin script includes the accented letters of Latin-1 and Latin Extended, but not the symbols there, such as «, © or ×.
select fuzzy_script('Ελλάδα');
-- 200
select fuzzy_script('hello мир');
-- 998
select fuzzy_script('日本');
-- 999Aliased as script_code(x).
Adapted from go-edlib by Hugo Bollon, libstrcmp by Ross Bayer and spellfix.c by D. Richard Hipp.
SQLite command-line interface:
sqlite> .load ./fuzzy
sqlite> select fuzzy_soundex('hello');
See How to install an extension for usage with IDE, Python, etc.
↓ Download the extension.
⛱ Explore other extensions.
★ Subscribe to stay on top of new features.