-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdegrootlib.cpp
More file actions
499 lines (431 loc) · 17 KB
/
degrootlib.cpp
File metadata and controls
499 lines (431 loc) · 17 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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
#include "degrootlib.h"
#include "third-party/json.hpp"
#include <algorithm>
#include <iostream>
#include <random>
using json = nlohmann::json;
deGrootLib::deGrootLib()
{
parseDataFile();
}
inline int deGrootLib::randomInt(int min, int max)
{
if (min > max) {
return 0;
}
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> distrib(min, max);
return distrib(gen);
}
inline bool deGrootLib::containsCaseInsensitive(const std::vector<std::string> &vec,
const std::string &target)
{
for (const std::string &str : vec) {
if (str.size() == target.size()) {
bool match = true;
for (size_t i = 0; i < str.size(); ++i) {
if (std::tolower(str[i]) != std::tolower(target[i])) {
match = false;
break;
}
}
if (match) {
return true;
}
}
}
return false;
}
std::string deGrootLib::degrootify(std::string message)
{
//if (!datafile_valid) {
// return message;
// }
bool do_pends = true;
std::string final_text = message;
if (message[0] == '-') {
do_pends = false;
final_text.erase(0, 1);
}
return modifySpeech(final_text, do_pends, false);
}
void deGrootLib::parseDataFile()
{
datafile_valid = true;
try {
json l_datafile_json = json::parse(json_literal);
// Prepended words
for (auto &[key, value] : l_datafile_json["prepended_words"].items()) {
prepended_words.push_back(key); // Add the word (key) to the vector
}
if (prepended_words.empty()) {
datafile_valid = false;
}
// Appended words
for (auto &[key, value] : l_datafile_json["appended_words"].items()) {
appended_words.push_back(key); // Add the word (key) to the vector
}
if (appended_words.empty()) {
datafile_valid = false;
}
// Replaced words
for (const auto &replacement : l_datafile_json["word_replacements"]) {
WordReplacement replacement_struct;
// Assign values to the struct members from the JSON object
replacement_struct.chance = (replacement["chance"].is_string())
? std::stoi(replacement["chance"].get<std::string>())
: replacement["chance"].get<int>();
replacement_struct.prepend_count
= (replacement["prepend_count"].is_string())
? std::stoi(replacement["prepend_count"].get<std::string>())
: replacement["prepend_count"].get<int>();
// Copy the arrays into the struct vectors
replacement_struct.prev_words = replacement["prev"].get<std::vector<std::string>>();
replacement_struct.words = replacement["word"].get<std::vector<std::string>>();
replacement_struct.plurals = replacement["word_plural"].get<std::vector<std::string>>();
replacement_struct.prepended = replacement["replacement_prepend"]
.get<std::vector<std::string>>();
replacement_struct.replacements = replacement["replacement"]
.get<std::vector<std::string>>();
replacement_struct.plural_replacements = replacement["replacement_plural"]
.get<std::vector<std::string>>();
// Add the struct to the vector
word_replacements.push_back(replacement_struct);
}
if (word_replacements.empty()) {
datafile_valid = false;
}
} catch (const json::exception &e) {
std::cerr << "Unable to load Medieval Mode data file. The following error occurred: "
<< e.what() << std::endl;
datafile_valid = false;
}
if (!datafile_valid) {
std::cerr << "Medieval Mode data file invalid, one or more vectors was empty" << std::endl;
}
}
std::string deGrootLib::getRandomPre()
{
if (randomInt(1, 4) != 1) {
return "";
}
if (prepended_words.empty()) {
return "";
}
static int prevPre = 0;
prevPre += (randomInt(1, 4));
while (prevPre >= prepended_words.size()) { // ensure we do not go out of bounds
prevPre -= prepended_words.size();
}
return prepended_words[prevPre];
}
std::string deGrootLib::getRandomPost()
{
if (randomInt(1, 5) != 1) {
return "";
}
if (appended_words.empty()) {
return "";
}
static int prevPost = 0;
prevPost += randomInt(1, 4);
while (prevPost >= appended_words.size()) { // ensure we do not go out of bounds
prevPost -= appended_words.size();
}
return appended_words[prevPost];
}
deGrootLib::MatchResult deGrootLib::wordMatches(WordReplacement *rep, ReplacementCheck *check)
{
if (rep->chance != 1) {
if (randomInt(1, rep->chance) > 1) {
return MATCHES_NOT;
}
}
// if it has prewords make sure the preword matches first
if (rep->prev_words.size() > 0) {
if (check->prev_word.length() <= 0
|| !containsCaseInsensitive(word_vector, check->prev_word)
|| !containsCaseInsensitive(rep->prev_words, check->prev_word)) {
return MATCHES_NOT;
}
check->used_prev_word = true;
}
// check match type
if (containsCaseInsensitive(rep->words, check->word)) {
return MATCHES_SINGULAR;
} else if (containsCaseInsensitive(rep->plurals, check->word)) {
return MATCHES_PLURAL;
} else {
// no match, reset and return failure
check->used_prev_word = false;
return MATCHES_NOT;
}
}
bool deGrootLib::replaceWord(ReplacementCheck *check,
std::string *rep_str,
bool symbols,
bool word_list_only)
{
*rep_str = "";
// First, see if we have a replacement
for (auto &&replacement : word_replacements) {
WordReplacement *rep_ptr = &replacement;
MatchResult result = wordMatches(rep_ptr, check);
if (result == MATCHES_NOT) {
continue;
}
if (rep_ptr->prepended.size() > 0) {
std::vector<int> vector_used;
for (int count = 0; count < rep_ptr->prepend_count; count++) {
// Ensure we don't choose two of the same prepends
int rnd = 0;
do {
rnd = randomInt(0, rep_ptr->prepended.size());
} while (std::find(vector_used.begin(), vector_used.end(), rnd)
!= vector_used.end());
vector_used.push_back(rnd);
rep_str->append(rep_ptr->prepended[rnd]);
if (count + 1 < rep_ptr->prepend_count) { // we have more prepends to prepend
rep_str->append(", ");
} else {
rep_str->append(" ");
}
}
}
if (result == MATCHES_SINGULAR) {
int rnd = randomInt(0, rep_ptr->replacements.size() - 1);
rep_str->append(rep_ptr->replacements[rnd]);
} else if (result == MATCHES_PLURAL) {
int rnd = randomInt(0, rep_ptr->plural_replacements.size() - 1);
rep_str->append(rep_ptr->plural_replacements[rnd]);
}
return true;
}
if (!symbols && !word_list_only) {
char fc = check->word[0];
// Randomly replace h's at the front of words with apostrophes
if (fc == 'h' && randomInt(1, 2) == 1) {
*rep_str = check->word.replace(0, 1, "'");
return true;
}
char lc = check->word[check->word.length() - 1];
if (check->word.length() > 3) {
char slc = check->word[check->word.length() - 2];
char lllc = check->word[check->word.length() - 3];
// Randomly motify words ending in "ed", by replacing the "e" with an apostrophe
if (slc == 'e' && lc == 'd' && lllc != 'e' && randomInt(1, 4) == 1) {
*rep_str = check->word.replace(check->word.length() - 2, 1, "'");
return true;
}
// Randomly append "th" or "st" to any word ending in "ke"
if (slc == 'k' && lc == 'e' && randomInt(1, 3) == 1) {
*rep_str = check->word;
if (randomInt(1, 2) == 1) {
rep_str->append("th");
} else {
rep_str->append("st");
}
return true;
}
}
if (check->word.length() >= 3) {
char slc = check->word[check->word.length() - 2];
// Randomly append "eth" to words with appropriate last letters.
if (randomInt(1, 5) == 1
&& (lc == 't' || lc == 'p' || lc == 'k' || lc == 'g' || lc == 'b' || lc == 'w')) {
*rep_str = check->word;
rep_str->append("eth");
return true;
}
// Randomly append "est" to any word ending in "ss"
if (lc == 's' && slc == 's' && randomInt(1, 5) == 1) {
*rep_str = check->word;
rep_str->append("est");
return true;
}
}
if (check->word.length() > 4) {
// Randomly prepend "a-" to words ending in "ing", and randomly replace the trailing g with an apostrophe
// i.e. "coming" -> "a-comin'", "dancing" -> "a-dancing"
char slc = check->word[check->word.length() - 2];
char lllc = check->word[check->word.length() - 3];
if (lllc == 'i' && slc == 'n' && lc == 'g') {
char sc = check->word[2];
if (sc != '-') {
rep_str->append("a-");
if (randomInt(1, 2) == 1) {
rep_str->append(check->word);
} else {
rep_str->append(check->word.replace(check->word.length() - 1, 2, "' "));
}
return true;
}
}
}
}
return false;
}
bool deGrootLib::performReplacement(std::string rep_str,
ReplacementCheck *check,
std::string stored_word,
std::string *out_text)
{
if (!rep_str.empty()) {
// Check to see if the previous word should be modified
char fc = std::tolower(rep_str[0]);
// moderately cursed c_str comparison. +1 to len is for the null terminator
if (!strnicmp(check->prev_word.c_str(),
"an",
std::max((int) (check->prev_word.length() + 1), 2))) {
if (fc != 'a' && fc != 'e' && fc != 'i' && fc != 'o' && fc != 'u') {
// Remove the trailing n
stored_word.erase(stored_word.size() - 2);
stored_word.append(" "); // 1 for space, 1 for n, then return space
}
} else if (check->prev_word == "a") {
if (fc == 'a' || fc == 'e' || fc == 'i' || fc == 'o' || fc == 'u') {
// Add a trailing n
stored_word.append("n");
}
}
}
// Only append the previous word if we didn't use it in our replacement
if (!check->used_prev_word) {
// Append the previous word
out_text->append(stored_word);
return true;
}
return false;
}
std::string deGrootLib::modifySpeech(std::string text, bool generate_pre_and_post, bool in_pre_post)
{
std::string final_text;
if (generate_pre_and_post) {
// See if we generate a pre. If we do, modify it as well so we can perform replacements on it.
std::string pre = getRandomPre();
if (pre != "") {
final_text.append(modifySpeech(pre, false, true) + " ");
}
}
// Iterate through all words and test them against the replacement list
// Note that a word is here defined as any group of letters separated
// by any character other than &
int prev_word_cur = 0;
int current_word_cur = 0;
int cur = 0;
int text_len = text.length();
std::string stored_word = "";
std::string current_word = "";
ReplacementCheck check;
while (true) {
if (cur > text_len) {
break;
} else if (cur == text_len) {
text.append(" "); // needed since the old version depended on null terminators
} else if ((text[cur] >= 'A' && text[cur] <= 'Z') || (text[cur] >= 'a' && text[cur] <= 'z')
|| text[cur] == '&') {
cur++;
continue;
}
// Not alphabetic or &. Hit the end of a word/string.
int current_word_len = cur - current_word_cur;
int prev_word_len = std::max(0,
current_word_cur - prev_word_cur - 1); // -1 for the space
current_word = text.substr(current_word_cur, current_word_len);
check.prev_word = text.substr(prev_word_cur, prev_word_len);
check.used_prev_word = false;
bool modify_word = true;
bool skip_one_letter = false;
// pre/post pend blocks only modify words that start with an '&'
if (in_pre_post) {
modify_word = (text[current_word_cur] == '&');
skip_one_letter = modify_word;
}
if (skip_one_letter) {
check.word = current_word.substr(1, current_word.length() - 1);
} else {
check.word = current_word;
}
if (current_word_len > 0) {
bool changed = modify_word ? replaceWord(&check, ¤t_word, false, in_pre_post)
: false;
// If the character that broke the last two words apart was an apostrophe, see if we can replace the whole word
if (changed && modify_word) {
if (!stored_word.empty()) {
int st_len = stored_word.length();
if (stored_word[st_len - 1] == '\'') {
check.word = stored_word;
check.word.append(current_word);
check.prev_word = "";
changed = replaceWord(&check, ¤t_word, false, in_pre_post);
if (changed) {
check.used_prev_word = true;
}
}
}
}
if (!stored_word.empty()) {
if (performReplacement(current_word, &check, stored_word, &final_text)) {
// Append a space, but not if the last character is an apostrophe
int st_len = stored_word.length();
if (stored_word[st_len - 1] != '\'') {
final_text.append(" ");
}
}
}
if (changed) {
stored_word = current_word;
// match case of the first letter in the word we're replacing
if (text[current_word_cur] >= 'A' && text[current_word_cur] <= 'Z') {
stored_word[0] = std::toupper(stored_word[0]);
} else if (text[current_word_cur] >= 'a'
&& text[current_word_cur] <= 'z') {
stored_word[0] = std::tolower(stored_word[0]);
}
} else {
stored_word = check.word;
}
}
// Finished?
if (cur == text_len) {
check.used_prev_word = false;
if (!stored_word.empty()) {
performReplacement("", &check, stored_word, &final_text);
}
}
// If it wasn't a space that ended this word, try checking it for a symbol
if (text[cur] != ' ') {
check.word = text[cur];
check.used_prev_word = false;
std::string symbol_rep = "";
if (replaceWord(&check, &symbol_rep, true, true)) {
stored_word.append(symbol_rep);
} else {
stored_word += text[cur];
}
}
// Move on
cur++;
prev_word_cur = current_word_cur;
current_word_cur = cur;
}
if (generate_pre_and_post) {
if (!final_text.empty()) {
char pszLC = final_text[final_text.length() - 1];
if (pszLC != '?' && pszLC != '!') {
// See if we generate a post. If we do, modify it as well so we can perform replacements on it.
std::string post = getRandomPost();
if (!post.empty()) {
if (pszLC != '.') {
final_text.append(". ");
} else {
final_text.append(" ");
}
final_text.append(modifySpeech(post, false, true));
}
}
}
}
return final_text;
}