mb_strlen($w) >= 3 && !in_array($w, $stop, true)))), 0, 20); } /** Scan eligible content in bounded batches; retain only the five best excerpts. */ public static function local_candidates(string $question): array { $checked = 0; $store = TheDotDevAgenticBridge_Content_Metadata::normalized_store(); $pages = (static function () use (&$checked, $store): \Generator { foreach (TheDotDevAgenticBridge_Content_Policy::each_preview_row() as $row) { $post = get_post($row['id']); if (!$post instanceof WP_Post || !TheDotDevAgenticBridge_Content_Metadata::is_artifact_eligible($post, $store)) continue; ++$checked; yield ['id'=>(int)$post->ID, 'title'=>$post->post_title, 'url'=>get_permalink($post), 'text'=>mb_substr(trim(wp_strip_all_tags(strip_shortcodes($post->post_content), true)), 0, 20000)]; } })(); $results = self::candidates($question, $pages); return ['checked'=>$checked, 'results'=>$results]; } public static function candidates(string $question, iterable $pages): array { $terms = self::terms($question); $results = []; foreach ($pages as $page) { $text = mb_substr((string) $page['text'], 0, 20000); $haystack = mb_strtolower($text); $matches = array_values(array_filter($terms, static fn($term) => preg_match('/(? count($b['matches']) <=> count($a['matches']) ?: $a['id'] <=> $b['id']); $results = array_slice($results, 0, 5); } usort($results, static fn($a, $b) => count($b['matches']) <=> count($a['matches']) ?: $a['id'] <=> $b['id']); return array_slice($results, 0, 5); } }