Essentially, it will be necessary to break the text into words for an array. Then we need to count the repeaters, order the result of the highest number of repeaters for the lowest number of repeaters and finally stay only with the first X.
For this we will use the PHP function array_count_values()
to count the values in the matrix, the PHP function str_word_count()
to count the number of times the word exists in the given text, the PHP function arsort()
to sort the array in descending order without losing the key relation and finally the PHP function array_slice()
to remain in the matrix only the desired amount of words:
/**
* Palavras Mais Repetidas
* Com base no texto recebido, devolver as primeiras X
* palavras mais repetidas
*
* @param string $texto O texto a avaliar
* @param integer $quantidade A quantidade de palavras a devolver
*
* @return array Matriz com as palavras mais repetidas
*/
function palavrasMaisRepetidas($texto="", $quantidade=4) {
$palavras = array_count_values(str_word_count($texto, 1));
arsort($palavras);
return array_slice($palavras, 0, $quantidade);
}
Example:
$texto = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas porttitor non felis quis dignissim. Morbi varius arcu lorem, eget efficitur nibh interdum vitae. Aenean tristique hendrerit diam a consequat. Nunc eleifend dolor ut rhoncus sollicitudin. Suspendisse tincidunt sodales turpis et egestas. Sed maximus libero malesuada lacus tempor, quis placerat nunc varius. Nam eget lectus imperdiet, lobortis mi sit amet, tristique justo. Fusce in felis et erat auctor vehicula quis dapibus libero. In commodo a leo eu eleifend.";
var_dump(palavrasMaisRepetidas($texto, 5));
Upshot:
array(4) {
["quis"]=>
int(3)
["tristique"]=>
int(2)
["varius"]=>
int(2)
["a"]=>
int(2)
}
See example in Ideone.
@user3163662 has different ways. How do you want to use the result? one by one, within an array? it matters the number of times it appeared or only that it is one of the x first?
– Sergio
You’re too quick to answer... or I’m getting old... I ended up using the same thing as you, but I did a beautiful job to deal with it :P
– Zuul
@Zuul: Here in Sweden it is cold, we have to move our fingers fast to warm up :)
– Sergio