PHP Levenshtein Closest Match

Ever have a need to find the closest word match? Say you have a word and need to get the closest match of it against a list of other words. Your original word may or may not be in the list?

Then you need Levenshtein... Levenshtein. calculates the closest match by the lowest number of operations (insert, replace or delete) it takes to get from one string to another.

I took the example from the PHP dot net site and wrapped it up in a little function.

PHP:
  1. //CALCULATE CLOSEST MATCH USING LEVENSHTEIN FUNCTION
  2. function closest($seed, $haystack){
  3.     $shortest = -1;
  4.     foreach ($haystack as $word){
  5.         $lev = levenshtein($seed, $word);
  6.         if ($lev == 0) {
  7.                $closest = $word; $shortest = 0; break;
  8.            }
  9.        if ($lev <= $shortest || $shortest <0) {
  10.            $closest  = $word; $shortest = $lev;
  11.            }
  12.     }
  13.     return $closest;
  14. }

Here is an example of its use:

PHP:
  1. $words = array('Master of Arts in Forensic Psychology','Bachelor of Arts in Psychology','Master of Business Administration', 'Doctor of Business Administration', 'Doctor of Education in Educational Leadership', 'Doctor of Psychology in Clinical Psychology');
  2.  
  3. echo closest('Master of Arts in Clinical Psychology', $words);
  4.  
  5. //RETURNS: Master of Arts in Forensic Psychology

These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Netvouz
  • DZone
  • Reddit
  • Furl
  • NewsVine
  • Simpy
  • Slashdot
  • Spurl
  • StumbleUpon
  • YahooMyWeb
  • TailRank
  • Technorati

Home | PHP | PHP Levenshtein Closest Match