You are here

readability.module in Readability Analyzer 7

Same filename and directory in other branches
  1. 6 readability.module

Analyzes node content for search engine optimization recommendations

File

readability.module
View source
<?php

/**
 * @file
 * Analyzes node content for search engine optimization recommendations
 */

/**
 * Implementation of hook_menu().
 */
function readability_menu() {
  $items = array();
  return $items;
}

/**
 *  Implementation of hook_contentalalysis_analyzers()
 *  
 */

/*
function readability_contentanalysis_analyzers() {
  $analyzers['readability'] = array(
    'title' => t('Readability'),
    'module' => 'readability',
    'callback' => 'readability_analyzer',

    'weight' => -5,
  ); 
  return $analyzers;  
}
*/
function readability_contentanalysis_analyzer_info() {
  $analyzers['readability'] = array(
    'title' => t('Readability'),
    'module' => 'readability',
    'callback' => 'readability_analyzer',
    'settings form elements callback' => 'readability_settings_form_elements',
    'weight' => -5,
  );
  $analyzers['readability']['settings'] = array(
    'readability_grade_range' => array(
      7,
      12,
    ),
    'readability_grade_range_warning' => array(
      5,
      14,
    ),
  );
  return $analyzers;
}
function readability_insight_analyzer_info() {
  $analyzers['readability'] = array(
    'title' => t('Readability'),
    'type' => 'content',
    'contentanalyzer' => 'readability',
  );
  $reports['readability'] = array(
    'title' => t('Readability'),
    'short title' => t('Read'),
    'score type' => 'value',
    'alerts' => array(
      'readability_grade_range',
    ),
  );
  $analyzers['readability']['reports'] = $reports;
  return $analyzers;
}
function readability_settings_form_elements($settings, $analyzer_def) {
  $defaults = $analyzer_def['settings'];
  $name = 'readability_grade_range';
  $form[$name] = array(
    '#type' => 'contentanalysis_slider_range',
    '#title' => t('Acceptable grade level range'),
    '#default' => isset($settings[$name]) ? $settings[$name] : $defaults[$name],
    '#min' => 1,
    '#max' => 20,
    '#slider_suffix' => t('normal: '),
  );
  $name = 'readability_grade_range_warning';
  $form[$name] = array(
    '#type' => 'contentanalysis_slider_range',
    '#default' => isset($settings[$name]) ? $settings[$name] : $defaults[$name],
    '#min' => 1,
    '#max' => 20,
    '#slider_suffix' => t('warning: '),
    '#description' => t('Use to set acceptable grade level range for content.'),
  );
  return $form;
}

/**
 * Implementation of hook_analyzer() via custom define callback
 * 
 * Analyzes content based on readability algorithms from php-text-statistics
 * 
 * @rerturn array
 *   readability analysis
 */
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;
}

Functions