Вот мое решение с использованием встроенных функций PHP:
most_frequent_words - Найти наиболее часто встречающиеся слова в строке
function most_frequent_words($string, $stop_words = [], $limit = 5) {
$string = strtolower($string); // Make string lowercase
$words = str_word_count($string, 1); // Returns an array containing all the words found inside the string
$words = array_diff($words, $stop_words); // Remove black-list words from the array
$words = array_count_values($words); // Count the number of occurrence
arsort($words); // Sort based on count
return array_slice($words, 0, $limit); // Limit the number of words and returns the word array
}
Возвращаемый массив содержит слова, которые чаще всего встречаются в строке.
Параметры:
строка $ строка - строка ввода.
массив $ stop_words (необязательно) - список слов, отфильтрованных из массива, пустой массив по умолчанию.
строка $ limit (необязательно) - ограничение количества возвращаемых слов, по умолчанию 5 .