Source for file Splitter.php
Documentation is available at Splitter.php
* Copyright (C) 2009 Mai (Me Artificial Intelligence)
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* @author Wouter Bulten (wouterbulten@mai-ai.org)
* @copyright Copyright (C) 2009 Mai (Me Artificial Intelligence)
* @license http://opensource.org/licenses/gpl-3.0.html GNU General Public License version 3 (GPLv3)
* @link http://www.launchpad.net/mai
* Mai_Word_Splitter splits a string in to smaller pieces.
* @author Wouter Bulten (wouterbulten@mai-ai.org)
* @copyright Copyright (C) 2009 Mai (Me Artificial Intelligence)
* @license http://opensource.org/licenses/gpl-3.0.html GNU General Public License version 3 (GPLv3)
* @link http://www.launchpad.net/mai
* Contains an array with words, sorted by sentence.
* @var array An array with container classes. Used when saving to classes.
* Contains the punctuation marks, sorted by sentence
* Contains the number of sentences.
* Punctuation marks that mark a new sentence.
* Type of storage (whether to use classes or not)
protected $_type = Mai::STORAGE_TYPE_ARRAY;
* Constructor checks if Mai has been set up.
public function __construct($type = Mai::STORAGE_TYPE_ARRAY)
trigger_error('Mai has not been setup yet. Please use Mai::init().', E_USER_ERROR);
if($type === Mai::STORAGE_TYPE_CLASS)
if(!isset ($string) || empty($string))
throw new InvalidArgumentException('String is not valid.');
* Runs the splitting process.
* @param Int $type Type of storage
//Contains the words, sorted by sentence
//The punctuation marks, sorted by sentence
$sentenceMarks = array();
foreach($explodedString as $key => $value)
//Look for the start of a new sentence
$sentenceMarks[$sentences] = $value{(strlen($value) - 1)};
//Remove it from the word
$value = substr($value, 0, - 1);
$wordArray[$sentences][] = $value;
//Increase the amount (let new words start in a new sentence)
$wordArray[$sentences][] = $value;
//The last sentence could not have a sentence mark.
//Then we have to increase the sentences counter.
$lastWordKey = count($explodedString) - 1;
//Only do this when we have reached the last word
//Store in local variables
//Words can also be stored in containers.
if($this->_type === Mai::STORAGE_TYPE_CLASS)
foreach($sentence as $wordKey => $word)
//Create a new instance of Mai_Container_Word and store.
* Return the word array. Is empty if called before run().
* Return the container array. Is empty if called before run().
* Return the sentence marks array. Is empty if called before run().
* Return the amount of sentences.
* Function to implement the IteratorAggregate interface.
if($this->_type === Mai::STORAGE_TYPE_CLASS)
|