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

Source for file Words.php

Documentation is available at Words.php

  1. <?php
  2. /**
  3.  * Mai_Analyzer_Words
  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 Analyzer
  27.  * @subpackage Words
  28.  * 
  29.  */
  30.  
  31. /**
  32.  * Mai_Analyzer_Words is an sentence analyzer. It compares words with defined sources.
  33.  * 
  34.  * @author Wouter Bulten (wouterbulten@mai-ai.org)
  35.  * @copyright Copyright (C) 2009  Mai (Me Artificial Intelligence)
  36.  * @license http://opensource.org/licenses/gpl-3.0.html GNU General Public License version 3 (GPLv3)
  37.  * @link http://www.launchpad.net/mai
  38.  * 
  39.  * @category Mai
  40.  * @package Analyzer
  41.  * @subpackage Words
  42.  */
  43.  
  44. class Mai_Analyzer_Words implements Mai_Analyzer_Interface
  45. {
  46.     /**
  47.      * @var array The word array to analyze.
  48.      */
  49.     private $_wordArray = array();
  50.     
  51.     /**
  52.      * @var array Container classes
  53.      */
  54.     private $_wordContainers = array();
  55.  
  56.     /**
  57.      * @var array Contains an array with data about the words.
  58.      */
  59.     private $_wordArrayData = array();
  60.  
  61.     /**
  62.      * @var string Contains the punctuation mark.
  63.      */
  64.     private $_sentenceMark;
  65.  
  66.     /**
  67.      * @var array An array with different input sources.
  68.      */
  69.     private $_sources = array();
  70.     
  71.     /**
  72.      * @var int The type of storage (class or array)
  73.      */
  74.     private $_storageType = Mai::STORAGE_TYPE_ARRAY;
  75.  
  76.     /**
  77.      * Check if Mai has been setup and that the creator is Mai_Analyzer.
  78.      */
  79.     public function __construct($creator$type Mai::STORAGE_TYPE_ARRAY)
  80.     {
  81.         if(!Mai::isInit())
  82.         {
  83.             trigger_error('Mai has not been setup yet. Please use Mai::init().'E_USER_ERROR);
  84.         }
  85.         
  86.         if(!($creator instanceof Mai_Analyzer&& !Mai_Config::getVar()->analyzer->allow_direct_access)
  87.         {
  88.             trigger_error('Please use an instance of Mai_Analyzer to create an instance of Mai_Analyzer_Words.');
  89.         }
  90.         
  91.         if($type === Mai::STORAGE_TYPE_CLASS)
  92.         {
  93.             $this->_storageType = Mai::STORAGE_TYPE_CLASS;
  94.         }
  95.        }
  96.  
  97.     /**
  98.      * Analyze the word array (or containers) and save locally.
  99.      */
  100.     public function run()
  101.     {
  102.         //wordArray must be valid
  103.         if(empty($this->_wordArray&& empty($this->_wordContainers))
  104.         {
  105.             throw new UnexpectedValueException('No words to analyze!');
  106.         }
  107.         
  108.         if(empty($this->_sources))
  109.         {
  110.             trigger_error('No sources set!'E_USER_NOTICE);
  111.         }
  112.  
  113.         if($this->_storageType === Mai::STORAGE_TYPE_ARRAY)
  114.         {
  115.             //Clear data from previous runs
  116.             unset($this->_wordArrayData);
  117.             
  118.             //Loop through every word and every source
  119.             $wordId 0;
  120.             foreach($this->_wordArray as $key => $word)
  121.             {
  122.                 foreach($this->_sources as $name => $data)
  123.                 {
  124.                     foreach($data as $dataName => $dataValue)
  125.                     {
  126.                         if($word === $dataValue['word'])
  127.                         {
  128.                             //Word has been found, save data.
  129.                             $this->_wordArrayData[$wordId$dataValue;
  130.                             Mai_Log::info('Word \''.$word.'\' found from source '.$name);
  131.                             
  132.                             //Break from data loop
  133.                             break;
  134.                             //Break from sources loop
  135.                             break;
  136.                         }
  137.                     }
  138.                 }
  139.     
  140.                 //If the word is not found, the data will be empty
  141.                 if(!isset($this->_wordArrayData[$wordId]))
  142.                 {
  143.                     //Fill it with 0
  144.                     $this->_wordArrayData[$wordId0;
  145.                 }
  146.     
  147.                 //Go to next word
  148.                 $wordId++;
  149.             }
  150.         }
  151.         elseif($this->_storageType === Mai::STORAGE_TYPE_CLASS)
  152.         {
  153.             //Loop through every word
  154.             foreach($this->_wordContainers as $key => $word)
  155.             {
  156.                 foreach($this->_sources as $name => $data)
  157.                 {
  158.                     foreach($data as $dataName => $dataValue)
  159.                     {
  160.                         if($word->getWord(=== $dataValue['word'])
  161.                         {
  162.                             //Word has been found, save data.
  163.                             $word->setType($dataValue['wordtype']);
  164.                             //@Mai Todo: Uncomment ww_id
  165.                             //$word->setWwId($dataValue['ww_id']);
  166.                             //@Mai Todo: Replaced by person?
  167.                             //$word->setTarget($dataValue['target']);
  168.                             $word->setTime($dataValue['time']);
  169.                             $word->setNumber($dataValue['number']);
  170.                             $word->setPerson($dataValue['person']);
  171.                             
  172.                             //Send message to log
  173.                             Mai_Log::info('Word \''.$word->getWord().'\' found from source '.$name);
  174.                             
  175.                             //Break from data loop
  176.                             break;
  177.                             //Break from sources loop
  178.                             break;
  179.                         }
  180.                     }
  181.                 }
  182.             }
  183.         }
  184.     }
  185.  
  186.     /**
  187.      * Set the word array to analyze. This function uses references.
  188.      * Original data will be changed by this analyzer.
  189.      * @param int $type 
  190.      * @param array $data 
  191.      */
  192.     public function set(array $data)
  193.     {
  194.         if($this->_storageType === Mai::STORAGE_TYPE_ARRAY)
  195.         {
  196.             $this->_wordArray = &$data;
  197.         }
  198.         elseif($this->_storageType === Mai::STORAGE_TYPE_CLASS)
  199.         {
  200.             $this->_wordContainers &$data;
  201.         }
  202.         else
  203.         {
  204.             trigger_error('Unknown type.'E_USER_ERROR);
  205.         }
  206.     }
  207.  
  208.     /**
  209.      * Add a word source to the analyzer.
  210.      * @param string $name 
  211.      * @param array $data 
  212.      */
  213.     public function addSource($namearray $data)
  214.     {
  215.         if(empty($name|| !is_string($name))
  216.         {
  217.             throw new UnexpectedValueException('Name of source has not been set or is invalid!');
  218.         }
  219.  
  220.         //Sources can be empty, just log it and continue
  221.         if(empty($data))
  222.         {
  223.             Mai_Log::info('Mai_Analyzer_Words: Source with name '.$name.' is empty.');
  224.         }
  225.         else
  226.         {
  227.             //Test the source
  228.             //Test an element of the array (random)
  229.             $test $data[array_rand($data)];
  230.             
  231.             if(!isset($test['word']))
  232.             {
  233.                 throw new UnexpectedValueException('Source with name '.$name.' is not valid. Each element should have a word assigned.');
  234.             }
  235.         }
  236.         
  237.         //Add
  238.         $this->_sources[$name$data;
  239.     }
  240.  
  241.     /**
  242.      * Remove source.
  243.      * @param string $name 
  244.      */
  245.     public function removeSource($name)
  246.     {
  247.         if(array_key_exists($name$this->_sources))
  248.         {
  249.             unset($this->_sources[$name]);
  250.         }
  251.         else
  252.         {
  253.             trigger_error('Source '.$name.' does not excist.'E_USER_NOTICE);
  254.         }
  255.     }
  256.  
  257.     /**
  258.      * Return data from the analyzed words.
  259.      * This class also uses references, using this function is not required.
  260.      * @return array 
  261.      */
  262.     public function returnWordData()
  263.     {
  264.         if($this->_storageType === Mai::STORAGE_TYPE_ARRAY)
  265.         {
  266.             return $this->_wordArrayData;
  267.         }
  268.         elseif($this->_storageType === Mai::STORAGE_TYPE_CLASS)
  269.         {
  270.             return $this->_wordContainers;
  271.         }
  272.     }
  273. }

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