Source for file Words.php
Documentation is available at Words.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_Analyzer_Words is an sentence analyzer. It compares words with defined sources.
* @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
* @var array The word array to analyze.
* @var array Container classes
* @var array Contains an array with data about the words.
* @var string Contains the punctuation mark.
* @var array An array with different input sources.
* @var int The type of storage (class or array)
* Check if Mai has been setup and that the creator is Mai_Analyzer.
public function __construct($creator, $type = Mai::STORAGE_TYPE_ARRAY)
trigger_error('Mai has not been setup yet. Please use Mai::init().', E_USER_ERROR);
trigger_error('Please use an instance of Mai_Analyzer to create an instance of Mai_Analyzer_Words.');
if($type === Mai::STORAGE_TYPE_CLASS)
* Analyze the word array (or containers) and save locally.
//wordArray must be valid
throw new UnexpectedValueException('No words to analyze!');
//Clear data from previous runs
//Loop through every word and every source
foreach($this->_sources as $name => $data)
foreach($data as $dataName => $dataValue)
if($word === $dataValue['word'])
//Word has been found, save data.
Mai_Log::info('Word \''. $word. '\' found from source '. $name);
//Break from sources loop
//If the word is not found, the data will be empty
//Loop through every word
foreach($this->_sources as $name => $data)
foreach($data as $dataName => $dataValue)
if($word->getWord() === $dataValue['word'])
//Word has been found, save data.
$word->setType($dataValue['wordtype']);
//@Mai Todo: Uncomment ww_id
//$word->setWwId($dataValue['ww_id']);
//@Mai Todo: Replaced by person?
//$word->setTarget($dataValue['target']);
$word->setTime($dataValue['time']);
$word->setNumber($dataValue['number']);
$word->setPerson($dataValue['person']);
Mai_Log::info('Word \''. $word->getWord(). '\' found from source '. $name);
//Break from sources loop
* Set the word array to analyze. This function uses references.
* Original data will be changed by this analyzer.
public function set(array $data)
if($this->_storageType === Mai::STORAGE_TYPE_ARRAY)
elseif($this->_storageType === Mai::STORAGE_TYPE_CLASS)
$this->_wordContainers = &$data;
* Add a word source to the analyzer.
public function addSource($name, array $data)
throw new UnexpectedValueException('Name of source has not been set or is invalid!');
//Sources can be empty, just log it and continue
Mai_Log::info('Mai_Analyzer_Words: Source with name '. $name. ' is empty.');
//Test an element of the array (random)
if(!isset ($test['word']))
throw new UnexpectedValueException('Source with name '. $name. ' is not valid. Each element should have a word assigned.');
trigger_error('Source '. $name. ' does not excist.', E_USER_NOTICE);
* Return data from the analyzed words.
* This class also uses references, using this function is not required.
|