php levenshtein,PHP: levenshtein - Manual

* The below func, btlfsa, (better than levenstien for spelling apps)

* produces better results when comparing words like haert against

* haart and heart.

*

* For example here is the output of levenshtein compared to btlfsa

* when comparing 'haert' to 'herat, haart, heart, harte'

*

* btlfsa('haert','herat'); output is.. 3

* btlfsa('haert','haart'); output is.. 3

* btlfsa('haert','harte'); output is.. 3

* btlfsa('haert','heart'); output is.. 2

*

* levenshtein('haert','herat'); output is.. 2

* levenshtein('haert','haart'); output is.. 1

* levenshtein('haert','harte'); output is.. 2

* levenshtein('haert','heart'); output is.. 2

*

* In other words, if you used levenshtein, 'haart' would be the

* closest match to 'haert'. Where as, btlfsa sees that it should be

* 'heart'

*/functionbtlfsa($word1,$word2)

{$score=0;// For each char that is different add 2 to the score

// as this is a BIG difference$remainder=preg_replace("/[".preg_replace("/[^A-Za-z0-9\']/",' ',$word1)."]/i",'',$word2);$remainder.=preg_replace("/[".preg_replace("/[^A-Za-z0-9\']/",' ',$word2)."]/i",'',$word1);$score=strlen($remainder)*2;// Take the difference in string length and add it to the score$w1_len=strlen($word1);$w2_len=strlen($word2);$score+=$w1_len>$w2_len?$w1_len-$w2_len:$w2_len-$w1_len;// Calculate how many letters are in different locations

// And add it to the score i.e.

//

// h e a r t

// 1 2 3 4 5

//

// h a e r t     a e        = 2

// 1 2 3 4 5   1 2 3 4 5

//$w1=$w1_len>$w2_len?$word1:$word2;$w2=$w1_len>$w2_len?$word2:$word1;

for($i=0;$i

{

if ( !isset($w2[$i]) ||$w1[$i] !=$w2[$i] )

{$score++;

}

}

return$score;

}// *************************************************************

// Here is a full code example showing the difference$misspelled='haert';// Imagine that these are sample suggestions thrown back by soundex or metaphone..$suggestions= array('herat','haart','heart','harte');// Firstly order an array based on levenshtein$levenshtein_ordered= array();

foreach ($suggestionsas$suggestion)

{$levenshtein_ordered[$suggestion] =levenshtein($misspelled,$suggestion);

}asort($levenshtein_ordered,SORT_NUMERIC);

print"Suggestions as ordered by levenshtein...

";print_r($levenshtein_ordered);

print"";// Secondly order an array based on btlfsa$btlfsa_ordered= array();

foreach ($suggestionsas$suggestion)

{$btlfsa_ordered[$suggestion] =btlfsa($misspelled,$suggestion);

}asort($btlfsa_ordered,SORT_NUMERIC);

print"Suggestions as ordered by btlfsa...

";print_r($btlfsa_ordered);

print"

";?>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值