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
https://forums.phpfreaks.com/topic/131326-levenshtein-search-problem/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.