2013-03-25 3 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); 

기본적으로 내가 원하는 것은 parenthiseised 단어를 무시하는 것입니다. 예를 들어, $ 응답 문자열에 이 정확하고이 괄호 안에 있습니다.이 때문에이 단어가 사용자의 응답을 다시 계산하는 것을 원하지 않습니다. 따라서 사용자가이 단어를 사용하면 해당 단어는 포함되지 않습니다. 그리고 사용자가이 단어들을 가지고 있지 않다면, 그것들에 포함되지 않습니다.

이것이 가능합니까?

+0

이 후 포함 당신' preg_replace' 함수? 당신이 이미 영숫자와 공간이 아닌 것을 제거하고있는 것처럼 보입니다. – Kyle

+0

_ 그 (것)들에 대하여 계산하십시오 나는 유사성 백분율을 바꾸지 않을 것이라는 점을 의미한다. 따라서 사용자가 괄호 안에있는 단어를 가지고 있거나 갖지 않으면 백분율이 변경되지 않습니다. 그러나 괄호 안의 값을 갖지 않으면 유사 율에 영향을줍니다. – Dutchcoffee

+0

** $ answer : **'이것은 수용 가능한 대답입니다. 괄호 안의 내용은 사용자의 대답에없는 경우 무시됩니다. 대답이있는 경우에는 해당 항목에 대해 계산해서는 안됩니다. '** $ response : **'이것은 정확하고 수용 가능한 대답 괄호 안의 내용은 사용자 응답에 존재하지 않는다면 무시됩니다. 존재한다면 그것들에 대해 계산해서는 안됩니다. ' – Dutchcoffee

답변

2

의견을 보내 주신 덕분에 솔루션에 썼습니다. 기능에 넣기에는 "긴"프로세스이기 때문입니다. 편집 : 디버깅 후에는 위치가 0 인 경우 strpos() 몇 가지 문제를 일으키는 것을 나왔다, 그래서는 OR 문 추가 :

는`$의 answer`과`$의 response`을 무엇
$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

+0

응답에 'correct and'가 포함되어 있기 때문에 백분율이 떨어집니다. 어쩌면 그 문제를 이해하지 못했을까요? – HamZa

+0

단어가 괄호로 묶여 있고 단어가 사용자의 응답에있는 경우 비율을 떨어 뜨리는 것은 원하지 않습니다. 단어가 괄호 안에없고 응답에 존재하지 않으면 백분율이 감소해야합니다. – Dutchcoffee

관련 문제