-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcgbot.cpp
More file actions
484 lines (358 loc) · 10.2 KB
/
Copy pathcgbot.cpp
File metadata and controls
484 lines (358 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
#include <iostream>
#include <time.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <vector>
#include <sstream>
#include <cstring>
using namespace std;
//************************************
// Constants
#define START "__START__"
#define END "__END__"
#define NICK "__NICK__"
#define MODE_NONE 0
#define MODE_IGNORE_END 1
#define MODE_FORCE_END 2
constexpr unsigned int MARKOV_LENGTH = 3;
constexpr unsigned int SOFT_MINIMUM_LENGTH = 5;
constexpr unsigned int HARD_MINIMUM_LENGTH = 2;
constexpr unsigned int SOFT_MAXIMUM_LENGTH = 15;
constexpr unsigned int HARD_MAXIMUM_LENGTH = 20;
constexpr unsigned int SOFT_TRY = 10;
// Memory usage
constexpr unsigned int MINIMUM_OCCURENCES = 3;
constexpr unsigned int MINIMUM_SCORE = 3;
constexpr unsigned int REHASH_THRESOLD = 100000;
constexpr bool CASE_INSENSITIVE = true;
typedef array<string, MARKOV_LENGTH> strings;
auto chieq = [](char c1, char c2) { return toupper(c1) == toupper(c2); };
//************************************
// Declarations
namespace std {
template<>
struct hash<strings> {
size_t operator()(const strings& v) const;
};
}
class Node {
public:
unsigned int occurences;
unsigned int total;
unsigned int end;
unordered_map<const strings*, unsigned int> nexts;
Node();
void increase(const strings& next);
template <int mode>
const strings* randomNext();
};
class Bot {
public:
string nickname;
bool enabled;
unsigned int startsTotal;
unordered_set<string> blacklist;
unordered_map<strings, Node> words;
unordered_set<pair<const strings, Node>*> starts;
Bot(string nickname);
void learn(string& message);
void talk(vector<string>& result);
void rehash();
vector<string> split(string& str);
const strings* randomStart();
template <bool start>
void add(strings& from, string& to);
};
//************************************
// Globals
//************************************
// Definitions
inline bool equali(string a, string b) {
return equal(a.begin(), a.end(), b.begin(), b.end(), chieq);
}
void clean(string& s) {
static auto isnotspace = [](char c) { return !isspace(c); };
// Trim left
s.erase(s.begin(), find_if(s.begin(), s.end(), isnotspace));
// Trim right
s.erase(find_if(s.rbegin(), s.rend(), isnotspace).base(), s.end());
// Clean bad caracters
s.erase(remove_if(s.begin(), s.end(), [](char c) { return c == '\n' || c == '\t' || c == '\r'; }), s.end);
// Clean double spaces
s.erase(unique(s.begin(), s.end(), [](char a, char b) { return a == b && a == ' '; }), s.end());
}
template <typename T>
inline void concat(vector<string>& dest, const T& src) {
dest.insert(dest.end(), src.begin(), src.end());
}
template <typename T>
void print(ostream& os, const T& vec) {
os << "<";
bool empty = true;
for (const string& str : vec) {
if (!empty) {
os << " ";
} else {
empty = false;
}
os << str;
}
os << ">";
}
inline ostream& operator<<(ostream& os, const vector<string>& vec) {
print(os, vec);
return os;
}
inline ostream& operator<<(ostream& os, const strings& vec) {
print(os, vec);
return os;
}
Node::Node() : occurences(0), total(0), end(0) {}
void Node::increase(const strings& next) {
nexts[&next] += 1;
total += 1;
if (next[MARKOV_LENGTH - 1] == END) {
end += 1;
}
}
template <int mode>
const strings* Node::randomNext() {
unsigned int total;
if (mode == MODE_FORCE_END && end > 0) {
total = end;
} else if (mode == MODE_IGNORE_END && end > 0) {
total = this->total - end;
} else {
total = this->total;
}
int r = 1 + rand() % total;
for (auto& p : nexts) {
if (mode == MODE_FORCE_END && end > 0 && p.first->at(MARKOV_LENGTH - 1) != END) {
continue;
}
if (mode == MODE_IGNORE_END && end > 0 && p.first->at(MARKOV_LENGTH - 1) == END) {
continue;
}
r -= p.second;
if (r <= 0) {
return p.first;
}
}
cerr << "randomNext - Returning null: mode " << mode << " r " << r << " total " << total << " end " << end << " this->total " << this->total << endl;
return NULL;
}
Bot::Bot(string nickname) : nickname(nickname), enabled(false), startsTotal(0), blacklist({ nickname }) {
}
vector<string> Bot::split(string& str) {
vector<string> result;
istringstream iss(str);
for (string s; iss >> s;) {
if (s == START || s == END || s == NICK) {
continue;
}
if (equali(s, nickname)) {
s = NICK;
}
result.push_back(s);
}
return result;
}
void Bot::rehash() {
bool found;
cout << "### Before rehash: words.size() " << words.size() << " starts.size() " << starts.size() << " startsTotal " << startsTotal << endl;
do {
found = false;
unordered_set<const strings*> deleted;
for (auto& p : words) {
if ((p.first[MARKOV_LENGTH - 1] != END && p.second.total <= 0) || p.second.occurences < MINIMUM_OCCURENCES) {
deleted.insert(&p.first);
if (p.first[0] == START) {
starts.erase(&p);
}
}
}
for (auto& p : words) {
if (deleted.find(&p.first) != deleted.end()) {
continue;
}
for (auto it = p.second.nexts.begin(); it != p.second.nexts.end();) {
if (deleted.find(it->first) != deleted.end() || it->second < MINIMUM_SCORE) {
p.second.total -= it->second;
if (p.second.total <= 0) {
found = true;
}
if (it->first->at(MARKOV_LENGTH - 1) == END) {
p.second.end -= it->second;
}
p.second.occurences -= 1;
if (p.second.occurences < MINIMUM_OCCURENCES) {
found = true;
}
it = p.second.nexts.erase(it);
} else {
++it;
}
}
}
for (auto it = words.begin(); it != words.end();) {
if (deleted.find(&it->first) != deleted.end()) {
it = words.erase(it);
} else {
++it;
}
}
} while (found);
words.rehash(0);
startsTotal = 0;
for (auto& v : starts) {
startsTotal += v->second.total;
}
cout << "### After rehash: words.size() " << words.size() << " starts.size() " << starts.size() << " startsTotal " << startsTotal << endl;
}
template <bool start>
void Bot::add(strings& from, string& to) {
strings dest;
for (unsigned int i = 0; i < MARKOV_LENGTH - 1; ++i) {
dest[i] = from[i + 1];
}
dest[MARKOV_LENGTH - 1] = to;
words[dest].occurences += 1;
words[from].increase(words.find(dest)->first);
if (start) {
words[from].occurences += 1;
startsTotal += 1;
starts.insert(&*words.find(from));
}
}
void Bot::learn(string& message) {
static string end(END);
vector<string> words = split(message);
if (words.size() <= 0 || words.size() < MARKOV_LENGTH - 1) {
return;
}
strings history;
history[0] = START;
for (unsigned int i = 0; i < MARKOV_LENGTH - 1; ++i) {
history[i + 1] = words[i];
}
for (unsigned int i = MARKOV_LENGTH - 1; i < words.size(); ++i) {
if (i == MARKOV_LENGTH - 1) {
add<true>(history, words[i]);
} else {
add<false>(history, words[i]);
}
for (unsigned int j = 0; j < MARKOV_LENGTH - 1; ++j) {
history[j] = history[j + 1];
}
history[MARKOV_LENGTH - 1] = words[i];
}
add<false>(history, end);
if (enabled && words.size() > REHASH_THRESOLD) {
rehash();
}
}
const strings* Bot::randomStart() {
int r = 1 + rand() % startsTotal;
for (const pair<const strings, Node>* p : starts) {
r -= p->second.total;
if (r <= 0) {
return &p->first;
}
}
cerr << "randomStart - Returning null: startsTotal " << startsTotal << " r " << r << endl;
return NULL;
}
void Bot::talk(vector<string>& result) {
result.clear();
const strings* history = randomStart();
concat(result, *history);
while (result.size() < HARD_MAXIMUM_LENGTH + 1) {
Node& node = words[*history];
if (result.size() < HARD_MINIMUM_LENGTH + 1) {
history = node.randomNext<MODE_IGNORE_END>();
} else if (result.size() > SOFT_MAXIMUM_LENGTH + 1) {
history = node.randomNext<MODE_FORCE_END>();
} else {
history = node.randomNext<MODE_NONE>();
}
const string& last = history->at(MARKOV_LENGTH - 1);
if (last == END) {
break;
}
result.push_back(last);
if (result.size() == HARD_MAXIMUM_LENGTH + 1) {
result.push_back("...");
}
}
}
namespace std {
size_t hash<strings>::operator()(const strings& v) const {
stringstream ss;
for (const string& str : v) {
ss << "_" << str;
}
return hash<string>()(ss.str());
}
}
//************************************
// Main
int main(int argc ,char **argv) {
if (argc < 2) {
cerr << "Not enought arguments" << endl;
return 1;
}
srand(time(NULL));
Bot bot(argv[1]);
for (int i = 2; i < argc; ++i) {
bot.blacklist.insert(argv[i]);
}
while (true) {
string username;
string body;
cin >> username;
if (username.length() == 10 && username[0] == '(' && username[9] == ')') {
cin >> username;
string useless;
cin >> useless;
}
getline(cin, body);
clean(body);
if (username.length() == 0 && body.length() == 0) {
cin.ignore();
continue;
}
if (username == "###") {
if (body == "ENABLE") {
bot.rehash();
bot.enabled = true;
} else if (body == "DISABLE") {
bot.enabled = false;
} else if (body == "STOP") {
break;
}
} else if (bot.blacklist.find(username) == bot.blacklist.end()) {
if (CASE_INSENSITIVE) {
transform(body.begin(), body.end(), body.begin(), ::tolower);
}
bot.learn(body);
if (bot.enabled && search(body.begin(), body.end(), bot.nickname.begin(), bot.nickname.end(), chieq) != body.end()) {
vector<string> output;
unsigned int counter = 0;
do {
bot.talk(output);
counter += 1;
} while (output.size() - 1 < (counter < SOFT_TRY ? SOFT_MINIMUM_LENGTH : HARD_MINIMUM_LENGTH));
for (unsigned int i = 1; i < output.size(); ++i) {
if (i != 1) {
cout << " ";
}
cout << (equali(output[i], NICK) ? username : output[i]);
}
cout << endl;
}
}
}
return 0;
}