Jump to content

levenshtein search problem


vipin8vit

A challenging problem in php.only if you are good you can do it.are you??  

  1. 1. A challenging problem in php.only if you are good you can do it.are you??

    • yes
      0
    • no
      0


Recommended Posts

i am trying a search with levenshtein but m stuck with a serious problem.The field "topic" in the database is a string of more than one word which m not able to solve with the type of search i am doing here.had there been one word in the string it would be fine but for such case its a total failure.

 

i also need to use the soundex() or some similar function as the entries in my database are no similar.and again one more problem php 4.4.2 that m using is case sensitive so levenshtein is not working fine.

 

m totally stuck.please fix the code.here is the code

...........................................................................

 

<?php
// input misspelled word

$trimmed=@$_GET['q'];
$input=trim($trimmed);


$arrWords=explode(" ",$input);

if(sizeof($arrWords)==0||$input==""){
echo"enter keyword";


echo"<a href=\"lev.php\">click here</a>";



}



$m=sizeof($arrWords);
echo"nkdkf$m";




$shortest = -1;



mysql_connect("localhost","root");

@mysql_select_db(test);

//foreach ($arrWords as $trimm){
//$query1 = "SELECT topic FROM forum_question WHERE topic LIKE \"%$trimm%\" ORDER BY id DESC" ; 









$query1="select topic from forum_question";
$result1=mysql_query($query1);

$i=0;
$count=0;
while($r=mysql_fetch_array($result1)){
$topic[$i]=$r["topic"];
//echo $topic,"\n";
//echo"<br>";

//$toopic=explode(" ",$topic);
//$n=sizeof($toopic);
//for($k=0;$k<$n;$k++){
//$toopic[$i][$k]=$toopic[$k];
//echo $toopic[$i][$k];
//}
$i++;
$count++;
}








//foreach ($topic as $word) {

    
$i=0;
for($i=0;$i<$m;$i++){
for($j=0;$j<9;$j++){
//for($k=0;$k<$n;$k++){

$lev = levenshtein($arrWords[$i], $topic[$j]);

echo"$lev\n\n";

    
    if ($lev == 0) {

        // closest word is this one (exact match)
        $closest = $topic[$j];
        $shortest = 0;

        // break out of the loop; we've found an exact match
       break;
    }

    
    if ($lev <= $shortest || $shortest < 0) {
        // set the closest match, and shortest distance
        $closest  = $topic[$j];
        $shortest = $lev;


}
  
}

echo "Input word: $arrWords[$i]\n";
if ($shortest == 0) {
    echo "Exact match found: $closest\n";
} else {
    echo "Did you mean: $closest?\n";

}
$closest="";
$shortest=-1;
}
?> 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.