1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

problem in search with levenshtein

Discussion in 'PHP' started by vipin8vit, Nov 4, 2008.

  1. #1
    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;
    }
    ?>
     
    vipin8vit, Nov 4, 2008 IP