Php 具有错误字符容差的最长公共子字符串

Php 具有错误字符容差的最长公共子字符串,php,string-matching,levenshtein-distance,longest-substring,Php,String Matching,Levenshtein Distance,Longest Substring,我在这里找到了一个脚本,它在查找最低公共子字符串时运行良好 但是,我需要它来容忍一些不正确/缺失的字符。我希望能够输入所需的相似性百分比,或者指定允许的缺失/错误字符数 例如,我想查找以下字符串: 大黄校车 在该字符串的内部: 那天下午他们乘坐了黄色的大校车 这是我当前使用的代码: function longest_common_substring($words) { $words = array_map('strtolower', array_map('trim', $words));

我在这里找到了一个脚本,它在查找最低公共子字符串时运行良好

但是,我需要它来容忍一些不正确/缺失的字符。我希望能够输入所需的相似性百分比,或者指定允许的缺失/错误字符数

例如,我想查找以下字符串:

大黄校车

在该字符串的内部:

那天下午他们乘坐了黄色的大校车

这是我当前使用的代码:

function longest_common_substring($words) {
    $words = array_map('strtolower', array_map('trim', $words));
    $sort_by_strlen = create_function('$a, $b', 'if (strlen($a) == strlen($b)) { return strcmp($a, $b); } return (strlen($a) < strlen($b)) ? -1 : 1;');
    usort($words, $sort_by_strlen);

    // We have to assume that each string has something in common with the first
    // string (post sort), we just need to figure out what the longest common
    // string is. If any string DOES NOT have something in common with the first
    // string, return false.
    $longest_common_substring = array();
    $shortest_string = str_split(array_shift($words));

    while (sizeof($shortest_string)) {
        array_unshift($longest_common_substring, '');
        foreach ($shortest_string as $ci => $char) {
            foreach ($words as $wi => $word) {
                if (!strstr($word, $longest_common_substring[0] . $char)) {
                    // No match
                    break 2;
                }
            }
            // we found the current char in each word, so add it to the first longest_common_substring element,
            // then start checking again using the next char as well
            $longest_common_substring[0].= $char;
        }
        // We've finished looping through the entire shortest_string.
        // Remove the first char and start all over. Do this until there are no more
        // chars to search on.
        array_shift($shortest_string);
    }

    // If we made it here then we've run through everything
    usort($longest_common_substring, $sort_by_strlen);

    return array_pop($longest_common_substring);
}
函数最长\u公共\u子字符串($words){
$words=数组映射('strtolower',数组映射('trim',$words));
$sort_by_strlen=创建函数('$a,$b','if(strlen($a)==strlen($b)){return strcmp($a,$b);}return(strlen($a)$char的最短字符串){
foreach($wi=>$word形式的单词){
if(!strstrstr($word,$longest\u common\u substring[0].$char)){
//没有对手
破口2;
}
}
//我们在每个单词中找到了当前字符,因此将其添加到第一个最长的\u common\u子字符串元素中,
//然后使用下一个字符再次开始检查
$longest\u common\u子字符串[0]。=$char;
}
//我们已经完成了整个最短的_字符串的循环。
//删除第一个字符并重新开始。这样做直到没有更多字符为止
//要搜索的字符。
数组\u移位($最短\u字符串);
}
//如果我们在这里成功了,那么我们已经经历了一切
usort($longest\u common\u substring,$sort\u by\u strlen);
返回数组\u pop($longest\u common\u substring);
}
非常感谢您的帮助

更新


PHP levenshtein函数限制为255个字符,我正在搜索的haystack中有1000多个字符。

将此作为第二个答案,因为它完全不是基于我以前的(不好的)答案

此代码基于和

它返回一个(可能是几个)最小levenshtein子字符串$haystack,给定$needle。现在,levenshtein距离只是编辑距离的一个度量,它可能并不真正适合您的需要“hte”在这个指标上更接近“he”,而不是“the”。我的一些例子说明了这种技术的局限性。我相信这比我之前给出的答案要可靠得多,但请告诉我它是如何为您工作的

// utility function - returns the key of the array minimum
function array_min_key($arr)
{
    $min_key = null;
    $min = PHP_INT_MAX;
    foreach($arr as $k => $v) {
        if ($v < $min) {
            $min = $v;
            $min_key = $k;
        }
    }
    return $min_key;
}

// Calculate the edit distance between two strings
function edit_distance($string1, $string2)
{
    $m = strlen($string1);
    $n = strlen($string2);
    $d = array();

    // the distance from '' to substr(string,$i)
    for($i=0;$i<=$m;$i++) $d[$i][0] = $i;
    for($i=0;$i<=$n;$i++) $d[0][$i] = $i;

    // fill-in the edit distance matrix
    for($j=1; $j<=$n; $j++)
    {
        for($i=1; $i<=$m; $i++)
        {
            // Using, for example, the levenshtein distance as edit distance
            list($p_i,$p_j,$cost) = levenshtein_weighting($i,$j,$d,$string1,$string2);
            $d[$i][$j] = $d[$p_i][$p_j]+$cost;
        }
    }

    return $d[$m][$n];
}

// Helper function for edit_distance()
function levenshtein_weighting($i,$j,$d,$string1,$string2)
{
    // if the two letters are equal, cost is 0
    if($string1[$i-1] === $string2[$j-1]) {
        return array($i-1,$j-1,0);
    }

    // cost we assign each operation
    $cost['delete'] = 1;
    $cost['insert'] = 1;
    $cost['substitute'] = 1;

    // cost of operation + cost to get to the substring we perform it on
    $total_cost['delete'] = $d[$i-1][$j] + $cost['delete'];
    $total_cost['insert'] = $d[$i][$j-1] + $cost['insert'];
    $total_cost['substitute'] = $d[$i-1][$j-1] + $cost['substitute'];

    // return the parent array keys of $d and the operation's cost
    $min_key = array_min_key($total_cost);
    if ($min_key == 'delete') {
        return array($i-1,$j,$cost['delete']);
    } elseif($min_key == 'insert') {
        return array($i,$j-1,$cost['insert']);
    } else {
        return array($i-1,$j-1,$cost['substitute']);
    }
}

// attempt to find the substring of $haystack most closely matching $needle
function shortest_edit_substring($needle, $haystack)
{
    // initialize edit distance matrix
    $m = strlen($needle);
    $n = strlen($haystack);
    $d = array();
    for($i=0;$i<=$m;$i++) {
        $d[$i][0] = $i;
        $backtrace[$i][0] = null;
    }
    // instead of strlen, we initialize the top row to all 0's
    for($i=0;$i<=$n;$i++) {
        $d[0][$i] = 0;
        $backtrace[0][$i] = null;
    }

    // same as the edit_distance calculation, but keep track of how we got there
    for($j=1; $j<=$n; $j++)
    {
        for($i=1; $i<=$m; $i++)
        {
            list($p_i,$p_j,$cost) = levenshtein_weighting($i,$j,$d,$needle,$haystack);
            $d[$i][$j] = $d[$p_i][$p_j]+$cost;
            $backtrace[$i][$j] = array($p_i,$p_j);
        }
    }

    // now find the minimum at the bottom row
    $min_key = array_min_key($d[$m]);
    $current = array($m,$min_key);
    $parent = $backtrace[$m][$min_key];

    // trace up path to the top row
    while(! is_null($parent)) {
        $current = $parent;
        $parent = $backtrace[$current[0]][$current[1]];
    }

    // and take a substring based on those results
    $start = $current[1];
    $end = $min_key;
    return substr($haystack,$start,$end-$start);
}

// some testing
$data = array( array('foo',' foo'), array('fat','far'), array('dat burn','rugburn'));
$data[] = array('big yellow school bus','they rode the bigyellow schook bus that afternoon');
$data[] = array('bus','they rode the bigyellow schook bus that afternoon');
$data[] = array('big','they rode the bigyellow schook bus that afternoon');
$data[] = array('nook','they rode the bigyellow schook bus that afternoon');
$data[] = array('they','console, controller and games are all in very good condition, only played occasionally. includes power cable, controller charge cable and audio cable. smoke free house. pes 2011 super street fighter');
$data[] = array('controker','console, controller and games are all in very good condition, only played occasionally. includes power cable, controller charge cable and audio cable. smoke free house. pes 2011 super street fighter');

foreach($data as $dat) {
    $substring = shortest_edit_substring($dat[0],$dat[1]);
    $dist = edit_distance($dat[0],$substring);
    printf("Found |%s| in |%s|, matching |%s| with edit distance %d\n",$substring,$dat[1],$dat[0],$dist);
}
//实用函数-返回数组最小值的键
函数数组\最小\键($arr)
{
$min_key=null;
$min=PHP\u INT\u MAX;
foreach($arr为$k=>$v){
如果($v<$min){
$min=$v;
$min_key=$k;
}
}
返回$min_键;
}
//计算两个字符串之间的编辑距离
函数编辑距离($string1,$string2)
{
$m=strlen($string1);
$n=strlen($string2);
$d=数组();
//从“”到substr的距离(字符串,$i)

对于($i=0;$iI建议您应该使用一个自定义字符串比较函数,该函数将使用一个符号公差。算法可能是这样的:将您的字符串一次按两个符号与最长的公共子字符串进行比较,一旦找到其中一个,继续逐符号比较。如果不匹配,请检查公差阈值,如果失败,请继续搜索查找LCS的可能开始。如果成功,请添加公差并检查下一个符号,将它们相互比较,并将第一个未处理的LCS符号进行比较。如果成功,请继续检查,就像刚刚发现遗漏或错误一样。Wagner-Fischer可能会为您提供一个良好的起点。您可以查看矩阵上的结果对角线,然后在这个基础上做点什么。我也会考虑。这个很好用,非常感谢你!如果有什么办法我可以给你买一瓶啤酒(或一盒),让我知道!