You are here

function readability_analyzer in Readability Analyzer 7

Same name and namespace in other branches
  1. 6 readability.module \readability_analyzer()

Implementation of hook_analyzer() via custom define callback

Analyzes content based on readability algorithms from php-text-statistics

@rerturn array readability analysis

1 string reference to 'readability_analyzer'
readability_contentanalysis_analyzer_info in ./readability.module

File

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

Code

function readability_analyzer($context, $analysis, $params) {
  $files[] = './' . drupal_get_path('module', 'readability') . '/php-text-statistics/TextStatistics.php';
  if (module_exists('libraries') && ($p = libraries_get_path('php-text-statistics'))) {
    $files[] = './' . $p . '/TextStatistics.php';
  }
  foreach ($files as $f) {
    if (file_exists($f)) {
      $file = $f;
      break;
    }
  }
  if (!$file) {
    $msg = t('The readability module requires the open source php-text-statistics class. ');
    $msg .= l(t('Download the class here.'), 'https://github.com/DaveChild/Text-Statistics', array(
      'attributes' => array(
        'target' => '_phptextstats',
      ),
    ));
    $msg .= "<br><br>";
    if (module_exists('libraries')) {
      $msg .= t('Download the files and place them in a folder named "php-text-statistics" under the libraries directory.');
    }
    else {
      $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;
  }
  require_once $file;

  // analyze body
  $body = strtolower($context['body']);
  $body_notags = strip_tags($body);
  $textStatistics = new TextStatistics();
  $stats = array();
  $analysis['body']['stats'] = array();
  $gmin = $context['analyzers']['readability']['settings']['readability_grade_range'][0];
  $gmax = $context['analyzers']['readability']['settings']['readability_grade_range'][1];
  $gminwarn = $context['analyzers']['readability']['settings']['readability_grade_range_warning'][0];
  $gmaxwarn = $context['analyzers']['readability']['settings']['readability_grade_range_warning'][1];

  //$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';
    }
    if ($v < $gminwarn || $v > $gmaxwarn) {
      $statuses[$k] = 'error';
    }
  }
  $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';
  $alert = '';
  if ($stats['average'] < $gmin || $stats['average'] > $gmax) {
    $statuses['average'] = 'warning';
    $analysis['#status'] = 'warning';
    $analysis['body']['#status'] = 'warning';

    // set insight alert
    $alert = $stats['average'] < $gmin ? 'low' : 'high';
    $alert_status = 1;
  }
  if ($stats['average'] < $gminwarn || $stats['average'] > $gmaxwarn) {
    $statuses['average'] = 'error';
    $analysis['#status'] = 'error';
    $analysis['body']['#status'] = 'error';
    $alert = $stats['average'] < $gminwarn ? 'low' : 'high';
    $alert_status = 0;
  }
  $analysis['#score'] = round($stats['average'], 1);
  $analysis['body']['messages'][] = contentanalysis_format_message(t('<strong>Average: %average</strong>', array(
    '%average' => number_format($stats['average'], 1),
  )), $statuses['average']);
  $analysis['#context']['analyzer_data']['readability'] = $stats;
  if ($alert) {
    if ($alert == 'low') {
      $msg = t('The readability grade level is to low.');
    }
    else {
      $msg = t('The readability grade level is to high.');
    }
    $analysis['#insight']['alerts']['readability_grade_range'] = array(
      'message' => $msg,
      'status' => $alert_status,
      'report' => 'readability',
    );
  }
  return $analysis;
}