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
|
#include "StarNameGenerator.hpp"
#include "StarRoot.hpp"
#include "StarAssets.hpp"
#include "StarJsonExtra.hpp"
namespace Star {
PatternedNameGenerator::PatternedNameGenerator() {
auto assets = Root::singleton().assets();
auto &files = assets->scanExtension("namesource");
assets->queueJsons(files);
for (auto& file : files) {
try {
auto sourceConfig = assets->json(file);
if (m_markovSources.contains(sourceConfig.getString("name")))
throw NameGeneratorException::format("Duplicate name source '{}', config file '{}'", sourceConfig.getString("name"), file);
m_markovSources.insert(sourceConfig.getString("name"), makeMarkovSource(sourceConfig.getUInt("prefixSize", 1),
sourceConfig.getUInt("endSize", 1), jsonToStringList(sourceConfig.get("sourceNames"))));
} catch (std::exception const& e) {
throw NameGeneratorException(strf("Error reading name source config {}", file), e);
}
}
auto profanityFilter = assets->json("/names/profanityfilter.config").toArray();
for (auto& naughtyWord : profanityFilter)
m_profanityFilter.add(naughtyWord.toString().toLower());
}
String PatternedNameGenerator::generateName(String const& rulesAsset) const {
RandomSource random;
return generateName(rulesAsset, random);
}
String PatternedNameGenerator::generateName(String const& rulesAsset, uint64_t seed) const {
RandomSource random = RandomSource(seed);
return generateName(rulesAsset, random);
}
String PatternedNameGenerator::generateName(String const& rulesAsset, RandomSource& random) const {
auto assets = Root::singleton().assets();
auto rules = assets->json(rulesAsset).toArray();
String res = "";
int tries = 100;
while ((res.empty() || isProfane(res)) && tries > 0) {
--tries;
res = processRule(rules, random);
}
return res;
}
String PatternedNameGenerator::processRule(JsonArray const& rule, RandomSource& random) const {
if (rule.empty())
return "";
Json meta;
String result;
String mode = "alts";
size_t index = 0;
bool titleCase = false;
if (rule[0].type() == Json::Type::Object) {
meta = rule[0];
mode = meta.getString("mode", mode);
titleCase = meta.getBool("titleCase", false);
index++;
}
if (mode == "serie") {
for (; index < rule.size(); index++) {
auto entry = rule[index];
if (entry.type() == Json::Type::Array)
result += processRule(entry.toArray(), random);
else
result += entry.toString();
}
} else if (mode == "alts") {
int i = index + random.randInt(rule.size() - 1 - index);
auto entry = rule[i];
if (entry.type() == Json::Type::Array)
result += processRule(entry.toArray(), random);
else
result += entry.toString();
} else if (mode == "markov") {
if (!m_markovSources.contains(meta.getString("source")))
throw NameGeneratorException::format("Unknown name source '{}'", meta.getString("source"));
auto source = m_markovSources.get(meta.getString("source"));
auto lengthRange = meta.getArray("targetLength");
auto targetLength = random.randUInt(lengthRange[0].toUInt(), lengthRange[1].toUInt());
size_t tries = 0;
String piece;
do {
++tries;
piece = random.randFrom(source.starts);
while (piece.length() < targetLength
|| !source.ends.contains(piece.slice(piece.length() - source.endSize, piece.length()))) {
String link = piece.slice(piece.length() - source.prefixSize, piece.length());
if (!source.chains.contains(link))
break;
piece += random.randFrom(source.chains.get(link));
}
} while (piece.length() > lengthRange[1].toUInt() && tries < 10);
result += piece;
} else
throw StarException::format("Unknown mode: {}", mode);
if (titleCase)
result = result.titleCase();
return result;
}
bool PatternedNameGenerator::isProfane(String const& name) const {
auto matchNames = name.toLower().rot13().splitAny(" -");
for (auto& naughtyWord : m_profanityFilter) {
for (auto& matchName : matchNames) {
auto find = matchName.find(naughtyWord);
if (find != NPos && (find == 0 || naughtyWord.size() + 1 >= matchName.size()))
return true;
}
}
return false;
}
MarkovSource PatternedNameGenerator::makeMarkovSource(size_t prefixSize, size_t endSize, StringList sourceNames) {
MarkovSource newSource;
newSource.prefixSize = prefixSize;
newSource.endSize = endSize;
for (auto sourceName : sourceNames) {
if (sourceName.length() < prefixSize || sourceName.length() < endSize)
continue;
sourceName = sourceName.toLower();
newSource.ends.add(sourceName.slice(sourceName.length() - endSize, sourceName.length()));
for (int i = 0; i + prefixSize <= sourceName.length(); ++i) {
auto prefix = sourceName.slice(i, i + prefixSize);
if (i == 0)
newSource.starts.append(prefix);
if (i + prefixSize < sourceName.length()) {
if (!newSource.chains.contains(prefix))
newSource.chains.insert(prefix, StringList());
newSource.chains.get(prefix).append(sourceName.slice(i + prefixSize, i + prefixSize + 1));
}
}
}
return newSource;
}
}
|