note 107114 added to function.levenshtein

From: Date: Sun, 08 Jan 2012 16:05:08 +0000
Subject: note 107114 added to function.levenshtein
Groups: php.notes 
Request: Send a blank email to php-notes+get-185460@lists.php.net to get a copy of this message
I really like your example for the use of the levenshtein function to match against an array. I ran into the need to specify the sensitivity of the result. There are circumstances when you want it to return false if the match is way out of line. I wouldn't want "marry had a little lamb" to match with "saw viii" simply because it was the best match in the array. Hence the need for sensitivity: function wordMatch($words, $input, $sensitivity){ $shortest = -1; foreach ($words as $word) { $lev = levenshtein($input, $word); if ($lev == 0) { $closest = $word; $shortest = 0; break; } if ($lev <= $shortest || $shortest < 0) { $closest = $word; $shortest = $lev; } } if($shortest <= $sensitivity){ return $closest; } else { return 0; } } $word = 'PINEEEEAPPLE'; $words = array('apple','pineapple','banana','orange', 'radish','carrot','pea','bean','potato'); echo wordMatch($words, strtolower($word), 2); ---- Server IP: 69.147.83.197 Probable Submitter: 67.185.69.138 ---- Manual Page -- http://www.php.net/manual/en/function.levenshtein.php Edit -- https://master.php.net/note/edit/107114 Del: integrated -- https://master.php.net/note/delete/107114/integrated Del: useless -- https://master.php.net/note/delete/107114/useless Del: bad code -- https://master.php.net/note/delete/107114/bad+code Del: spam -- https://master.php.net/note/delete/107114/spam Del: non-english -- https://master.php.net/note/delete/107114/non-english Del: in docs -- https://master.php.net/note/delete/107114/in+docs Del: other reasons-- https://master.php.net/note/delete/107114 Reject -- https://master.php.net/note/reject/107114 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#185460) next »