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

Source for file Word.php

Documentation is available at Word.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: Word.php 129208 2003-05-29 16:22:44Z gschlossnagle $
  20.  
  21. /**
  22.  * Text_Word calculates the number of syllables in a word, based off of
  23.  * the number of contiguous vowel groupings in the word and applying
  24.  * matches to detect special cases.
  25.  
  26.  * require_once 'Text/Word.php'
  27.  * $word = new Text_Word('word');
  28.  * $word->numSyllables();  // returns 1
  29.  *
  30.  * @package Text_Statistics
  31.  * @author  George Schlossnagle <george@omniti.com>
  32.  */
  33. class Text_Word
  34. {
  35.     /**
  36.      * The word
  37.      *
  38.      * @var    string 
  39.      * @access public
  40.      */
  41.     var $word;
  42.  
  43.     /**
  44.      * The number of syllables.  This is internal, the value should be
  45.      * accessed through the accessor.
  46.      *
  47.      * @var    number 
  48.      * @access protected
  49.      */
  50.     var $_numSyllables = 0;
  51.  
  52.     /**
  53.      * The special cases of fragments which detect as 1 but should be 2
  54.      * syllables.
  55.      *
  56.      * @var    array 
  57.      * @access static protected
  58.      */
  59.     var $doubleSyllables = array('/\wlien/'// alien but not lien
  60.                                  '/bl$/',   // syllable
  61.                                  '/io/',    // biography
  62.                                  );
  63.  
  64.     /**
  65.      * The special cases of fragments which detect as 2 but should be 1
  66.      * syllables.
  67.      *
  68.      * @var    array 
  69.      * @access static protected
  70.      */
  71.     var $silentSyllables = array('/\wely$/',    // absolutely but not ely
  72.                                  '/\wion/',
  73.                                  '/iou/',
  74.                                 );
  75.     
  76.     /**
  77.      * Constructs a word by name.
  78.      *
  79.      * @param  string 
  80.      * @access public
  81.      */
  82.     function Text_Word($name ''
  83.     {
  84.         $this->word = $name;
  85.     }
  86.  
  87.     /**
  88.      * Helper function, canocalizes the word.
  89.      *
  90.      * @param  string 
  91.      * @access protected
  92.      */
  93.     function _mungeWord($scratch
  94.     {
  95.         // conanonicalize the word
  96.         $scratch strtolower($scratch);
  97.         // trailings e's are almost always silent in English
  98.         // so remove them
  99.         $scratch preg_replace("/e$/"""$scratch);
  100.         return $scratch;
  101.     }
  102.  
  103.     /**
  104.      * Helper function, counts syllable exceptions
  105.      *
  106.      * @param  string 
  107.      * @access protected
  108.      */
  109.     function _countSpecialSyllables($scratch
  110.     {
  111.         $mod = 0;
  112.         // decrement our syllable count for words that contain
  113.         // 'silent' syllables, e.g. ely in 'abosultely'
  114.         foreach$this->silentSyllables as $pat {
  115.             if(preg_match($pat$scratch)) {
  116.                 $mod--;
  117.             }
  118.         }
  119.         // increment syllable count for certain conjoined vowel
  120.         // patterns which produce two syllables e.g.
  121.         // 'io' in 'biology'
  122.         foreach$this->doubleSyllables as $pat {
  123.             if(preg_match($pat$scratch)) {
  124.                 $mod++;
  125.             }
  126.         }
  127.         return $mod;
  128.     }
  129.  
  130.     /**
  131.      * Returns the number of syllables.  Caches the value in the object.
  132.      *
  133.      * @access public
  134.      */
  135.     function numSyllables(
  136.     {
  137.         // cache the value in this object
  138.         if($this->_numSyllables{
  139.             return $this->_numSyllables;
  140.         }
  141.         $scratch $this->_mungeWord($this->word);
  142.         // Split the word on the vowels.  a e i o u, and for us always y
  143.         $fragments preg_split("/[^aeiouy]+/"$scratch);
  144.  
  145.         // remove null elements at the head and tail of 
  146.         // $fragments
  147.         if(!$fragments[0]{
  148.             array_shift($fragments);
  149.         }
  150.         if($fragments && !$fragments[count($fragments- 1]{
  151.             array_pop($fragments);
  152.         }
  153.  
  154.         // modify our syllable count for special cases
  155.         $this->_numSyllables += $this->_countSpecialSyllables($scratch);
  156.         // now count our syllable
  157.         if(count($fragments)) {
  158.             $this->_numSyllables += count($fragments);
  159.         }
  160.         else {
  161.             $this->_numSyllables = 1;
  162.         }
  163.         return $this->_numSyllables;
  164.     }
  165. }
  166. ?>

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