Skip to content

Latest commit

 

History

History
319 lines (212 loc) · 8.39 KB

fuzzy.md

File metadata and controls

319 lines (212 loc) · 8.39 KB

fuzzy: Fuzzy string matching and phonetics in SQLite

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

Concepts

Unicode and ASCII

The string distances, except for fuzzy_editdist, work with Unicode characters, not bytes:

select fuzzy_leven('café', 'cafe');
-- 1

fuzzy_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'));
-- M460

Invalid UTF-8 bytes are treated as separate replacement characters (U+FFFD). A NUL character is an ordinary character, it doesn't end the string.

NULL and invalid input

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') is NULL. Numbers are converted to text, so fuzzy_leven(123, 124) is 1.
  • A non-ASCII string passed to fuzzy_editdist or a phonetic code function: fuzzy_caver('café') is NULL.
  • Strings of different lengths passed to fuzzy_hamming: fuzzy_hamming('abc', 'ab') is NULL.

String distances

These functions measure the distance between two strings.

damlev • editdist • hamming • jarowin • leven • osadist

fuzzy_damlev

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');
-- 2

Unlike fuzzy_osadist, it allows editing a substring more than once:

select fuzzy_damlev('ca', 'abc');
-- 2

Aliased as dlevenshtein(x, y).

fuzzy_editdist

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');
-- 215

If 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

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');
-- 2

Aliased as hamming(x, y).

fuzzy_jarowin

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.907142857142857

Aliased as jaro_winkler(x, y).

fuzzy_leven

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');
-- 3

Aliased as levenshtein(x, y).

fuzzy_osadist

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');
-- 3

Aliased as osa_distance(x, y).

Phonetic codes

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

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');
-- AWSM111111

Aliased as caverphone(x).

fuzzy_phonetic

fuzzy_phonetic(x)

Calculates the spellcheck phonetic code from the spellfix extension. Ignores case.

select fuzzy_phonetic('awesome');
-- ABACAMA

Aliased as phonetic_hash(x).

fuzzy_soundex

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');
-- A250

Aliased as soundex(x).

fuzzy_rsoundex

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');
-- A03080

Aliased as rsoundex(x).

Transliteration

translit • script

fuzzy_translit

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('привет');
-- privet

Characters without a transliteration (and invalid UTF-8 bytes) become ?:

select fuzzy_translit('oh my 😅');
-- oh my ?

Aliased as translit(x).

fuzzy_script

fuzzy_script(x)

Returns the ISO 15924 numeric code of the script the string is written in:

  • 215 for Latin,
  • 220 for Cyrillic,
  • 200 for Greek,
  • 125 for Hebrew,
  • 160 for Arabic,
  • 998 for a mix of these,
  • 999 for 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('日本');
-- 999

Aliased as script_code(x).

Acknowledgements

Adapted from go-edlib by Hugo Bollon, libstrcmp by Ross Bayer and spellfix.c by D. Richard Hipp.

Installation and usage

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.