mb_strlen($w) >= 3 && !in_array($w, $stop, true)))), 0, 20); } public static function candidates(string $question, array $pages): array { $terms = self::terms($question); $results = []; foreach (array_slice($pages, 0, 50) 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']); return array_slice($results, 0, 5); } }