From e127598802a826755120ce1eebcb5f85fcabc607 Mon Sep 17 00:00:00 2001 From: SansGuidon Date: Tue, 14 Jan 2025 14:57:16 +0000 Subject: [PATCH] fix(autoTag) improve word matching accuracy --- auto_tag_plugin/auto_tag_plugin.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/auto_tag_plugin/auto_tag_plugin.php b/auto_tag_plugin/auto_tag_plugin.php index 60ffb21..7ff599f 100644 --- a/auto_tag_plugin/auto_tag_plugin.php +++ b/auto_tag_plugin/auto_tag_plugin.php @@ -152,7 +152,6 @@ function fetch_page_content($url) function calculate_tags(array $keywordsToTags, array $searchContents): array { - // Context weights $contextWeights = [ 'title' => 3, 'url' => 3, @@ -166,16 +165,17 @@ function calculate_tags(array $keywordsToTags, array $searchContents): array foreach ($keywordsToTags as $keywords => $tags) { $keywordList = explode(',', $keywords); foreach ($keywordList as $keyword) { - $keyword = trim($keyword); + $keyword = preg_quote(trim($keyword), '/'); + $regex = '/\b' . $keyword . '\b/i'; + foreach ($searchContents as $context => $content) { - if (stripos($content, $keyword) !== false) { + if (preg_match($regex, $content)) { $tagList = explode(',', $tags); foreach ($tagList as $tag) { $tag = trim($tag); if (!isset($tagScores[$tag])) { $tagScores[$tag] = 0; } - // Add weight based on context $tagScores[$tag] += $contextWeights[$context]; } }