Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- string Markovsky::Reply(string message)
- {
- FilterMessage(message);
- string replystring;
- vector < string > curlines;
- splitString(message, curlines, ". ");
- vector < string > curwords;
- int sz, i;
- for (sz = curlines.size(), i = 0; i < sz; i++)
- {
- tokenizeString(curlines[i], curwords);
- }
- if (curwords.empty())
- return replystring;
- // Filter out the words we don't know about
- int known = -1;
- vector < string > index;
- for (sz = curwords.size(), i = 0; i < sz; i++)
- {
- string &x = curwords[i];
- if (words.find(x) == words.end())
- continue;
- int k = words[x].size();
- if ((known == -1) || (k < known))
- {
- index.clear();
- index.push_back(x);
- known = k;
- }
- else if (k == known)
- {
- index.push_back(x);
- }
- }
- if (index.empty())
- return replystring;
- deque < string > sentence;
- // pick a random word to start building the reply
- int x = rand() % (index.size());
- sentence.push_back(index[x]);
- // Build on the left edge
- bool done = false;
- while (!done)
- {
- // cline = line, w = word number
- int c = this->words[sentence[0]].size();
- context_t l = this->words[sentence[0]][rand() % (c)];
- int w = l.second;
- // NOTE: cline is needed since tokenizeString messes with its arguments
- string cline = *(l.first);
- vector < string > cwords;
- tokenizeString(cline, cwords);
- int depth = rand() % (max_context_depth - min_context_depth) + min_context_depth;
- for (int i = 1; i <= depth; i++)
- {
- if ((w - i) < 0)
- {
- done = true;
- break;
- }
- else
- {
- sentence.push_front(cwords[w - i]);
- }
- if ((w - i) == 0)
- {
- done = true;
- break;
- }
- }
- }
- // Build on the right edge
- done = false;
- while (!done)
- {
- if (words.find(sentence.back()) == words.end())
- {
- printf("%s:%i: words.find(sentence.back()) == words.end()\n",
- __FILE__, __LINE__);
- }
- int c = this->words[sentence.back()].size();
- context_t l = this->words[sentence.back()][rand() % (c)];
- int w = l.second;
- string cline = *(l.first);
- vector < string > cwords;
- tokenizeString(cline, cwords);
- int depth = rand() % (max_context_depth - min_context_depth) + min_context_depth;
- for (int i = 1; i <= depth; i++)
- {
- if ((w + i) >= cwords.size())
- {
- done = true;
- break;
- }
- else
- {
- sentence.push_back(cwords[w + i]);
- }
- }
- }
- for (i = 0, sz = sentence.size() - 1; i < sz; i++)
- {
- replystring += sentence[i];
- replystring += ' ';
- }
- replystring += sentence.back();
- return replystring;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement