开发者

How to add spelling suggestions to PHP/SQL search script

Creating a search function on my site using php/sql, simple enough - just using a SELECT ALL query on the database using the LIKE clause and echoing the result on the page. My question is, how can I add spelling suggestions in case the user mistyped their search query. Mysql doesn't return anything unless the search term matches exactly with the database content, e.g. "Dofs" will not return "Dogs". So how can s开发者_Python百科pelling suggestions be added?

Thanks.


Following you will find an excellent article by Peter Norvig on how to write a spell checker:

http://www.norvig.com/spell-correct.html

and the following two links are implementations in PHP of the code found in the article:

http://www.phpclasses.org/browse/package/4859.html http://soundofemotion.com/spellcorrect.txt

Hope this helps.


What about PHP's pspell extension?

<?php
$pspell_link = pspell_new("en");

if (!pspell_check($pspell_link, "dofs")) {
    $suggestions = pspell_suggest($pspell_link, "dofs");

    foreach ($suggestions as $suggestion) {
        echo "Possible spelling: $suggestion<br />";
    }
}
?>

This PHP extension requires that you have aspell libraries installed.


You need to check out something like the following:

http://phpir.com/spelling-correction

You will need a dictionary and the levenstien function basically.


in addition to Joe's excellent solution, you can make a soap call to provide alternate spellings (based on a search engine's language corpus)

Yahoo Spelling Suggestion: http://developer.yahoo.com/search/web/V1/spellingSuggestion.html

Google spelling request: http://code.google.com/apis/soapsearch/reference.html#1_3


Coming here in March 2020, then https://tigitz.github.io/php-spellchecker/ is worth looking at.

From the docs:

PHP-Spellchecker is a spellchecker abstraction library for PHP. By providing a unified interface for many different spellcheckers, you’re able to swap out spellcheckers without extensive rewrites.

Using PHP-Spellchecker can eliminate vendor-lock in, reduce technical debt, and improve the testability of your code.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜