featurefilewriter::~featurefilewriter()
{

    LOG(LTKLogger::LTK_LOGLEVEL_DEBUG) << "Entering " <<
        "featurefilewriter::~featurefilewriter()" << endl;

    int returnStatus = SUCCESS;

    returnStatus = deletePreprocessor();
    if(returnStatus != SUCCESS)
    {
        LOG(LTKLogger::LTK_LOGLEVEL_ERR) << "Error: " << returnStatus << " " <<
            " featurefilewriter::~featurefilewriter()" << endl;
        throw LTKException(returnStatus);
    }

    //Unloading the feature Extractor instance
    returnStatus = deleteFeatureExtractorInstance();
    if(returnStatus != SUCCESS)
    {
        LOG(LTKLogger::LTK_LOGLEVEL_ERR) << "Error: " <<  returnStatus << " " <<
            " featurefilewriter::~featurefilewriter()" << endl;
        throw LTKException(returnStatus);
    }

	if(NULL != m_OSUtilPtr)
	{
		delete m_OSUtilPtr;
		m_OSUtilPtr = NULL;
	}
    LOG(LTKLogger::LTK_LOGLEVEL_DEBUG) << "Exiting " <<
        "featurefilewriter::~featurefilewriter()" << endl;
}
/******************************************************************************
* AUTHOR		: Balaji R.
* DATE			: 23-DEC-2004
* NAME			: LTKTraceGroup
* DESCRIPTION	: Initialization constructor
* ARGUMENTS		: inTraceVector - vector of traces to be set to class member
* RETURNS		:
* NOTES			:
* CHANGE HISTROY
* Author			Date				Description of change
******************************************************************************/
LTKTraceGroup::LTKTraceGroup(const LTKTraceVector& inTraceVector,
                             float xScaleFactor, float yScaleFactor) :
	m_traceVector(inTraceVector)
{
	LOG( LTKLogger::LTK_LOGLEVEL_DEBUG) <<
		"Enter: LTKTraceGroup::LTKTraceGroup(LTKTraceVector&, float,float)"<<endl;

	if(xScaleFactor <= 0)
	{
		LOG( LTKLogger::LTK_LOGLEVEL_ERR) <<
        "Error: "<<EINVALID_X_SCALE_FACTOR <<": "<<
		getErrorMessage(EINVALID_X_SCALE_FACTOR) <<
		"LTKTraceGroup::LTKTraceGroup(LTKTraceVector&, float,float)"<<endl;

		throw LTKException(EINVALID_X_SCALE_FACTOR);
	}

	if(yScaleFactor <= 0)
	{
		LOG( LTKLogger::LTK_LOGLEVEL_ERR) <<
        "Error: "<<EINVALID_Y_SCALE_FACTOR <<": "<<
		getErrorMessage(EINVALID_Y_SCALE_FACTOR) <<
		"LTKTraceGroup::LTKTraceGroup(LTKTraceVector&, float,float)"<<endl;

		throw LTKException(EINVALID_Y_SCALE_FACTOR);
	}

	m_xScaleFactor = xScaleFactor;
	m_yScaleFactor = yScaleFactor;

	LOG( LTKLogger::LTK_LOGLEVEL_DEBUG) <<
		"Exit: LTKTraceGroup::LTKTraceGroup(LTKTraceVector&, float,float)"<<endl;


}
Exemple #3
0
float HolisticRecognizer::holisticDistance(const float2DVector& test,const HolisticShapeModel& train, float bestSoFar)
{
	LOG( LTKLogger::LTK_LOGLEVEL_DEBUG) << "Entered HolisticRecognizer::holisticDistance"  << endl;

	float distance=0.0f;		// distance between testVec and floatVec

	int loopIndex;				// Index used for looping
	
	// check and validate the test vector size

	if (test.size()!=1)
	{
		throw LTKException(ECOMPUTE_DISTANCE_ERROR);
	}

	floatVector testVec = test.at(0);

	// check and validate the train vector size
	
	if ((train.getModel()).size()!=1)
	{
		throw LTKException(ECOMPARISON_ERROR);
	}
	
	floatVector trainVec = (train.getModel()).at(0);

	int vecSize = testVec.size();

	// check and validate the train & test vector sizes

	if (vecSize!=trainVec.size())
	{
		throw LTKException(ETRAIN_TEST_VECTOR_SIZE_MISMATCH);
	}

	// calculating the euclidean distance 

	for(loopIndex = 0; loopIndex < vecSize; ++loopIndex)
	{
		distance += (testVec.at(loopIndex)-trainVec.at(loopIndex))*(testVec.at(loopIndex)-trainVec.at(loopIndex));
	}

	distance = sqrt(distance);

	LOG( LTKLogger::LTK_LOGLEVEL_DEBUG) << "Exiting HolisticRecognizer::holisticDistance"  << endl;

	return distance;
}
BoxedFieldRecognizer::~BoxedFieldRecognizer()
{
	LOG(LTKLogger::LTK_LOGLEVEL_DEBUG)
	    <<"Entering: BoxedFieldRecognizer::~BoxedFieldRecognizer"
	    <<endl;

	//unload the model data

	int errorCode = FAILURE;
	if((errorCode = unloadModelData()) != SUCCESS)
	{
		LOG(LTKLogger::LTK_LOGLEVEL_ERR)
        	<<"Error: BoxedFieldRecognizer::~BoxedFieldRecognizer"<<endl;

		throw LTKException(errorCode);
	}

    delete m_OSUtilPtr;

	LOG(LTKLogger::LTK_LOGLEVEL_DEBUG)
		<<"Exiting: BoxedFieldRecognizer::~BoxedFieldRecognizer"
		<<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*)" <<endl;

       throw LTKException(ENULL_POINTER);
    }

	m_recognitionFlags.clear();
	m_wordRecPtr = wordRecPtr;


	
	LOG( LTKLogger::LTK_LOGLEVEL_DEBUG) << 
        " Exiting: LTKRecognitionContext::LTKRecognitionContext(LTKWordRecognizer*)" << endl;
}
/******************************************************************************
* AUTHOR		: Dinesh M
* DATE			: 1-Oct-2007
* NAME			: NPenShapeFeatureExtractor
* DESCRIPTION	: parameterized constructor
* ARGUMENTS		:
* RETURNS		:
* NOTES			:
* CHANGE HISTROY
* Author			Date
*******************************************************************************/
NPenShapeFeatureExtractor::NPenShapeFeatureExtractor(const LTKControlInfo& controlInfo):
m_windowSize(FEATEXTR_NPEN_DEF_WINDOW_SIZE)
{    
	string cfgFilePath = "";
    
	// Config file
    if ( ! ((controlInfo.lipiRoot).empty()) &&
         ! ((controlInfo.projectName).empty()) &&
         ! ((controlInfo.profileName).empty()) &&
         ! ((controlInfo.cfgFileName).empty()))
    {
        // construct the cfg path using project and profile name
         cfgFilePath = (controlInfo.lipiRoot) + PROJECTS_PATH_STRING +
                       (controlInfo.projectName) + PROFILE_PATH_STRING +
                       (controlInfo.profileName) + SEPARATOR +
                       (controlInfo.cfgFileName) + CONFIGFILEEXT;
    }
    else if ( ! ((controlInfo.cfgFilePath).empty() ))
    {
        cfgFilePath = controlInfo.cfgFilePath;
    }
    else
    {
        throw LTKException(EINVALID_PROJECT_NAME);
    }

    
	int returnVal = readConfig(cfgFilePath);
    
    if (returnVal != SUCCESS)
	{
	    LOG(LTKLogger::LTK_LOGLEVEL_ERR) << 
            "Error: NPenShapeFeatureExtractor::NPenShapeFeatureExtractor()" <<endl;
        
		throw LTKException(returnVal);
	}

    LOG(LTKLogger::LTK_LOGLEVEL_DEBUG) << "Exiting " <<
        "NPenShapeFeatureExtractor::NPenShapeFeatureExtractor()" << endl;
    
}
Exemple #7
0
	void LTKGrammar::findPossiblePaths()
	{

		LOG(LTKLogger::LTK_LOGLEVEL_DEBUG) <<"Entering LTKGrammar::findPossiblePaths" <<endl;

		m_paths=m_cfgMap[m_startSymbol];	//Getting the definition of start symbol production
		
		vector<string> replacedStrings;
		
		for(int i=0;i<m_paths.size();++i)
		{
			for(int j=0;j<m_paths[i].size();++j)
			{
				if(m_cfgMap.find(m_paths[i][j])!=m_cfgMap.end())	//if the term is a production
				{
						if(replacePaths(m_paths[i][j],m_paths,m_cfgMap[m_paths[i][j]]))	//replace in the list of paths wherever the term occurs
						{
							j--;
						}
						else	//if unable to replace the production
						{
							m_paths.clear();
							return;
						}

					if(m_paths[i].size()>10000)	//if the path size has exploded, which could be due to recursive definition
					{
						LOG(LTKLogger::LTK_LOGLEVEL_ERR) << "Cyclic dependency exists! Unable to find paths!" <<endl;
						m_paths.clear();
						throw LTKException(ECYCLIC_DEPENDENCY);
						return ;
					}

										
				}
			}


		}

	
		LOG(LTKLogger::LTK_LOGLEVEL_DEBUG) <<"Exiting LTKGrammar::findPossiblePaths" <<endl;
	}
LTKWordRecoConfig::LTKWordRecoConfig(const string& lipiRoot)
{
	LOG( LTKLogger::LTK_LOGLEVEL_DEBUG) << 
        " Entering: LTKWordRecoConfig::LTKWordRecoConfig(const string&)" << endl;

	if(lipiRoot=="")
	{
		LOG(LTKLogger::LTK_LOGLEVEL_ERR)
          <<"Error : "<< EEMPTY_STRING <<":"<< getErrorMessage(EEMPTY_STRING)
          <<" LTKWordRecoConfig::LTKWordRecoConfig(const string&)" <<endl;

		throw LTKException(EEMPTY_STRING);
	}

	m_lipiRoot = lipiRoot;

	LOG(LTKLogger::LTK_LOGLEVEL_DEBUG) <<  "m_lipiRoot = " << m_lipiRoot <<endl;
	
	LOG( LTKLogger::LTK_LOGLEVEL_DEBUG) << 
        " Exiting: LTKWordRecoConfig::LTKWordRecoConfig(const string&)" << endl;
}
LTKShapeRecoConfig::LTKShapeRecoConfig(const  string& lipiRoot, const string& shapeSet,
					 const  string& profile, const string& shapeRecognizerName, int numShapes) :
					m_lipiRoot(lipiRoot), m_shapeSet(shapeSet), m_profile(profile),
					m_shapeRecognizerName(shapeRecognizerName), m_numShapes(numShapes)
{
	LOG( LTKLogger::LTK_LOGLEVEL_DEBUG) <<  
        "Entered initialization constructor of LTKShapeRecoConfig"  << endl;

	LOG( LTKLogger::LTK_LOGLEVEL_DEBUG) <<  
        "m_lipiRoot = " << m_lipiRoot  << endl;

	LOG( LTKLogger::LTK_LOGLEVEL_DEBUG) <<  
        "m_shapeSet = " << m_shapeSet  << endl;

	LOG( LTKLogger::LTK_LOGLEVEL_DEBUG) <<  
        "m_profile = " << m_profile  << endl;

	LOG( LTKLogger::LTK_LOGLEVEL_DEBUG) <<  
        "m_shapeRecognizerName = " << m_shapeRecognizerName  << endl;

	LOG( LTKLogger::LTK_LOGLEVEL_DEBUG) <<  
        "m_numShapes = " << m_numShapes  << endl;

	int errorCode;
	if(m_numShapes <= 0)
	{
		errorCode = EINVALID_NUM_OF_SHAPES;

		LOG( LTKLogger::LTK_LOGLEVEL_ERR) << 
            "Invalid value for number of shapes :" << m_numShapes << endl;
		
		throw LTKException(errorCode);
	}

	LOG( LTKLogger::LTK_LOGLEVEL_DEBUG) <<  
        "Exiting initialization constructor of LTKShapeRecoConfig"  << endl;
}
/*****************************************************************************
* AUTHOR		: Deepu V.
* DATE			: 22-AUG-2005
* NAME			: initializeWordRecognizer
* DESCRIPTION	: Initialization of Boxed word Recognizer. This function performs
*                      -Create & initialize shape recognizer
* ARGUMENTS		:
* RETURNS		:
* NOTES			:
* CHANGE HISTROY
* Author			Date				Description of
*****************************************************************************/
BoxedFieldRecognizer::BoxedFieldRecognizer(const LTKControlInfo& controlInfo)
:LTKWordRecognizer(BOXFLD),
m_shapeRecognizer(NULL),
m_numShapeRecoResults(DEFAULT_SHAPE_RECO_CHOICES),
m_shapeRecoMinConfidence(DEFAULT_SHAPE_RECO_MIN_CONFID),
m_module_createShapeRecognizer(NULL),
m_module_deleteShapeRecognizer(NULL),
m_numCharsProcessed(0),
m_numTracesProcessed(0),
m_boxedShapeProject(""),
m_boxedShapeProfile(""),
m_lipiRoot(""),
m_boxedConfigFile(""),
m_logFile(""),
m_logLevel(LTKLogger::LTK_LOGLEVEL_ERR),
m_toolkitVersion(""),
m_OSUtilPtr(LTKOSUtilFactory::getInstance())
{

	string boxedShapeProfile;   //profile name

	int errorCode = 0;

	LTKControlInfo tempControlInfo = controlInfo;

    if ( tempControlInfo.lipiRoot.empty() )
    {
        throw LTKException(ELIPI_ROOT_PATH_NOT_SET);
    }

    if ( tempControlInfo.projectName.empty() )
    {
        throw LTKException(EINVALID_PROJECT_NAME);
    }

    if( (tempControlInfo.profileName).empty() )
	{
		tempControlInfo.profileName = DEFAULT_PROFILE;
	}

    if ( tempControlInfo.toolkitVersion.empty() )
    {
        throw LTKException(ENO_TOOLKIT_VERSION);
    }

    // initialize the data members
    m_lipiRoot          = tempControlInfo.lipiRoot;
    m_toolkitVersion    = tempControlInfo.toolkitVersion;


	//constructing the boxed Config filename
	m_boxedConfigFile  = m_lipiRoot + PROJECTS_PATH_STRING +
		                 tempControlInfo.projectName + PROFILE_PATH_STRING +
		                 tempControlInfo.profileName + SEPARATOR + BOXFLD + CONFIGFILEEXT;

	readClassifierConfig();


	LOG(LTKLogger::LTK_LOGLEVEL_DEBUG)
	    <<"Entering: BoxedFieldRecognizer::BoxedFieldRecognizer(const LTKControlInfo& )"
	    <<endl;

	//Creating the shape recognizer object
	if((errorCode = createShapeRecognizer(m_boxedShapeProject, m_boxedShapeProfile,&m_shapeRecognizer)) != SUCCESS)
	{
		 LOG(LTKLogger::LTK_LOGLEVEL_ERR)
        <<"Error: BoxedFieldRecognizer::BoxedFieldRecognizer(const LTKControlInfo&)"<<endl;

	   throw LTKException(errorCode);
	}

	if(m_shapeRecognizer == NULL)
	{
		LOG(LTKLogger::LTK_LOGLEVEL_ERR)
		    <<"Error : "<< ECREATE_SHAPEREC <<":"<< getErrorMessage(ECREATE_SHAPEREC)
            <<" BoxedFieldRecognizer::BoxedFieldRecognizer(const LTKControlInfo&)" <<endl;

		throw LTKException(ECREATE_SHAPEREC);
	}

	//loading the model data file
	if( (errorCode = (m_shapeRecognizer->loadModelData())) != SUCCESS )
	{
		m_module_deleteShapeRecognizer(m_shapeRecognizer);
		m_shapeRecognizer = NULL;

		LOG(LTKLogger::LTK_LOGLEVEL_ERR)
        	<<"Error: BoxedFieldRecognizer::BoxedFieldRecognizer(const LTKControlInfo&)"<<endl;

		throw LTKException(errorCode);
	}

	m_numCharsProcessed = 0;  //initializing number of characters processed
	m_numTracesProcessed = 0; //initializing number of traces processed


	LOG(LTKLogger::LTK_LOGLEVEL_DEBUG)
	    <<"Exiting: BoxedFieldRecognizer::BoxedFieldRecognizer(const LTKControlInfo&)"
	    <<endl;
}
int featurefilewriter::Initalize(const string& cfgFilepath,
								 const string& strLipiPath,
								 const string& strOutputPath)
{
	try
	{
		LTKControlInfo tmpControlInfo;
		tmpControlInfo.lipiRoot = strLipiPath;
		tmpControlInfo.toolkitVersion = SUPPORTED_MIN_VERSION;
		tmpControlInfo.cfgFilePath = cfgFilepath;
		
	    if( (tmpControlInfo.lipiRoot).empty() )
	    {
	        throw LTKException(ELIPI_ROOT_PATH_NOT_SET);
	    }
	    if ( tmpControlInfo.toolkitVersion.empty() )
	    {
	        throw LTKException(ENO_TOOLKIT_VERSION);
	    }
	    if ( tmpControlInfo.cfgFilePath.empty() )
	    {
	        throw LTKException(EINVALID_CFG_FILE_ENTRY);
	    }

	    assignDefaultValues();

	    m_lipiRootPath = tmpControlInfo.lipiRoot;
	    m_nnCfgFilePath = cfgFilepath;
		m_FDTFilePath = strOutputPath;

	    int errorCode = initializePreprocessor(tmpControlInfo,&m_ptrPreproc);

	    if( errorCode != SUCCESS)
	    {
	        LOG(LTKLogger::LTK_LOGLEVEL_ERR) << "Error: " << errorCode << " " <<
	            "featurefilewriter::Initalize()" <<endl;
	        throw LTKException(errorCode);
	    }


		//Reading configuration file
	    errorCode = readClassifierConfig();

	    if( errorCode != SUCCESS)
	    {
			cout<<endl<<"Encountered error in readClassifierConfig"<<endl;
	        LOG(LTKLogger::LTK_LOGLEVEL_ERR) << "Error: " << 
				errorCode << " " <<
	            "featurefilewriter::Initalize()" <<endl;
	        throw LTKException(errorCode);
	    }

	    errorCode = initializeFeatureExtractorInstance(tmpControlInfo);

	    if( errorCode != SUCCESS)
	    {
	        LOG(LTKLogger::LTK_LOGLEVEL_ERR) << "Error: " << errorCode << " " <<
	            "featurefilewriter::Initalize()" <<endl;
	        throw LTKException(errorCode);
	    }

	}
	catch(LTKException e)
	{
		deletePreprocessor();

		//Unloading the feature Extractor instance
	    deleteFeatureExtractorInstance();

		if(NULL != m_OSUtilPtr)
		{
			delete m_OSUtilPtr;
			m_OSUtilPtr = NULL;
		}
		throw e;
	}

	return 0;
}
Exemple #12
0
int HolisticRecognizer::train(const string& trainingList, string& strModelDataHeaderInfoFile, string &comment, string &dataset, const string &inFileType)
{
	LOG( LTKLogger::LTK_LOGLEVEL_DEBUG) << "Entered HolisticRecognizer::train"  << endl;

	string path;							//	file path of a ink file

	string shapeId;							//	shape id of the ink file

	int shapeCount = 0;						//	count of the no. of shape samples read so far

	int prevShape = 0;						//	shape id of the previous shape

	int numStrokes = 0;						//	number of strokes in a particular character

	int numFeatures = 0;					//	number of features being used

	int	traceIndex;							//	variable to loop over all traces of the trace group

	bool firstFileFlag = true;				//	flag is used for writing the number of features in the dat file

	LTKCaptureDevice captureDevice;

	LTKScreenContext screenContext;

	int iMajor, iMinor, iBugFix;			// Version identifiers

	char versionInfo[VERSION_STR_LEN];					// Holds the version information.

    string version;	///, pca="PCA";


	//	opening the file containing the training list of each of the shapes	
	
	ifstream in(trainingList.c_str());

	//	throwing error if unable to open the training list file

	if(!in)
	{
		throw LTKException(ETRAINLIST_FILE_OPEN);
	}

	//	opening the output reference model file

	ofstream out(m_referenceModelFile.c_str(),ios::out);

	// throwing error if unable to open the reference model file

	if(!out)
	{
		throw LTKException(EMODEL_DATA_FILE_OPEN);
	}

	//	writing the header information into the reference model file

	out << m_numShapes << endl;

	LOG( LTKLogger::LTK_LOGLEVEL_DEBUG) << "m_numShapes = " << m_numShapes << endl;

	//	reading the ink file paths from the training list file
	
	while(in)
	{
		
		getline(in, path, ' ');

		getline(in, shapeId);

		if(atoi(shapeId.c_str()) != prevShape)
		{
			++shapeCount;

			prevShape = atoi(shapeId.c_str()); 
		}

		// checking for end of training data

		if(shapeCount == m_numShapes)
		{
			break;
		}

		LTKTraceGroup traceGroup;

		float2DVector featureVector;

		cout << path ;

		// reading the ink files

		LTKInkFileReader::readUnipenInkFile(path, traceGroup, captureDevice, screenContext);

		numStrokes = traceGroup.getNumTraces();
		
		// extracting features from the traceGroup

		m_featureExtractorObj.extractFeatures(traceGroup, captureDevice, screenContext, ltkShapeRecPtr, featureVector);

		// writing the number of features in the dat file only the first time

		if (firstFileFlag==true)
		{
			numFeatures = featureVector.at(0).size();			

			out << numFeatures << endl;
		}

		firstFileFlag = false;

		// writing the shapeId and the corresponding features in the dat file

		out << shapeId << " ";

		out << numStrokes << " ";
		
		for(traceIndex = 0; traceIndex < numStrokes; ++traceIndex)
		{
			for(int loopIndex = 0; loopIndex < numFeatures; ++loopIndex)
			{
				out << featureVector.at(traceIndex).at(loopIndex) << " ";
			}
		}

		out << endl;
		
	}//while(in)

	//	closing the training list file and returning

	in.close();

	out.close();

	getCurrentVersion(&iMajor,&iMinor,&iBugFix);

	sprintf(versionInfo, "%d.%d.%d",iMajor, iMinor, iBugFix);

	version = versionInfo;

	headerInfo[RECVERSION] = version;
	string algoName = "holistic";
	headerInfo[RECNAME] = algoName;
	headerInfo[COMMENT]=comment;
	headerInfo[DATASET]=dataset;
	if(errorCode = cheSumGen.addHeaderInfo(strModelDataHeaderInfoFile, m_referenceModelFile, headerInfo))
	{
		return errorCode;
	}

	LOG( LTKLogger::LTK_LOGLEVEL_DEBUG) << "Holistic Engine Trained"  << endl;

	LOG( LTKLogger::LTK_LOGLEVEL_DEBUG) << "Exiting HolisticRecognizer::train"  << endl;

	return SUCCESS;
}
Exemple #13
0
		void LTKGrammar::initialize(string grammarFileName)
		{
			LOG(LTKLogger::LTK_LOGLEVEL_DEBUG) << "Entering LTKGrammar::intialize" <<endl;
			vector<string> symbols;		//holds the terminals and productions
			vector<string> terminals;	//holds terminals

			ifstream input(grammarFileName.c_str());

			if(input)
			{
				bool gotStartSymbol=false;

				//reading the grammar file line by line
				while(!input.eof())
				{
					string eachLine;	//holds each line of the grammar file

					getline(input,eachLine);

					if(!eachLine.empty())
					{

						//if the line is production definition and NOT symbol ID map
						if(eachLine.find_first_of("{")==eachLine.npos)
						{
							eachLine=trim(eachLine);	//trim is called everwhere to remove leading and trailing spaces of the string
							
							vector<string> ruleLeftRight;	//holds the CFG rule (Production name and its values)

							LTKStringUtil::tokenizeString(eachLine, "=", ruleLeftRight);

							vector<string> termsVec;	//holds each expression of the production value

							LTKStringUtil::tokenizeString(trim(ruleLeftRight[1]), "|", termsVec);

							vector<vector<string> > cfgMapValue;	//Holds all value expressions for a production

							for(int i=0;i<termsVec.size();++i)
							{
								vector<string> termContents;

								LTKStringUtil::tokenizeString(trim(termsVec[i]), ",", termContents);

								for(int j=0;j<termContents.size();++j)
								{
									termContents[j]=trim(termContents[j]);
									bool containsSymbol=false;

									//making sure the list of terminals and production names in the symbols vector is unique
									for(int k=0;k<symbols.size();++k)
									{
										if(symbols[k]==termContents[j])	//if it already contains
										{

											containsSymbol=true;
											break;
										}
										
									}
									if(!containsSymbol)	//if it doesn't contain
									{

										symbols.push_back(termContents[j]);
									}
								}
								cfgMapValue.push_back(termContents);
							}

							//populating the m_cfgMap which represents the CFG
							m_cfgMap.insert(make_pair(trim(ruleLeftRight[0]),cfgMapValue));

							if(!gotStartSymbol)
							{
								m_startSymbol=trim(ruleLeftRight[0]);	//getting the start symbol of the CFG
								gotStartSymbol=true;
							}

						}
						else	//It is map for symbol IDs
						{
							vector<string> keyValue;	//holds the terminal name

							LTKStringUtil::tokenizeString(eachLine, "=", keyValue);

							keyValue[0]=trim(keyValue[0]);
							keyValue[1]=trim(keyValue[1]);
							keyValue[1]=keyValue[1].substr(1,keyValue[1].length()-2);	//getting the comma-delimited symbol IDs

							vector<string> stringIDs;

							LTKStringUtil::tokenizeString(keyValue[1], ",", stringIDs);

							vector<int> ids(stringIDs.size());

							//converting the string symbol IDs to integer symbol IDs
							for(int k=0;k<ids.size();++k)
							{
								ids[k]=atoi(trim(stringIDs[k]).c_str());
								m_idTerminalsMap.insert(make_pair(ids[k],keyValue[0]));	//constructing the symbol ID to terminal map
							}
							
							m_terminalsIDMap.insert(make_pair(keyValue[0],ids));	//constructing the terminals to symbol ID map
							
							//constructing unique list of terminal names
							bool containsTerminal=false;
							for(int a=0;a<terminals.size();++a)
							{
								if(terminals[a]==keyValue[0])
								{
									containsTerminal=true;
									break;
								}

							}
							if(!containsTerminal)
							{
								terminals.push_back(keyValue[0]);
							}

						}
					}

				}

				input.close();
			}
			else
			{
				LOG(LTKLogger::LTK_LOGLEVEL_ERR) << "Grammar file "+grammarFileName+" does not exist!" <<endl;
				throw LTKException(EGRAMMER_FILE_NOT_EXIST);
			}

			bool valuesProvided=false;

			//checking whether list of symbol IDs is given for all the terminals
			for(int s=0;s<symbols.size();++s)
			{
				if(m_cfgMap.find(symbols[s])==m_cfgMap.end())	//if the term is not a production
				{
					valuesProvided=false;
					for(int v=0;v<terminals.size();++v)
					{
						if(terminals[v]==symbols[s])	//value provided for the terminal
						{
							valuesProvided=true;
							break;
						}
					}
					if(!valuesProvided)	//The term is not a production and values (list of symbol IDs) are not provided for it
					{
						LOG(LTKLogger::LTK_LOGLEVEL_ERR) << "Values for the terminal '"+symbols[s]+"' is not provided!" <<endl;
						throw LTKException(EVALUES_NOT_PROVIDED);

					}
				}
			}

			if(!valuesProvided)
			{
				LOG(LTKLogger::LTK_LOGLEVEL_ERR) << "No productions or terminals identified in the CFG. Please check the CFG format" <<endl;
				throw LTKException(ECONFIG_FILE_FORMAT);
				return;
			}

			findPossiblePaths();	//finds all possible paths for the CFG with the start symbol

			//To determin unique paths
			vector<vector<string> > uniquePaths;
			for(int i=0;i<m_paths.size();++i)
			{
				bool exists=false;
				for(int j=0;j<uniquePaths.size();++j)
				{
					if(uniquePaths[j]==m_paths[i])
					{
						exists=true;
						break;
					}
				}
				if(!exists)
				{
					uniquePaths.push_back(m_paths[i]);
				}
			}

			m_paths=uniquePaths;	//re-assigning unique paths to the paths determined

            isInitialized=true;
          
			LOG(LTKLogger::LTK_LOGLEVEL_DEBUG) << "Exiting LTKGrammar::intialize" <<endl;

		}
Exemple #14
0
	bool LTKGrammar::replacePaths(string term,vector<vector<string> >& originalVector,vector<vector<string> >& replaceVector)
	{
		
		LOG(LTKLogger::LTK_LOGLEVEL_DEBUG) <<"Entering LTKGrammar::replacePaths" <<endl;

		bool found=false;	//indicates whether is search term is found

		int iValue=0,jValue=0;	//indices whether the term is found

		bool checkedForRecursion=false;	

		int recursionCount=0;	//indicates how many times the term replacement happens

		while(true)
		{
			++recursionCount;

			if(recursionCount>150)
			{
				LOG(LTKLogger::LTK_LOGLEVEL_ERR) << "Cyclic dependency exists! Unable to find paths!" <<endl;
				throw LTKException(ECYCLIC_DEPENDENCY);
				return false;
			}
		
			bool foundTerm=false;

			//searching for the term in the paths determined so far			
			for(int i=iValue;i<originalVector.size();++i)
			{

				for(int j=0;j<originalVector[i].size();++j)
				{
					if(originalVector[i][j]==term)
					{
						iValue=i;
						jValue=j;
						if(!checkedForRecursion)
						{
							checkedForRecursion=true;
							for(int c=0;c<replaceVector.size();++c)
							{
								for(int d=0;d<replaceVector[c].size();++d)
								{
									if(replaceVector[c][d]==term)
									{
										LOG(LTKLogger::LTK_LOGLEVEL_ERR) << "Cyclic dependency exists! Unable to find paths!" <<endl;
										throw LTKException(ECYCLIC_DEPENDENCY);
										return false;
									}
								}
							}
						}
						originalVector[i].erase(originalVector[i].begin()+j);
						foundTerm=true;
						break;
					}
				}
				if(foundTerm)
				{
					break;
				}
			}
			if(foundTerm)	//Replacing the found term with its production values
			{
				originalVector.insert(originalVector.begin()+iValue+1,replaceVector.size()-1,*(originalVector.begin()+iValue));
							
				for(int k=iValue;k<(iValue+replaceVector.size());++k)
				{
					originalVector[k].insert(originalVector[k].begin()+jValue,replaceVector[k-iValue].begin(),replaceVector[k-iValue].end());

				}
				
				found=true;

				continue;
			}
			else
			{
				break;
			}
		}

		LOG(LTKLogger::LTK_LOGLEVEL_DEBUG) << "Exiting LTKGrammar::replacePaths" <<endl;

		return found;
	}