برگزیده های پرشین تولز

levenshtein - تابعی جالب برای بررسی فاصله بین دو متغیر

sama_sally

Registered User
تاریخ عضویت
5 آپریل 2005
نوشته‌ها
2,598
لایک‌ها
1
سن
35
محل سکونت
Essen, Deutschland
دیشب که بیخودی تویه manual **چرخ میزدم ... به این تابع بر خوردم: ()levenshtein این تابع فاصله (distance) بین حروف یا اعداد دو متغیر رو بررسی میکنه. این کد هم تویه منوال بود که یه کار شبیه به دیکشنری گوگل میکنه:
تا حالا تویه گوگل دیدید که وقتی کلمه ای رو اشتباه مینویسید این میاد: did you mean ...?
اینم یه چیزی مثل اونه:
PHP:
<?php
// input misspelled word
$input = 'پرشیت تولر';

// array of words to check against
$words  = array('سالار','کابلی','مجید آنلاین','پرشین تولز',
               'radish','carrot','pea','bean','potato');

// no shortest distance found, yet
$shortest = -1;

// loop through words to find the closest
foreach ($words as $word) {

   // calculate the distance between the input word,
   // and the current word
   $lev = levenshtein($input, $word);

   // check for an exact match
   if ($lev == 0) {

       // closest word is this one (exact match)
       $closest = $word;
       $shortest = 0;

       // break out of the loop; we've found an exact match
       break;
   }

   // if this distance is less than the next found shortest
   // distance, OR if a next shortest word has not yet been found
   if ($lev <= $shortest || $shortest < 0) {
       // set the closest match, and shortest distance
       $closest  = $word;
       $shortest = $lev;
   }
}

echo "Input word: $input\n";
if ($shortest == 0) {
   echo "Exact match found: $closest\n";
} else {
   echo "Did you mean: $closest?\n";
}

?>
اید کد رو اجرا کنید ببینید چی میشه.!!
به نظرم جالب اومد گفتم با شما هم در میون بذارم!
 

Mehdi

مدیر بازنشسته
تاریخ عضویت
1 آگوست 2004
نوشته‌ها
5,602
لایک‌ها
49
محل سکونت
Anywhere
باحاله
76.gif
 

Parham

Registered User
تاریخ عضویت
24 سپتامبر 2003
نوشته‌ها
1,042
لایک‌ها
2
خیلی جالب بود!:)
 

shankimout

Registered User
تاریخ عضویت
17 می 2004
نوشته‌ها
1,524
لایک‌ها
3
محل سکونت
ساری . . . . . . . . . . . . . . Permanently Bann
خیلی جا ها بدرد میخوره . اینم باهاش یکیه

کد:
[B]similar_text[/B]

(PHP 3 >= 3.0.7, PHP 4, PHP 5)
similar_text --  Calculate the similarity between two strings 
[B]Description[/B]

int [B]similar_text[/B] ( string first, string second [, float &percent] )

This calculates the similarity between two strings as described in Oliver [1993]. Note that this implementation does not use a stack as in Oliver's pseudo code, but recursive calls which may or may not speed up the whole process. Note also that the complexity of this algorithm is O(N**3) where N is the length of the longest string. 
By passing a reference as third argument, [B]similar_text()[/B] will calculate the similarity in percent for you. It returns the number of matching chars in both strings. 
See also [URL="mk:@MSITStore:K:\Web\PHP\Articles\php_manual_en.chm::/en/function.levenshtein.html"][B][U][COLOR=#800080]levenshtein()[/COLOR][/U][/B][/URL], and [URL="mk:@MSITStore:K:\Web\PHP\Articles\php_manual_en.chm::/en/function.soundex.html"][B][U][COLOR=#800080]soundex()[/COLOR][/U][/B][/URL].
 

hba

کاربر فعال صفحات داینامیک
کاربر فعال
تاریخ عضویت
8 آگوست 2004
نوشته‌ها
1,511
لایک‌ها
1
سن
39
محل سکونت
تهران-ونک-php-mysql
می رم برای تست باید جالب باشه
 

sama_sally

Registered User
تاریخ عضویت
5 آپریل 2005
نوشته‌ها
2,598
لایک‌ها
1
سن
35
محل سکونت
Essen, Deutschland
آره تابع شانکیماوتم جالبه
کد:
See also levenshtein(), and soundex().
 
بالا