keha76

LIX - Swedish Readability Calculator

Aug 24th, 2012
221
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. /**
  3.  *    Läsbarhetsindex ( LIX )
  4.  *    Swedish Readability Calculator
  5.  *
  6.  *    @author Kenth Hagström
  7.  *    @version 1.0
  8.  *    @license GPL
  9.  *    @param $text string
  10.  *    @param $periods array
  11. **/
  12. function lix( $text, $periods = array( '.', '!', '?', ':' ) )
  13. {
  14.     // Remove HTML tags from the text content
  15.     $text = strip_tags( $text );
  16.  
  17.     // Get all words from the text and count 'em
  18.     $all_words = str_word_count( $text, 2 );
  19.     $total_words = count( $all_words );
  20.    
  21.     // Determine the number of sentences
  22.     $total_sentences   = 0;
  23.     foreach( $periods as $punctuation )
  24.     {
  25.         $total_sentences += substr_count( $text, $punctuation );
  26.     }
  27.  
  28.     // Get the number of long words, ie words longer than 6 characters
  29.     $long_words = 0;
  30.     foreach( $all_words as $word )
  31.     {
  32.         if( strlen( $word ) > 6 )
  33.         {
  34.             $long_words++;
  35.         }
  36.     }
  37.  
  38.     // Some basic math, http://sv.wikipedia.org/wiki/LIX
  39.     $average_sentence_length =  $total_words / $total_sentences;
  40.     $part_long_words = 100 * ( $long_words / $total_words );
  41.     $lix = $average_sentence_length + $part_long_words;
  42.  
  43.     return $lix;
  44. }
  45. ?>
RAW Paste Data