The editdist3 algorithm is a function that computes the minimum edit distance (a.k.a. the Levenshtein distance) between two input strings. Features of editdist3 include: * It works with unicode (UTF8) text. * A table of insertion, deletion, and substitution costs can be provided by the application. * Multi-character insertsions, deletions, and substitutions can be enumerated in the cost table.
The cost table can be named anything you want - it does not have to be called "editcost". And the table can contain additional columns. However, it the table must contain the four columns show above, with exactly the names shown. The iLang column is a non-negative integer that identifies a set of costs appropriate for a particular language. The editdist3 function will only use a single iLang value for any given edit-distance computation. The default value is 0. It is recommended that applications that only need to use a single langauge always use iLang==0 for all entries. The iCost column is the numeric cost of transforming cFrom into cTo. This value should be a non-negative integer, and should probably be less than 100. The default single-character insertion and deletion costs are 100 and the default single-character to single-character substitution cost is 150. A cost of 10000 or more is considered "infinite" and causes the rule to be ignored. The cFrom and cTo columns show edit transformation strings. Either or both columns may contain more than one character. Or either column (but not both) may hold an empty string. When cFrom is empty, that is the cost of inserting cTo. When cTo is empty, that is the cost of deleting cFrom. In the spellfix1 algorithm, cFrom is the text as the user entered it and cTo is the correctly spelled text as it exists in the database. The goal of the editdist3 algorithm is to determine how close the user-entered text is to the dictionary text. There are three special-case entries in the cost table:CREATE TABLE editcost( iLang INT, -- The language ID cFrom TEXT, -- Convert text from this cTo TEXT, -- Convert text into this iCost INT -- The cost of doing the conversionnn );
| cFrom | cTo | Meaning |
|---|---|---|
| '' | '?' | The default insertion cost |
| '?' | '' | The default deletion cost |
| '?' | '?' | The default substitution cost |
The rule above says that the letter "a" in user input can be matched against the letter "ä" in the dictionary with a penalty of 5.INSERT INTO editcost(iLang, cFrom, cTo, iCost) VALUES(0, 'a', 'ä', 5);
The number of characters in cFrom and cTo do not need to be the same. The rule above says that "ss" on user input will match "ß" with a penalty of 8.INSERT INTO editcost(iLang, cFrom, cTo, iCost) VALUES(0, 'ss', 'ß', 8);