题外话- PHP最长公共子序列LCS

#1 278086346

最近在做文本相似度对比,研究了好多代码, 最长公共子序列LCS 感觉最准确,但是碰上一个问题,就是如果字符太大,就会奔溃,大概在1000字符。请问JAKE老大,能否帮我看看这段代码,能不能解决下这个字符问题。。。现在可以测试通过,$a,$b的内容在增加一点点,就会奔溃
class LCS {
    var $str1;
    var $str2;
    var $c = array();
    /*返回串一和串二的最长公共子序列
*/
    function getLCS($str1, $str2, $len1 = 0, $len2 = 0) {
        $this->str1 = $str1;
        $this->str2 = $str2;
        if ($len1 == 0) $len1 = strlen($str1);
        if ($len2 == 0) $len2 = strlen($str2);
        $this->initC($len1, $len2);
        return $this->printLCS($this->c, $len1 - 1, $len2 - 1);
    }
    /*返回两个串的相似度
*/
    function getSimilar($str1, $str2) {
        $len1 = strlen($str1);
        $len2 = strlen($str2);
        $len = strlen($this->getLCS($str1, $str2, $len1, $len2));
        return $len * 2 / ($len1 + $len2);
    }
    function initC($len1, $len2) {
        for ($i = 0; $i < $len1; $i++) $this->c[$i][0] = 0;
        for ($j = 0; $j < $len2; $j++) $this->c[0][$j] = 0;
        for ($i = 1; $i < $len1; $i++) {
            for ($j = 1; $j < $len2; $j++) {
                if ($this->str1[$i] == $this->str2[$j]) {
                    $this->c[$i][$j] = $this->c[$i - 1][$j - 1] + 1;
                } else if ($this->c[$i - 1][$j] >= $this->c[$i][$j - 1]) {
                    $this->c[$i][$j] = $this->c[$i - 1][$j];
                } else {
                    $this->c[$i][$j] = $this->c[$i][$j - 1];
                }
            }
        }
    }
    function printLCS($c, $i, $j) {
        if ($i == 0 || $j == 0) {
            if ($this->str1[$i] == $this->str2[$j]) return $this->str2[$j];
            else return "";
        }
        if ($this->str1[$i] == $this->str2[$j]) {
            return $this->printLCS($this->c, $i - 1, $j - 1).$this->str2[$j];
        } else if ($this->c[$i - 1][$j] >= $this->c[$i][$j - 1]) {
            return $this->printLCS($this->c, $i - 1, $j);
        } else {
            return $this->printLCS($this->c, $i, $j - 1);
        }
    }
}

$lcs = new LCS();
//返回最长公共子序列
// $lcs->getLCS("hello word","hello china");
//返回相似度
$a="Dreams! How nice do they sound? I am not talking about the dreams we have when we are asleep but about the life long aspirations1 in each one of us. I am sure many or rather shoud I say all of us have goals in our lives. It could be the goal to be the next millionaire or to be a popular celebrity2. Even if your goal is something small, you should be proud that you do have a goal.Some never tend to think about goals in life. Although this may sound as a surprise it is ture. These people take life as it comes without planning and go with the flow of events in their life. But again there are many who love planning for everything in their life.In fact, having a goal in life would make anyone more focused and determined3 ou're skilled, you will find that you can get your desired job easily. So, do you see that all it takes for your goals to materialize is some work on your part? Dreams! How nice do they sound? I am not talking about the dreams we have when we are asleep but about the life long aspirations1 in each one of us. I ";
$b="Dreams! How nice do they sound? I am not talking about the dreams we have when we are asleep but about the life long aspirations1 in each one of us. I am sure many or rather shoud I say all of us have goals in our lives. It could be the goal to be the next millionaire or to be a popular celebrity2. Even if your goal is something small, you should be proud that you do have a goal.Some never tend to think about goals in life. Although this may sound as a surprise it is ture. These people take life as it comes without planning and go with the flow of events in their life. But again there are many who love planning for everything in their life.In fact, having a goal in life would make anyone more focused and determined3 work on your dream. How, you might ask? Start learning web-designing skills and when you're skilled, you will find that you can get your desired job easily. SoDreams! How nice do they sound? I am not talking about the dreams we have when we are asleep but about the life long aspirations1 in each one of us. I ";
echo $lcs->getSimilar("$a","$b");






2015-09-06 20:29:03

#2 jake

PHP有个检查字符串相似度的函数,是不是你想要的?

http://cn.php.net/manual/zh/function.similar-text.php

2015-09-07 08:46:37