Text_Statistics
[ class tree: Text_Statistics ] [ index: Text_Statistics ] [ all elements ]

Source for file Statistics.php

Documentation is available at Statistics.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 softtabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2002 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.0 of the PHP license,       |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Author: George Schlossnagle <george@omniti.com>                      |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: Statistics.php 251907 2008-02-01 06:52:59Z cweiske $
  20.  
  21. require_once "Text/Word.php";
  22.  
  23. /**
  24. *  Text_Statistics calculates some basic readability metrics on a
  25. *  block of text.  The number of words, the number of sentences,
  26. *  and the number of total syllables is counted.  These statistics
  27. *  can be used to calculate the Flesch score for a sentence, which
  28. *  is  a number (usually between 0 and 100) that represents the
  29. *  readability of the text.  A basic breakdown of scores is:
  30. *
  31. *  90 to 100  5th grade
  32. *  80 to 90   6th grade
  33. *  70 to 80   7th grade
  34. *  60 to 70   8th and 9th grade
  35. *  50 to 60   10th to 12th grade (high school)
  36. *  30 to 50   college
  37. *  0 to 30    college graduate
  38. *
  39. *  More info can be read up on at
  40. *  http://www.mang.canterbury.ac.nz/courseinfo/AcademicWriting/Flesch.htm
  41. *
  42. *  require 'Text/Statistics.php';
  43. *  $block = Text_Statistics($sometext);
  44. *  $block->flesch; // returns flesch score for $sometext
  45. *
  46. *  see the unit tests for additional examples.
  47. *
  48. *  @package Text_Statistics
  49. *  @author  George Schlossnagle <george@omniti.com>
  50. */
  51. {
  52.     /**
  53.      * The document text.
  54.      *
  55.      * @access public
  56.      * @var string 
  57.      */
  58.     var $text = '';
  59.  
  60.     /**
  61.      * The number of syllables in the document.
  62.      *
  63.      * @var int 
  64.      * @access public
  65.      */
  66.     var $numSyllables = 0;
  67.  
  68.     /**
  69.      * The number of words in the document.
  70.      *
  71.      * @var int 
  72.      * @access public
  73.      */
  74.     var $numWords = 0;
  75.  
  76.     /**
  77.      * The number of unique words in the document.
  78.      *
  79.      * @var    int 
  80.      * @access public
  81.      */
  82.     var $uniqWords = 0;
  83.  
  84.     /**
  85.      * The number of sentences in the document.
  86.      *
  87.      * @var    int 
  88.      * @access public
  89.      */
  90.     var $numSentences = 0;
  91.  
  92.     /**
  93.      * The Flesch score of the document.
  94.      * It is FALSE if there were no words in the document.
  95.      *
  96.      * @var    float 
  97.      * @access public
  98.      */
  99.     var $flesch = 0;
  100.  
  101.     /**
  102.      * Flesch-Kincaid grade level
  103.      * It is FALSE if there were no words in the document.
  104.      *
  105.      * @var    float 
  106.      * @access public
  107.      */
  108.     var $gradeLevel = 0;
  109.  
  110.     /**
  111.      * Some abbreviations we should expand.  This list could/should
  112.      * be much larger.
  113.      *
  114.      * @var    array 
  115.      * @access protected
  116.      */
  117.     var $_abbreviations = array('/Mr\./'   => 'Mister',
  118.                                 '/Mrs\./i' => 'Misses'// Phonetic
  119.                                 '/etc\./i' => 'etcetera',
  120.                                 '/Dr\./i'  => 'Doctor',
  121.                                 '/Jr\./i' => 'Junior',
  122.                                 '/Sr\./i' => 'Senior',
  123.                                );
  124.  
  125.     /**
  126.      * List of all words that have been found already.
  127.      * @var array 
  128.      */
  129.     var $_uniques = array();
  130.  
  131.  
  132.  
  133.     /**
  134.      * Constructor.
  135.      *
  136.      * @param  string 
  137.      * @access public
  138.      */
  139.     function Text_Statistics($block)
  140.     {
  141.         $this->text = trim($block);
  142.         $this->_analyze();
  143.         $this->text = null;
  144.     }
  145.  
  146.  
  147.  
  148.     /**
  149.      * Returns the character frequencies.
  150.      *
  151.      * @return array of frequencies, where the index is the ASCII byte char value
  152.      * @access public
  153.      * @author Jesus M. Castagnetto <jmcastagnetto@php.net>
  154.      */
  155.     function getCharFreq({
  156.         return $this->_charFreq;
  157.     }
  158.  
  159.  
  160.  
  161.     /**
  162.      * Returns the number of paragaphs.
  163.      * Paragraphs are defined as chunks of text separated by
  164.      * and empty line.
  165.      *
  166.      * @return long 
  167.      * @access public
  168.      * @author Jesus M. Castagnetto <jmcastagnetto@php.net>
  169.      */
  170.     function getNumParagraphs({
  171.         return $this->_numParas;
  172.     }
  173.  
  174.  
  175.  
  176.     /**
  177.      * Compute statistics for the document object.
  178.      *
  179.      * @access protected
  180.      */
  181.     function _analyze()
  182.     {
  183.         // char frequencies
  184.         $this->_charFreq count_chars($this->text);
  185.         $this->text = preg_replace(array_keys($this->_abbreviations),
  186.                       array_values($this->_abbreviations),
  187.                       $this->text);
  188.         preg_match_all('/[.!?](\s|$)/'$this->text$matches);
  189.         $this->numSentences = count($matches[0]);
  190.         $lines              explode("\n"$this->text);
  191.         // Set ourselves as 'out of text block to start
  192.         $intext = 0;
  193.         foreach$lines as $line {
  194.             // A paragraph is when we enter a text line
  195.             // after a line that was all whitespace
  196.             if(preg_match("/\S/"$line)) {
  197.                 if($intext == 0{
  198.                     $this->_numParas++;
  199.                     $intext = 1;
  200.                 }
  201.             }
  202.             else {
  203.                 $intext = 0;
  204.             }
  205.             $this->_analyze_line($line);
  206.         }
  207.  
  208.         if ($this->numSentences == 0{
  209.             $this->numSentences = 1;
  210.         }
  211.         if ($this->numWords == 0{
  212.             $this->flesch       = false;
  213.             $this->gradeLevel   = false;
  214.             return;
  215.         }
  216.  
  217.         $wordsPerSent     $this->numWords / $this->numSentences;
  218.         $syllablesPerWord $this->numSyllables / $this->numWords;
  219.  
  220.         $this->flesch = 206.835
  221.                         - 1.015 * $wordsPerSent
  222.                         - 84.6 * $syllablesPerWord;
  223.         $this->gradeLevel = 0.39 * $wordsPerSent
  224.                         + 11.8 * $syllablesPerWord
  225.                         - 15.59;
  226.     }
  227.  
  228.  
  229.  
  230.     /**
  231.      * Helper function, computes statistics on a given line.
  232.      *
  233.      * @param  string 
  234.      * @access protected
  235.      */
  236.     function _analyze_line($line)
  237.     {
  238.         // expand abbreviations for counting syllables
  239.         preg_match_all("/\b(\w[\w'-]*)\b/"$line$words);
  240.         foreach$words[1as $word {
  241.             $w_obj = new Text_Word($word);
  242.             $this->numSyllables += $w_obj->numSyllables();
  243.             $this->numWords++;
  244.             if(!isset($this->_uniques[$word])) {
  245.                 $this->_uniques[$word= 1;
  246.             else {
  247.                $this->uniqWords++;
  248.             }
  249.         }
  250.     }
  251. }
  252. ?>

Documentation generated on Mon, 11 Mar 2019 15:36:08 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.