質問

I have a MySQL query as below;

$query = "SELECT * FROM dictionary WHERE word LIKE '%".$word."%' ORDER BY word LIMIT 10";

This will search for (and display) words in my dictionary db.

the search will return;

Drunken for Drunk, etc, etc.. , Drunken for Drunke, etc, etc.. and Drunken for Drunken, etc, etc..

But it won't return Drunken for Drunkin. I would like to display this word as a suggestion word (like the one we see in google). How can I do this?

below is my complete code for reference;

$db = new pdo("mysql:host=localhost;dbname=dictionary", "root", "password");
$query = "SELECT * FROM dictionary WHERE word LIKE '%".$word."%' ORDER BY word LIMIT 10";
$result = $db->query($query);
$end_result = '';
if ($result) {
    while ( $r = $result->fetch(PDO::FETCH_ASSOC) ) {
        $end_result .= $r['word'].'<br>';
    }
}
echo $end_result;
役に立ちましたか?

解決

Try Using SOUNDEX

Soundex keys have the property that words pronounced similarly produce the same soundex key, and can thus be used to simplify searches in databases where you know the pronunciation but not the spelling. This soundex function returns a string 4 characters long, starting with a letter.

Here are some questions on SO which may guide you in the right direction:

Hope this helps.

他のヒント

Soundex might help for simple cases, but if you want to implement something like this properly you'll need a Lucene search index where you can perform fuzzy (=unprecise) searches on. Have a look at the Apache Solr PHP port.

You will need something more complex than just a LIKE statement. It displays Drunken for Drunk because Drunken contains the word Drunk.

More complex algorithms look at the keyboard layout for possible typos: for example if you search for 'drumk' you could interprete that the letter M is next to N (on a QWERTY keyboard) and suggest drunk.

You could also try to remove the last character from the search query if there are no results: For example: if you search for drunke and nothing is found, you can try searching for drunk and so on.

Hope this helps.

Just for the record, I am surprised there is no mention here of the levenshtein function, used for measuring the "distance" or similarity of two character strings, although it is true that for English words nothing seems to beat the soundex type algorithm or, more recently, Metaphone.

With a few lines of codes you will get the "Did you mean" from Google kind of stuff. See example #1 from the php.net documentation.

Note that this example is based on a static array of words. If you need to retrieve those words from your database, you will need to implement a few lines more...

Easiest way:

SELECT * FROM table_name WHERE soundex(field_name) LIKE CONCAT('%', soundex('searching_element'), '%')

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top