chordsoflife Posted November 26, 2008 Share Posted November 26, 2008 I'm working on a "Did you mean" feature for my site, and it's working 100%. My problem is that I don't really understand what's going on.. I only managed to implement it. So what I'd like to do now is limit the suggestions unless it actually has a half decent one, but I'm not sure how to do that. Here's my code so far: $bands = q("SELECT fldBand, fldConcertDate FROM tblConcert WHERE fldConcertDate >= '$date'"); $shortest = -1; // loop through words to find the closest foreach ($bands as $band) { extract($band); $band = $band[fldBand]; $lev = levenshtein($search_for, $band); if ($lev == 0) { $closest = $band; $shortest = 0; break; } if ($lev <= $shortest || $shortest < 0) { $closest = $band; $shortest = $lev; } } echo "<p>Sorry, upcoming shows for <b>$search_for</b> could not be found. "; if ($shortest != 0) { echo "Did you mean: <a href=\"?fldArtist=" . str_replace(" ", "+", $closest) . "\">$closest</a>?</p>"; } echo "<p>Do you know of a show that we don't have listed? <a href=\"contact.php\">Contact us</a>!</p>\n"; Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/134359-levenshtein-function-help/ Share on other sites More sharing options...
trq Posted November 26, 2008 Share Posted November 26, 2008 So you wrote this code but aren't sure how it works? Quote Link to comment https://forums.phpfreaks.com/topic/134359-levenshtein-function-help/#findComment-699499 Share on other sites More sharing options...
chordsoflife Posted November 26, 2008 Author Share Posted November 26, 2008 Yea, it's essentially the "carrrots" to "carrot" example from the php website. If I asked how to create that functionality I'd get a link there anyway I understand how it works and what it does, I'm just not sure what the actual numbers are saying. Quote Link to comment https://forums.phpfreaks.com/topic/134359-levenshtein-function-help/#findComment-699502 Share on other sites More sharing options...
.josh Posted November 26, 2008 Share Posted November 26, 2008 I understand how it works and what it does, I'm just not sure what the actual numbers are saying. That makes no sense. If you understand how it works and what it does, then...well, what's left to understand? So what exactly do you mean by "actual numbers" Quote Link to comment https://forums.phpfreaks.com/topic/134359-levenshtein-function-help/#findComment-699531 Share on other sites More sharing options...
Maq Posted November 26, 2008 Share Posted November 26, 2008 Yea, it's essentially the "carrrots" to "carrot" example from the php website. Sorry, never heard of it... We would also need to know what the levenshtein(); function looks like. Quote Link to comment https://forums.phpfreaks.com/topic/134359-levenshtein-function-help/#findComment-699538 Share on other sites More sharing options...
.josh Posted November 26, 2008 Share Posted November 26, 2008 He's talking about the example use of the function in the manual. levenshtein Quote Link to comment https://forums.phpfreaks.com/topic/134359-levenshtein-function-help/#findComment-699539 Share on other sites More sharing options...
chordsoflife Posted November 26, 2008 Author Share Posted November 26, 2008 Sorry, I don't think I'm being unclear, but I'll try to explain. I understand what the function does. I understand the point of using it. What I don't understand is what the numbers in the function are, or rather, what the effect of changing them would be. I'm talking about the manual example, but a response applying it to mine would be great. The numbers, if not clear, would be for the $shortest varable set to -1, and 0 for the if $lev == 0 THen, my original question: "So what I'd like to do now is limit the suggestions unless it actually has a half decent one, but I'm not sure how to do that." Quote Link to comment https://forums.phpfreaks.com/topic/134359-levenshtein-function-help/#findComment-699581 Share on other sites More sharing options...
.josh Posted November 26, 2008 Share Posted November 26, 2008 Well what I'm saying is that if you say you understand what the function does, then you should understand the numbers. -1, as the comment in the code says, is a flag to specify that no matches are found yet. 0 means you found a 100% match, >1 means the levenshtein distance between $string1 and $string2. The description in the manual tells you The Levenshtein distance is defined as the minimal number of characters you have to replace, insert or delete to transform str1 into str2 . So for instance: cat vs. cat : nothing has to be changed, so the distance is 0. 0 == match. rat vs. cat : only 1 character has to be replaced to change rat to cat, so the distance would be 1. rats vs. cat : 1 character has to be changed (r to c) and one char has to be removed (s), so distance would be 2. dog vs. cat : all 3 characters in dog have to be changed to make cat, so the distance would be 3. So, the smaller the number, the closer a match there will be. It's up to you what's the maximum number returned to accept as an acceptable suggestion. You just have to look at the average length chars for your data. Quote Link to comment https://forums.phpfreaks.com/topic/134359-levenshtein-function-help/#findComment-699596 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.