// function for finding the best keyword from the database // this function also handles context and also verifies the // boundaries of keywords that are found inside the user input void Eliza::find_keyword() { response_list.clear(); std::string bestKey; bool bGeneralKey = 0; int bestPos = -1; int dbase_size = database.size(); for(int index = 0; index < dbase_size; ++index) { verify_context(database[index].contexts); if(good_context()) { keyword_list = database[index].keywords; size_t keyNum = keyword_list.size(); for(int i = 0; i < keyNum; ++i) { std::string thisKey = keyword_list[i]; std::string keyword = thisKey; trimLR(keyword, "_"); keyword.insert(0, " "); keyword.append(" "); if(thisKey == "*" && bestKey == "") { keyword = m_sInput; bGeneralKey = 1; } int thisPos = m_sInput.find(keyword); if(thisPos == std::string::npos) { continue; } verify_keyword_boundary(thisKey, thisPos); if(wrong_boundary()) { continue; } if(bGeneralKey) { thisKey = "*"; bGeneralKey = 0; } trimLR(thisKey, "_"); if(isGoodKey(thisKey, bestKey, thisPos, bestPos)) { if(thisKey.length() > bestKey.length()) { response_list = database[index].responses; } else { add_response(database[index].responses); } bestKey = thisKey; bestPos = thisPos; } } } } bestKey == "*" ? m_sKeyWord = m_sInput : m_sKeyWord = bestKey; }
// implementing the 'sentence transposition' feature void TechBot::changePT(std::string &str) { std::string buffer = " " + str + " "; bool bchangePTd = 0; for(int i = 0; i < transposListSize; ++i) { std::string first = transposList[i].first; std::string second = transposList[i].second; while(replace(buffer, first, second) != std::string::npos) { bchangePTd = 1; } } if( !bchangePTd ) { for(int i = 0; i < transposListSize; ++i ) { std::string first = transposList[i].first; std::string second = transposList[i].second; while(replace(buffer, second, first) != std::string::npos) ; } } trimLR(buffer, " "); str = buffer; }
void Eliza::save_user_name() { if(m_sKeyWord == "MY NAME IS" || m_sKeyWord == "YOU CAN CALL ME") { extract_suffix(); m_sUserName = m_sSuffix; trimLR(m_sUserName, " "); } }
void TechBot::RecogniseTopic() { TB_Subject.erase(); // resets subject variable RightClear(TB_Input, " "); trimLR(TB_KeyWord, "_"); size_t pos = TB_Input.find(TB_KeyWord); if(pos != std::string::npos) { TB_Subject = TB_Input.substr(pos + TB_KeyWord.length() + 1, TB_Input.length() - 1); } }
inline void Eliza::extract_suffix() { int pos = m_sInput.find(m_sKeyWord); m_sSuffix.erase(); if(pos != std::string::npos) { if(m_sSymbol == "*") { pos += m_sKeyWord.length(); m_sSuffix = m_sInput.substr(pos, m_sInput.length() - pos); } else if(m_sSymbol == "@") { m_sSuffix = m_sInput.substr(pos, m_sInput.length()); } else if(m_sSymbol == "%") { m_sSuffix = m_sPrevResponse; } trimLR(m_sSuffix, " "); } }