2013-03-25 86 views
1

我需要一些與句子比較的幫助。句子比較:忽略單詞

$answer = "This is the (correct and) acceptable answer. Content inside the parenthesis are ignored if not present in the user's answer. If it is present, it should not count against them."; 
    $response = "This is the correct and acceptable answer. Content inside the parenthesis are ignored if not present in the user's answer. If it is present, it should not count against them."; 

    echo "<strong>Acceptable Answer:</strong>"; 
    echo "<pre style='white-space:normal;'>$answer</pre><hr/>"; 
    echo "<strong>User's Answer:</strong>"; 
    echo "<pre>".$response."</pre>"; 

    // strip content in brackets 
    $answer = preg_replace("/\([^)]*\)|[()]/", "", $answer); 

    // strip punctuation 
    $answer = preg_replace("/[^a-zA-Z 0-9]+/", " ", $answer); 
    $response = preg_replace("/[^a-zA-Z 0-9]+/", " ", $response); 

    $common = similar_text($answer, $response, $percent); 
    $orgcount = strlen($answer); 
    printf("The user's response has %d/$orgcount characters in common (%.2f%%).", $common, $percent); 

基本上我想要做的是忽略父母的話。例如,在$ answer字符串中,正確,而位於括號內 - 因此,我不希望這些單詞再次計算用戶的回覆。因此,如果用戶擁有這些詞語,則不會對他們進行計數。如果用戶沒有這些詞,那麼它就不算對他們。

這可能嗎?

+0

做什麼'$ answer'和'$ response'包含後您的' preg_replace'功能?看起來你已經剝離出任何不是字母數字和空格的東西。 – Kyle 2013-03-25 17:29:32

+0

_Count反對他們_我的意思是它不會改變相似性百分比。所以,如果用戶有或沒有加括號的單詞,它不會改變百分比。但是,如果他們沒有不加括號的內容,會影響相似性百分比。 – Dutchcoffee 2013-03-25 17:32:08

+0

** $ answer:**'這是可以接受的答案如果不存在於用戶的答案中,則括號內的內容將被忽略如果它存在,則不應該對它們進行計數** ** $ response:**'這是正確和可接受的答案如果不存在於用戶的答案中,則括號內的內容將被忽略如果它存在,則不應該對它們進行計數「 – Dutchcoffee 2013-03-25 17:34:27

回答

2

感謝評論,我寫了一個解決方案,因爲它是一個「長」的過程,我儘管把它放在一個函數中。 編輯:調試後,它傳出strpos()是造成一些麻煩,如果位置是0,所以我增加了一個OR聲明:

$answer = "(This) is the (correct and) acceptable answer. (random this will not count) Content inside the parenthesis are ignored if not present in the user's answer. If it is present, it should not count against them."; 
$response = "This is the correct and acceptable answer. Content inside the parenthesis are ignored if not present in the user's answer. If it is present, it should not count against them."; 

echo 'The user\'s response has '.round(compare($answer, $response),2).'% characters in common'; // The user's response has 100% characters in common 

function compare($answer, $response){ 
    preg_match_all('/\((?P<parenthesis>[^\)]+)\)/', $answer, $parenthesis); 

    $catch = $parenthesis['parenthesis']; 
    foreach($catch as $words){ 
     if(!strpos($response, $words) === false || strpos($response, $words) === 0){ // if it does exist then remove brackets 
      $answer = str_replace('('.$words.')', $words, $answer); 
     }else{ //if it does not exist remove the brackets with the words 
      $answer = str_replace('('.$words.')', '', $answer); 
     } 
    } 
    /* To sanitize */ 
    $answer = preg_replace(array('/[^a-zA-Z0-9]+/', '/ +/'), array(' ', ' '), $answer); 
    $response = preg_replace(array('/[^a-zA-Z 0-9]+/', '/ +/'), array(' ', ' '), $response); 
    $common = similar_text($answer, $response, $percent); 
    return($percent); 
} 
+0

剛剛嘗試過,是的,不幸的是它仍然下降了百分比。 – Dutchcoffee 2013-03-25 18:27:40

+0

當然,這個百分比會下降,因爲響應包含「正確的」和「。也許我不太瞭解這個問題? – HamZa 2013-03-25 18:41:16

+0

如果單詞括在圓括號之間並且它們存在於用戶的回覆中,我不希望百分比下降。如果單詞不在括號內,並且它們不存在於響應中,則百分比應該下降。 – Dutchcoffee 2013-03-25 18:45:11