Source for file TalkController.php
Documentation is available at TalkController.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
* [1] 'invalid-session' When no session excists.
* [2] 'invalid-input' When the input is invalid or not set.
* [3] 'no-message' No message set
* [4] '...' A response from Mai
* @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
* Combines the different components of Mai Lib to parse the text of the user.
Mai_Registry::set('output', Mai_Config::getVar()->output->invalid_session);
$filterChain = new Zend_Filter();
$filterChain->addFilter(new Zend_Filter_StringToLower())
->addFilter(new Zend_Filter_StringTrim())
->addFilter(new Zend_Filter_StripTags());
$inputText = $this->getRequest()->getParam('text', false);
$inputType = $this->getRequest()->getParam('type', 1);
$inputTextFiltered = $filterChain->filter($inputText);
$inputType = (in_array($inputType, Mai::$allowedTextTypes)) ? $inputType : false;
//Check if the input is correct
if(!$inputTextFiltered || $inputTextFiltered != strtolower(trim($inputText)) || !$inputType)
//Send the invalid input message
Mai_Registry::set('output', Mai_Config::getVar()->output->invalid_input);
Mai_Log::info('TalkController: Input valid');
case Mai::MSG_TYPE_NORMAL:
Mai_Registry::set('output', '-SERVER-Dit type wordt nog niet ondersteund.');
//Send output to the view
$this->view->response = Mai_Registry::get('output');
//Create a new container for the input
$inputContainer = Mai_Container::newContainerInput();
$inputContainer->setType = Mai::MSG_TYPE_NORMAL;
//Create a new word splitter and set the text (we save to containers)
$wordSplitter = new Mai_Word_Splitter(Mai::STORAGE_TYPE_CLASS);
$wordSplitter->setString($inputText);
//Run the word splitter (and store in containers)
//We save the sentence containers to the input container
foreach($wordSplitter as $key => $sentence)
$inputContainer->insertSentence($sentence);
//Save the result to te registry
//Mai_Registry::set('word-array', $wordSplitter->returnWordArray());
//Mai_Registry::set('sentence-marks', $wordSplitter->returnSentenceMarks());
//Mai_Registry::set('sentence-amount', $wordSplitter->returnSentencesAmount());
//$sentenceMarks = $wordSplitter->returnSentenceMarks();
$analyzer = Mai_Analyzer::newWordAnalyzer(Mai::STORAGE_TYPE_CLASS);
//To add the OpenTaal source we have to find the first 3 letters from every word
foreach($letters as $key => $pair)
$sourceName = 'OpenTaal-csv-'. $pair;
$analyzer->addSource($sourceName, $data);
//Note: Sources ar not yet used, there is no learning system yet
//$analyzer->addSource('local-cache', $this->getCacheWords());
//$analyzer->addSource('user-db', $this->getWordsFromDb());
foreach($inputContainer as $key => $sentence)
//Get all the words (by reference)
$words = &$sentence->getAll();
//Set the array (sentence) (by reference)
The following code is not part of Mai App 0.1.1
This will be added in Mai App 0.2
//Require object to create patterns
$patternCreator = Mai_Creator::newPatternCreator();
//Store patterns in a variable
//Create pattern for every sentence
foreach($wordData as $sentenceKey => $sentenceValue)
$patternCreator->setWordArray($sentenceValue);
Mai_Log::info('TalkController: Pattern created for sentence '.$sentenceKey.': '.$patternCreator->returnPattern()->returnPattern());
//Store pattern in an array
$patterns[$sentenceKey] = $patternCreator->returnPattern();
Mai_Registry::set('patterns', $patterns);
if(Zend_Registry::get('config')->application->development)
Mai::setEnv(Mai::ENV_DEVELOPMENT);
Mai::setEnv(Mai::ENV_PRODUCTION);
* Create a new instance of Mai_Cache_Words and loads the cache file defined in config.ini
* @return array Words from the cache file.
$cacheWords = Mai_Cache::newCacheWords(
BASE_PATH . Zend_Registry::get('config')->cache->basewords->path,
Zend_Registry::get('config')->cache->basewords->filename,
Zend_Registry::get('config')->cache->basewords->varname
$words = $cacheWords->returnCacheFromFile();
Mai_Log::info('TalkController: Loaded '. count($words). ' word(s) from cache.');
* Create a new instance of Mai_Cache_Words and loads the csv cache file defined in config.ini
* @return array Words from the cache file.
$cacheWords = Mai_Cache::newCacheWords(
BASE_PATH . Zend_Registry::get('config')->cache->csv->path,
Zend_Registry::get('config')->cache->csv->filename . '_' . $letterPair,
Zend_Registry::get('config')->cache->csv->varname
$words = $cacheWords->returnCacheFromFile();
Mai_Log::info('TalkController: Loaded '. count($words). ' word(s) from CSV. Letter Pair: '. $letterPair);
* Selects all the words from the db for the current user.
* @return Array Words from the db
$query = Doctrine_Query::create(Zend_Registry::get('conn_dbuser'))
$words = $query->fetchArray();
Mai_Log::info('TalkController: Loaded '. count($words). ' word(s) from the database.');
* Selects patterns from the database.
$query = Doctrine_Query::create(Zend_Registry::get('conn_dbuser'))
$patterns = $query->fetchArray();
Mai_Log::info('TalkController: Loaded '. count($patterns). ' patterns(s) from the database.');
* Returns an array with the first 3 letters of every word
* @param Mai_Container_Input $inputContainer
$firstLettersArray = array();
//Loop through every word
foreach($inputContainer->getAll() as $key => $sentence)
foreach($sentence->getAll() as $key => $word)
//Use the key 'small' for words smaller than 3 letters
if(strlen($word->getWord()) < 3)
$letterPair = substr($word->getWord(), 0, 3);
if(!in_array($letterPair, $firstLettersArray))
$firstLettersArray[] = $letterPair;
return $firstLettersArray;
|