diff options
Diffstat (limited to 'plugins/Boltun/src/Engine/TalkEngine.cpp')
-rw-r--r-- | plugins/Boltun/src/Engine/TalkEngine.cpp | 160 |
1 files changed, 80 insertions, 80 deletions
diff --git a/plugins/Boltun/src/Engine/TalkEngine.cpp b/plugins/Boltun/src/Engine/TalkEngine.cpp index c5cc859bb3..0f53403003 100644 --- a/plugins/Boltun/src/Engine/TalkEngine.cpp +++ b/plugins/Boltun/src/Engine/TalkEngine.cpp @@ -33,12 +33,12 @@ //#define EXCLUDE_SPECIAL_WORDS
#ifdef DEBUG_SHOW_VARIANTS
-extern void AddBotMessage(tstring s);
+extern void AddBotMessage(wstring s);
#endif
using namespace std;
-void TalkBot::UpdateStartChar(tstring &str)
+void TalkBot::UpdateStartChar(wstring &str)
{
if (!makeLowercase)
return;
@@ -46,21 +46,21 @@ void TalkBot::UpdateStartChar(tstring &str) size_t l = str.length();
if (l) {
// Answers starting with ' ' must remain unchanged.
- if (str[0] == _T(' ')) {
+ if (str[0] == ' ') {
str = str.substr(1);
return;
}
- TCHAR *strl = NEWTSTR_ALLOCA(str.c_str()), *stru = NEWTSTR_ALLOCA(str.c_str());
+ wchar_t *strl = NEWWSTR_ALLOCA(str.c_str()), *stru = NEWWSTR_ALLOCA(str.c_str());
CharLower(strl);
CharUpper(stru);
for (size_t i = 0; i < l; i++) {
- TCHAR cl = strl[i];
- TCHAR cu = stru[i];
+ wchar_t cl = strl[i];
+ wchar_t cu = stru[i];
if (i != l - 1) {
// Do not react to BLONDE ANSWERS
- TCHAR ncl = strl[i+1];
- TCHAR ncu = stru[i+1];
+ wchar_t ncl = strl[i+1];
+ wchar_t ncu = stru[i+1];
if (ncl != ncu && str[i+1] == ncu)
break;
}
@@ -84,27 +84,27 @@ TalkBot::~TalkBot() delete contactDatas;
}
-tstring TalkBot::GetInitMessage(MCONTACT contact)
+wstring TalkBot::GetInitMessage(MCONTACT contact)
{
ContactData* d = contactDatas->GetData(contact);
- tstring s = d->initial.GetString();
+ wstring s = d->initial.GetString();
contactDatas->PutData(contact);
return s;
}
-tstring TalkBot::ReplaceAliases(const tstring &message)
+wstring TalkBot::ReplaceAliases(const wstring &message)
{
- const TCHAR dividers[] = L" \t\n\r,./?\\|;:'\"~!#^&*()_-+=[{]}—\1";
- tstring sentence = message;
- tstring result;
- map<size_t, tstring> sm;
+ const wchar_t dividers[] = L" \t\n\r,./?\\|;:'\"~!#^&*()_-+=[{]}—\1";
+ wstring sentence = message;
+ wstring result;
+ map<size_t, wstring> sm;
//Find smiles
for (size_t i = 0; i < sentence.length() - 1; i++) {
unsigned max = (int)(sentence.length() - i);
if (max > mind.GetData()->maxSmileLen)
max = mind.GetData()->maxSmileLen;
for (unsigned j = max; j > 0; j--) {
- tstring item = sentence.substr(i, j);
+ wstring item = sentence.substr(i, j);
if (mind.GetData()->smiles.find(item) != mind.GetData()->smiles.end()) {
sm[i] = item;
sentence.replace(i, j, L"\1");
@@ -116,10 +116,10 @@ tstring TalkBot::ReplaceAliases(const tstring &message) bool hadQuestionSigns = false;
int it = 0;
while (it != len) {
- while (it != len && _tcschr(dividers, sentence[it])) {
- if (sentence[it] == _T('?'))
+ while (it != len && wcschr(dividers, sentence[it])) {
+ if (sentence[it] == '?')
hadQuestionSigns = true;
- map<size_t, tstring>::iterator smit;
+ map<size_t, wstring>::iterator smit;
if (sentence[it] == '\1') {
smit = sm.find(it);
result.append((*smit).second);
@@ -132,17 +132,17 @@ tstring TalkBot::ReplaceAliases(const tstring &message) break;
int start = it;
while (true) {
- while (it != len && !_tcschr(dividers, sentence[it]))
+ while (it != len && !wcschr(dividers, sentence[it]))
it++;
- if (it == len || sentence[it] != _T('-'))
+ if (it == len || sentence[it] != '-')
break;
//If we have-a-word-with-minus, we shouldn't split it
- if (_tcschr(dividers, sentence[it + 1]))
+ if (wcschr(dividers, sentence[it + 1]))
break;
it += 2;
}
- tstring str = sentence.substr(start, it - start);
- map<tstring, tstring>::const_iterator al = mind.GetData()->aliases.find(str);
+ wstring str = sentence.substr(start, it - start);
+ map<wstring, wstring>::const_iterator al = mind.GetData()->aliases.find(str);
if (al != mind.GetData()->aliases.end())
result.append((*al).second);
else
@@ -151,9 +151,9 @@ tstring TalkBot::ReplaceAliases(const tstring &message) return result;
}
-tstring TalkBot::AllReplies(const tstring &incomingMessage, ContactData *contactData, Level &maxValue, std::multimap<Level, tstring> &mm)
+wstring TalkBot::AllReplies(const wstring &incomingMessage, ContactData *contactData, Level &maxValue, std::multimap<Level, wstring> &mm)
{
- tstring res;
+ wstring res;
//Part 1
if (FindExact(contactData, incomingMessage, mind.GetData()->widelyUsed, res)) //widelyUsed
{
@@ -170,7 +170,7 @@ tstring TalkBot::AllReplies(const tstring &incomingMessage, ContactData *contact maxValue = LOOKSLIKE;
}
//Part 3
- vector<tstring> sentences;
+ vector<wstring> sentences;
SplitSectences(incomingMessage, sentences);
ValueChooser<> ch(sentences, true); //Using random order of sentences.
while ((res = ch.GetString()) != L"") {
@@ -197,7 +197,7 @@ tstring TalkBot::AllReplies(const tstring &incomingMessage, ContactData *contact maxValue = LOOKSLIKE;
}
//Part 6
- vector<tstring> keywords, otherwords;
+ vector<wstring> keywords, otherwords;
bool isQuestion;
SplitAndSortWords(res, keywords, otherwords, isQuestion);
//Part 7, 8
@@ -258,12 +258,12 @@ tstring TalkBot::AllReplies(const tstring &incomingMessage, ContactData *contact maxValue = FAIL;
}
}
- return tstring();
+ return wstring();
}
-TalkBot::MessageInfo* TalkBot::Reply(MCONTACT contact, tstring incomingMessage, bool saveChoice)
+TalkBot::MessageInfo* TalkBot::Reply(MCONTACT contact, wstring incomingMessage, bool saveChoice)
{
- TCHAR* str = new TCHAR[incomingMessage.length() + 1];
+ wchar_t* str = new wchar_t[incomingMessage.length() + 1];
mir_tstrcpy(str, incomingMessage.c_str());
CharLower(str);
incomingMessage = str;
@@ -274,8 +274,8 @@ TalkBot::MessageInfo* TalkBot::Reply(MCONTACT contact, tstring incomingMessage, MessageInfo *info;
//only 2-3 repeats
if (contactData->repeatCount < 2 || contactData->repeatCount == 2 && (rand() % 2)) {
- const vector<tstring>& v = mind.GetData()->repeats;
- tstring res = v[rand() % v.size()];
+ const vector<wstring>& v = mind.GetData()->repeats;
+ wstring res = v[rand() % v.size()];
#ifdef DEBUG_PREFIXES
info = new MessageInfo(incomingMessage, L"(repeat_norm) " + res);
#else
@@ -294,10 +294,10 @@ TalkBot::MessageInfo* TalkBot::Reply(MCONTACT contact, tstring incomingMessage, return info;
}
- multimap<Level, tstring> mm;
+ multimap<Level, wstring> mm;
Level maxValue = NOTHING;
- tstring res = AllReplies(incomingMessage, contactData, maxValue, mm);
+ wstring res = AllReplies(incomingMessage, contactData, maxValue, mm);
if (!res.empty()) {
UpdateStartChar(res);
#ifdef DEBUG_PREFIXES
@@ -328,7 +328,7 @@ TalkBot::MessageInfo* TalkBot::Reply(MCONTACT contact, tstring incomingMessage, }
//Also does Part 12
- tstring final = ChooseResult(contactData, maxValue, mm);
+ wstring final = ChooseResult(contactData, maxValue, mm);
MessageInfo *info = new MessageInfo(incomingMessage, final);
UpdateStartChar(final);
if (saveChoice)
@@ -337,14 +337,14 @@ TalkBot::MessageInfo* TalkBot::Reply(MCONTACT contact, tstring incomingMessage, return info;
}
-bool TalkBot::FindExact(ContactData *contactData, const tstring &incomingMessage,
- const multimap<tstring, tstring>& map, tstring& res)
+bool TalkBot::FindExact(ContactData *contactData, const wstring &incomingMessage,
+ const multimap<wstring, wstring> &map, wstring &res)
{
int max = (int)map.count(incomingMessage);
if (!max) {
- TCHAR c = incomingMessage[incomingMessage.length() - 1];
- if (c != _T('?') && c != _T('.') && c != _T('!'))
- return FindExact(contactData, incomingMessage + _T('.'), map, res);
+ wchar_t c = incomingMessage[incomingMessage.length() - 1];
+ if (c != '?' && c != '.' && c != '!')
+ return FindExact(contactData, incomingMessage + L'.', map, res);
return false;
}
pair<mm_cit, mm_cit> range = map.equal_range(incomingMessage);
@@ -372,7 +372,7 @@ void TalkBot::RecordAnswer(ContactData *contactData, const TalkBot::MessageInfo& contactData->lastMessage = info.Question;
}
-bool TalkBot::FindAny(ValueChooser<> &ch, tstring& res)
+bool TalkBot::FindAny(ValueChooser<> &ch, wstring& res)
{
if (!ch.GetContainer().size())
return false;
@@ -380,23 +380,23 @@ bool TalkBot::FindAny(ValueChooser<> &ch, tstring& res) return true;
}
-void TalkBot::SplitSectences(const tstring &incomingMessage, vector<tstring>& vec)
+void TalkBot::SplitSectences(const wstring &incomingMessage, vector<wstring>& vec)
{
//FIXME: (THINK ABOUT IT:-))these chars not always mark the end of sentence.
- const TCHAR symbols[] = L".?!";
+ const wchar_t symbols[] = L".?!";
int it = 0, len = (int)incomingMessage.length();
while (it != len) {
- while (it != len && _istspace(incomingMessage[it]))
+ while (it != len && iswspace(incomingMessage[it]))
it++;
int start = it;
while (it != len) {
- if (_tcschr(symbols, incomingMessage[it++])) {
+ if (wcschr(symbols, incomingMessage[it++])) {
//Test for a :-! smile
- if (it > 2 && incomingMessage[it - 1] == _T('!')
- && incomingMessage[it - 2] == _T('-')
- && incomingMessage[it - 3] == _T(':'))
+ if (it > 2 && incomingMessage[it - 1] == '!'
+ && incomingMessage[it - 2] == '-'
+ && incomingMessage[it - 3] == ':')
continue;
- while (it != len && _tcschr(symbols, incomingMessage[it]))
+ while (it != len && wcschr(symbols, incomingMessage[it]))
it++;
break;
}
@@ -406,9 +406,9 @@ void TalkBot::SplitSectences(const tstring &incomingMessage, vector<tstring>& ve }
#ifdef _DEBUG
-tstring LevelToStr(TalkBot::Level target)
+wstring LevelToStr(TalkBot::Level target)
{
- tstring lev;
+ wstring lev;
switch (target) {
case TalkBot::BEST: lev = L"BEST(0)"; break;
case TalkBot::LOOKSLIKE: lev = L"LOOKSLIKE(1)"; break;
@@ -420,41 +420,41 @@ tstring LevelToStr(TalkBot::Level target) }
#endif
-tstring TalkBot::ChooseResult(ContactData *contactData, Level maxValue, const multimap<Level, tstring> &mm)
+wstring TalkBot::ChooseResult(ContactData *contactData, Level maxValue, const multimap<Level, wstring> &mm)
{
#ifdef DEBUG_SHOW_VARIANTS
AddBotMessage(L">>Availabe:");
- for (multimap<Level, tstring>::iterator it = mm.begin(); it != mm.end(); it++)
+ for (multimap<Level, wstring>::iterator it = mm.begin(); it != mm.end(); it++)
AddBotMessage(LevelToStr((*it).first) + L": " + (*it).second);
AddBotMessage(L">>Result:");
#endif
if (maxValue == NOTHING)
return L"";
Level target = maxValue;
- typedef multimap<Level, tstring>::const_iterator lt_cit;
+ typedef multimap<Level, wstring>::const_iterator lt_cit;
pair<lt_cit, lt_cit> range = mm.equal_range(target);
for (lt_cit it = range.first; it != range.second; ++it)
contactData->chooser.AddChoice((*it).second);
#ifdef DEBUG_SHOW_LEVEL
- tstring lev = LevelToStr(target);
+ wstring lev = LevelToStr(target);
return lev + L": " + contactData->chooser.Choose();
#else
return contactData->chooser.Choose();
#endif
}
-void TalkBot::FindByKeywords(ContactData *contactData, const vector<tstring> &keywords, tstring& res/*, tstring& ures*/,
+void TalkBot::FindByKeywords(ContactData *contactData, const vector<wstring> &keywords, wstring& res/*, wstring& ures*/,
bool isQuestion)
{
if (keywords.size() == 0)
return;
- const multimap<WordsList, tstring> &keys = isQuestion ? mind.GetData()->qkeywords :
+ const multimap<WordsList, wstring> &keys = isQuestion ? mind.GetData()->qkeywords :
mind.GetData()->keywords;
- for (multimap<WordsList, tstring>::const_iterator it = keys.begin(); it != keys.end(); ++it) {
+ for (multimap<WordsList, wstring>::const_iterator it = keys.begin(); it != keys.end(); ++it) {
float prio;
if ((*it).first.MatchesAll(keywords/*, strict*/, prio))
#ifdef DEBUG_SHOW_SOLUTION_REASON
- contactData->chooser.AddChoice((tstring)(*it).first + L": - " + (*it).second, prio);
+ contactData->chooser.AddChoice((wstring)(*it).first + L": - " + (*it).second, prio);
#else
contactData->chooser.AddChoice((*it).second, prio);
#endif
@@ -462,16 +462,16 @@ void TalkBot::FindByKeywords(ContactData *contactData, const vector<tstring> &ke res = contactData->chooser.Choose();
}
-bool TalkBot::FindByOthers(ContactData *contactData, const vector<tstring> &otherwords, tstring& res, bool isQuestion)
+bool TalkBot::FindByOthers(ContactData *contactData, const vector<wstring> &otherwords, wstring& res, bool isQuestion)
{
- //vector<tstring> results;
- const multimap<WordsList, tstring> &specs = isQuestion ? mind.GetData()->qspecialEscapes :
+ //vector<wstring> results;
+ const multimap<WordsList, wstring> &specs = isQuestion ? mind.GetData()->qspecialEscapes :
mind.GetData()->specialEscapes;
- for (multimap<WordsList, tstring>::const_iterator it = specs.begin();
+ for (multimap<WordsList, wstring>::const_iterator it = specs.begin();
it != specs.end(); ++it)
if ((*it).first.MatchesAny(otherwords)) {
#ifdef DEBUG_SHOW_SOLUTION_REASON
- contactData->chooser.AddChoice((tstring)(*it).first + L": - " + (*it).second);
+ contactData->chooser.AddChoice((wstring)(*it).first + L": - " + (*it).second);
#else
contactData->chooser.AddChoice((*it).second);
#endif
@@ -487,20 +487,20 @@ const Mind& TalkBot::GetMind() const return mind;
}
-void TalkBot::SplitAndSortWords(tstring sentence, vector<tstring>& keywords,
- vector<tstring>& otherwords, bool& isQuestion)
+void TalkBot::SplitAndSortWords(wstring sentence, vector<wstring>& keywords,
+ vector<wstring>& otherwords, bool& isQuestion)
{
- const TCHAR dividers[] = L" \t\n\r,./?\\|;:'\"~!#^&*()_-+=[{]}—";
+ const wchar_t dividers[] = L" \t\n\r,./?\\|;:'\"~!#^&*()_-+=[{]}—";
int len = (int)sentence.length();
- vector<tstring> words;
- map<size_t, tstring> sm;
+ vector<wstring> words;
+ map<size_t, wstring> sm;
//Find smiles
for (size_t i = 0; i < sentence.length() - 1; i++) {
unsigned max = (int)(sentence.length() - i);
if (max > mind.GetData()->maxSmileLen)
max = mind.GetData()->maxSmileLen;
for (unsigned j = max; j > 0; j--) {
- tstring item = sentence.substr(i, j);
+ wstring item = sentence.substr(i, j);
if (mind.GetData()->smiles.find(item)
!= mind.GetData()->smiles.end()) {
sm[i] = item;
@@ -512,11 +512,11 @@ void TalkBot::SplitAndSortWords(tstring sentence, vector<tstring>& keywords, len = (int)sentence.length();
bool hadQuestionSigns = false;
for (int it = 0; it != len;) {
- while (it != len && _tcschr(dividers, sentence[it])) {
- if (sentence[it] == _T('?'))
+ while (it != len && wcschr(dividers, sentence[it])) {
+ if (sentence[it] == '?')
hadQuestionSigns = true;
- map<size_t, tstring>::iterator smit;
- if (_istspace(sentence[it]) && (smit = sm.find(it)) != sm.end())
+ map<size_t, wstring>::iterator smit;
+ if (iswspace(sentence[it]) && (smit = sm.find(it)) != sm.end())
words.push_back((*smit).second);
it++;
}
@@ -526,20 +526,20 @@ void TalkBot::SplitAndSortWords(tstring sentence, vector<tstring>& keywords, hadQuestionSigns = false;
int start = it;
while (true) {
- while (it != len && !_tcschr(dividers, sentence[it]))
+ while (it != len && !wcschr(dividers, sentence[it]))
it++;
- if (it == len || sentence[it] != _T('-'))
+ if (it == len || sentence[it] != '-')
break;
// If we have-a-word-with-minus, we shouldn't split it
- if (_tcschr(dividers, sentence[it + 1]))
+ if (wcschr(dividers, sentence[it + 1]))
break;
it += 2;
}
- tstring str = sentence.substr(start, it - start);
+ wstring str = sentence.substr(start, it - start);
words.push_back(str);
}
isQuestion = hadQuestionSigns;
- for (vector<tstring>::iterator it = words.begin(); it != words.end(); ++it) {
+ for (vector<wstring>::iterator it = words.begin(); it != words.end(); ++it) {
if (!isQuestion)
if (mind.GetData()->question.find(*it) != mind.GetData()->question.end())
isQuestion = true;
|