简体   繁体   English

PHP Levenshtein查询结果

[英]PHP Levenshtein on Query Result

I want to perform a levenshtein on a mysql query result. 我想对mysql查询结果执行levenshtein。

The query looks like this: 查询如下所示:

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

And here I prepare everything for the levenshtein operation: 在这里,我为levenshtein操作做好了一切准备:

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

And this is just the levenshtein operation from the manual: 这只是手册中的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";
}

Thanks to Jaitsu I got rid of the error/warning message, however the levenshtein is now throwing an unexpected result: 感谢Jaitsu,我摆脱了错误/警告消息,但是levenshtein现在引发了意外的结果:

Whatever the input is, it won't ever find a matching result and the closest match will always be = H 无论输入是什么,都永远不会找到匹配结果,并且最接近的匹配始终为= H

Examples: 例子:

Input word: Battlefield 3 Back to Karkand Did you mean: H? 输入词:《战地风云3》(Battlefield 3)
Input word: Starcraft 2 Wings of Liberty Did you mean: H? 输入词:《星际争霸2:自由之翼》你的意思是:H?

To be honest, I don't have a clue what#s going on right now... 老实说,我不知道现在发生了什么...

Your logic for fetching the games (words) from your database is correct, but you need to remove... 您从数据库中获取游戏(单词)的逻辑是正确的,但是您需要删除...

$words = $row_GID['game'];

and pass in the $row_GID variable to your loop. 并将$row_GID变量传递给您的循环。

Then in your foreach loop... 然后在您的foreach循环中...

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM