You are here

function readability_analyzer in Readability Analyzer 6

Same name and namespace in other branches
  1. 7 readability.module \readability_analyzer()
1 string reference to 'readability_analyzer'
readability_contentanalysis_analyzers in ./readability.module

File

./readability.module, line 46
Analyzes node content for search engine optimization recommendations

Code

function readability_analyzer($context, $analysis, $params) {
  $file = './' . drupal_get_path('module', 'readability') . '/php-text-statistics/TextStatistics.php';
  if (!file_exists($file)) {
    $msg = t('The readability module requires the open source php-text-statistics class. ');
    $msg .= l(t('Download the class here.'), 'http://code.google.com/p/php-text-statistics/', array(
      'attributes' => array(
        'target' => '_phptextstats',
      ),
    ));
    $msg .= "<br><br>";
    $msg .= t(' Download the files and place them in a folder named "php-text-statistics" under the readability module directory.');
    $analysis['messages'][] = contentanalysis_format_message($msg, 'error');
    return $analysis;
  }
  include $file;

  // analyze body
  $body = strtolower($context['body']);
  $body_notags = strip_tags($body);
  $textStatistics = new TextStatistics();
  $stats = array();
  $analysis['body']['stats'] = array();
  $gmin = variable_get('readability_targetgrade_min', READABILITY_DEFAULT_TARGETGRADE_MIN);
  $gmax = variable_get('readability_targetgrade_min', READABILITY_DEFAULT_TARGETGRADE_MAX);

  //$stats['flesch_kincaid_reading_ease'] = $textStatistics->flesch_kincaid_reading_ease($body_notags);

  //$analysis['body']['stats']['flesch_kincaid_reading_ease'] = contentanalysis_format_stat(t('Flesch Kincaid reading ease'),$stats['flesch_kincaid_reading_ease'],1);
  $stats['flesch_kincaid_grade_level'] = $textStatistics
    ->flesch_kincaid_grade_level($body_notags);
  $stats['gunning_fog_score'] = $textStatistics
    ->gunning_fog_score($body_notags);
  $stats['coleman_liau_index'] = $textStatistics
    ->coleman_liau_index($body_notags);
  $stats['smog_index'] = $textStatistics
    ->smog_index($body_notags);
  $stats['automated_readability_index'] = $textStatistics
    ->automated_readability_index($body_notags);
  $statuses = array();
  $total = 0;
  foreach ($stats as $k => $v) {
    $total += $v;
    $statuses[$k] = 'status';
    if ($v < $gmin || $v > $gmax) {
      $statuses[$k] = 'warning';
    }
  }
  $names = array(
    'flesch_kincaid_grade_level' => t('Flesch Kincaid'),
    'gunning_fog_score' => t('Gunning Fog Score'),
    'coleman_liau_index' => t('Coleman Liau Index'),
    'smog_index' => t('SMOG Index'),
    'automated_readability_index' => t('Automated Readability Index'),
  );
  foreach ($stats as $k => $v) {
    $analysis['body']['messages'][] = contentanalysis_format_message($names[$k] . ": " . number_format($v, 1), $statuses[$k]);
  }
  $stats['average'] = $total / 5;
  $statuses['average'] = 'complete';
  $analysis['#status'] = 'complete';
  $analysis['body']['#status'] = 'complete';
  if ($stats['average'] < $gmin || $stats['average'] > $gmax) {
    $statuses['average'] = 'warning';
    $analysis['#status'] = 'warning';
    $analysis['body']['#status'] = 'warning';
  }
  $analysis['body']['messages'][] = contentanalysis_format_message("<strong>" . t('Average') . ": " . number_format($stats['average'], 1) . "</strong>", $statuses['average']);
  $analysis['#context']['analyzer_data']['readability'] = $stats;
  return $analysis;
}