mysql クエリの結果に対してレーベンシュタインを実行したいと考えています。
クエリは次のようになります。
$query_GID = "select `ID`,`game` from `gkn_catalog`";
$result_GID = $dbc->query($query_GID);
$row_GID = mysqli_fetch_array($result_GID,MYSQLI_ASSOC);
そしてここで、レーベンシュタイン操作のすべてを準備します。
$shortest = -1;
$input = $game_title;
そして、これはマニュアルのレーベンシュタイン操作です。
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 のおかげで、エラー/警告メッセージを取り除くことができましたが、レーベンシュタインが予期しない結果をスローするようになりました。
入力が何であれ、一致する結果が見つかることはなく、最も近い一致は常に = Hになります。
例:
入力単語: バトルフィールド 3 カーカンドに戻る もしかして: H?
入力ワード: Starcraft 2 Wings of Liberty もしかして: H?
正直なところ、今何が起こっているのかわかりません...