关于查询结果的PHP Levenshtein

时间:2013-03-11 12:49:45

标签: php mysql arrays levenshtein-distance

我想在mysql查询结果上执行levenshtein。

查询如下所示:

$query_GID = "select `ID`,`game` from `gkn_catalog`";
$result_GID = $dbc->query($query_GID);
$row_GID = mysqli_fetch_array($result_GID,MYSQLI_ASSOC);

在这里,我为levenshtein操作做好准备:

$shortest = -1;
$input = $game_title;

这只是手册中的levenshtein操作:

   foreach ($row_GID as $row) {
$word = $row['game'];

// 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";
}

感谢Jaitsu我摆脱了错误/警告信息,但是levenshtein现在正在抛出意想不到的结果:

无论输入是什么,它都找不到匹配的结果,最接近的匹配总是= H

示例:

  

输入词:战地3回到Karkand你的意思是:H?
输入字:   星际2自由之翼你的意思是:H?

说实话,我不知道现在正在发生什么......

1 个答案:

答案 0 :(得分:1)

从数据库中获取游戏(单词)的逻辑是正确的,但您需要删除...

$words = $row_GID['game'];

并将$row_GID变量传递给您的循环。

然后在你的foreach循环中......

foreach ($row_GID as $row) {
    $word = $row['game'];
    //proceed as normal
}