In a new website I am developing for a client I had to add the usual "Did you mean… ?" in the search results for her. Si I started thinking for the easiest way to do this.

There are actually a lot of php functions out there to look for similar text. The most obvious one?
similar_text()

similar_text — Calculate the similarity between two strings

You must pass 2 parameters plus an optional third. The two first are the strings to compare, and the optional one is the percentage of "closeness" you want them to have. It is quite useful, although it is too expensive in terms of time to use with huge database searches, so I wouldn't recommend it.

There are two other methods that might be good for some cases, and another function that is just the best. I'll show you first the best way to achieve this:
The Levenshtein algorithm, which basically finds the number of characters you must add, edit, or remove from a string to make it match another one. At first it doesn't sound too useful, but take a look at this example:

 1 // input misspelled word
 2 $input = 'carrrot';
 3 
 4 // array of words to check against
 5 $words  = array('apple','pineapple','banana','orange',
 6                 'radish','carrot','pea','bean','potato');
 7 
 8 // no shortest distance found, yet
 9 $shortest = -1;
10 
11 // loop through words to find the closest
12 foreach ($words as $word) {
13 
14     // calculate the distance between the input word,
15     // and the current word
16     $lev = levenshtein($input, $word);
17 
18     // check for an exact match
19     if ($lev == 0) {
20 
21         // closest word is this one (exact match)
22         $closest = $word;
23         $shortest = 0;
24 
25         // break out of the loop; we've found an exact match
26         break;
27     }
28 
29     // if this distance is less than the next found shortest
30     // distance, OR if a next shortest word has not yet been found
31     if ($lev <= $shortest || $shortest < 0) {
32         // set the closest match, and shortest distance
33         $closest  = $word;
34         $shortest = $lev;
35     }
36 }
37  
38 echo "Input word: $input\n";
39 if ($shortest == 0) {
40     echo "Exact match found: $closest\n";
41 } else {
42     echo "Did you mean: $closest?\n";
43 }

This is an example where even a misspelled word can be found. It uses the Levenshtein to look for the word which is the most similar one, and then it is returned.
This is the output of the code before:

Input word: carrrot
Did you mean: carrot?

The use of this function is quite simple, although there are many optional parameters for more precise use. See the php.net reference for this function. The biggest problem is that you would have to run the algorithm against all words to get the actual one. I would only recommend it on a subset of words already narrowed down with a more generic approach.

Other algorithms that could be used for this are soudex and metaphone. Soundex will create a key that is the same for all words that are pronounced in a similar way.
For example, the following code:

 1 echo soundex('beard');
 2 echo soundex('bird');
 3 echo soundex('bear');
 4 
 5 /*
 6  * Output:
 7  * B630
 8  * B630
 9  * B600
10  */

Where beard and bird are the same. This could make suggestions fast if you have already created a column in the mysql tables with the soundex key of the tags for example, so that you could search not only for the string, but also for its soundex key…

UPDATE:

You can use MySQL's built in function SOUNDEX() to search both for the string as-is, or for the soundex too, to provide also misspelled words.

And finally, the metaphone function, it's a variation of the soundex key that produces also a key that is the same for all words pronounced the same, but more accurately than soundex, since metaphone actually knows the rules of English pronounciation.
The use would be exactly the same as soundex, and if you are going to use something of the sort I would recommend it for its improved accuracy.
But bear in mind that both soundex and metaphone won't probably work fine in most other languages, or at least for languages with phonemes that don't exist in English.


blog comments powered by Disqus