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

Source for file Pattern.php

Documentation is available at Pattern.php

  1. <?php
  2. /**
  3.  * Mai_Creator_Pattern
  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 Creator
  27.  * @subpackage Pattern
  28.  */
  29.  
  30. /**
  31.  * Mai_Creator_Pattern is a creator class for patterns.
  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 Creator
  40.  * @subpackage Pattern
  41.  *  
  42.  */
  43.  
  44. class Mai_Creator_Pattern implements Mai_Creator_Interface
  45. {
  46.     /**
  47.      * @var array Contains an array with words.
  48.      */
  49.     protected $_wordArray = array();
  50.  
  51.     /**
  52.      * @var Mai_Pattern Contains the pattern.
  53.      */
  54.     protected $_pattern;
  55.  
  56.     /**
  57.      * Constructor checks if Mai has been set up.
  58.      */
  59.     public function __construct($creator)
  60.     {
  61.         if(!Mai::isInit())
  62.         {
  63.             trigger_error('Mai has not been setup yet. Please use Mai::init().'E_USER_ERROR);
  64.         }
  65.         
  66.         //This class should only be created by Mai_Creator
  67.         if(!($creator instanceof Mai_Creator&& Mai_Config::getVar()->cache->allow_direct_access != 1)
  68.         {
  69.             trigger_error('Please use Mai_Creator to create an instance of Mai_Creator_Pattern.'E_USER_NOTICE);
  70.         }
  71.     }
  72.  
  73.     public function setWordArray(array $wordArray)
  74.     {
  75.         $this->_wordArray $wordArray;
  76.     }
  77.     
  78.     /**
  79. /**
  80.      * Runs the splitting process.
  81.      * @return void 
  82.      */
  83.     public function run()
  84.     {
  85.         if(empty($this->_wordArray))
  86.         {
  87.             throw new InvalidArgumentException('Word array not set.');
  88.         }
  89.  
  90.         $pattern ''//Pattern will be stored in this variable
  91.         $patternArray array()//An array of the pattern will be stored inside this variable.
  92.         
  93.         //Create pattern
  94.         foreach($this->_wordArray as $key => $value)
  95.         {
  96.             if($key !== 'sentence-mark')
  97.             {
  98.                 $patternArray[$value;
  99.                 
  100.                 //$value is 0 if the word is unknown.
  101.                 if($value === 0)
  102.                 {
  103.                     $pattern .= '-0';
  104.                 }
  105.                 else
  106.                 {
  107.                     //Add the word type to the pattern
  108.                     $pattern .= '-' . (string) $value['wordtype'];
  109.                 }
  110.             }
  111.         }
  112.         //Remove first - from the pattern
  113.         $pattern substr($pattern1);
  114.  
  115.         //Create new pattern
  116.         $this->_pattern = new Mai_Pattern($this->_wordArray$pattern$patternArray);
  117.     }
  118.     
  119.     /**
  120.      * Return the created pattern.
  121.      * @return Mai_Pattern 
  122.      */
  123.     public function returnPattern()
  124.     {
  125.         return $this->_pattern;
  126.     }
  127. }

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