0

I want to perform a levenshtein on a mysql query result.

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:

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

And this is just the levenshtein operation from the manual:

   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:

Whatever the input is, it won't ever find a matching result and the closest match will always be = H

Examples:

Input word: Battlefield 3 Back to Karkand Did you mean: H?
Input word: Starcraft 2 Wings of Liberty Did you mean: H?

To be honest, I don't have a clue what#s going on right now...

SubZero
  • 113
  • 7
  • what is `$words`? it's not an array and that's your problem – JamesHalsall Mar 11 '13 at 12:52
  • How do I make it an array then? I want it to store the result from my query... The manual declares an array like this: `$words = array('apple','pineapple','banana','orange', 'radish','carrot','pea','bean','potato');` but how do I declare an array with the result of my query? – SubZero Mar 11 '13 at 12:55
  • 1
    The Levenshtein distance between any other game name and 'Halo' is guaranteed to be no greater than 4 (the number of letters in Halo) plus the number of letters in the other game's name. The concept of "closest name" under the Levenshtein measure has some subtlety. You might want to take a look at MySQL FULLTEXT searching. – O. Jones Mar 11 '13 at 13:25

1 Answers1

1

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.

Then in your foreach loop...

foreach ($row_GID as $row) {
    $word = $row['game'];
    //proceed as normal
}
JamesHalsall
  • 13,224
  • 4
  • 41
  • 66
  • Thank you for providing the solution! Now it's working without throwing any errors however the result I am getting is quite... unexpected. For instance if the input is: **The Elder Scrolls Skyrim** the levenshtein ask if I meant H... but there's no name called **H** in my Database so I guess it took the first letter of the name Halo which is also unexpected... – SubZero Mar 11 '13 at 13:03