Estimate keyword density for a given block of text

I would like to use php to pass a keyword phrase to a function and to parse a block of text and return the keyword density of the input phrase as a percentage of the total number of words in the text block.

+3
source share
4 answers
$text = 'lorem ipsum etc';
$keyword = 'lorem ipsum';

$word_count = explode(' ', $text);
$word_count = count($word_count);

$keyword_count = preg_match_all("#{$keyword}#si", $text, $matches);
$keyword_count = count($matches);

$density = $keyword_count / $word_count * 100;

echo number_format($density, 2) . '%';
+2
source

:

  • , preg_split.
  • count, .
  • array_count_values .
  • .

, , .

0
source

Source: https://habr.com/ru/post/1771987/


All Articles