We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,001 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

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

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?

2
Contributors
2
Replies
7 Hours
Discussion Span
1 Year Ago
Last Updated
3
Views
Question
Answered
deraad
Newbie Poster
20 posts since Aug 2011
Reputation Points: 10
Solved Threads: 2
Skill Endorsements: 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;
as.bhanuprakash
Junior Poster
109 posts since Dec 2009
Reputation Points: 16
Solved Threads: 21
Skill Endorsements: 1

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

deraad
Newbie Poster
20 posts since Aug 2011
Reputation Points: 10
Solved Threads: 2
Skill Endorsements: 0
Question Answered as of 1 Year Ago by as.bhanuprakash

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0861 seconds using 2.68MB