0

我有一个正在处理的基本搜索脚本。我希望用户能够输入多个关键字。如果其中一个关键字拼写错误,我想为搜索结果更改该词和/或显示“您的意思是……”消息。

我已经尝试过 levenshtein 但它似乎只适用于一个单词,而且似乎不是很可靠。在使用此功能时,在测试中,我想出了这个:

<?php
$input = 'ornage ptoato';

$possible_words = explode(' ', trim(strtolower($input)));

foreach($possible_words as $value){

   $words  = array('sony','red', 'indigo','orange','bell','toshiba','potato');

   $shortest = -1;

   foreach ($words as $word) {

       $lev = levenshtein($value, $word);

       if ($lev == 0) {

           $closest = $word;
           $shortest = 0;

           break;
       }

       if ($lev <= $shortest || $shortest < 0) {
           // set the closest match, and shortest distance
           $closest  = $word;
           $shortest = $lev;
       }
   }

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

?>

foreach 中有 foreach 是因为我试图为搜索字符串中的每个单词执行此操作。

我基本上希望它像谷歌的“你的意思是..”和 eBay 的“找到一二的 0 个结果,所以我们搜索一二三”。

4

1 回答 1

1

您的代码需要稍作调整。

<?php
$input = 'ornage ptoato toshiba butts';
$possible_words = explode(' ', trim(strtolower($input)));
$words = array('sony','red', 'indigo','orange','bell','toshiba','potato');
$threshold = 4;

foreach($possible_words as $value){
    $shortest = -1;
    if( in_array($value, $words) ) {
        printf("Exact match for word: %s\n", $value);
    } else {
        foreach ($words as $word) {
             $lev = levenshtein($value, $word);

             if ($lev <= $shortest || $shortest < 0) {
                  // set the closest match, and shortest distance
                  $closest  = $word;
                  $shortest = $lev;
             }
        }
        if($shortest < $threshold) {
            printf("You typed: %s.\nAssuming you meant: %s\n", $value, $closest);
        } else {
            printf("Could not find acceptable match for: %s\n", $value);
        }
    }
}
  1. 检查可接受的匹配项需要进入外循环。
  2. 在计算 Levenshtein 距离之前,您可以使用它in_array()来搜索完全匹配
  3. 您可能只想在合理范围内匹配单词。[心疼$threshold]

输出:

You typed: ornage.
Assuming you meant: orange
You typed: ptoato.
Assuming you meant: potato
Exact match for word: toshiba
Could not find acceptable match for: butts
于 2012-11-15T00:00:55.193 回答