Advertisement
OtakuMegane

markovsky.cpp Reply function

Feb 9th, 2018
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.15 KB | None | 0 0
  1. string Markovsky::Reply(string message)
  2. {
  3.     FilterMessage(message);
  4.     string replystring;
  5.  
  6.     vector < string > curlines;
  7.     splitString(message, curlines, ". ");
  8.     vector < string > curwords;
  9.  
  10.     int sz, i;
  11.     for (sz = curlines.size(), i = 0; i < sz; i++)
  12.     {
  13.         tokenizeString(curlines[i], curwords);
  14.     }
  15.  
  16.     if (curwords.empty())
  17.         return replystring;
  18.  
  19.     // Filter out the words we don't know about
  20.     int known = -1;
  21.     vector < string > index;
  22.     for (sz = curwords.size(), i = 0; i < sz; i++)
  23.     {
  24.         string &x = curwords[i];
  25.         if (words.find(x) == words.end())
  26.             continue;
  27.         int k = words[x].size();
  28.         if ((known == -1) || (k < known))
  29.         {
  30.             index.clear();
  31.             index.push_back(x);
  32.             known = k;
  33.         }
  34.         else if (k == known)
  35.         {
  36.             index.push_back(x);
  37.         }
  38.     }
  39.  
  40.     if (index.empty())
  41.         return replystring;
  42.  
  43.     deque < string > sentence;
  44.  
  45.     // pick a random word to start building the reply
  46.     int x = rand() % (index.size());
  47.     sentence.push_back(index[x]);
  48.  
  49.     // Build on the left edge
  50.     bool done = false;
  51.     while (!done)
  52.     {
  53.         // cline = line, w = word number
  54.         int c = this->words[sentence[0]].size();
  55.         context_t l = this->words[sentence[0]][rand() % (c)];
  56.         int w = l.second;
  57.         // NOTE: cline is needed since tokenizeString messes with its arguments
  58.         string cline = *(l.first);
  59.         vector < string > cwords;
  60.         tokenizeString(cline, cwords);
  61.  
  62.         int depth = rand() % (max_context_depth - min_context_depth) + min_context_depth;
  63.         for (int i = 1; i <= depth; i++)
  64.         {
  65.             if ((w - i) < 0)
  66.             {
  67.                 done = true;
  68.                 break;
  69.             }
  70.             else
  71.             {
  72.                 sentence.push_front(cwords[w - i]);
  73.             }
  74.             if ((w - i) == 0)
  75.             {
  76.                 done = true;
  77.                 break;
  78.             }
  79.         }
  80.     }
  81.  
  82.     // Build on the right edge
  83.     done = false;
  84.     while (!done)
  85.     {
  86.         if (words.find(sentence.back()) == words.end())
  87.         {
  88.             printf("%s:%i: words.find(sentence.back()) == words.end()\n",
  89.             __FILE__, __LINE__);
  90.         }
  91.  
  92.         int c = this->words[sentence.back()].size();
  93.         context_t l = this->words[sentence.back()][rand() % (c)];
  94.         int w = l.second;
  95.         string cline = *(l.first);
  96.         vector < string > cwords;
  97.         tokenizeString(cline, cwords);
  98.  
  99.         int depth = rand() % (max_context_depth - min_context_depth) + min_context_depth;
  100.         for (int i = 1; i <= depth; i++)
  101.         {
  102.             if ((w + i) >= cwords.size())
  103.             {
  104.                 done = true;
  105.                 break;
  106.             }
  107.             else
  108.             {
  109.                 sentence.push_back(cwords[w + i]);
  110.             }
  111.         }
  112.     }
  113.  
  114.     for (i = 0, sz = sentence.size() - 1; i < sz; i++)
  115.     {
  116.         replystring += sentence[i];
  117.         replystring += ' ';
  118.     }
  119.     replystring += sentence.back();
  120.  
  121.     return replystring;
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement