gpt4 book ai didi

php - 当 haystack 包含额外标记时,文本解析器会在 needle 上给出假阴性

转载 作者:搜寻专家 更新时间:2023-10-31 21:44:42 25 4
gpt4 key购买 nike

下面的代码采用一个关键字和一串文本(经过 html 标记清理),并确定该关键字是否出现在经过清理的内容的最后一句话中。

有一个小故障我想不通。当内容末尾包含一个空格或带有不间断空格的段落标签时,即

This is the last sentence.<p>&nbsp;</p>

我得到了假阴性(不匹配),尽管事实上 (1) 关键字肯定在最后一句话中,并且 (2) strip_tags() 函数应该在最后呈现标签的外观不是问题.

有人知道为什么会这样吗?

function plugin_get_kw_last_sentence($post) {
$theContent = strip_tags(strtolower($post->post_content));
$theKeyword = 'test';
$thePiecesByKeyword = plugin_get_chunk_keyword($theKeyword,$theContent);
if (count($thePiecesByKeyword)>0) {
$theCount = $thePiecesByKeyword[count($thePiecesByKeyword)-1];
$theCount = trim($theCount,'.');
if (substr_count($theCount,'.')>0) {
return FALSE;
} else {
return TRUE;
}
}
return FALSE;
}

function plugin_get_chunk_keyword($theKeyword, $theContent) {
if (!plugin_get_kw_in_content($theKeyword,$theContent)) {
return array();
}

$myPieceReturn = preg_split('/\b' . $theKeyword . '\b/i', $theContent);
return $myPieceReturn;
}

最佳答案

如果我正确理解您的逻辑,我认为可以仅在正则表达式中涵盖您那里发生的很多事情。整个逻辑不能简化为:

function plugin_get_kw_last_sentence($post) {
$pattern = '/' . $theKeyword . '[^.!?]*[.!?][^.!?]*$/';
$subject = strip_tags(strtolower($post->post_content));
return preg_match($pattern, $subject);
}

正则表达式在找到您的关键字和最后一个句子结尾标点符号且它们之间没有其他句子结尾标点符号时匹配。

现在这显然不是防弹的,因为诸如头衔(即先生、夫人)等...以及包括这些句末标点符号在内的任何其他内容都会让您失望。这应该可以满足您的要求,因为您给定的代码也没有考虑到这些情况。

关于php - 当 haystack 包含额外标记时,文本解析器会在 needle 上给出假阴性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5799263/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com