2013-07-10 14 views
0

我想比較一組文本,並得到如何類似/相關他們對彼此,我使用similar_text(),但我發現它不是那麼準確。謝謝。我想比較一組文本,並獲得他們對彼此有多麼相似/相關,我使用similar_text(),但我發現它不是那麼準確

例如下面的文本給了我66%

文本1:創新遊戲,吃飯,睡覺,呼吸創新。它喜歡創意&激情動力互聯網驅動器。我們瞭解時間最大的資產,挑戰時間截止日期。

Text2:蘇聯共產主義政策;德國聯盟組織僞裝精力毆打通緝。

我的代碼如下:

echo $student_answer = removeCommonWords($answer)."<br><br>"; 

$student_answer = strip_tags($student_answer); 

echo $memo = removeCommonWords2($memo)."<br><br>"; 

echo similar_text($memo, $student_answer); 
+0

你將不得不根據你定義「準確」的含義。例如,告訴我們你對兩個樣本字符串的期望結果,以及你用來得出這個結果的方法的簡短描述。 – RandomSeed

+0

按照此代碼的百分比準確度:echo similar_text($ memo,$ student_answer,$ percentage); 假設你想比較學生的答案和備忘錄中的答案。 – user2553806

+0

但是你說這個函數沒有給你滿意的結果? – RandomSeed

回答

0

您可以使用JS版本:

http://phpjs.org/functions/similar_text/

的JS代碼顯示了precent代碼(你可以修改代碼):

return (sum * 200)/(firstLength + secondLength); 

我希望這能幫到你!

編輯:

如何使用similar_text在JS?

  1. 創建一個名爲similar_text.js一個文件,複製&粘貼此代碼在它:

    function similar_text (first, second, percent) { 
    // http://kevin.vanzonneveld.net 
    // + original by: Rafał Kukawski (http://blog.kukawski.pl) 
    // + bugfixed by: Chris McMacken 
    // + added percent parameter by: Markus Padourek (taken from http://www.kevinhq.com/2012/06/php-similartext-function-in-javascript_16.html) 
    // *  example 1: similar_text('Hello World!', 'Hello phpjs!'); 
    // *  returns 1: 7 
    // *  example 2: similar_text('Hello World!', null); 
    // *  returns 2: 0 
    // *  example 3: similar_text('Hello World!', null, 1); 
    // *  returns 3: 58.33 
    if (first === null || second === null || typeof first === 'undefined' || typeof second === 'undefined') { 
        return 0; 
    } 
    
    first += ''; 
    second += ''; 
    
    var pos1 = 0, 
        pos2 = 0, 
        max = 0, 
        firstLength = first.length, 
        secondLength = second.length, 
        p, q, l, sum; 
    
    max = 0; 
    
    for (p = 0; p < firstLength; p++) { 
        for (q = 0; q < secondLength; q++) { 
        for (l = 0; 
        (p + l < firstLength) && (q + l < secondLength) && (first.charAt(p + l) === second.charAt(q + l)); l++); 
        if (l > max) { 
         max = l; 
         pos1 = p; 
         pos2 = q; 
        } 
        } 
    } 
    
    sum = max; 
    
    if (sum) { 
        if (pos1 && pos2) { 
        sum += this.similar_text(first.substr(0, pos2), second.substr(0, pos2)); 
        } 
    
        if ((pos1 + max < firstLength) && (pos2 + max < secondLength)) { 
        sum += this.similar_text(first.substr(pos1 + max, firstLength - pos1 - max), second.substr(pos2 + max, secondLength - pos2 - max)); 
        } 
    } 
    
    if (!percent) { 
        return sum; 
    } else { 
        return (sum * 200)/(firstLength + secondLength); 
    } 
    } 
    
  2. 在你把下面一行:

    <script type="text/JavaScript" src="YOUR_PATH/similar_text.js"></script> 
    
  3. 現在你可以使用它在你的身上:

    <script> 
        similar_text('Hello World!', 'Hello phpjs!'); 
        </script> 
    

它將輸出7.

希望這WIL幫助您!

+0

好吧,我對JS很不好,但我會看看它,謝謝。 – user2553806

+0

我很樂意幫助你在JS中使用similar_text。 –

+0

我會用步驟編輯我的答案。 –

相關問題