4

谁能建议我一个更好的方法(或最首选的方法)来使用模糊逻辑找到两个字符串之间的匹配百分比(即这两个字符串(例如名称)在百分比方面的相关程度)。?谁能帮我写代码?真的我想知道从哪里开始..

4

2 回答 2

8
$str1 = 'Hello';
$str2 = 'Hello, World!';
$percent;
similar_text($str1, $str2, $percentage);

http://php.net/manual/en/function.similar-text.php

于 2013-09-16T16:43:40.650 回答
6

字比较器

这是基于单词的比较 - 它比基于字符的比较快得多,而且按单词比较人类文本通常更有意义。但是,字长确实很重要。该算法考虑到这一点,以获得更好的结果。最后检查测试结果;我认为它们几乎就是人类会说的。

function wordSimilarity($s1,$s2) {

    $words1 = preg_split('/\s+/',$s1);
    $words2 = preg_split('/\s+/',$s2);
    $diffs1 = array_diff($words2,$words1);
    $diffs2 = array_diff($words1,$words2);

    $diffsLength = strlen(join("",$diffs1).join("",$diffs2));
    $wordsLength = strlen(join("",$words1).join("",$words2));
    if(!$wordsLength) return 0;

    $differenceRate = ( $diffsLength / $wordsLength );
    $similarityRate = 1 - $differenceRate;
    return $similarityRate;

}

此函数为您提供介于 0 和 1 之间的浮点值,其中 1 是总相似度。

让我们看看一些测试


    $test = "this is something you've never done before";
    
    wordSimilarity($test,"this is something you've never done before");  //  1.000
    wordSimilarity($test,"this is something");                           //  0.588
    wordSimilarity($test,"this is nothing you have ever done");          //  0.312
    wordSimilarity($test,"leave me alone with lorem ipsum");             //  0.000
    wordSimilarity($test,"before you do something you've never done");   //  0.845
    wordSimilarity($test,"never have i ever done this");                 //  0.448

于 2015-08-14T13:57:43.230 回答