Compare 5000 strings with PHP Levenshtein

前端 未结 8 1679
长情又很酷
长情又很酷 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:37

    If you want to find all similar values, you will have to compare all items to all others. But choosing the right array functions will speed things up significantly. Here is a quick example (the results array could have been better):

    $results = array();
    $count = count($entries);
    while ($count != 0) {
        # The entry to process
        $entry = array_shift($entries);
        # Get levenshtein distances to all others
        $result = array_map(
            'levenshtein',
            # array_map() needs two arrays, this one is an array consisting of
            # multiple entries of the value that we are processing
            array_fill($entry, 0, $count),
            $toCompare
        );
        $results[] = array($entry => $result);
        $count--;
    }
    

提交回复
热议问题