📄 graphbuilderaiml.cpp
字号:
logging("Putting the input into topic Star");
m_topicStar.push_back(starString);
}
else
{
logging("Putting the input into default/regular star");
m_star.push_back(starString);
}
}
}
GraphBuilderAIML::~GraphBuilderAIML()
{
LOG_BOT_METHOD("GraphBuilderAIML::~GraphBuilderAIML()");
///@todo put this into the adt
typedef map<String, CustomTagWithDelete>::iterator I;
for(I it = m_customTags.begin(); it != m_customTags.end(); ++it)
{
/*
* Use the custom tag to delete its self
*/
CustomTagWithDelete &tag = it->second;
(tag.m_deleteFunc)(tag.m_customTags);
///@todo figure out why this can't be free'd.
///@todo This can't be freed because of a dependency
//There's a dep where GraphBuilderAIML has a shared_ptr
//to the guts of the dll.
//FreeLibrary(tag.m_dllHandle);
}
}
GraphBuilder *GraphBuilderAIML::clone()
// throw(Exception &)
{
try
{
LOG_BOT_METHOD("GraphBuilder *GraphBuilderAIML::clone()");
return new GraphBuilderAIML(*this);
}
catch(exception &e)
{
throw ExceptionImpl(e.what());
}
}
StringPimpl GraphBuilderAIML::getPreviousBotResponse(const unsigned int &previousBotResponse, const unsigned int &sentence) const
throw(IllegalArgumentException &, Exception &)
{
try
{
///@todo Finish this with the sentence integer index.
if(previousBotResponse - 1 >= m_previousBotResponse.size())
{
logging("Error the index is wrong");
throw IllegalArgumentExceptionImpl("previousBotResponse is greater than the number of bot responses");
return StringPimpl("");
}
else
{
DequeString sentencePunct = m_previousBotResponse.at(previousBotResponse - 1);
if(sentence - 1 >= sentencePunct.size())
{
logging("Error the sentence index is wrong");
throw IllegalArgumentExceptionImpl("previousBotResponse's sentence is greater than the number of bot responses");
return StringPimpl("");
}
else
{
//Still use the "at" for safety checks
return sentencePunct.at(sentence - 1).c_str();
}
}
}
catch(exception &e)
{
throw ExceptionImpl(e.what());
}
}
StringPimpl GraphBuilderAIML::getPreviousBotResponsePunctuation(const unsigned int &previousBotResponse,
const unsigned int &sentence) const
throw(IllegalArgumentException &, Exception &)
{
try
{
///@todo Finish this with the sentence integer index.
if(previousBotResponse - 1 >= m_previousBotResponseWithPunct.size())
{
logging("Error the index is wrong");
throw IllegalArgumentExceptionImpl("previousBotResponse is greater than the number of bot responses");
return StringPimpl();
}
else
{
DequeString sentencePunct = m_previousBotResponseWithPunct.at(previousBotResponse - 1);
if(sentence - 1 >= sentencePunct.size())
{
logging("Error the sentence index is wrong");
throw IllegalArgumentExceptionImpl("previousBotResponse's sentence is greater than the number of bot responses");
return StringPimpl();
}
else
{
//Still use the "at" for safety checks
return sentencePunct.at(sentence - 1).c_str();
}
}
}
catch(exception &e)
{
throw ExceptionImpl(e.what());
}
}
StringPimpl GraphBuilderAIML::getPreviousUserInput(const unsigned int &previousUserInput, const unsigned int &sentence) const
throw(IllegalArgumentException &, Exception &)
{
try
{
///@todo Finish this with the sentence integer index.
if(previousUserInput - 1 >= m_previousUserInput.size())
{
logging("Error the index is wrong");
throw IllegalArgumentExceptionImpl("previousUserInput is greater than the number of previous user inputs");
}
else
{
//Still use the "at" for safety checks
return StringPimpl(m_previousUserInput.at(previousUserInput - 1).c_str());
}
}
catch(exception &e)
{
throw ExceptionImpl(e.what());
}
}
StringPimpl GraphBuilderAIML::getBotPredicate(const char * const name) const
throw(Exception &)
{
try
{
LOG_BOT_METHOD("String GraphBuilderAIML::getBotPredicate(String name)");
logging("<Input>:" + String(name));
typedef MapStringString::const_iterator CI;
String upperCaseName(name);
to_upper(upperCaseName);
CI it = m_botPredicates.find(upperCaseName);
if(it != m_botPredicates.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::setBotPredicate(const char * const name, const char * const value)
throw(Exception &)
{
try
{
LOG_BOT_METHOD("void GraphBuilderAIML::setBotPredicate(String name, String value)");
logging("<Input> name: " + String(name));
logging("<Input> value: " + String(value));
String upperCaseName(name);
to_upper(upperCaseName);
m_botPredicates[upperCaseName] = value;
}
catch(exception &e)
{
throw ExceptionImpl(e.what());
}
}
void GraphBuilderAIML::setBotConfigurationSchema(const char *const schema)
throw(Exception &)
{
try
{
LOG_BOT_METHOD("void GraphBuilderAIML::setBotConfigurationSchema(const char *const schema)");
logging("<Input> schema:" + String(schema));
logging("file exists, using it as the schema");
m_configurationSchema = schema;
}
catch(exception &e)
{
throw ExceptionImpl(e.what());
}
}
void GraphBuilderAIML::setBotConfigurationValidation(const bool trueOrFalse)
throw(Exception &)
{
try
{
if(trueOrFalse == true)
{
logging(String("<Input> trueOrFalse:") + "true");
m_doConfigurationValidation = true;
}
else
{
logging(String("<Input> trueOrFalse:") + "false");
m_doConfigurationValidation = false;
}
}
catch(exception &e)
{
throw ExceptionImpl(e.what());
}
}
void GraphBuilderAIML::parseSubstitutionFile(const char *const fileName)
throw(XMLErrorException &, FileNotFoundException &, Exception &)
{
try
{
LOG_BOT_METHOD("void GraphBuilderAIML::parseSubstitutionFile(const char *const fileName)");
logging("<Input> file: " + String(fileName));
parseConfigurationFile(fileName);
}
catch(exception &e)
{
throw ExceptionImpl(e.what());
}
}
void GraphBuilderAIML::setInputSubstitution(const char *const find, const char *const replace)
throw(IllegalArgumentException &, Exception &)
{
try
{
m_inputSubstitutions.push_back(SubstitutionRegEx(regex(find, boost::regex::icase), replace));
}
catch(bad_expression &)
{
throw IllegalArgumentExceptionImpl("Bad expression in the find regular expression");
}
catch(runtime_error &)
{
throw IllegalArgumentExceptionImpl("Run time error in the find regular expression");
}
catch(exception &e)
{
throw ExceptionImpl(e.what());
}
}
StringPimpl GraphBuilderAIML::inputSubstitute(const StringPimpl &input)
// throw(Exception &)
{
LOG_BOT_METHOD("StringPimpl GraphBuilderAIML::inputSubstitute(const String &input)");
typedef vector<SubstitutionRegEx>::const_iterator CI;
String returnResponse(input.c_str());
for(CI it = m_inputSubstitutions.begin(); it != m_inputSubstitutions.end(); ++it)
{
replace_all_regex(returnResponse, (*it).m_find, (*it).m_replace);
}
return returnResponse.c_str();
}
void GraphBuilderAIML::setGenderSubstitution(const char *const find, const char *const replace)
throw(IllegalArgumentException &, Exception &)
{
try
{
m_genderSubstitutions.push_back(SubstitutionRegEx(regex(find, boost::regex::icase), replace));
}
catch(bad_expression &)
{
throw IllegalArgumentExceptionImpl("Bad expression in the find regular expression");
}
catch(runtime_error &)
{
throw IllegalArgumentExceptionImpl("Run time error in the find regular expression");
}
catch(exception &e)
{
throw ExceptionImpl(e.what());
}
}
void GraphBuilderAIML::setPersonSubstitution(const char *const find, const char *const replace)
throw(IllegalArgumentException &, Exception &)
{
try
{
m_personSubstitutions.push_back(SubstitutionRegEx(regex(find, boost::regex::icase), replace));
}
catch(bad_expression &)
{
throw IllegalArgumentExceptionImpl("Bad expression in the find regular expression");
}
catch(runtime_error &)
{
throw IllegalArgumentExceptionImpl("Run time error in the find regular expression");
}
catch(exception &e)
{
throw ExceptionImpl(e.what());
}
}
void GraphBuilderAIML::setPerson2Substitution(const char *const find, const char *const replace)
throw(IllegalArgumentException &, Exception &)
{
try
{
m_person2Substitutions.push_back(SubstitutionRegEx(regex(find, boost::regex::icase), replace));
}
catch(bad_expression &)
{
throw IllegalArgumentExceptionImpl("Bad expression in the find regular expression");
}
catch(runtime_error &)
{
throw IllegalArgumentExceptionImpl("Run time error in the find regular expression");
}
catch(exception &e)
{
throw ExceptionImpl(e.what());
}
}
StringPimpl GraphBuilderAIML::genderSubstitute(const StringPimpl &input) const
throw(Exception &)
{
LOG_BOT_METHOD("String GraphBuilderAIML::genderSubstitute(const String &input) const");
try
{
return substitute(input, m_genderSubstitutions);
}
catch(exception &e)
{
throw ExceptionImpl(e.what());
}
}
StringPimpl GraphBuilderAIML::personSubstitute(const StringPimpl &input) const
throw(Exception &)
{
LOG_BOT_METHOD("String GraphBuilderAIML::personSubstitute(const StringPimpl &input) const");
try
{
return substitute(input, m_personSubstitutions);
}
catch(exception &e)
{
throw ExceptionImpl(e.what());
}
}
StringPimpl GraphBuilderAIML::person2Substitute(const StringPimpl &input) const
throw(Exception &)
{
LOG_BOT_METHOD("String GraphBuilderAIML::person2Substitute(const StringPimpl &input) const");
try
{
return substitute(input, m_person2Substitutions);
}
catch(exception &e)
{
throw ExceptionImpl(e.what());
}
}
StringPimpl GraphBuilderAIML::substitute(const StringPimpl &input, const std::vector<SubstitutionRegEx> &substitution) const
// throw(Exception &)
{
LOG_BOT_METHOD("GraphBuilderAIML::substitute(const char * const input, const std::vector<SubstitutionRegEx> &substitution)");
String inputString(input.c_str());
///@todo put this into the adt
map<int, pair<String, String> > mapIntegerMapStringString;
typedef find_iterator<string::iterator> string_find_iterator;
typedef vector<SubstitutionRegEx>::const_iterator CI;
try
{
for(CI it = substitution.begin(); it != substitution.end(); ++it)
{
for(string_find_iterator ij=
make_find_iterator(inputString, regex_finder(it->m_find));
ij!=string_find_iterator();
++ij)
{
/*
* Get the index of where the iterator is
* at in the string
*/
String::difference_type stringIndex;
stringIndex = distance(inputString.begin(), ij->begin());
/*
* Store the string to be replaced
* along with the string that will
* be replacing it. Store as the key
* the actual placement of where the
* string to be replaced is located at.
* This way we can iterate over the
* string and replace the strings in
* order.
*
* Only insert into the map if it already does
* not have a replacement for that exact part of
* the string. This gives the replacements at the
* begining of the substitutions higher precedence
* then any that are lower.
*/
if(mapIntegerMapStringString.find(stringIndex) == mapIntegerMapStringString.end())
{
stringstream ss;
ss << *ij;
mapIntegerMapStringString[stringIndex] =
pair<String, String>(ss.str(), it->m_replace);
}
}
}
///@todo put this into the adt
typedef map<int, pair<String, String> >::const_iterator CIM;
/*
* Iterate over the string in order and replace the strings
* in order. Keep track using "diff" how much extra room
* to make or to subtract from the string as we replace the
* olds substrings with our new strings.
*/
int diff = 0;
int stringIndex = 0;
for(CIM it = mapIntegerMapStringString.begin(); it != mapIntegerMapStringString.end(); ++it)
{
int positionInTheString = it->first;
/*
* Check if the replacement string overrlaps with the
* current replacement. If it does then continue,
* otherwise we'd be stepping on the toes of the previous
* replacement.
*/
if(positionInTheString < stringIndex)
{
continue;
}
pair<String, String> wordToReplaceAndBeReplaced = it->second;
String wordToReplaceWith(wordToReplaceAndBeReplaced.second);
int sizeOfWordToReplace = wordToReplaceAndBeReplaced.first.size();
int diffBeforeReplacing = inputString.size();
inputString.replace(positionInTheString + diff, sizeOfWordToReplace, wordToReplaceWith);
diff += inputString.size() - diffBeforeReplacing;
stringIndex = positionInTheString + wordToReplaceWith.size();
logging("Word right now is:" + inputString);
}
logging("Word all finished replacing and returning" + inputString);
return inputString.c_str();
}
catch(bad_expression &)
{
throw ExceptionImpl("Bad expression with regular expression in GraphBuilderAIML::substitute");
}
catch(runtime_error &)
{
throw ExceptionImpl("RunTime error with regular expression in GraphBuilderAIML::substitute");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -