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 回答