⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.cpp

📁 AIML的实现
💻 CPP
字号:
//Rebecca includes#include <rebecca/all.h>using namespace rebecca;//Std includes#include <iostream>#include <string>using namespace std;/** * My Custom CallBacks class. *  * This class inherits from Rebecca's  * CallBacks class and implements the  * callbacks for error reporting and  * informational reporting purposes. * * Although, I pain stakenly reimplemnt  * every single method, you don't have to. * Just pick and choose which ones you  * want to implement, and ignore the rest.   * All of the methods of CallBacks has a  * default do nothing implementation. */class myCallBacks : public CallBacks{	public:		/**		 * This is called for each AIML 		 * "Gossip" tag.		 *		 * I am just printing out the gossip.		 * You can do other things like store it 		 * in a file and then reload the file at 		 * startup as a type of persistance.		 */		void storeGossip(const char * const gossip) 		{ 			cout << "[Gossip: " << gossip << " ]" << endl;		}		/**		 * This is called for each AIML 		 * category that is loaded into 		 * memory.  		 *		 * Loadtime occurs whenver the call 		 * GraphBuilder::createGraph() is made.		 * For every 5000 categories loaded into		 * Rebecca's internal data structure 		 * this prints a output message about it.		 */		void categoryLoaded()		{			static int i = 0;			const int numberOfCategories = 5000;						/*			 * Clever way to say if "i" is a multiple 			 * of 5000 then print out the number of 			 * categories loaded so far.			 */			if(++i % numberOfCategories == 0)			{				cout << "[" << i << " categories loaded]" << endl;			}		}		/**		 * Before each AIML file is parsed this method is called.         *		 * \param fileName The name of the file about to be parsed.		 */		void filePreLoad(const char *const fileName)		{			cout << "[Loading      " << fileName << "]" << endl;		}				/**		 * After each AIML file is parsed, this method is called.         *		 * \param fileName The name of the file just parsed.		 */		void filePostLoad(const char *const fileName)		{			cout << "[Done loading " << fileName << "]" << endl;		}		/**		 * When the "srai" AIML tag is called, the text 		 * is sent to this method.		 *		 * Usually refered to as symbolic reduction, you 		 * can see what text is being re-fed back into the 		 * AIML GraphBuilder::getResponse() by AIML its self.		 *		 * \param symbol The text which is being sent back		 * into GraphBuilder::getResponse().		 */		void symbolicReduction(const char *const symbol)		{			cout << "Symbolic reduction: " << symbol << endl;		}		/**		 * A infinite symbolic reduction has occured and 		 * has been terminated.		 *		 * This method is called when symbolic reduction ends		 * up in a infinite loop and has been terminated.  This		 * is just to alert you to the fact.		 */		void infiniteSymbolicReduction()		{			cout << "[Infinite Symbolic reduction detected]" << endl;		}				/**		 * Sends you a message about a XMLParseError. 		 *		 * Either with AIML files or RebeccaAIML configuration		 * files.		 *		 * \param message The human readable message.		 */ 		virtual void XMLParseError(const char * const message) 		{ 			cout << message << endl;		} 		/**		 * Sends you a message about a XMLParseWarning. 		 *		 * Either with AIML files or RebeccaAIML configuration		 * files.		 *		 * \param message The human readable message.		 */ 		virtual void XMLParseWarning(const char * const message)		{ 			cout << message << endl;		} 		/**		 * Sends you a message about a XMLParseFatalError. 		 *		 * Either with AIML files or RebeccaAIML configuration		 * files.		 *		 * \param message The human readable message.		 */ 		virtual void XMLParseFatalError(const char * const message) 		{ 			cout << message << endl;		} 				/**		 * During runtime, the "thatStar" AIML tag's size is 		 * exceeded.		 *		 * Runtime is during a call to GraphBuilder::getResponse()		 */		virtual void thatStarTagSizeExceeded() 		{ 			cout << "[Warning thatStar Tag Size Exceeded]" << endl;		}				/**		 * During runtime, the "topicStar" AIML tag's size is 		 * exceeded.		 *		 * Runtime is during a call to GraphBuilder::getResponse()		 */		virtual void topicStarTagSizeExceeded() 		{ 			cout << "[Warning topicStar Tag Size Exceeded]" << endl;				}		/**		 * During runtime, the "star" AIML tag's size is 		 * exceeded.		 *		 * Runtime is during a call to GraphBuilder::getResponse()		 */		virtual void starTagSizeExceeded() 		{ 			cout << "[Warning star Tag Size Exceeded]" << endl;		}		/**		 * A AIML "Input" tag has a non number in its index attribute.		 *		 * This method will only be called during loadtime, GraphBuilder::createGraph().		 *		 * \param message The human readable message.		 */		virtual void inputTagNumericConversionError(const char * const message) 		{ 			cout << "inputTagNuermicConversionError:" << message << endl;				}		/**		 * During runtime, the "input" AIML tag's size is 		 * exceeded.		 *		 * Runtime is during a call to GraphBuilder::getResponse()		 */		virtual void inputTagSizeExceeded() 		{ 			cout << "[Warning input Tag Size Exceeded]" << endl;		} 		/**		 * A AIML "Star" tag has a non number in its index attribute.		 *		 * This method will only be called during loadtime, GraphBuilder::createGraph().		 *		 * \param message The human readable message.		 */		virtual void starTagNumericConversionError(const char * const message) 		{ 			cout << "starTagNuermicConversionError:" << message << endl;						} 		/**		 * During runtime, the "that" AIML tag's size is 		 * exceeded.		 *		 * Runtime is during a call to GraphBuilder::getResponse()		 */		virtual void thatTagSizeExceeded() 		{ 				}		/**		 * A AIML "That" tag has a non number in its index attribute.		 *		 * This method will only be called during loadtime, GraphBuilder::createGraph().		 *		 * \param message The human readable message.		 */		virtual void thatTagNumericConversionError(const char * const message) 		{ 			cout << "thatTagNumericConversionError:" << message << endl;		} 		/**		 * A AIML "TopicStar" tag has a non number in its index attribute.		 *		 * This method will only be called during loadtime, GraphBuilder::createGraph().		 *		 * \param message The human readable message.		 */		virtual void topicStarTagNumericConversionError(const char * const message) 		{ 				} 				/**		 * A AIML "thatStar" tag has a non number in its index attribute.		 *		 * This method will only be called during loadtime, GraphBuilder::createGraph().		 *		 * \param message The human readable message.		 */		virtual void thatStarTagNumericConversionError(const char * const message) { }};/** * Console version to query a AIML set. *  * I use the annotated alice data set that's  * provided as an example. */int main (int argc, char* args[]) {	try	{ 		/*		 * This is responsible for memory management of 		 * GraphBuilder.		 */		AimlFacade aiml;		/*		 * Get the GraphBuilder concrete class that 		 * was created inside of AimlFacade.		 * DO NOT try to delete GraphBuilder.  Let		 * AimlFacade handle that when it falls out 		 * of scope.		 */		GraphBuilder &builder = aiml.getGraphBuilder();				/*		 * Create an instantiation of our custom 		 * callbacks we created above.		 */		myCallBacks callback;		/* Give the address to Rebecca for usesage.		 * Rebecca DOES NOT delete it.  		 */		builder.setCallBacks(&callback);//		builder.addCustomTagLibrary("customGraphvizTagLayer");		cout << "[Rebecca loading]" << endl;		/* 		 * Set the schemas for the AIML XML (AIML.xsd)		 * and for Rebecca's own configuration files.		 * The schema's have to be relative to where the files		 * you are going to parse are going to be at.		 */		builder.setAIMLSchema("../../resources/schema/AIML.xsd");		builder.setCommonTypesSchema("../resources/schema/common-types.xsd");		builder.setBotConfigurationSchema("../resources/schema/bot-configuration.xsd");		/* 		 * Set that "yes" we do want to do XML validation on		 * both the AIML XML and Rebecca's own configuration 		 * files.		 */		builder.setAIMLValidation();		builder.setBotConfigurationValidation();		/*		 * Parse Rebecca's configuration files to setup 		 * Rebecca's ability to handle input subsitutions, 		 * what a sentence splitter is, and what bot properties		 * she should have.		 */		builder.parseSubstitutionFile("../../conf/substitutions.xml");		builder.parseSentenceSplitterFile("../../conf/sentence-splitters.xml");		builder.parsePropertiesFile("../../conf/properties.xml");				/*		 * Add the entire directory.  Every file that has the 		 * extension ".aiml" will be added to the internal queue 		 * for latter processing.		 *///		builder.addFile("../../aiml/annotated_alice/That.aiml");		builder.addDirectory("../../aiml/test");//		builder.addDirectory("../../aiml/testing");//		builder.addDirectory("../../aiml/basic_aiml");//		builder.addDirectory("../../aiml/aaa");//		builder.addFile("../../resources/testing/AIML.aiml");		//builder.addDirectory("../../aiml/custom_tags");		/*		 * No other files to add to the internal queue.  		 * So, let's create the AIML graph, the internal 		 * data structures.		 */		builder.createGraph();		//exit(0);		/*		 * Get the number of AIML categories loaded in total.		 */		int size = builder.getSize();		//Print out the number of categories loaded.		cout << endl << endl;		cout << "[Rebecca now fully loaded]" << endl;		cout << "[Number of categories loaded: " << size << "]" << endl;		cout << "[Type /exit to exit]" << endl << endl << endl;		/*		 * Get the botName which should be Rebecca since that is 		 * the name give in the configuration file properties.xml		 * which we parsed above.		 */		string botName = builder.getBotPredicate("name").c_str();		/*		 * Send a initial conversation of "connect" to 		 * annotated alice and get the response.		 */		StringPimpl response = builder.getResponse("connect");				//Send the initial opening line of the bot		cout << botName << " says: " << response.c_str() << endl;		/*		 * The main loop to get the input 		 * from the user until the user types '/exit'		 */		while(true)		{			string input;			cout << "You say> ";			//Get input from the user from the keyboard			getline(cin, input); 						if(input == "/exit")			{				/*				 * The user wants to exit so break				 * out of the while(true) loop				 */				break;			}			else //The user gave an input to the bot			{				//Here we get some internal Rebecca information.				cout << endl					<< "Internal information:" << endl 					<< "=====================" << endl					<< input << " : " 					<< builder.getThat().c_str() << " : "					<< builder.getTopic().c_str() << endl;				/*				 * Ahhh finally.  We give the user input to Rebecca Aiml's loaded 				 * AIML and get the response back.				 */				StringPimpl response = builder.getResponse(input.c_str());								cout << "=====================" << endl << endl;								//Print out what Rebecca says.				cout << botName << " says: " << response.c_str() << endl;			}		}	}	/* 	 * All the exceptions are grouped here but you 	 * might not want this since it's a bit harder 	 * to determine where they came from.	 */	catch(DirectoryNotFoundException &e)	{		cout << "[A Directory Was Not Found Terminating]" << endl;		cout << "[" << e.what() << "]" << endl;		return 1;	}	catch(FileNotFoundException &e)	{		cout << "[A File Was Not Found Terminating]" << endl;		cout << "[" << e.what() << "]" << endl;		return 1;	}	catch(IllegalArgumentException &e)	{		cout << "[IllegalArgument Found Terminating]" << endl;		cout << "[" << e.what() << "]" << endl;		return 1;	}	catch(InitializationException &e)	{		cout << "[Initialization Exception Found Terminating]" << endl;		cout << "[" << e.what() << "]" << endl;		return 1;	}	catch(XMLErrorException &e)	{		cout << "[XMLError Exception Found Terminating]" << endl;		cout << "[" << e.what() << "]" << endl;		return 1;	}	catch(Exception &e)	{		cout << "[An uknown exception occured, Terminating program]" << endl;		cout << "[" << e.what() << "]";		return 1;	}	//Everything smooth.  Exit normally.	return 0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -