You are here

function contentoptimizer_calc_stats in Content Optimizer 8

Same name and namespace in other branches
  1. 6.2 contentoptimizer.module \contentoptimizer_calc_stats()
  2. 6 contentoptimizer.module \contentoptimizer_calc_stats()
  3. 7.2 contentoptimizer.module \contentoptimizer_calc_stats()

Calculates content stats

Parameters

string $content: Subject to be have stats calculated on

string $keyword: Keyword target

Return value

array Asscociated array of various content statistics

1 call to contentoptimizer_calc_stats()
contentoptimizer_analyzer in ./contentoptimizer.module
Implementation of hook_contentanalysis_analyzer() via custom define callback

File

./contentoptimizer.module, line 995
Analyzes content for search engine optimization recommendations

Code

function contentoptimizer_calc_stats($content, $keyword) {
  $ret = array();
  $ret['char_count'] = strlen($content);
  $ret['word_count'] = str_word_count($content);
  $ret['keyword_count'] = 0;
  $ret['keyword_density'] = 0;
  $ret['keyword_prominence'] = 0;
  if ($keyword) {
    $content_segs = explode($keyword, $content);
    $ret['keyword_count'] = count($content_segs) - 1;
    $ret['keyword_density'] = 0;
    if ($ret['word_count']) {
      $ret['keyword_density'] = 100 * $ret['keyword_count'] / $ret['word_count'];
    }
    $ret['keyword_positionsum'] = 0;
    $i = 0;
    foreach ($content_segs as $seg) {
      if ($i >= $ret['keyword_count']) {
        break;
      }
      $wordpos = str_word_count($seg) + 1;
      $ret['keyword_positionsum'] += $wordpos;
      $i++;
    }

    // prominence = ($totalwords - (($positionsum - 1) / $positionsnum)) * (100 / $totalwords)
    if ($ret['keyword_count']) {
      $ret['keyword_prominence'] = ($ret['word_count'] - ($ret['keyword_positionsum'] - 1) / $ret['keyword_count']) * (100 / $ret['word_count']);
    }
  }
  return $ret;
}