1,105,585 Community Members

Creating a "did you mean" feature for multiple terms?

Member Avatar
deraad
Newbie Poster
20 posts since Aug 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
1
 

I'm making a feature like stated in the title that gives suggestions based on what terms exist in the database;

I have a working example below that works for one term. such as "oranges", or "yellow bananas".

However, I need it to work for multiple terms so if my database contains the words "grapes", "blue", and "today", and the user types in "graps, bleu, todya" it will run through each word inputed by the user, to each word in the database, and find a suggestion for each word, then display them all. so the suggestions would look like this: "grapes, blue, today", when the user inputs: "graps, bleu, todya".

here is my current code that works for one term:

$input = $_POST['input']; // will be a list of comma seperated terms from the user(only works with one term now);

$words = "yellow,green,grapes,blue,court"; // for simplicity, real terms in database
$words = explode(',', $words); // words is now an array

$shortest = -1;
foreach ($words as $word) 
{
    $lev = levenshtein($input, $word);
 
    if ($lev == 0) 
    {
        $closest = $word;
        $shortest = 0;
        break;
    }
 
    if ($lev <= $shortest || $shortest < 0) 
    {
        $closest  = $word;
        $shortest = $lev;
        
    }
}

echo $closest; // the result(suggestion);

}

This code works perfectly until the user types in a comma, then another word...

I've tried exploding the input by it's commas then adding another foreach statement like the code below, but it isn't working...

$input = $_POST['input'];
$input = explode(',', $input);

$words = "yellow,green,grapes,blue,court";
$words = explode(',', $words);
$shortest = -1;

foreach($input as $compare)
{
foreach ($words as $word) 
{
    $lev = levenshtein($compare, $word);
 
    if ($lev == 0) 
    {
        $closest = $word;
        $shortest = 0;
        break;
    }
 
    if ($lev <= $shortest || $shortest < 0) 
    {
        $closest  = $word;
        $shortest = $lev;
        
    }
}
echo $closest . ", ";
}

I've tried many variations of this as well and I really can't understand why it isn't working... :(

any suggestions?

Member Avatar
as.bhanuprakash
Junior Poster
109 posts since Dec 2009
Reputation Points: 2 [?]
Q&As Helped to Solve: 21 [?]
Skill Endorsements: 1 [?]
 
0
 

Hi,

Below is the code with a small change. I have shifted the $shortest = -1 line inside the first foreach() loop.
I hope it helps.

//$input = $_POST['input'];
$input = "bleu,grun,yelow,integer,crout";
$input = explode(',', $input);

$words = "yellow,green,grapes,blue,court";
$words = explode(',', $words);

//$shortest = -1; Shift this inside the first foreach loop.

$output = "<table border='1'><tr><th>Compare</th><th>Word</th><th>Shortest</th></tr>";

foreach($input as $compare)
{
$shortest = -1;
foreach ($words as $word) 
{
    $lev = levenshtein($compare, $word);
 
    if ($lev == 0) 
    {
        $closest = $word;
        $shortest = 0;
        break;
    }
 
    if ($lev <= $shortest || $shortest < 0) 
    {
        $closest  = $word;
        $shortest = $lev;
        
    }
}
$output .= "<tr><td>".$compare."</td><td>".$closest."</td><td>".$shortest."</td></tr>";
}

$output .= "</table>";
echo $output;
Member Avatar
deraad
Newbie Poster
20 posts since Aug 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
0
 

Perfect!! that's exactly what I was trying to fix.. Thank you!

Question Answered as of 2 Years Ago by as.bhanuprakash
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article