Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/facebook/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 句子比较:忽略单词_Php_Regex - Fatal编程技术网

Php 句子比较:忽略单词

Php 句子比较:忽略单词,php,regex,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

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

    $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=“这是(正确且)可接受的答案。如果括号内的内容不在用户答案中,则将忽略。如果存在,则不应计入括号内的内容。”;
$response=“这是正确且可接受的答案。如果用户答案中不存在括号内的内容,则将忽略该内容。如果存在,则不应计入括号内的内容。”;
回声“可接受的答案:”;
回声“$answer
”; 回显“用户回答:”; 回显“$response.”; //括号中的条带内容 $answer=preg_replace(“/\([^)]*\)|[()]/”,“”,$answer); //条状标点符号 $answer=preg_replace(“/[^a-zA-Z 0-9]+/”,“,$answer”); $response=preg_replace(“/[^a-zA-Z 0-9]+/”,“,$response”); $common=相似的文本($answer,$response,$percent); $orgcount=strlen($answer); printf(“用户的响应有%d/$orgcount个常用字符(%.2f%%)。”,$common,$percent);
基本上我想做的是忽略父母的话语。例如,在$answer字符串中,correct和都在括号中-因此,我不希望这些单词再次计入用户的响应。因此,如果用户有这些单词,则不计入它们。如果用户没有这些词,也不算不上


这可能吗?

多亏了这些评论,我写了一个解决方案,因为我认为将它放入函数中是一个“漫长”的过程。 编辑:调试后发现,如果位置为
0
,则
strpos()
会导致一些问题,因此我添加了一条
语句:

$answer=“(这)是(正确且)可接受的答案。(随机不计算在内)如果括号内的内容不存在于用户的答案中,则将被忽略。如果存在,则不应计入括号内的内容。”;
$response=“这是正确且可接受的答案。如果用户答案中不存在括号内的内容,则将忽略该内容。如果存在,则不应计入括号内的内容。”;
回显“用户的响应有”。四舍五入(比较($answer,$response),2)。'%characters in common';//用户的响应有100%的公共字符
函数比较($answer,$response){
preg_match_all(“/\(?P[^\)]+)\)/”,$answer,$括号);
$catch=$contracts['contracts'];
foreach($catch作为$words){
如果(!strpos($response,$words)==false | | strpos($response,$words)==0){//如果确实存在,请删除括号
$answer=str_replace(“(“$words”)”,$words,$answer);
}else{//如果不存在,请删除括号中的单词
$answer=str_替换(“(“$words”)”,“$answer”;
}
}
/*消毒*/
$answer=preg_replace(数组('/[^a-zA-Z0-9]+/','/+/')、数组(''')、$answer);
$response=preg_replace(数组('/[^a-zA-Z 0-9]+/','/+/')、数组(''')、$response);
$common=相似的文本($answer,$response,$percent);
回报率(百分比);
}

在执行
preg\u replace
功能后,
$answer
$response
包含哪些内容?看起来你已经去掉了任何不是字母数字和空格的东西。我的意思是这不会改变相似性百分比。因此,如果用户有或没有括号中的单词,它不会改变百分比。但是,如果他们没有没有没有括号的东西,这将影响相似性百分比。$answer:
这是括号内可接受的答案内容,如果用户的答案中不存在,则忽略该内容如果存在,则不应计入这些内容。
$response:
这是括号内正确且可接受的答案内容,如果用户的答案中不存在,则忽略该内容用户的答案如果存在,则不应计入他们的答案
,您的代码当前失败的原因是什么?@MikeM it当前影响带括号的单词的百分比。使用删除括号中单词的正则表达式,并且用户有这些单词,它会降低百分比。只是尝试了一下,是的,不幸的是,它仍然会降低百分比。当然,百分比会降低,因为响应包含
正确和
。也许我不太理解这个问题?我不希望如果单词被括在括号中,并且它们存在于用户的响应中,那么百分比会下降。如果单词不在括号中,并且它们在响应中不存在,那么百分比应该会下降。@Dutchcoffee好的,我接近解决方案了。还有一个问题:如果括号中的单词不在回答中,它应该降低百分比吗?不用担心。非常感谢你的帮助。我非常感激。
$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);
}