cvs: pear /Text_Statistics/Text Statistics.php Word.php /Text_Statistics/tests Statistics.phpt Word.phpt

From: Date: Sun, 02 Feb 2003 00:17:09 +0000
Subject: cvs: pear /Text_Statistics/Text Statistics.php Word.php /Text_Statistics/tests Statistics.phpt Word.phpt
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-8667@lists.php.net to get a copy of this message
gschlossnagle Sat Feb 1 19:17:09 2003 EDT Added files: /pear/Text_Statistics/Text Statistics.php Word.php /pear/Text_Statistics/tests Statistics.phpt Word.phpt Log: initial import

Index: pear/Text_Statistics/Text/Statistics.php +++ pear/Text_Statistics/Text/Statistics.php <?php /* vim: set expandtab tabstop=4 softtabstop=4 shiftwidth=4: */ // +----------------------------------------------------------------------+ // | PHP version 4ÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊ| // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2002 The PHP GroupÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊ| // +----------------------------------------------------------------------+ // | This source file is subject to version 2.0 of the PHP license,ÊÊÊÊÊÊÊ| // | that is bundled with this package in the file LICENSE, and isÊÊÊÊÊÊÊÊ| // | available at through the world-wide-web atÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊ| // | http://www.php.net/license/2_02.txt.ÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊ| // | If you did not receive a copy of the PHP license and are unable toÊÊÊ| // | obtain it through the world-wide-web, please send a note toÊÊÊÊÊÊÊÊÊÊ| // | license@php.net so we can mail you a copy immediately.ÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊ| // +----------------------------------------------------------------------+ // | Author: George Schlossnagle <george@omniti.com> | // +----------------------------------------------------------------------+ // // $Id: Statistics.php,v 1.1 2003/02/02 00:17:09 gschlossnagle Exp $ /* * Text_Statistics calculates some basic readability metrics on a * block of text. The number of words, the number of sentences, * and the number of total syllables is counted. These statistics * can be used to calculate the Flesch score for a sentence, which * is a number (usually between 0 and 100) that represents the * readability of the text. A basic breakdown of scores is: * * 90 to 100 5th grade * 80 to 90 6th grade * 70 to 80 7th grade * 60 to 70 8th and 9th grade * 50 to 60 10th to 12th grade (high school) * 30 to 50 college * 0 to 30 college graduate * * More info can be read up on at * http://www.mang.canterbury.ac.nz/courseinfo/AcademicWriting/Flesch.htm * * require 'Text/Statistics.php'; * $block = Text_Statistics($sometext); * $block->flesch; // returns flesch score for $sometext * * see the unit tests for additional examples. * * @package Text_Statistics * @author George Schlossnagle <george@omniti.com> */ require_once "Text/Word.php"; class Text_Statistics { /* * The document text. * * @var string * @access public */ var $text = ''; /* * The number of syllables in the document. * * @var number * @access public */ var $numSyllables = 0; /* * The number of words in the document. * * @var number * @access public */ var $numWords = 0; /* * The number of unique words in the document. * * @var number * @access public */ var $uniqWords = 0; /* * The number of sentences in the document. * * @var number * @access public */ var $numSentences = 0; /* * The Flesch score of the document. * * @var number * @access public */ var $flesch = 0; /* * Some abbreviations we should expand. THis list could/should * be much larger. * * @var number * @access protected */ var $_abbreviations = array('/Mr\./' => 'Misterr', '/Mrs\./i' => 'Misses', // Phonetic '/etc\./i' => 'etcetera', '/Dr\./i' => 'Doctor', ); /* * Constructor. * * @param string * @access public */ function Text_Statistics($block) { $this->text = $block; $this->_analyze(); } /* * Compute statistics for the document object. * * @access protected */ function _analyze() { $lines = explode("\n", $this->text); foreach( $lines as $line ) { $this->_analyze_line($line); } $this->flesch = 206.835 - (1.015 * ($this->numWords/$this->numSentences)) - (84.6 * ($this->numSyllables/$this->numWords)); } /* * Helper function, computes statistics on a given line. * * @param string * @access protected */ function _analyze_line($line) { // expand abbreviations for counting syllables $line = preg_replace(array_keys($this->_abbreviations), array_values($this->_abbreviations), $line); preg_match_all("/\b(\w[\w'-]*)\b/", $line, $words); foreach( $words[1] as $word ) { $w_obj = new Text_Word($word); $this->numSyllables += $w_obj->numSyllables(); $this->numWords++; if($this->_uniques[strtolower($word)]++ == 0) { $this->uniqWords++; } } preg_match_all("/[.!?]/", $line, $matches); $this->numSentences += count($matches[0]); } } ?> Index: pear/Text_Statistics/Text/Word.php +++ pear/Text_Statistics/Text/Word.php <?php /* vim: set expandtab tabstop=4 softtabstop=4 shiftwidth=4: */ // +----------------------------------------------------------------------+ // | PHP version 4ÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊ| // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2002 The PHP GroupÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊ| // +----------------------------------------------------------------------+ // | This source file is subject to version 2.0 of the PHP license,ÊÊÊÊÊÊÊ| // | that is bundled with this package in the file LICENSE, and isÊÊÊÊÊÊÊÊ| // | available at through the world-wide-web atÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊ| // | http://www.php.net/license/2_02.txt.ÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊ| // | If you did not receive a copy of the PHP license and are unable toÊÊÊ| // | obtain it through the world-wide-web, please send a note toÊÊÊÊÊÊÊÊÊÊ| // | license@php.net so we can mail you a copy immediately.ÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊ| // +----------------------------------------------------------------------+ // | Author: George Schlossnagle <george@omniti.com> | // +----------------------------------------------------------------------+ // // $Id: Word.php,v 1.1 2003/02/02 00:17:09 gschlossnagle Exp $ /* * Text_Word calculates the number of syllables in a word, based off of * the number of contiguous vowel groupings in the word and applying * matches to detect special cases. * require_once 'Text/Word.php' * $word = new Text_Word('word'); * $word->numSyllables(); // returns 1 * * @package Text_Word * @author George Schlossnagle <george@omniti.com> */ class Text_Word { /* The word * * @var string * @access public */ var $word; /* The number of syllables. This is internal, the value should be * accessed through the accessor. * * @var number * @access protected */ var $_numSyllables = 0; /* The special cases of fragments which detect as 1 but should be 2 * syllables. * * @var array * @access static protected */ var $doubleSyllables = array('/\wlien/', // alien but not lien '/bl$/', // syllable '/io/', // biography ); /* The special cases of fragments which detect as 2 but should be 1 * syllables. * * @var array * @access static protected */ var $silentSyllables = array('/\wely$/', // absolutely but not ely '/\wion/', '/iou/', ); /* * Constructs a word by name. * * @param string * @access public */ function Text_Word($name = '') { $this->word = $name; } /* * Helper function, canocalizes the word. * * @param string * @access protected */ function _mungeWord($scratch) { // conanonicalize the word $scratch = strtolower($scratch); // trailings e's are almost always silent in English // so remove them $scratch = preg_replace("/e$/", "", $scratch); return $scratch; } /* * Helper function, counts syllable exceptions * * @param string * @access protected */ function _countSpecialSyllables($scratch) { $mod = 0; // decrement our syllable count for words that contain // 'silent' syllables, e.g. ely in 'abosultely' foreach( $this->silentSyllables as $pat ) { if(preg_match($pat, $scratch)) { $mod--; } } // increment syllable count for certain conjoined vowel // patterns which produce two syllables e.g. // 'io' in 'biology' foreach( $this->doubleSyllables as $pat ) { if(preg_match($pat, $scratch)) { $mod++; } } return $mod; } /* * Returns the number of syllables. Caches the value in the object. * * @access public */ function numSyllables() { // cache the value in this object if($this->_numSyllables) { return $this->_numSyllables; } $scratch = $this->_mungeWord($this->word); // Split the word on the vowels. a e i o u, and for us always y $fragments = preg_split("/[^aeiouy]+/", $scratch); // remove null elements at the head and tail of // $fragments if(!$fragments[0]) { array_shift($fragments); } if(!$fragments[count($fragments) - 1]) { array_pop($fragments); } // modify our syllable count for special cases $this->_numSyllables += $this->_countSpecialSyllables($scratch); // now count our syllable if(count($fragments)) { $this->_numSyllables += count($fragments); } else { $this->_numSyllables = 1; } return $this->_numSyllables; } } ?> Index: pear/Text_Statistics/tests/Statistics.phpt +++ pear/Text_Statistics/tests/Statistics.phpt <?php /* vim: set expandtab tabstop=4 softtabstop=4 shiftwidth=4: */ // +----------------------------------------------------------------------+ // | PHP version 4ÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊ| // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2002 The PHP GroupÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊ| // +----------------------------------------------------------------------+ // | This source file is subject to version 2.0 of the PHP license,ÊÊÊÊÊÊÊ| // | that is bundled with this package in the file LICENSE, and isÊÊÊÊÊÊÊÊ| // | available at through the world-wide-web atÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊ| // | http://www.php.net/license/2_02.txt.ÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊ| // | If you did not receive a copy of the PHP license and are unable toÊÊÊ| // | obtain it through the world-wide-web, please send a note toÊÊÊÊÊÊÊÊÊÊ| // | license@php.net so we can mail you a copy immediately.ÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊ| // +----------------------------------------------------------------------+ // | Author: George Schlossnagle <george@omniti.com> | // +----------------------------------------------------------------------+ // // $Id: Statistics.phpt,v 1.1 2003/02/02 00:17:09 gschlossnagle Exp $ require_once "PHPUnit.php"; require_once "Text/Statistics.php"; class TextTestCase extends PHPUnit_TestCase { var $sample; var $sampleAbbr; var $object; function setUp() { $this->sample = " Returns the number of words in the analysed text file or block. A word must consist of letters a-z with at least one vowel sound, and optionally an apostrophe or hyphen."; $this->object = new Text_Statistics($this->sample); $this->sampleAbbr = " Dear Mr. Schlossnagle, Your request for a leave of absense has been approved. Enjoy your vacation. "; } function TextTestCase($name) { $this->PHPUnit_TestCase($name); } function testNumSentences() { $this->assertEquals(2, $this->object->numSentences); } function testNumWords() { $this->assertEquals(31, $this->object->numWords); } function testNumSyllables() { $this->assertEquals(45, $this->object->numSyllables); } function testNumSentencesAbbr() { $this->assertEquals(2, $this->object->numSentences); } } $suite = new PHPUnit_TestSuite('TextTestCase'); $result = PHPUnit::run($suite); echo $result->toString(); ?> Index: pear/Text_Statistics/tests/Word.phpt +++ pear/Text_Statistics/tests/Word.phpt <?php /* vim: set expandtab tabstop=4 softtabstop=4 shiftwidth=4: */ // +----------------------------------------------------------------------+ // | PHP version 4ÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊ| // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2002 The PHP GroupÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊ| // +----------------------------------------------------------------------+ // | This source file is subject to version 2.0 of the PHP license,ÊÊÊÊÊÊÊ| // | that is bundled with this package in the file LICENSE, and isÊÊÊÊÊÊÊÊ| // | available at through the world-wide-web atÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊ| // | http://www.php.net/license/2_02.txt.ÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊ| // | If you did not receive a copy of the PHP license and are unable toÊÊÊ| // | obtain it through the world-wide-web, please send a note toÊÊÊÊÊÊÊÊÊÊ| // | license@php.net so we can mail you a copy immediately.ÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊ| // +----------------------------------------------------------------------+ // | Author: George Schlossnagle <george@omniti.com> | // +----------------------------------------------------------------------+ // // $Id: Word.phpt,v 1.1 2003/02/02 00:17:09 gschlossnagle Exp $ require_once "PHPUnit.php"; require_once "Text/Word.php"; class Text_WordTestCase extends PHPUnit_TestCase { var $known_words = array ( 'the' => 1, 'late' => '1', 'hello' => '2', 'frantic' => '2', 'programmer' => '3'); var $special_words = array ( 'absolutely' => 4, 'alien' => 3, 'ion' => 2, 'tortion' => 2, 'gracious' => 2, 'lien' => 1, 'syllable' => 3); function Text_WordTestCase($name) { $this->PHPUnit_TestCase($name); } function testKnownWords() { foreach ($this->known_words as $word => $syllables) { $obj = new Text_Word($word); $this->assertEquals($syllables, $obj->numSyllables(), "$word has incorrect syllable count"); } } function testSpecialWords() { foreach ($this->special_words as $word => $syllables) { $obj = new Text_Word($word); $this->assertEquals($syllables, $obj->numSyllables(), "$word has incorrect syllable count"); } } } if(realpath($_SERVER[PHP_SELF]) == __FILE__) { $suite = new PHPUnit_TestSuite('Text_WordTestCase'); $result = PHPUnit::run($suite); echo $result->toString(); } ?>
« previous php.pear.cvs (#8667) next »