Compare 5000 strings with PHP Levenshtein

前端 未结 8 1661
长情又很酷
长情又很酷 2021-01-30 12:15

I have 5000, sometimes more, street address strings in an array. I\'d like to compare them all with levenshtein to find similar matches. How can I do this without looping throug

相关标签:
8条回答
  • 2021-01-30 12:39

    I think you cannot avoid looping through the array as the levenstein() function takes only strings and not an array as input.

    You can do something like:

    for($i=0;$i<count($array)-1;$i++)
    {
        for($j=$i+1;$j<count($array);$j++)
        {
            $lev = levenshtein($array[$i],$array[$j]);
            if($lev == 0)
            {
                // exact match
            }
            else if($lev <= THRESHOLD)
            {
                // similar
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-30 12:50
    $stringA = "this is php programming language";
    $stringB = "this is complete programming script in which java php and  all other minor languages include";
    
    echo "string 1---->".$stringA."<br />";
    echo "string 2---->".$stringB."<br />";
    // changing string to arrays
    $array1 = explode(' ', $stringA);
    $array2 = explode(' ', $stringB);
    
    // getting same element from two strings
    $c = array_intersect($array1, $array2);
    // changing array to the string
    $d=implode(' ',$c);
    
    echo "string same elements---> ".$d."<br />";
    
    
    // getting difrent  element from two arrays
    $result = array_diff($array2, $array1);
    // changing array to the string
    $zem = implode(' ',$result);
    
    if (!empty($zem)) {
      echo "string diffrence---> ".$zem."<br />";
    } else {
      echo "string diffrence--->both strings are same <br />";
    }
    
    similar_text($stringA, $d, $p);
    echo " similarity between the string is ".$p."% <br />";
    
    0 讨论(0)
提交回复
热议问题