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

📄 graphbuilderaiml.cpp

📁 AIML的实现
💻 CPP
📖 第 1 页 / 共 4 页
字号:
		LOG_BOT_METHOD("StringPimpl GraphBuilderAIML::getResponse(const char * const input)");
		logging("<Input> input:" + String(input));

		m_previousSrai.clear();
		StringPimpl s = getResponseInternal(input);

		/*
		 * We have to call this in case a learn
		 * tag was encountered and it added files
		 * to the internal queue's.  The reason that
		 * a learn tag cannot directly call this is that
		 * it would invalidate iterators and cause a crash.
		 * Thus we have to create the graph here.
		 */
		createGraph();
		return s;
	}
	catch(exception &e)
	{
		throw ExceptionImpl(e.what());
	}
}

void GraphBuilderAIML::setAIMLValidation(bool trueOrFalse)
	throw(Exception &)
{
	try
	{
		LOG_BOT_METHOD("void GraphBuilderAIML::setAIMLValidation(bool trueOrFalse)");

		if(trueOrFalse == true)
		{
			logging(String("<Input> trueOrFalse:") + "true");
			m_setAIMLValidation= true;
		}
		else
		{
			logging(String("<Input> trueOrFalse:") + "false");
			m_setAIMLValidation = false;
		}
	}
	catch(exception &e)
	{
		throw ExceptionImpl(e.what());
	}
}

void GraphBuilderAIML::removePunctuation(String &stringToRemove)
//	throw(Exception &)
{

	//I use this to set reserve big enough for one character
	String s("x");
	for(unsigned int i = 0; i < m_sentenceSplitters.size(); ++i)
	{
		s[0] = m_sentenceSplitters.at(i);
		erase_all(stringToRemove, s);
	}

	const String comma(",");
	erase_all(stringToRemove, comma);
}

void GraphBuilderAIML::setThat(const char * const that)
	throw(Exception &)
{
	typedef tokenizer<char_separator<char> > tokenize;
	typedef tokenize::const_iterator CI;

	try
	{	
		String stringOfThat(that);
		if(!stringOfThat.empty() && stringOfThat != "")
		{
			m_thatString = that;

			if(!(m_sentenceSplitters.empty()))
			{
				char_separator<char> sep(m_sentenceSplitters.c_str());
				String temp(that);
				tokenize tokens(temp, sep);
				
				for(CI it = tokens.begin(); it != tokens.end(); ++it)
				{
					m_thatString = *it;
				}

				removePunctuation(m_thatString);
			}
		}
	}
	catch(exception &e)
	{
		throw ExceptionImpl(e.what());
	}
}

void GraphBuilderAIML::setTopic(const char * const topic)
	throw(Exception &)
{
	try
	{
		String topicToStore(topic);
		removePunctuation(topicToStore);
		setPredicate("TOPIC", topicToStore.c_str());
	}
	catch(exception &e)
	{
		throw ExceptionImpl(e.what());
	}
}

StringPimpl GraphBuilderAIML::getThat() const
	throw(Exception &)
{
	try
	{
		return StringPimpl(m_thatString.c_str());
	}
	catch(exception &e)
	{
		throw ExceptionImpl(e.what());
	}
}

StringPimpl GraphBuilderAIML::getTopic() const
	throw(Exception &)
{
	try
	{
		return getPredicate("topic");
	}
	catch(exception &e)
	{
		throw ExceptionImpl(e.what());
	}
}

StringPimpl GraphBuilderAIML::getThatStar(const unsigned int &index) const
	throw(IllegalArgumentException &, Exception &)
{
	if(index > m_thatStar.size())
	{
		throw IllegalArgumentExceptionImpl("The index is greater than thatStar's size");
	}
	else
	{
		try
		{
			//To be on the safe side let us still use the exception throwing "at"
			return m_thatStar.at(m_thatStar.size() - index).c_str();
		}
		catch(exception &e)
		{
			throw ExceptionImpl(e.what());
		}
	}
}

StringPimpl GraphBuilderAIML::getTopicStar(const unsigned int &index) const
	throw(IllegalArgumentException &, Exception &)
{
	if(index > m_topicStar.size())
	{
		throw IllegalArgumentExceptionImpl("The index is greater than topicStar's size");
	}
	else
	{   
		try
		{
			//To be on the safe side let us still use the exception throwing "at"
			return m_topicStar.at(m_topicStar.size() - index).c_str();
		}
		catch(exception &e)
		{
			throw ExceptionImpl(e.what());
		}
	}
}

void GraphBuilderAIML::setUseThatStar()
//	throw(Exception &)
{
	m_useTopicStar = false;
	m_useThatStar = true;
}

void GraphBuilderAIML::setUseTopicStar()
//	throw(Exception &)
{
	m_useThatStar = false;
	m_useTopicStar = true;
}

void GraphBuilderAIML::setUseDefaultStar()
//	throw(Exception &)
{
	m_useThatStar = false;
	m_useTopicStar = false;
}

void GraphBuilderAIML::setPredicate(const char * const name, const char * const value)
	throw(Exception &)
{
	try
	{
		LOG_BOT_METHOD("void GraphBuilderAIML::setPredicate(String name, String value)");
		logging("<Input> name: " + String(name));
		logging("<Input> value: " + String(value));
		
		String upperCaseName(name);
		to_upper(upperCaseName);

		if(upperCaseName == "TOPIC")
		{
			logging("Detected a change in topic");
			String tempValue(value);
			trim(tempValue);

			if(tempValue == "")
			{
				logging("The value is empty, defaulting to * for topic");	
				m_predicates[upperCaseName] = "*";
			}
			else
			{
				logging("Normal textual change in topic");
				m_predicates[upperCaseName] = tempValue;
			}
		}
		else
		{
			logging("There is no change in topic.  This is just a normal predicate");
			m_predicates[upperCaseName] = value;
		}
	}
	catch(exception &e)
	{
		throw ExceptionImpl(e.what());
	}
}

bool GraphBuilderAIML::predicateMatch(const char * const predicateName, const char * const aimlPattern) const
	throw(Exception &) 
{
	try
	{
		LOG_BOT_METHOD("bool GraphBuilderAIML::predicateMatch(const char * const predicateName, const char * const aimlPattern)");
		logging("<Input> predicateName:" + String(predicateName));
		logging("<Input> aimlPattern:" + String(aimlPattern));

		StringPimpl predicateValue = getPredicate(predicateName);

		if(!predicateValue.empty())
		{
			logging("predicate Value:" + String(predicateValue.c_str()));	
			typedef tokenizer<char_separator<char> > tokenize;
			typedef tokenizer<char_separator<char> >::const_iterator CI;

			shared_ptr<Template> templateToAdd(new Template);
			templateToAdd->addCharacters(predicateName);
			QueueString wordStack;

			char_separator<char> sep(" ");
			String stringToTok(aimlPattern);
			tokenize pathTokens(stringToTok, sep);

			CI it;
			for(it = pathTokens.begin(); it != pathTokens.end(); ++it)
			{
				logging("Pushing onto word stack: " + *it);
				wordStack.push(*it);
			}	

			wordStack.push("<THAT>");
			wordStack.push("*");
			wordStack.push("<TOPIC>");
			wordStack.push("*");

			GraphBuilderInternalNoOps noOpsGraphBuilder;
			NodeMapper predicateNodeMapper(noOpsGraphBuilder);
			predicateNodeMapper.add(templateToAdd, wordStack);
			String s = predicateNodeMapper.getTemplateString(String(predicateValue.c_str()));

			if(!s.empty())
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		else
		{
			logging("Warning, returned empty string on the predicate Name.  The name does not exist");
			return false;
		}
	}
	catch(exception &e)
	{
		throw ExceptionImpl(e.what());
	}
}

StringPimpl GraphBuilderAIML::getPredicate(const char * const name) const
	throw(Exception &)
{
	try
	{
		LOG_BOT_METHOD("String GraphBuilderAIML::getPredicate(String name)");
		logging("<Input>:" + String(name));
		typedef MapStringString::const_iterator CI;

		String upperCaseName(name);
		to_upper(upperCaseName);

		CI it = m_predicates.find(upperCaseName); 

		if(it != m_predicates.end())
		{
			logging("Predicate was Found and it's value is:" + it->second);
			return it->second.c_str();
		}
		else
		{
			logging("Predicate was not found");
			return String().c_str();
		}	
	}
	catch(exception &e)
	{	
		throw ExceptionImpl(e.what());
	}
}


void GraphBuilderAIML::createGraph()
	throw(XMLErrorException &, Exception &)
{
	try
	{
		LOG_BOT_METHOD("void GraphBuilderAIML::createGraph()");
		
		/*
		 * Iterate and add all the files in the file queue
		 */
		typedef MapStringBool::iterator I;
		for(I i = m_filesGraphed.begin(); i != m_filesGraphed.end(); ++i)
		{
			if(i->second == false)
			{
				logging(i->first + " has not been added to graph yet"); 
				Transcode transcodedString(i->first.c_str());
				LocalFileInputSource lfi(transcodedString.getXmlChar());
				makeGraph(lfi, i->first);
				i->second = true;
			}
			else
			{	//Do nothing
				logging(i->first + " has been added to the graph already");
				logging("Just returning, without doing anything");
			}
		}
		
		/*
		 * Iterate and all the strings from the string queue
		 */
		for(I i = m_stringsGraphed.begin(); i != m_stringsGraphed.end(); ++i)
		{
			if(i->second == false)
			{
				logging(i->first + " has not been added to the graph yet");
				MemBufInputSource mis((const XMLByte*)i->first.c_str(), i->first.size(), "StringInMemory", false);
				makeGraph(mis, "AIML String in memory");
				i->second = true;
			}
			else
			{	//Do nothing
				logging("AIML string has been added to the graph already");
				logging("Just returning, without doing anything");
			}
		}

		/*
		 * Clear the AIML caches now that we fully loaded 
		 * everything available.
		 */
		m_stringsGraphed.clear();
		m_filesGraphed.clear();
	}
	catch(exception &e)
	{
		throw ExceptionImpl(e.what());
	}
}

void GraphBuilderAIML::makeGraph(const InputSource &source, const String &file)
//	throw(XMLErrorException &, Exception &)
{
	LOG_BOT_METHOD("void makeGraph(const String &file)");
	logging("adding file:" + file + " to graph");
	
	try 
	{
		if(m_setAIMLValidation && (!m_aimlSchema.empty()))
		{
			String SchemaLoc = "http://alicebot.org/2001/AIML-1.0.1 " + m_aimlSchema;

			m_AIMLparser->setDoSchema(true);
			m_AIMLparser->setDoValidation(true);    // optional.
			m_AIMLparser->setDoNamespaces(true);    // optional
			m_AIMLparser->setExternalSchemaLocation(SchemaLoc.c_str());
		}
	
		logging("Beginging parsing of file");
		m_callBacks->filePreLoad(file.c_str());
		m_AIMLparser->parse(source);
		
		m_callBacks->filePostLoad(file.c_str());
		logging("done parsing file");
	}
	catch (const XMLException& toCatch) 
	{
		Transcode message(toCatch.getMessage());
		String msg("XML Exception from within the parser:" + message.getString());
		logging(msg);
		throw XMLErrorExceptionImpl(msg.c_str());

	}
	catch (const SAXParseException& toCatch) 
	{
		Transcode message(toCatch.getMessage());
		String msg("XML Exception from within the parser:" + message.getString());
		logging(msg);	
		throw XMLErrorExceptionImpl(msg.c_str());
	}

}

void GraphBuilderAIML::initializeXerces()
//	throw(XMLException &, Exception &)
{
	LOG_BOT_METHOD("GraphBuilderAIML::initializeXerces()");
	
	logging("calling XMLPlaformUtils::Initialize");
	XMLPlatformUtils::Initialize();
}

void GraphBuilderAIML::clearStars()
//	throw(Exception &)
{
	m_star.clear();
	m_topicStar.clear();
	m_thatStar.clear();
}

StringPimpl GraphBuilderAIML::getStar(unsigned const int &index) const
throw(IllegalArgumentException &, Exception &)
{
	LOG_BOT_METHOD("StringPimpl GraphBuilderAIML::getStar(const int &index) const");

	if(index > m_star.size() || index < 0)
	{		
		logging("index is greater than star size");
		throw IllegalArgumentExceptionImpl("index is greater than star size");
	}
	else
	{	
		try
		{
			return m_star.at(m_star.size() - index).c_str();
		}
		catch(exception &e)
		{
			throw ExceptionImpl(e.what());
		}
	}
}

void GraphBuilderAIML::addStar(const StringPimpl &star)
//	throw(Exception &)
{
	LOG_BOT_METHOD("void GraphBuilderAIML::addStar(const StringPimpl &star)");

	if(star.empty())
	{
		logging("Warning star is empty");
	}
	else
	{	
		String starString(star.c_str());

		logging("<Input>:" + starString);
		if(m_useThatStar)
		{
			logging("Putting the input into That Star");
			m_thatStar.push_back(starString);
		}
		else if(m_useTopicStar)
		{

⌨️ 快捷键说明

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