diff --git a/public/index.php b/public/index.php index 1c502e7..ac36f28 100644 --- a/public/index.php +++ b/public/index.php @@ -566,6 +566,51 @@ switch ($action) { $categorySidebar[$aCat][] = $a; } } + // Articles proches : un search par mot du titre → OR implicite, cumul des scores + $similarArticles = []; + require_once BASE_PATH . '/src/SearchEngine.php'; + $_simEngine = new SearchEngine(); + $_relatedUuids = array_column($relatedArticles, 'uuid'); + $_stopWords = ['avec', 'dans', 'pour', 'une', 'les', 'des', 'sur', 'par', 'qui', 'que', + 'tout', 'mais', 'donc', 'comment', 'quand', 'plus', 'cette', 'cet', 'ces', + 'mon', 'ton', 'son', 'notre', 'votre', 'leur', 'tres', 'bien', 'fait', + 'aussi', 'comme', 'plus', 'sans', 'sous', 'entre', 'vers', 'chez']; + $_simPool = array_values(array_filter( + $_allPublished, + static function (array $a) use ($article, $privateCats, $_relatedUuids): bool { + if ($a['uuid'] === $article['uuid']) { + return false; + } + if (in_array($a['uuid'], $_relatedUuids, true)) { + return false; + } + if (strtotime((string)($a['published_at'] ?? '')) > time() && !isLoggedIn()) { + return false; + } + $cat = trim($a['category'] ?? ''); + return $cat === '' || !in_array($cat, $privateCats, true) || isLoggedIn(); + } + )); + $_titleWords = array_unique(array_values(array_filter( + preg_split('/\W+/u', mb_strtolower($article['title']), -1, PREG_SPLIT_NO_EMPTY) ?: [], + fn ($w) => mb_strlen($w) >= 4 && !in_array($w, $_stopWords, true) + ))); + $_scoreMap = []; + $_articleMap = []; + foreach ($_titleWords as $_word) { + foreach ($_simEngine->search($_word, $_simPool) as $_r) { + $_uuid = $_r['article']['uuid']; + $_scoreMap[$_uuid] = ($_scoreMap[$_uuid] ?? 0.0) + $_r['score']; + $_articleMap[$_uuid] = $_r['article']; + } + } + arsort($_scoreMap); + $similarArticles = array_map( + fn ($uuid) => $_articleMap[$uuid], + array_slice(array_keys($_scoreMap), 0, 5) + ); + unset($_simEngine, $_simPool, $_titleWords, $_stopWords, $_scoreMap, $_articleMap, $_relatedUuids); + unset($_allPublished); include BASE_PATH . '/templates/post_view.php'; diff --git a/templates/post_view.php b/templates/post_view.php index 3c31e10..fa34d9c 100644 --- a/templates/post_view.php +++ b/templates/post_view.php @@ -258,6 +258,27 @@ $hasSources = (!empty($externalLinks) || !empty($files))
Aucun autre article dans cette catégorie.
+ +