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文字列では、正しく、括弧で囲まれています。このため、これらの単語がユーザーの応答に対してカウントされないようにします。したがって、ユーザーがこれらの単語を持っている場合、それはそれらに対してカウントされません。そして、ユーザーがこれらの単語を持っていない場合、それはそれらに対してカウントされません。

これは可能ですか?

4

1 に答える 1

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);
}
于 2013-03-25T18:07:56.570 に答える