句子比较:忽略单词

时间:2013-03-25 17:25:51

标签: php regex

我需要一些句子比较的帮助。

    $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字符串中,正确且在括号中 - 因此,我不希望这些单词再次计算用户的响应。因此,如果用户有这些单词,则不会计入这些单词。如果用户没有这些单词,则不会计入这些单词。

这可能吗?

1 个答案:

答案 0 :(得分: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);
}