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

Source for file TalkController.php

Documentation is available at TalkController.php

  1. <?php
  2. /**
  3.  * TalkController
  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.  * @package Controllers
  26.  * @subpackage Ai
  27.  * 
  28.  */
  29.  
  30. /**
  31.  * TalkController
  32.  * 
  33.  * Possible responses:
  34.  * 
  35.  * [1] 'invalid-session' When no session excists.
  36.  * [2] 'invalid-input' When the input is invalid or not set.
  37.  * [3] 'no-message' No message set
  38.  * [4] '...' A response from Mai
  39.  * 
  40.  * @author Wouter Bulten (wouterbulten@mai-ai.org)
  41.  * @copyright Copyright (C) 2009  Mai (Me Artificial Intelligence)
  42.  * @license http://opensource.org/licenses/gpl-3.0.html GNU General Public License version 3 (GPLv3)
  43.  * @link http://www.launchpad.net/mai
  44.  * 
  45.  * @package Controllers
  46.  * @subpackage Ai
  47.  * 
  48.  * 
  49.  */
  50.  
  51. class TalkController extends Zend_Controller_Action
  52. {
  53.     /**
  54.      * Combines the different components of Mai Lib to parse the text of the user.
  55.      */
  56.     public function indexAction()
  57.     {
  58.         //Start up Mai Library
  59.         $this->startUpMai();
  60.         
  61.          //Check the user session
  62.         if(!User_Session::isValid())
  63.         {
  64.             Mai_Registry::set('output'Mai_Config::getVar()->output->invalid_session);
  65.         }
  66.         else
  67.         {        
  68.             //Create a filter
  69.             $filterChain new Zend_Filter();
  70.             
  71.             $filterChain->addFilter(new Zend_Filter_StringToLower())
  72.                         ->addFilter(new Zend_Filter_StringTrim())
  73.                         ->addFilter(new Zend_Filter_StripTags());
  74.     
  75.             //Get the input
  76.             $inputText $this->getRequest()->getParam('text'false);
  77.             $inputType $this->getRequest()->getParam('type'1);
  78.             
  79.             //Filter the input
  80.             $inputTextFiltered $filterChain->filter($inputText);
  81.             $inputType (in_array($inputTypeMai::$allowedTextTypes)) $inputType false;
  82.  
  83.             //Free chain
  84.             unset($filterChain);
  85.                 
  86.             //Check if the input is correct
  87.             if(!$inputTextFiltered || $inputTextFiltered != strtolower(trim($inputText)) || !$inputType)
  88.             {
  89.                 //Send the invalid input message
  90.                 Mai_Registry::set('output'Mai_Config::getVar()->output->invalid_input);
  91.             }
  92.             else
  93.             {
  94.                 Mai_Log::info('TalkController: Input valid');
  95.  
  96.                 switch($inputType)
  97.                 {
  98.                     case Mai::MSG_TYPE_NORMAL:
  99.                         $this->processNormal($inputTextFiltered);
  100.                         break;
  101.                         
  102.                     default:
  103.                         Mai_Registry::set('output''-SERVER-Dit type wordt nog niet ondersteund.');
  104.                         break;
  105.                 }
  106.             }
  107.             
  108.             $this->endMai();
  109.         }
  110.         
  111.         //Send output to the view
  112.         $this->view->response Mai_Registry::get('output');
  113.     }
  114.  
  115.     /**
  116.      * Process normal text.
  117.      * @param $inputText 
  118.      * @return void 
  119.      */
  120.     private function processNormal($inputText)
  121.     {
  122.         //Create a new container for the input
  123.         $inputContainer Mai_Container::newContainerInput();
  124.         
  125.         //Set type
  126.         $inputContainer->setType Mai::MSG_TYPE_NORMAL;
  127.         
  128.         //Create a new word splitter and set the text (we save to containers)
  129.         $wordSplitter new Mai_Word_Splitter(Mai::STORAGE_TYPE_CLASS);
  130.         $wordSplitter->setString($inputText);
  131.  
  132.         //Run the word splitter (and store in containers)
  133.         $wordSplitter->run();
  134.  
  135.         //We save the sentence containers to the input container
  136.         foreach($wordSplitter as $key => $sentence)
  137.         {
  138.             $inputContainer->insertSentence($sentence);
  139.         }
  140.  
  141.         //Save the result to te registry
  142.         //Mai_Registry::set('word-array', $wordSplitter->returnWordArray());
  143.         //Mai_Registry::set('sentence-marks', $wordSplitter->returnSentenceMarks());
  144.         //Mai_Registry::set('sentence-amount', $wordSplitter->returnSentencesAmount());
  145.         //$sentenceMarks = $wordSplitter->returnSentenceMarks();
  146.         
  147.         //Free wordSplitter
  148.         unset($wordSplitter);
  149.         
  150.         //Create a new analyzer
  151.         $analyzer Mai_Analyzer::newWordAnalyzer(Mai::STORAGE_TYPE_CLASS);
  152.     
  153.         //To add the OpenTaal source we have to find the first 3 letters from every word
  154.         $letters $this->getFirstLettersWords($inputContainer);
  155.         
  156.         //Add csv source
  157.         foreach($letters as $key => $pair)
  158.         {
  159.             $sourceName 'OpenTaal-csv-'.$pair;
  160.             $data $this->getCsvWords($pair);
  161.             //Add to analyzer
  162.             $analyzer->addSource($sourceName$data);
  163.         }
  164.         
  165.         //Add other sources
  166.         //Note: Sources ar not yet used, there is no learning system yet
  167.         //$analyzer->addSource('local-cache', $this->getCacheWords());
  168.         //$analyzer->addSource('user-db',        $this->getWordsFromDb());
  169.  
  170.         //Analyze each sentence
  171.         foreach($inputContainer as $key => $sentence)
  172.         {            
  173.             //Get all the words (by reference)
  174.             $words &$sentence->getAll();
  175.             
  176.             //Set the array (sentence) (by reference)
  177.             $analyzer->set(&$words);
  178.  
  179.             //Run the analyzer
  180.             $analyzer->run();
  181.         }
  182.  
  183.         //Delete analyzer
  184.         unset($analyzer);
  185.  
  186.         /*
  187.         --- Note ---
  188.         The following code is not part of Mai App 0.1.1
  189.         This will be added in Mai App 0.2
  190.         ---      ---
  191.         
  192.         //Require object to create patterns
  193.         $patternCreator = Mai_Creator::newPatternCreator();
  194.  
  195.         //Store patterns in a variable
  196.         $patterns = array();
  197.  
  198.         //Create pattern for every sentence
  199.         foreach($wordData as $sentenceKey => $sentenceValue)
  200.         {
  201.             $patternCreator->setWordArray($sentenceValue);
  202.             $patternCreator->run();
  203.  
  204.             Mai_Log::info('TalkController: Pattern created for sentence '.$sentenceKey.': '.$patternCreator->returnPattern()->returnPattern());
  205.  
  206.             //Store pattern in an array
  207.             $patterns[$sentenceKey] = $patternCreator->returnPattern();
  208.         }
  209.  
  210.         //Unset patternCreator
  211.         unset($patternCreator);
  212.         
  213.         //Store in registry
  214.         Mai_Registry::set('patterns', $patterns);
  215.         */
  216.     }
  217.     
  218.     /**
  219.      * Start up Mai Library
  220.      * @return void 
  221.      */
  222.     private function startUpMai()
  223.     {
  224.         //Set env for Mai
  225.         if(Zend_Registry::get('config')->application->development)
  226.         {
  227.             Mai::setEnv(Mai::ENV_DEVELOPMENT);
  228.         }
  229.         else
  230.         {
  231.             Mai::setEnv(Mai::ENV_PRODUCTION);
  232.         }
  233.  
  234.         //Start up Mai library
  235.         Mai::init();
  236.     }
  237.  
  238.     /**
  239.      * End Mai library.
  240.      * @return void 
  241.      */
  242.     private function endMai()
  243.     {
  244.         Mai::end();
  245.     }
  246.     
  247.     /**
  248.      * Create a new instance of Mai_Cache_Words and loads the cache file defined in config.ini
  249.      * @return array Words from the cache file.
  250.      */
  251.     private function getCacheWords()
  252.     {
  253.         $cacheWords Mai_Cache::newCacheWords(
  254.                     BASE_PATH Zend_Registry::get('config')->cache->basewords->path,
  255.                     Zend_Registry::get('config')->cache->basewords->filename,
  256.                     Zend_Registry::get('config')->cache->basewords->varname
  257.                 );
  258.  
  259.         $words $cacheWords->returnCacheFromFile();
  260.  
  261.         Mai_Log::info('TalkController: Loaded '.count($words).' word(s) from cache.');
  262.  
  263.         return $words;
  264.     }
  265.  
  266.     /**
  267.      * Create a new instance of Mai_Cache_Words and loads the csv cache file defined in config.ini
  268.      * @return array Words from the cache file.
  269.      */
  270.     private function getCsvWords($letterPair)
  271.     {
  272.         $cacheWords Mai_Cache::newCacheWords(
  273.                     BASE_PATH Zend_Registry::get('config')->cache->csv->path,
  274.                     Zend_Registry::get('config')->cache->csv->filename '_' $letterPair,
  275.                     Zend_Registry::get('config')->cache->csv->varname
  276.                 );
  277.  
  278.         $words $cacheWords->returnCacheFromFile();
  279.  
  280.         Mai_Log::info('TalkController: Loaded '.count($words).' word(s) from CSV. Letter Pair: '.$letterPair);
  281.  
  282.         return $words;
  283.     }
  284.     
  285.     /**
  286.      * Selects all the words from the db for the current user.
  287.      * @return Array Words from the db
  288.      */
  289.     private function getWordsFromDb()
  290.     {
  291.         $query Doctrine_Query::create(Zend_Registry::get('conn_dbuser'))
  292.                  ->from('Word w')
  293.                  ->where('w.user_id = ?'User_Session::returnUserId());
  294.         $words $query->fetchArray();
  295.  
  296.         Mai_Log::info('TalkController: Loaded '.count($words).' word(s) from the database.');
  297.  
  298.         return $words;
  299.     }
  300.     
  301.     /**
  302.      * Selects patterns from the database.
  303.      * @return array 
  304.      */
  305.     private function getPatternsFromDb()
  306.     {
  307.         $query Doctrine_Query::create(Zend_Registry::get('conn_dbuser'))
  308.                  ->from('Word w')
  309.                  ->where('w.user_id = ?'User_Session::returnUserId());
  310.         $patterns $query->fetchArray();
  311.  
  312.         Mai_Log::info('TalkController: Loaded '.count($patterns).' patterns(s) from the database.');
  313.  
  314.         return $patterns;
  315.     }
  316.     
  317.     /**
  318.      * Returns an array with the first 3 letters of every word
  319.      * @param Mai_Container_Input $inputContainer 
  320.      * @return array 
  321.      */
  322.     private function getFirstLettersWords(Mai_Container_Input $inputContainer)
  323.     {
  324.         //Container
  325.         $firstLettersArray array();
  326.         
  327.         //Loop through every word
  328.         foreach($inputContainer->getAll(as $key => $sentence)
  329.         {
  330.             foreach($sentence->getAll(as $key => $word)
  331.             {
  332.                 //Use the key 'small' for words smaller than 3 letters
  333.                 if(strlen($word->getWord()) 3)
  334.                 {
  335.                     $letterPair 'small';
  336.                 }
  337.                 else
  338.                 {
  339.                     $letterPair substr($word->getWord()03);
  340.                 }
  341.                 
  342.                 if(!in_array($letterPair$firstLettersArray))
  343.                 {
  344.                     $firstLettersArray[$letterPair;
  345.                 }
  346.             }
  347.         }
  348.         
  349.         //return pairs
  350.         return $firstLettersArray;
  351.     }
  352. }

Documentation generated on Mon, 27 Jul 2009 19:51:59 +0200 by phpDocumentor 1.4.1