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

Source for file Splitter.php

Documentation is available at Splitter.php

  1. <?php
  2. /**
  3.  * Mai_Word_Splitter
  4.  * 
  5.  * Copyright (C) 2009  Mai (Me Artificial Intelligence)
  6.  *
  7.  * This program is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License as published by
  9.  * the Free Software Foundation, either version 3 of the License, or
  10.  * (at your option) any later version.
  11.  *
  12.  * This program is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.  * GNU General Public License for more details.
  16.  *
  17.  * You should have received a copy of the GNU General Public License
  18.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  19.  * 
  20.  * @author Wouter Bulten (wouterbulten@mai-ai.org)
  21.  * @copyright Copyright (C) 2009  Mai (Me Artificial Intelligence)
  22.  * @license http://opensource.org/licenses/gpl-3.0.html GNU General Public License version 3 (GPLv3)
  23.  * @link http://www.launchpad.net/mai
  24.  * 
  25.  * @category Mai
  26.  * @package Word
  27.  * @subpackage Splitter
  28.  */
  29.  
  30. /**
  31.  * Mai_Word_Splitter splits a string in to smaller pieces.
  32.  * 
  33.  * @author Wouter Bulten (wouterbulten@mai-ai.org)
  34.  * @copyright Copyright (C) 2009  Mai (Me Artificial Intelligence)
  35.  * @license http://opensource.org/licenses/gpl-3.0.html GNU General Public License version 3 (GPLv3)
  36.  * @link http://www.launchpad.net/mai
  37.  * 
  38.  * @category Mai
  39.  * @package Word
  40.  * @subpackage Splitter
  41.  *  
  42.  */
  43.  
  44. class Mai_Word_Splitter implements IteratorAggregate
  45. {
  46.     /**
  47.      * The string to split.
  48.      * @var string 
  49.      */
  50.     protected $_wordsString;
  51.     
  52.     /**
  53.      * Contains an array with words, sorted by sentence.
  54.      * @var array 
  55.      */
  56.     protected $_wordArray = array();
  57.     
  58.     /**
  59.      * @var array An array with container classes. Used when saving to classes.
  60.      */
  61.     protected $_containerArray = array();
  62.     
  63.     /**
  64.      * Contains the punctuation marks, sorted by sentence
  65.      * @var array 
  66.      */
  67.     protected $_sentenceMarks = array();
  68.     
  69.     /**
  70.      * Contains the number of sentences.
  71.      * @var integer 
  72.      */
  73.     protected $_sentences = 0;
  74.     
  75.     /**
  76.      * Punctuation marks that mark a new sentence.
  77.      * @var array 
  78.      */
  79.     protected $_newSentenceMarks = array('!''.'',''?');
  80.     
  81.     /**
  82.      * Type of storage (whether to use classes or not)
  83.      * @var int 
  84.      */
  85.     protected $_type = Mai::STORAGE_TYPE_ARRAY;
  86.  
  87.     /**
  88.      * Constructor checks if Mai has been set up.
  89.      */
  90.     public function __construct($type Mai::STORAGE_TYPE_ARRAY)
  91.     {
  92.         if(!Mai::isInit())
  93.         {
  94.             trigger_error('Mai has not been setup yet. Please use Mai::init().'E_USER_ERROR);
  95.         }
  96.         
  97.         //Store the type
  98.         if($type === Mai::STORAGE_TYPE_CLASS)
  99.         {
  100.             $this->_type = Mai::STORAGE_TYPE_CLASS;
  101.         }
  102.     }
  103.  
  104.     public function setString($string)
  105.     {
  106.         if(!isset($string|| empty($string))
  107.         {
  108.             throw new InvalidArgumentException('String is not valid.');
  109.         }
  110.         
  111.         $this->_wordsString = $string;
  112.     }
  113.     
  114.     /**
  115.      * Runs the splitting process.
  116.      * @param Int $type Type of storage
  117.      * @return void 
  118.      */
  119.     public function run()
  120.     {
  121.         if(!isset($this->_wordsString|| empty($this->_wordsString))
  122.         {
  123.             trigger_error('String is not valid.'E_USER_ERROR);
  124.         }
  125.         
  126.         //Split the string
  127.         $explodedString explode(' '$this->_wordsString);
  128.         //Amount of sentences
  129.         $sentences 0;
  130.         //Contains the words, sorted by sentence
  131.         $wordArray array();
  132.         //The punctuation marks, sorted by sentence
  133.         $sentenceMarks array();
  134.         
  135.         foreach($explodedString as $key => $value)
  136.         {
  137.             //Look for the start of a new sentence
  138.             if(in_array($value{(strlen($value1)}$this->_newSentenceMarks))
  139.             {
  140.                 //Store the mark
  141.                 $sentenceMarks[$sentences$value{(strlen($value1)};
  142.                 //Remove it from the word
  143.                 $value substr($value0-1);
  144.                 //Store the value
  145.                 $wordArray[$sentences][$value;
  146.                 //Increase the amount (let new words start in a new sentence)
  147.                 $sentences++;
  148.                 
  149.             }
  150.             else
  151.             {
  152.                 //Store the value
  153.                 $wordArray[$sentences][$value;
  154.  
  155.                 //The last sentence could not have a sentence mark.
  156.                 //Then we have to increase the sentences counter.
  157.                 $lastWordKey count($explodedString1;
  158.                 //Only do this when we have reached the last word
  159.                 if($key == $lastWordKey)
  160.                 {
  161.                     $sentences++;
  162.                 }
  163.             }            
  164.         }
  165.         
  166.         //Store in local variables
  167.         $this->_wordArray = $wordArray;
  168.         $this->_sentenceMarks = $sentenceMarks;
  169.         $this->_sentences = $sentences;
  170.         
  171.         //Words can also be stored in containers.
  172.         if($this->_type === Mai::STORAGE_TYPE_CLASS)
  173.         {
  174.             foreach($this->_wordArray as $key => $sentence)
  175.             {
  176.                 $sentenceContainer Mai_Container::newContainerSentence();
  177.                 
  178.                 foreach($sentence as $wordKey => $word)
  179.                 {
  180.                     //Create a new instance of Mai_Container_Word and store.
  181.                      $sentenceContainer->insertWord(Mai_Container::newContainerWord($word));
  182.                 }
  183.                 
  184.                 //Store sentence mark
  185.                 if(isset($this->_sentenceMarks[$key]))
  186.                 {
  187.                     $sentenceContainer->setSentenceMark($this->_sentenceMarks[$key]);
  188.                 }
  189.                 
  190.                 //Store container local
  191.                 $this->_containerArray[$key$sentenceContainer;
  192.             }
  193.         }
  194.     }
  195.     
  196.     /**
  197.      * Return the word array. Is empty if called before run().
  198.      * @return array 
  199.      */
  200.     public function returnWordArray()
  201.     {
  202.         return $this->_wordArray;
  203.     }
  204.     
  205.     /**
  206.      * Return the container array. Is empty if called before run().
  207.      * @return array 
  208.      */
  209.     public function returnContainerArray()
  210.     {
  211.         return $this->_containerArray;
  212.     }
  213.     
  214.     /**
  215.      * Return the sentence marks array. Is empty if called before run().
  216.      * @return array 
  217.      */
  218.     public function returnSentenceMarks()
  219.     {
  220.         return $this->_sentenceMarks;
  221.     }
  222.     
  223.     /**
  224.      * Return the amount of sentences.
  225.      * @return integer 
  226.      */
  227.     public function returnSentencesAmount()
  228.     {
  229.         return $this->_sentences;
  230.     }
  231.     
  232.     /**
  233.      * Function to implement the IteratorAggregate interface.
  234.      * @return ArrayIterator 
  235.      */
  236.     public function getIterator()
  237.     {
  238.         if($this->_type === Mai::STORAGE_TYPE_CLASS)
  239.         {
  240.             return new ArrayIterator($this->_containerArray);
  241.         }
  242.         
  243.         return new ArrayIterator($this->_wordArray);
  244.     }
  245. }

Documentation generated on Mon, 27 Jul 2009 19:55:13 +0200 by phpDocumentor 1.4.1