/***************************************************************************************** * Copyright (c) 2006 Hewlett-Packard Development Company, L.P. * Permission is hereby granted, free of charge, to any person obtaining a copy of * this software and associated documentation files (the "Software"), to deal in * the Software without restriction, including without limitation the rights to use, * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the * Software, and to permit persons to whom the Software is furnished to do so, * subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. *****************************************************************************************/ /************************************************************************ * SVN MACROS * * $LastChangedDate$ * $Revision$ * $Author$ * ************************************************************************/ /************************************************************************ * FILE DESCR: Implementation of LTKRecognitionContext that holds the context * for recognition * * CONTENTS: * addTrace * addTraceGroup * beginRecoUnit * endRecoUnit * getAllInk * getConfidThreshold * getDeviceContext * getFlag * getLanguageModel * getNextBestResults * getNumResults * getScreenContext * getTopResult * setConfidThreshold * setDeviceContext * setFlag * setLanguageModel * setNumResults * setScreenContext * addRecognitionResult * recognize * reset * * AUTHOR: Deepu V. * * DATE: February 22, 2005 * CHANGE HISTORY: * Author Date Description of change * Thanigai 3-AUG-2005 Added default constructor and setWordRecoEngine * methods. * * Deepu 30-AUG-2005 Replaced LTKWordRecoEngine with LTKWordRecognizer * Changed the representation of m_recognitionFlags * since there was a problem with dlls ************************************************************************/ #include "LTKRecognitionContext.h" #include "LTKMacros.h" #include "LTKErrors.h" #include "LTKTrace.h" #include "LTKErrorsList.h" #include "LTKTraceGroup.h" #include "LTKWordRecoResult.h" #include "LTKWordRecognizer.h" #include "LTKLoggerUtil.h" #include "LTKException.h" /********************************************************************************** * AUTHOR : Thanigai * DATE : 3-AUG-2005 * NAME : LTKRecognitionContext * DESCRIPTION : Default constructor * ARGUMENTS : * RETURNS : * NOTES : * CHANGE HISTROY * Author Date Description of change *************************************************************************************/ LTKRecognitionContext::LTKRecognitionContext() :m_confidThreshold(0), m_numResults(0), m_nextBestResultIndex(0), m_wordRecPtr(NULL) { LOG( LTKLogger::LTK_LOGLEVEL_DEBUG) << " Entering: LTKRecognitionContext::LTKRecognitionContext()" << endl; LOG( LTKLogger::LTK_LOGLEVEL_DEBUG) << " Exiting: LTKRecognitionContext::LTKRecognitionContext()" << endl; } /********************************************************************************** * AUTHOR : Deepu V. * DATE : 22-FEB-2005 * NAME : LTKRecognitionContext * DESCRIPTION : Initialization constructor * ARGUMENTS : * RETURNS : * NOTES : * CHANGE HISTROY * Author Date Description of change *************************************************************************************/ LTKRecognitionContext::LTKRecognitionContext(LTKWordRecognizer *wordRecPtr ) :m_wordRecPtr(wordRecPtr), m_confidThreshold(0), m_numResults(0), m_nextBestResultIndex(0) { LOG( LTKLogger::LTK_LOGLEVEL_DEBUG) << " Entering: LTKRecognitionContext::LTKRecognitionContext(LTKWordRecognizer*)" << endl; if(m_wordRecPtr == NULL) { LOG(LTKLogger::LTK_LOGLEVEL_ERR) <<"Error : "<< ENULL_POINTER <<":"<< getErrorMessage(ENULL_POINTER) <<" LTKRecognitionContext::LTKRecognitionContext(LTKWordRecognizer*)" <processInk(*this); } LOG( LTKLogger::LTK_LOGLEVEL_DEBUG) << " Exiting: LTKRecognitionContext::addTrace()" << endl; return SUCCESS; } /********************************************************************************** * AUTHOR : Deepu V. * DATE : 22-FEB-2005 * NAME : addTraceGroup * DESCRIPTION : Adds a vector of tracegroup for recognition in the recognition context * ARGUMENTS : fieldInk - the ink to be added. * RETURNS : SUCCESS/FAILURE * NOTES : * CHANGE HISTROY * Author Date Description of change *************************************************************************************/ int LTKRecognitionContext::addTraceGroups (const LTKTraceGroupVector& fieldInk) { int numTraceGroups = fieldInk.size(); //number of trace groups int numTraces =0; //number of traces in each trace group string tempStr; //strokes temporary string for getFlag int recMode =0; // the recognition mode LOG( LTKLogger::LTK_LOGLEVEL_DEBUG) << " Entering: LTKRecognitionContext::addTraceGroups()" << endl; for(int i =0; iprocessInk(*this); } LOG( LTKLogger::LTK_LOGLEVEL_DEBUG) << " Exiting: LTKRecognitionContext::addTraceGroups()" << endl; return SUCCESS; } /********************************************************************************** * AUTHOR : Deepu V. * DATE : 28-FEB-2005 * NAME : beginRecoUnit * DESCRIPTION : This function marks the beginning of a recognition unit of Ink. * ARGUMENTS : none * RETURNS : SUCCESS/FAILURE * NOTES : * CHANGE HISTROY * Author Date Description of change *************************************************************************************/ void LTKRecognitionContext::beginRecoUnit ( ) { LOG( LTKLogger::LTK_LOGLEVEL_DEBUG) << " Entering: LTKRecognitionContext::beginRecoUnit()" << endl; LOG( LTKLogger::LTK_LOGLEVEL_DEBUG) << " Exiting: LTKRecognitionContext::beginRecoUnit()" << endl; } /********************************************************************************** * AUTHOR : Deepu V. * DATE : 11-MAR-2005 * NAME : clearRecognitionResult * DESCRIPTION : clears all the recognition results * ARGUMENTS : none * RETURNS : SUCCESS/FAILURE * NOTES : * CHANGE HISTROY * Author Date Description of change *************************************************************************************/ int LTKRecognitionContext::clearRecognitionResult ( ) { LOG( LTKLogger::LTK_LOGLEVEL_DEBUG) << " Entering: LTKRecognitionContext::clearRecognitionResult()" << endl; //clearing the results m_results.clear(); //reset the index of next best result m_nextBestResultIndex = 0; m_fieldInk.clear(); int errorCode; if((errorCode=m_wordRecPtr->reset())!=SUCCESS) { LOG(LTKLogger::LTK_LOGLEVEL_ERR) <<"Error: LTKRecognitionContext::clearRecognitionResult()"<endRecoUnit(); LOG( LTKLogger::LTK_LOGLEVEL_DEBUG) << " Exiting: LTKRecognitionContext::endRecoUnit()" << endl; } /********************************************************************************** * AUTHOR : Deepu V. * DATE : 28-FEB-2005 * NAME : getAllInk * DESCRIPTION : Access function for the internal Ink data. * ARGUMENTS : none * RETURNS : reference to internal Ink data * NOTES : * CHANGE HISTROY * Author Date Description of change *************************************************************************************/ const LTKTraceVector& LTKRecognitionContext::getAllInk () const { LOG( LTKLogger::LTK_LOGLEVEL_DEBUG) << " Entering: LTKRecognitionContext::getAllInk()" << endl; LOG( LTKLogger::LTK_LOGLEVEL_DEBUG) << " Exiting: LTKRecognitionContext::getAllInk()" << endl; return m_fieldInk; } /********************************************************************************** * AUTHOR : Deepu V. * DATE : 28-FEB-2005 * NAME : getConfidThreshold * DESCRIPTION : Access function for internal confidence threshold * ARGUMENTS : none * RETURNS : confidence threshold * NOTES : * CHANGE HISTROY * Author Date Description of change *************************************************************************************/ float LTKRecognitionContext::getConfidThreshold () const { LOG( LTKLogger::LTK_LOGLEVEL_DEBUG) << " Entering: LTKRecognitionContext::getConfidThreshold()" << endl; LOG( LTKLogger::LTK_LOGLEVEL_DEBUG) << " Exiting: LTKRecognitionContext::getConfidThreshold()" << endl; return m_confidThreshold; } /********************************************************************************** * AUTHOR : Deepu V. * DATE : 28-FEB-2005 * NAME : getDeviceContext * DESCRIPTION : Access function for device context * ARGUMENTS : none * RETURNS : reference to LTKCapture device * NOTES : * CHANGE HISTROY * Author Date Description of change *************************************************************************************/ const LTKCaptureDevice& LTKRecognitionContext::getDeviceContext ( ) const { LOG( LTKLogger::LTK_LOGLEVEL_DEBUG) << " Entering: LTKRecognitionContext::getDeviceContext()" << endl; LOG( LTKLogger::LTK_LOGLEVEL_DEBUG) << " Exiting: LTKRecognitionContext::getDeviceContext()" << endl; return m_deviceContext; } /********************************************************************************** * AUTHOR : Deepu V. * DATE : 28-FEB-2005 * NAME : getFlag * DESCRIPTION : Returns the value of the flag * ARGUMENTS : key - index of map * RETURNS : value of queried flag (int) * NOTES : * CHANGE HISTROY * Author Date Description of change *************************************************************************************/ int LTKRecognitionContext::getFlag (const string& key,int& outValue) const { LOG( LTKLogger::LTK_LOGLEVEL_DEBUG) << " Entering: LTKRecognitionContext::getFlag()" << endl; if(key=="") { LOG(LTKLogger::LTK_LOGLEVEL_ERR) <<"Error : "<< EEMPTY_STRING <<":"<< getErrorMessage(EEMPTY_STRING) <<" LTKRecognitionContext::getFlag()" < >::const_iterator iter,iterEnd; iterEnd = m_recognitionFlags.end(); //Iterating through the vector to find the key for(iter = m_recognitionFlags.begin(); iter != iterEnd; ++iter) { if( (*iter).first == key ) { outValue = (*iter).second; LOG( LTKLogger::LTK_LOGLEVEL_DEBUG) << " Exiting: LTKRecognitionContext::getFlag()" << endl; return SUCCESS; } } LOG(LTKLogger::LTK_LOGLEVEL_ERR) <<"Error : "<< EKEY_NOT_FOUND <<":"<< getErrorMessage(EKEY_NOT_FOUND) <<" LTKRecognitionContext::getFlag()" <m_languageModels.find(key); if(iterMap != m_languageModels.end() ) { outValue = iterMap->second; LOG( LTKLogger::LTK_LOGLEVEL_DEBUG) << " Exiting: LTKRecognitionContext::getLanguageModel()" << endl; return SUCCESS; } LOG(LTKLogger::LTK_LOGLEVEL_ERR) <<"Error : "<< EKEY_NOT_FOUND <<":"<< getErrorMessage(EKEY_NOT_FOUND) <<" LTKRecognitionContext::getLanguageModel()" <::const_iterator resultBegin, resultEnd, resultIter; //Finding index of requested results resultBegin = m_results.begin() + m_nextBestResultIndex; //Finding index of requested results resultEnd = m_results.begin() + m_nextBestResultIndex + numResults; if(resultBegin > resultEnd ) { LOG(LTKLogger::LTK_LOGLEVEL_DEBUG) << "Exiting LTKRecognitionContext::getNextBestResults" < m_results.end() ) resultEnd = m_results.end(); //pushing back the results for(resultIter = resultBegin; resultIter< resultEnd; ++resultIter) { outWordRecResults.push_back(*resultIter); } //updating next best result index m_nextBestResultIndex += numResults; LOG( LTKLogger::LTK_LOGLEVEL_DEBUG) << " Exiting: LTKRecognitionContext::getNextBestResults()" << endl; return SUCCESS; } /********************************************************************************** * AUTHOR : Deepu V. * DATE : 28-FEB-2005 * NAME : getNumResults * DESCRIPTION : parameter number of results * ARGUMENTS : none * RETURNS : number of results (int) * NOTES : * CHANGE HISTROY * Author Date Description of change *************************************************************************************/ int LTKRecognitionContext::getNumResults () const { LOG( LTKLogger::LTK_LOGLEVEL_DEBUG) << " Entering: LTKRecognitionContext::getNumResults()" << endl; LOG( LTKLogger::LTK_LOGLEVEL_DEBUG) << " Exiting: LTKRecognitionContext::getNumResults()" << endl; return m_numResults; } /********************************************************************************** * AUTHOR : Deepu V. * DATE : 28-FEB-2005 * NAME : getScreenContext * DESCRIPTION : access function for the screen context * ARGUMENTS : none * RETURNS : reference to screencontext object * NOTES : * CHANGE HISTROY * Author Date Description of change *************************************************************************************/ const LTKScreenContext& LTKRecognitionContext::getScreenContext ( ) const { LOG( LTKLogger::LTK_LOGLEVEL_DEBUG) << " Entering: LTKRecognitionContext::getScreenContext()" << endl; LOG( LTKLogger::LTK_LOGLEVEL_DEBUG) << " Exiting: LTKRecognitionContext::getScreenContext()" << endl; return m_screenContext; } /********************************************************************************** * AUTHOR : Deepu V. * DATE : 28-FEB-2005 * NAME : getTopResult * DESCRIPTION : get the top result from the recognition context * ARGUMENTS : result - will be assigned to the top recognition result * RETURNS : SUCCESS/FAILURE * NOTES : * CHANGE HISTROY * Author Date Description of change *************************************************************************************/ int LTKRecognitionContext::getTopResult (LTKWordRecoResult& outTopResult) { LOG( LTKLogger::LTK_LOGLEVEL_DEBUG) << " Entering: LTKRecognitionContext::getTopResult()" << endl; if(m_results.size() == 0) { LOG(LTKLogger::LTK_LOGLEVEL_ERR) <<"Error : "<< EEMPTY_WORDREC_RESULTS <<":"<< getErrorMessage(EEMPTY_WORDREC_RESULTS) <<" LTKRecognitionContext::getTopResult()" < >::iterator iter,iterEnd;//iterators for iterating through all flags LOG( LTKLogger::LTK_LOGLEVEL_DEBUG) << " Entering: LTKRecognitionContext::setFlag()" << endl; if(key=="") { LOG(LTKLogger::LTK_LOGLEVEL_ERR) <<"Error : "<< EEMPTY_STRING <<":"<< getErrorMessage(EEMPTY_STRING) <<" LTKRecognitionContext::setFlag()" <(key,value)); } LOG( LTKLogger::LTK_LOGLEVEL_DEBUG) << " Exiting: LTKRecognitionContext::setFlag()" << endl; return SUCCESS; } /********************************************************************************** * AUTHOR : Deepu V. * DATE : 28-FEB-2005 * NAME : setLanguageModel * DESCRIPTION : sets the language model * ARGUMENTS : property - name of ppty to be set (DICTIONARY, GRAMMAR) * : value - value to be set * RETURNS : SUCCESS/FAILURE * NOTES : * CHANGE HISTROY * Author Date Description of change *************************************************************************************/ int LTKRecognitionContext::setLanguageModel (const string& property, const string& value) { LOG( LTKLogger::LTK_LOGLEVEL_DEBUG) << " Entering: LTKRecognitionContext::setLanguageModel()" << endl; if(property=="" || value=="") { LOG(LTKLogger::LTK_LOGLEVEL_ERR)<< "Either property or value is empty"<recognize(*this)) != SUCCESS) { LOG(LTKLogger::LTK_LOGLEVEL_ERR) <<"Error: LTKRecognitionContext::recognize()"<reset(resetParam))!=SUCCESS) { LOG(LTKLogger::LTK_LOGLEVEL_ERR) <<"Error: LTKRecognitionContext::reset()"<