/**********************************************************************************
* AUTHOR		: Deepu V.
* DATE			: 22-AUG-2005
* NAME			: recognize
* DESCRIPTION	: This is the recognize call
*               : The results of the recognition is set on the Recognition context
*               : object.  In case of BATCH_MODE recognition recognition of every
*               : character is performed.  otherwise the recognizer updates the outputs
*               : with the recognized results
* ARGUMENTS		: rc - The recognition context for the current recognition
* RETURNS		: SUCCESS/FAILURE
* NOTES			:
* CHANGE HISTROY
* Author			Date				Description of
*************************************************************************************/
int BoxedFieldRecognizer::recognize (LTKRecognitionContext& rc)
{
	LOG(LTKLogger::LTK_LOGLEVEL_DEBUG)
	    <<"Entering: BoxedFieldRecognizer::recognize"
	    <<endl;

	string 	tempStr = REC_UNIT_INFO;  //temp string required to pass the arguments to set/get Flags

	int tempFlagValue = 0; //temp int to hold flag values

	int errorCode = 0;

	vector <LTKWordRecoResult>::iterator resultIter,resultEnd;  //iterates through decoded recognition results

	int numWordRecoResults ; //Number of results required by the application

	int resultIndex ; //index to iterate through the results

	vector<unsigned short> resultString; //result

	float normConf; //normalized confidence

	//Returning FAILURE if the recognition context
	//is not segmented into characters

	if((errorCode=rc.getFlag(tempStr,tempFlagValue))!=SUCCESS)
	{
		LOG(LTKLogger::LTK_LOGLEVEL_ERR)
            <<"Error: BoxedFieldRecognizer::recognize"<<endl;

		LTKReturnError(errorCode);
	}

	if( tempFlagValue != REC_UNIT_CHAR)
	{
		LOG(LTKLogger::LTK_LOGLEVEL_ERR)
		    <<"Error : "<< EINVALID_SEGMENT <<":"<< getErrorMessage(EINVALID_SEGMENT)
            <<" BoxedFieldRecognizer::recognize" <<endl;

		LTKReturnError(EINVALID_SEGMENT);
	}

	tempStr =REC_MODE;

	if((errorCode=rc.getFlag(tempStr,tempFlagValue))!=SUCCESS)
	{
		LOG(LTKLogger::LTK_LOGLEVEL_ERR)
            <<"Error: BoxedFieldRecognizer::recognize"<<endl;

		LTKReturnError(errorCode);
	}

	if(tempFlagValue == REC_MODE_BATCH)
	{
		//clear all the recognizer state
		clearRecognizerState();
		recognizeTraces(rc);
	}
	else if (tempFlagValue == REC_MODE_STREAMING)
	{
		recognizeTraces(rc);
	}
	else
	{
		LOG(LTKLogger::LTK_LOGLEVEL_ERR)
		    <<"Error : "<< EINVALID_REC_MODE <<":"<< getErrorMessage(EINVALID_REC_MODE)
            <<" BoxedFieldRecognizer::recognize" <<endl;

		LTKReturnError(EINVALID_REC_MODE);
	}

	/* Now all the recognized results are in
	 * m_decodedResults.
	 */

	resultEnd =	m_decodedResults.end();

	for(resultIter = m_decodedResults.begin(); resultIter != resultEnd ; ++resultIter)
	{
		normConf = (*resultIter).getResultConfidence();

		normConf /= ((*resultIter).getResultWord()).size();

		(*resultIter).setResultConfidence(normConf);

	}

	//number of requested results
	numWordRecoResults =  rc.getNumResults();

	//checking with existing recognition results' size
	if(numWordRecoResults > m_decodedResults.size())
	{
		LOG(LTKLogger::LTK_LOGLEVEL_INFO)
			<< "Don't have enough results to populate. Num of results available = "
			<< m_decodedResults.size() <<", however, results asked for ="
			<< numWordRecoResults <<endl;
	}

	resultEnd = m_decodedResults.end();
	for(resultIndex =0,resultIter = m_decodedResults.begin();(resultIndex <numWordRecoResults)&&(resultIter != resultEnd); ++resultIndex,++resultIter)
	{
		//map the shape ids to unicode IDs
		if((errorCode = LTKStrEncoding::shapeStrToUnicode(m_boxedShapeProject,(*resultIter).getResultWord(),resultString)) != SUCCESS)
		{
			LOG(LTKLogger::LTK_LOGLEVEL_ERR)
            	<<"Error: BoxedFieldRecognizer::recognize"<<endl;

			LTKReturnError(errorCode);
		}

		//adding the recognition result to recognition context
		 rc.addRecognitionResult(LTKWordRecoResult(resultString,
												   (*resultIter).getResultConfidence()));

		resultString.clear();

	}

	//clearing state of the recognizer
	clearRecognizerState();


	LOG(LTKLogger::LTK_LOGLEVEL_DEBUG)
		<<"Exiting: BoxedFieldRecognizer::recognize"
	    <<endl;

	return SUCCESS;
}