Debian Codegraph @

www/spellfix1.html

raw
1 <!DOCTYPE html>
2 <html><head>
3 <meta name="viewport" content="width=device-width, initial-scale=1.0">
4 <meta http-equiv="content-type" content="text/html; charset=UTF-8">
5 <link href="sqlite.css" rel="stylesheet">
6 <title>The Spellfix1 Virtual Table</title>
7 <!-- path= -->
8 </head>
9 <body>
10 <div class=nosearch>
11 <a href="index.html">
12 <img class="logo" src="images/sqlite370_banner.gif" alt="SQLite" border="0">
13 </a>
14 <div><!-- IE hack to prevent disappearing logo --></div>
15 <div class="tagline desktoponly">
16 Small. Fast. Reliable.<br>Choose any three.
17 </div>
18 <div class="menu mainmenu">
19 <ul>
20 <li><a href="index.html">Home</a>
21 <li class='mobileonly'><a href="javascript:void(0)" onclick='toggle_div("submenu")'>Menu</a>
22 <li class='wideonly'><a href='about.html'>About</a>
23 <li class='desktoponly'><a href="docs.html">Documentation</a>
24 <li class='desktoponly'><a href="download.html">Download</a>
25 <li class='wideonly'><a href='copyright.html'>License</a>
26 <li class='desktoponly'><a href="support.html">Support</a>
27 <li class='desktoponly'><a href="prosupport.html">Purchase</a>
28 <li class='search' id='search_menubutton'>
29 <a href="javascript:void(0)" onclick='toggle_search()'>Search</a>
30 </ul>
31 </div>
32 <div class="menu submenu" id="submenu">
33 <ul>
34 <li><a href='about.html'>About</a>
35 <li><a href='docs.html'>Documentation</a>
36 <li><a href='download.html'>Download</a>
37 <li><a href='support.html'>Support</a>
38 <li><a href='prosupport.html'>Purchase</a>
39 </ul>
40 </div>
41 <div class="searchmenu" id="searchmenu">
42 <form method="GET" action="search">
43 <select name="s" id="searchtype">
44 <option value="d">Search Documentation</option>
45 <option value="c">Search Changelog</option>
46 </select>
47 <input type="text" name="q" id="searchbox" value="">
48 <input type="submit" value="Go">
49 </form>
50 </div>
51 </div>
52 <script>
53 function toggle_div(nm) {
54 var w = document.getElementById(nm);
55 if( w.style.display=="block" ){
56 w.style.display = "none";
57 }else{
58 w.style.display = "block";
59 }
60 }
61 function toggle_search() {
62 var w = document.getElementById("searchmenu");
63 if( w.style.display=="block" ){
64 w.style.display = "none";
65 } else {
66 w.style.display = "block";
67 setTimeout(function(){
68 document.getElementById("searchbox").focus()
69 }, 30);
70 }
71 }
72 function div_off(nm){document.getElementById(nm).style.display="none";}
73 window.onbeforeunload = function(e){div_off("submenu");}
74 /* Disable the Search feature if we are not operating from CGI, since */
75 /* Search is accomplished using CGI and will not work without it. */
76 if( !location.origin || !location.origin.match || !location.origin.match(/http/) ){
77 document.getElementById("search_menubutton").style.display = "none";
78 }
79 /* Used by the Hide/Show button beside syntax diagrams, to toggle the */
80 function hideorshow(btn,obj){
81 var x = document.getElementById(obj);
82 var b = document.getElementById(btn);
83 if( x.style.display!='none' ){
84 x.style.display = 'none';
85 b.innerHTML='show';
86 }else{
87 x.style.display = '';
88 b.innerHTML='hide';
89 }
90 return false;
91 }
92 var antiRobot = 0;
93 function antiRobotGo(){
94 if( antiRobot!=3 ) return;
95 antiRobot = 7;
96 var j = document.getElementById("mtimelink");
97 if(j && j.hasAttribute("data-href")) j.href=j.getAttribute("data-href");
98 }
99 function antiRobotDefense(){
100 document.body.onmousedown=function(){
101 antiRobot |= 2;
102 antiRobotGo();
103 document.body.onmousedown=null;
104 }
105 document.body.onmousemove=function(){
106 antiRobot |= 2;
107 antiRobotGo();
108 document.body.onmousemove=null;
109 }
110 setTimeout(function(){
111 antiRobot |= 1;
112 antiRobotGo();
113 }, 100)
114 antiRobotGo();
115 }
116 antiRobotDefense();
117 </script>
118 <div class=fancy>
119 <div class=nosearch>
120 <div class="fancy_title">
121 The Spellfix1 Virtual Table
122 </div>
123 <div class="fancy_toc">
124 <a onclick="toggle_toc()">
125 <span class="fancy_toc_mark" id="toc_mk">&#x25ba;</span>
126 Table Of Contents
127 </a>
128 <div id="toc_sub"><div class="fancy-toc1"><a href="#overview">1. Overview</a></div>
129 <div class="fancy-toc1"><a href="#search_refinements">2. Search Refinements</a></div>
130 <div class="fancy-toc1"><a href="#virtual_table_details">3. Virtual Table Details</a></div>
131 <div class="fancy-toc1"><a href="#algorithm">4. Algorithm</a></div>
132 <div class="fancy-toc1"><a href="#configurable_edit_distance">5. Configurable Edit Distance</a></div>
133 <div class="fancy-toc1"><a href="#dealing_with_unusual_and_difficult_spellings">6. Dealing With Unusual And Difficult Spellings</a></div>
134 <div class="fancy-toc1"><a href="#auxiliary_functions">7. Auxiliary Functions</a></div>
135 <div class="fancy-toc1"><a href="#the_editdist3_function">8. The editdist3 function</a></div>
136 <div class="fancy-toc1"><a href="#the_editdist3_cost_table">9. The editdist3 COST table</a></div>
137 <div class="fancy-toc1"><a href="#experimenting_with_the_editcost3_function">10. Experimenting with the editcost3() function</a></div>
138 </div>
139 </div>
140 <script>
141 function toggle_toc(){
142 var sub = document.getElementById("toc_sub")
143 var mk = document.getElementById("toc_mk")
144 if( sub.style.display!="block" ){
145 sub.style.display = "block";
146 mk.innerHTML = "&#x25bc;";
147 } else {
148 sub.style.display = "none";
149 mk.innerHTML = "&#x25ba;";
150 }
151 }
152 </script>
153 </div>
154
155
156
157
158 <h1 id="overview"><span>1. </span>Overview</h1>
159
160 <p>This spellfix1 <a href="vtab.html">virtual table</a> can be used to search
161 a large vocabulary for close matches. For example, spellfix1
162 can be used to suggest corrections to misspelled words. Or,
163 it could be used with <a href="fts3.html#fts4">FTS4</a> to do full-text search using potentially
164 misspelled words.
165
166 </p><p>The implementation for the spellfix1 virtual table is held in the
167 SQLite source tree in the miscellaneous extensions folder and in
168 particular in the file
169 <a href="https://www.sqlite.org/src/finfo?name=ext/misc/spellfix.c">ext/misc/spellfix1.c</a>.
170 The spellfix1 virtual table is not included in the SQLite <a href="amalgamation.html">amalgamation</a>
171 and is not a part of any standard SQLite build. It is a <a href="loadext.html">loadable extension</a>.
172
173 </p><p>Once the spellfix1 extension is loaded, an instance of the spellfix1
174 virtual table is created like this:
175
176 </p><blockquote><pre>
177 CREATE VIRTUAL TABLE demo USING spellfix1;
178 </pre></blockquote>
179
180 <p>The "spellfix1" term is the name of the spellfix module and must be
181 entered as shown. The "demo" term is the
182 name of the virtual table you will be creating and can be altered
183 to suit the needs of your application. The virtual table is initially
184 empty. In order for the virtual table to be useful, you will need to
185 populate it with your vocabulary. Suppose you
186 have a list of words in a table named "big_vocabulary". Then do this:
187
188 </p><blockquote><pre>
189 INSERT INTO demo(word) SELECT word FROM big_vocabulary;
190 </pre></blockquote>
191
192 <p>If you intend to use this virtual table in cooperation with an <a href="fts3.html#fts4">FTS4</a>
193 table (for spelling correction of search terms) then you might extract
194 the vocabulary using an <a href="fts3.html#fts4aux">fts4aux</a> table:
195
196 </p><blockquote><pre>
197 INSERT INTO demo(word) SELECT term FROM search_aux WHERE col='*';
198 </pre></blockquote>
199
200 <p>You can also provide the virtual table with a "rank" for each word.
201 The "rank" is an estimate of how common the word is. Larger numbers
202 mean the word is more common. If you omit the rank when populating
203 the table, then a rank of 1 is assumed. But if you have rank
204 information, you can supply it and the virtual table will show a
205 slight preference for selecting more commonly used terms. To
206 populate the rank from an fts4aux table "search_aux" do something
207 like this:
208
209 </p><blockquote><pre>
210 INSERT INTO demo(word,rank)
211 SELECT term, documents FROM search_aux WHERE col='*';
212 </pre></blockquote>
213
214 <p>To query the virtual table, include a MATCH operator in the WHERE
215 clause. For example:
216
217 </p><blockquote><pre>
218 SELECT word FROM demo WHERE word MATCH 'kennasaw';
219 </pre></blockquote>
220
221 <p>Using a dataset of American place names (derived from
222 <a href="http://geonames.usgs.gov/domestic/download_data.htm">http://geonames.usgs.gov/domestic/download_data.htm</a>) the query above
223 returns 20 results beginning with:
224
225 </p><blockquote><pre>
226 kennesaw
227 kenosha
228 kenesaw
229 kenaga
230 keanak
231 </pre></blockquote>
232
233 <p>If you append the character '*' to the end of the pattern, then
234 a prefix search is performed. For example:
235
236 </p><blockquote><pre>
237 SELECT word FROM demo WHERE word MATCH 'kennes*';
238 </pre></blockquote>
239
240 <p>Yields 20 results beginning with:
241
242 </p><blockquote><pre>
243 kennesaw
244 kennestone
245 kenneson
246 kenneys
247 keanes
248 keenes
249 </pre></blockquote>
250
251 <h1 id="search_refinements"><span>2. </span>Search Refinements</h1>
252
253 <p>By default, the spellfix1 table returns no more than 20 results.
254 (It might return less than 20 if there were fewer good matches.)
255 You can change the upper bound on the number of returned rows by
256 adding a "top=N" term to the WHERE clause of your query, where N
257 is the new maximum. For example, to see the 5 best matches:
258
259 </p><blockquote><pre>
260 SELECT word FROM demo WHERE word MATCH 'kennes*' AND top=5;
261 </pre></blockquote>
262
263 <p>Each entry in the spellfix1 virtual table is associated with
264 a particular language, identified by the integer "langid" column.
265 The default langid is 0 and if no other actions are taken, the
266 entire vocabulary is a part of the 0 language. But if your application
267 needs to operate in multiple languages, then you can specify different
268 vocabulary items for each language by specifying the langid field
269 when populating the table. For example:
270
271 </p><blockquote><pre>
272 INSERT INTO demo(word,langid) SELECT word, 0 FROM en_vocabulary;
273 INSERT INTO demo(word,langid) SELECT word, 1 FROM de_vocabulary;
274 INSERT INTO demo(word,langid) SELECT word, 2 FROM fr_vocabulary;
275 INSERT INTO demo(word,langid) SELECT word, 3 FROM ru_vocabulary;
276 INSERT INTO demo(word,langid) SELECT word, 4 FROM cn_vocabulary;
277 </pre></blockquote>
278
279 <p>After the virtual table has been populated with items from multiple
280 languages, specify the language of interest using a "langid=N" term
281 in the WHERE clause of the query:
282
283 </p><blockquote><pre>
284 SELECT word FROM demo WHERE word MATCH 'hildes*' AND langid=1;
285 </pre></blockquote>
286
287 <p>Note that if you do not include the "langid=N" term in the WHERE clause,
288 the search will be against language 0 (English in the example above.)
289 All spellfix1 searches are against a single language id. There is no
290 way to search all languages at once.
291
292
293 </p><h1 id="virtual_table_details"><span>3. </span>Virtual Table Details</h1>
294
295 <p>Each row in the spellfix1 virtual table has a unique rowid
296 with seven columns plus five extra hidden columns.
297 The columns are as follows:
298
299 </p><dl>
300 <dt><p><b>rowid</b></p></dt><dd>
301 A unique integer number associated with each
302 vocabulary item in the table. This can be used
303 as a foreign key on other tables in the database.
304
305 </dd><dt><p><b>word</b></p></dt><dd>
306 The text of the word that matches the pattern.
307 Both word and pattern can contain unicode characters
308 and can be mixed case.
309
310 </dd><dt><p><b>rank</b></p></dt><dd>
311 This is the rank of the word, as specified in the
312 original INSERT statement.
313
314
315 </dd><dt><p><b>distance</b></p></dt><dd>
316 This is an edit distance or Levenshtein distance going
317 from the pattern to the word.
318
319 </dd><dt><p><b>langid</b></p></dt><dd>
320 This is the language-id of the word. All queries are
321 against a single language-id, which defaults to 0.
322 For any given query this value is the same on all rows.
323
324 </dd><dt><p><b>score</b></p></dt><dd>
325 The score is a combination of rank and distance. The
326 idea is that a lower score is better. The virtual table
327 attempts to find words with the lowest score and
328 by default (unless overridden by ORDER BY) returns
329 results in order of increasing score.
330
331 </dd><dt><p><b>matchlen</b></p></dt><dd>
332 In a prefix search, the matchlen is the number of characters in
333 the string that match against the prefix. For a non-prefix search,
334 this is the same as length(word).
335
336 </dd><dt><p><b>phonehash</b></p></dt><dd>
337 This column shows the phonetic hash prefix that was used to restrict
338 the search. For any given query, this column should be the same for
339 every row. This information is available for diagnostic purposes and
340 is not normally considered useful in real applications.
341
342 </dd><dt><p><b>top</b></p></dt><dd>
343 (HIDDEN) For any query, this value is the same on all
344 rows. It is an integer which is the maximum number of
345 rows that will be output. The actually number of rows
346 output might be less than this number, but it will never
347 be greater. The default value for top is 20, but that
348 can be changed for each query by including a term of
349 the form "top=N" in the WHERE clause of the query.
350
351 </dd><dt><p><b>scope</b></p></dt><dd>
352 (HIDDEN) For any query, this value is the same on all
353 rows. The scope is a measure of how widely the virtual
354 table looks for matching words. Smaller values of
355 scope cause a broader search. The scope is normally
356 chosen automatically and is capped at 4. Applications
357 can change the scope by including a term of the form
358 "scope=N" in the WHERE clause of the query. Increasing
359 the scope will make the query run faster, but will reduce
360 the possible corrections.
361
362 </dd><dt><p><b>srchcnt</b></p></dt><dd>
363 (HIDDEN) For any query, this value is the same on all
364 rows. This value is an integer which is the number of
365 words examined using the edit-distance algorithm to
366 find the top matches that are ultimately displayed. This
367 value is for diagnostic use only.
368
369 </dd><dt><p><b>soundslike</b></p></dt><dd>
370 (HIDDEN) When inserting vocabulary entries, this field
371 can be set to a spelling that matches what the word
372 sounds like. See the DEALING WITH UNUSUAL AND DIFFICULT
373 SPELLINGS section below for details.
374
375 </dd><dt><p><b>command</b></p></dt><dd>
376 (HIDDEN) The value of the "command" column is always NULL. However,
377 applications can insert special strings into the "command" column in order
378 to provoke certain behaviors in the spellfix1 virtual table.
379 For example, inserting the string 'reset' into the "command" column
380 will cause the virtual table to reread its edit distance weights
381 (if there are any).
382 </dd></dl>
383
384 <h1 id="algorithm"><span>4. </span>Algorithm</h1>
385
386 <p>The spellfix1 virtual table creates a single
387 shadow table named "%_vocab" (where the % is replaced by the name of
388 the virtual table; Ex: "demo_vocab" for the "demo" virtual table).
389 the shadow table contains the following columns:
390
391 </p><dl>
392 <dt><p><b>id</b></p></dt><dd>
393 The unique id (INTEGER PRIMARY KEY)
394
395 </dd><dt><p><b>rank</b></p></dt><dd>
396 The rank of word.
397
398 </dd><dt><p><b>langid</b></p></dt><dd>
399 The language id for this entry.
400
401 </dd><dt><p><b>word</b></p></dt><dd>
402 The original UTF8 text of the vocabulary word
403
404 </dd><dt><p><b>k1</b></p></dt><dd>
405 The word transliterated into lower-case ASCII.
406 There is a standard table of mappings from non-ASCII
407 characters into ASCII. Examples: "æ" -> "ae",
408 "þ" -> "th", "ß" -> "ss", "á" -> "a", ... The
409 accessory function spellfix1_translit(X) will do
410 the non-ASCII to ASCII mapping. The built-in lower(X)
411 function will convert to lower-case. Thus:
412 k1 = lower(spellfix1_translit(word)).
413
414 If the word is already all lower-case ASCII, then the k1 column
415 will contain a NULL. This reduces the storage requirements for
416 the %_vocab table and helps spellfix to run a little faster.
417 Therefore, it is advantageous to populate as much of the spellfix
418 table as possible using lower-case ASCII vocabulary.
419
420 </dd><dt><p><b>k2</b></p></dt><dd>
421 This field holds a phonetic code derived from coalesce(k1,word).
422 Letters that have similar sounds are mapped into the same symbol.
423 For example, all vowels and vowel clusters become the
424 single symbol "A". And the letters "p", "b", "f", and
425 "v" all become "B". All nasal sounds are represented
426 as "N". And so forth. The mapping is based on
427 ideas found in Soundex, Metaphone, and other
428 long-standing phonetic matching systems. This key can
429 be generated by the function spellfix1_phonehash(X).
430 Hence: k2 = spellfix1_phonehash(coalesce(k1,word))
431 </dd></dl>
432
433 <p>There is also a function for computing the Wagner edit distance or the
434 Levenshtein distance between a pattern and a word. This function
435 is exposed as spellfix1_editdist(X,Y). The edit distance function
436 returns the "cost" of converting X into Y. Some transformations
437 cost more than others. Changing one vowel into a different vowel,
438 for example is relatively cheap, as is doubling a constant, or
439 omitting the second character of a double-constant. Other transformations
440 or more expensive. The idea is that the edit distance function returns
441 a low cost for words that are similar and a higher cost for words
442 that are further apart. In this implementation, the maximum cost
443 of any single-character edit (delete, insert, or substitute) is 100,
444 with lower costs for some edits (such as transforming vowels).
445
446 </p><p>The "score" for a comparison is the edit distance between the pattern
447 and the word, adjusted down by the base-2 logarithm of the word rank.
448 For example, a match with distance 100 but rank 1000 would have a
449 score of 122 (= 100 - log2(1000) + 32) whereas a match with distance
450 100 with a rank of 1 would have a score of 131 (100 - log2(1) + 32).
451 (NB: The constant 32 is added to each score to keep it from going
452 negative in case the edit distance is zero.) In this way, frequently
453 used words get a slightly lower cost which tends to move them toward
454 the top of the list of alternative spellings.
455
456 </p><p>A straightforward implementation of a spelling corrector would be
457 to compare the search term against every word in the vocabulary
458 and select the 20 with the lowest scores. However, there will
459 typically be hundreds of thousands or millions of words in the
460 vocabulary, and so this approach is not fast enough.
461
462 </p><p>Suppose the term that is being spell-corrected is X. To limit
463 the search space, X is converted to a k2-like key using the
464 equivalent of:
465
466 </p><blockquote><pre>
467 key = spellfix1_phonehash(lower(spellfix1_translit(X)))
468 </pre></blockquote>
469
470 <p>This key is then limited to "scope" characters. The default scope
471 value is 4, but an alternative scope can be specified using the
472 "scope=N" term in the WHERE clause. After the key has been truncated,
473 the edit distance is run against every term in the vocabulary that
474 has a k2 value that begins with the abbreviated key.
475
476 </p><p>For example, suppose the input word is "Paskagula". The phonetic
477 key is "BACACALA" which is then truncated to 4 characters "BACA".
478 The edit distance is then run on the 4980 entries (out of
479 272,597 entries total) of the vocabulary whose k2 values begin with
480 BACA, yielding "Pascagoula" as the best match.
481
482 </p><p>Only terms of the vocabulary with a matching langid are searched.
483 Hence, the same table can contain entries from multiple languages
484 and only the requested language will be used. The default langid
485 is 0.
486
487 <a name="configeditdist"></a>
488
489 </p><h1 id="configurable_edit_distance"><span>5. </span>Configurable Edit Distance</h1>
490
491 <p>The built-in Wagner edit-distance function with fixed weights can be
492 replaced by the <a href="spellfix1.html#editdist3">editdist3()</a> edit-distance function
493 with application-defined weights and support for unicode, by specifying
494 the "edit_cost_table=<i>TABLENAME</i>" parameter to the spellfix1 module
495 when the virtual table is created.
496 For example:
497
498 </p><blockquote><pre>
499 CREATE VIRTUAL TABLE demo2 USING spellfix1(edit_cost_table=APPCOST);
500 </pre></blockquote>
501
502 <p>The <a href="spellfix1.html#editdist3">editdist3()</a> edit-distance function can also be selected or
503 deselected at run-time by inserting an appropriate string into the
504 "command" column of the virtual table:</p>
505
506 <blockquote><pre>
507 INSERT INTO demo2(command) VALUES('edit_cost_table=APPCOST');
508 </pre></blockquote>
509
510
511 <p>In the examples above, the APPCOST table would be interrogated to find
512 the edit distance coefficients. It is the presence of the "edit_cost_table="
513 parameter to the spellfix1 module name that causes editdist3() to be used
514 in place of the built-in edit distance function. If APPCOST is an empty
515 string, then the built-in Wagner edit-distance function is used.
516
517 </p><p>The edit distance coefficients are normally read from the APPCOST table
518 once and there after stored in memory. Hence, run-time changes to the
519 APPCOST table will not normally affect the edit distance results.
520 However, inserting the special string 'reset' into the "command" column of the
521 virtual table causes the edit distance coefficients to be reread the
522 APPCOST table. Hence, applications should run a SQL statement similar
523 to the following when changes to the APPCOST table occur:
524
525 </p><blockquote>
526 INSERT INTO demo2(command) VALUES("reset");
527 </blockquote>
528
529 <h1 id="dealing_with_unusual_and_difficult_spellings"><span>6. </span>Dealing With Unusual And Difficult Spellings</h1>
530
531 <p>The algorithm above works quite well for most cases, but there are
532 exceptions. These exceptions can be dealt with by making additional
533 entries in the virtual table using the "soundslike" column.
534
535 </p><p>For example, many words of Greek origin begin with letters "ps" where
536 the "p" is silent. Ex: psalm, pseudonym, psoriasis, psyche. In
537 another example, many Scottish surnames can be spelled with an
538 initial "Mac" or "Mc". Thus, "MacKay" and "McKay" are both pronounced
539 the same.
540
541 </p><p>Accommodation can be made for words that are not spelled as they
542 sound by making additional entries into the virtual table for the
543 same word, but adding an alternative spelling in the "soundslike"
544 column. For example, the canonical entry for "psalm" would be this:
545
546 </p><blockquote><pre>
547 INSERT INTO demo(word) VALUES('psalm');
548 </pre></blockquote>
549
550 <p>To enhance the ability to correct the spelling of "salm" into
551 "psalm", make an addition entry like this:
552
553 </p><blockquote><pre>
554 INSERT INTO demo(word,soundslike) VALUES('psalm','salm');
555 </pre></blockquote>
556
557 <p>It is ok to make multiple entries for the same word as long as
558 each entry has a different soundslike value. Note that if no
559 soundslike value is specified, the soundslike defaults to the word
560 itself.
561
562 </p><p>Listed below are some cases where it might make sense to add additional
563 soundslike entries. The specific entries will depend on the application
564 and the target language.
565
566 </p><ul>
567 <li>Silent "p" in words beginning with "ps": psalm, psyche
568 </li><li>Silent "p" in words beginning with "pn": pneumonia, pneumatic
569 </li><li>Silent "p" in words beginning with "pt": pterodactyl, ptolemaic
570 </li><li>Silent "d" in words beginning with "dj": djinn, Djikarta
571 </li><li>Silent "k" in words beginning with "kn": knight, Knuthson
572 </li><li>Silent "g" in words beginning with "gn": gnarly, gnome, gnat
573 </li><li>"Mac" versus "Mc" beginning Scottish surnames
574 </li><li>"Tch" sounds in Slavic words: Tchaikovsky vs. Chaykovsky
575 </li><li>The letter "j" pronounced like "h" in Spanish: LaJolla
576 </li><li>Words beginning with "wr" versus "r": write vs. rite
577 </li><li>Miscellaneous problem words such as "debt", "tsetse",
578 "Nguyen", "Van Nuyes".
579 </li></ul>
580
581 <h1 id="auxiliary_functions"><span>7. </span>Auxiliary Functions</h1>
582
583 <p>The source code module that implements the spellfix1 virtual table also
584 implements several SQL functions that might be useful to applications
585 that employ spellfix1 or for testing or diagnostic work while developing
586 applications that use spellfix1. The following auxiliary functions are
587 available:
588
589 </p><dl>
590 <dt><p><b>editdist3(P,W)<br>editdist3(P,W,L)<br>editdist3(T)</b></p></dt><dd>
591 These routines provide direct access to the version of the Wagner
592 edit-distance function that allows for application-defined weights
593 on edit operations. The first two forms of this function compare
594 pattern P against word W and return the edit distance. In the first
595 function, the langid is assumed to be 0 and in the second, the
596 langid is given by the L parameter. The third form of this function
597 reloads edit distance coefficients from the table named by T.
598
599 </dd><dt><p><b>spellfix1_editdist(P,W)</b></p></dt><dd>
600 This routine provides access to the built-in Wagner edit-distance
601 function that uses default, fixed costs. The value returned is
602 the edit distance needed to transform W into P.
603
604 </dd><dt><p><b>spellfix1_phonehash(X)</b></p></dt><dd>
605 This routine constructs a phonetic hash of the pure ascii input word X
606 and returns that hash. This routine is used internally by spellfix1 in
607 order to transform the K1 column of the shadow table into the K2
608 column.
609
610 </dd><dt><p><b>spellfix1_scriptcode(X)</b></p></dt><dd>
611 Given an input string X, this routine attempts to determine the dominant
612 script of that input and returns the ISO-15924 numeric code for that
613 script. The current implementation understands the following scripts:
614 <ul>
615 <li> 215 - Latin
616 </li><li> 220 - Cyrillic
617 </li><li> 200 - Greek
618 </li></ul>
619 Additional language codes might be added in future releases.
620
621 </dd><dt><p><b>spellfix1_translit(X)</b></p></dt><dd>
622 This routine transliterates unicode text into pure ascii, returning
623 the pure ascii representation of the input text X. This is the function
624 that is used internally to transform vocabulary words into the K1
625 column of the shadow table.
626
627 </dd></dl>
628
629 <a name="editdist3"></a>
630
631 <h1 id="the_editdist3_function"><span>8. </span>The editdist3 function</h1>
632
633 <p>The editdist3 algorithm is a function that computes the minimum edit
634 distance (a.k.a. the Levenshtein distance) between two input strings.
635 The editdist3 algorithm is a configurable alternative to the default
636 edit distance function of spellfix1.
637 Features of editdist3 include:
638
639 </p><ul>
640 <li><p>It works with unicode (UTF8) text.
641
642 </p></li><li><p>A table of insertion, deletion, and substitution costs can be
643 provided by the application.
644
645 </p></li><li><p>Multi-character insertions, deletions, and substitutions can be
646 enumerated in the cost table.
647 </p></li></ul>
648
649 <h1 id="the_editdist3_cost_table"><span>9. </span>The editdist3 COST table</h1>
650
651 <p>To program the costs of editdist3, create a table such as the following:
652
653 </p><blockquote><pre>
654 CREATE TABLE editcost(
655 iLang INT, -- The language ID
656 cFrom TEXT, -- Convert text from this
657 cTo TEXT, -- Convert text into this
658 iCost INT -- The cost of doing the conversion
659 );
660 </pre></blockquote>
661
662 <p>The cost table can be named anything you want - it does not have to be
663 called "editcost". And the table can contain additional columns.
664 The only requirement is that the
665 table must contain the four columns show above, with exactly the names shown.
666
667 </p><p>The iLang column is a non-negative integer that identifies a set of costs
668 appropriate for a particular language. The editdist3 function will only use
669 a single iLang value for any given edit-distance computation. The default
670 value is 0. It is recommended that applications that only need to use a
671 single language always use iLang==0 for all entries.
672
673 </p><p>The iCost column is the numeric cost of transforming cFrom into cTo. This
674 value should be a non-negative integer, and should probably be less than 100.
675 The default single-character insertion and deletion costs are 100 and the
676 default single-character to single-character substitution cost is 150. A
677 cost of 10000 or more is considered "infinite" and causes the rule to be
678 ignored.
679
680 </p><p>The cFrom and cTo columns show edit transformation strings. Either or both
681 columns may contain more than one character. Or either column (but not both)
682 may hold an empty string. When cFrom is empty, that is the cost of inserting
683 cTo. When cTo is empty, that is the cost of deleting cFrom.
684
685 </p><p>In the spellfix1 algorithm, cFrom is the text as the user entered it and
686 cTo is the correctly spelled text as it exists in the database. The goal
687 of the editdist3 algorithm is to determine how close the user-entered text is
688 to the dictionary text.
689
690 </p><p>There are three special-case entries in the cost table:
691
692 </p><table border="1" align="center">
693 <tr><th>cFrom</th><th>cTo</th><th>Meaning</th></tr>
694 <tr><td>''</td><td>'?'</td><td>The default insertion cost</td></tr>
695 <tr><td>'?'</td><td>''</td><td>The default deletion cost</td></tr>
696 <tr><td>'?'</td><td>'?'</td><td>The default substitution cost</td></tr>
697 </table>
698
699 <p>If any of the special-case entries shows above are omitted, then the
700 value of 100 is used for insertion and deletion and 150 is used for
701 substitution. To disable the default insertion, deletion, and/or substitution
702 set their respective cost to 10000 or more.
703
704 </p><p>Other entries in the cost table specific transforms for particular
705 characters.
706 The cost of specific transforms should be less than the default costs, or else
707 the default costs will take precedence and the specific transforms will never
708 be used.
709
710 </p><p>Some example, cost table entries:
711
712 </p><blockquote><pre>
713 INSERT INTO editcost(iLang, cFrom, cTo, iCost)
714 VALUES(0, 'a', 'ä', 5);
715 </pre></blockquote>
716
717 <p>The rule above says that the letter "a" in user input can be matched against
718 the letter "ä" in the dictionary with a penalty of 5.
719
720 </p><blockquote><pre>
721 INSERT INTO editcost(iLang, cFrom, cTo, iCost)
722 VALUES(0, 'ss', 'ß', 8);
723 </pre></blockquote>
724
725 <p>The number of characters in cFrom and cTo do not need to be the same. The
726 rule above says that "ss" on user input will match "ß" with a penalty of 8.
727
728 </p><h1 id="experimenting_with_the_editcost3_function"><span>10. </span>Experimenting with the editcost3() function</h1>
729
730 <p>The spellfix1 virtual table
731 uses editdist3 if the "edit_cost_table=TABLE" option
732 is specified as an argument when the spellfix1 virtual table is created.
733 But editdist3 can also be tested directly using the built-in "editdist3()"
734 SQL function. The editdist3() SQL function has 3 forms:
735
736 </p><ol>
737 <li> editdist3('TABLENAME');
738 </li><li> editdist3('string1', 'string2');
739 </li><li> editdist3('string1', 'string2', langid);
740 </li></ol>
741
742 <p>The first form loads the edit distance coefficients from a table called
743 'TABLENAME'. Any prior coefficients are discarded. So when experimenting
744 with weights and the weight table changes, simply rerun the single-argument
745 form of editdist3() to reload revised coefficients. Note that the
746 edit distance
747 weights used by the editdist3() SQL function are independent from the
748 weights used by the spellfix1 virtual table.
749
750 </p><p>The second and third forms return the computed edit distance between strings
751 'string1' and "string2'. In the second form, a language id of 0 is used.
752 The language id is specified in the third form.
753 </p>
754