lipsum-cpp 0.5.4
A basic library written in C++ for generating placeholder Lorem Ipsum text.
Loading...
Searching...
No Matches
generatorcore.inl
Go to the documentation of this file.
1
13
14#pragma once
15
16#include "core/internal.hpp"
17
18#define LPSM_ASSIGN_CHECK(name) \
19 isChecked = !isChecked ? assignSetting(#name, name) : true
20
21namespace lipsum
22{
23 void GeneratorSettings::change_setting(const std::string& setting,
24 const ArgVec2& value)
25 {
26 auto assignSetting = [&](const char* name, ArgVec2& target) -> bool
27 {
28 if (setting == name)
29 {
30 target = value;
31 return true;
32 }
33 return false;
34 };
35 bool isChecked = false;
36 LPSM_VERBOSE_LOG(Trace,
37 "Changing setting ",
38 setting,
39 " to (",
40 value.min,
41 ", ",
42 value.max,
43 ")");
44
45 LPSM_ASSIGN_CHECK(word);
46 LPSM_ASSIGN_CHECK(frag);
47 LPSM_ASSIGN_CHECK(sent);
48 LPSM_ASSIGN_CHECK(para);
49#ifndef LIPSUM_MIN_BUILD
50 LPSM_ASSIGN_CHECK(point);
51 LPSM_ASSIGN_CHECK(wordFmt);
52 LPSM_ASSIGN_CHECK(fragFmt);
53 LPSM_ASSIGN_CHECK(wordURL);
54 LPSM_ASSIGN_CHECK(level);
55 LPSM_ASSIGN_CHECK(jsonLength);
56 LPSM_ASSIGN_CHECK(csvRows);
57 LPSM_ASSIGN_CHECK(csvCols);
58#endif
59 if (!isChecked)
60 {
61 internal::LogWarn(internal::LogType::Warn,
62 "lpsm::GeneratorSettings::change_setting(): "
63 "unknown "
64 "setting ",
65 setting,
66 ", ignoring");
67 }
68 }
69
71 {
72 LPSM_VERBOSE_LOG(Info, "Toggling option lazy");
73 lazy = !lazy;
74 }
75
76 /*
77 * SETUP
78 */
79
81 {
82 m_Gen.seed(std::random_device{}());
83 LPSM_VERBOSE_LOG(Info, "Creating a lpsm::Generator");
84 }
85
87 {
88 LPSM_VERBOSE_LOG(Info, "Destroying a lpsm::Generator");
89 }
90
92 {
93 m_Gen.seed(seed);
94 LPSM_VERBOSE_LOG(Info, "Creating a lpsm::Generator with seed ", seed);
95 }
96
97 Generator::Generator(const std::string& path)
98 {
99 load_source(path);
100 LPSM_VERBOSE_LOG(Info, "Creating a lpsm::Generator with source ", path);
101 }
102
103 Generator::Generator(const std::string& path, int seed)
104 {
105 load_source(path);
106 m_Gen.seed(seed);
107 LPSM_VERBOSE_LOG(Info,
108 "Creating a lpsm::Generator with seed ",
109 seed,
110 " and source ",
111 path);
112 }
113
115 {
116 m_Settings.toggle_lazy();
117 }
118
119 void Generator::load_source(const std::string& path)
120 {
121 m_Source.load(path);
122 LPSM_VERBOSE_LOG(Trace, "Loading source ", path);
123 }
124
125 void Generator::load_seed(int seed)
126 {
127 m_Gen.seed(seed);
128 LPSM_VERBOSE_LOG(Trace, "Loading seed ", seed);
129 }
130
131 void Generator::change_setting(const std::string& setting,
132 const ArgVec2& value)
133 {
134 m_Settings.change_setting(setting, value);
135 }
136
137 void Generator::change_setting(const std::string& setting,
138 int minValue,
139 int maxValue)
140 {
141 change_setting(setting, ArgVec2(minValue, maxValue));
142 }
143
144 /*
145 * PRIVATE
146 */
147
148 char Generator::random_number(char min, char max)
149 {
150 return static_cast<char>(
151 random_number(static_cast<int>(min), static_cast<int>(max)));
152 }
153
154 bool Generator::random_number(bool min, bool max)
155 {
156 int choice = random_number(0, 1);
157 return choice == 0 ? min : max;
158 }
159
160 int Generator::weighted_random_idx(const std::vector<int>& weights)
161 {
162 std::discrete_distribution<> dist(weights.begin(), weights.end());
163 return dist(m_Gen);
164 }
165
166#ifndef LIPSUM_MIN_BUILD
167 std::string Generator::tld()
168 {
169 std::vector<std::string> tlds = {{".com"},
170 {".org"},
171 {".net"},
172 {".edu"},
173 {".io"},
174 {".ca"},
175 {".co.uk"}};
176 static const std::vector<int> weights = {70, 10, 7, 5, 5, 2, 1};
177 int idx = weighted_random_idx(weights);
178 return tlds.at(idx);
179 }
180
181 std::string Generator::single_sentence(const ArgVec2& wordArg,
182 const ArgVec2& frag)
183 {
184 static const std::vector<int> weights = {88, 7, 3, 2};
185 static const std::vector<int> weightsEndMark = {80, 15, 5};
186 static const std::vector<std::string> mapped = {", ",
187 "; ",
188 ": ",
189 " - "};
190 static const std::vector<std::string> mappedEndMark = {".", "?", "!"};
191 std::string result;
192 int words;
193 int frags = frag.roll(m_Gen, m_Settings.lazy);
194 for (int i = 0; i < frags; ++i)
195 {
196 words = wordArg.roll(m_Gen, m_Settings.lazy);
197 result += word(words);
198 int check = weighted_random_idx(weights);
199 // don't do if only one fragment
200 if (i != frags - 1)
201 {
202 result += mapped.at(check);
203 }
204 }
205 result += mappedEndMark.at(weighted_random_idx(weightsEndMark));
206 result.at(0) = LPSM_SAFE_CCTYPE(char, std::toupper, result.at(0));
207 return result;
208 }
209
210 std::string Generator::single_fmt_paragraph(bool useLipsum, bool useHtml)
211 {
212 std::string ret;
213 int sents = m_Settings.sent.roll(m_Gen, m_Settings.lazy);
214 int fmtRoll;
215 bool addLink;
216 bool isBold;
217 constexpr int FMT_PARA_CHANCE_FMT = 14;
218
219 // 1 in 15
220 static const std::vector<int> weights = {FMT_PARA_CHANCE_FMT, 1};
221
222 if (useHtml)
223 {
224 ret += "<p>";
225 }
226 for (int i = 0; i < sents; ++i)
227 {
228 fmtRoll = weighted_random_idx(weights);
229 if (fmtRoll == 1)
230 {
231 addLink = LPSM_FLIP_COIN;
232 isBold = LPSM_FLIP_COIN;
233 }
234 if (i == 0 && useLipsum)
235 {
237 }
238 else if ((fmtRoll == 1) && addLink)
239 {
240 ret += fmt_link(useHtml);
241 }
242 else if ((fmtRoll == 1) && !addLink)
243 {
244 ret += fmt_emphasis(isBold, useHtml);
245 }
246 else
247 {
248 ret += single_sentence(m_Settings.word, m_Settings.frag);
249 }
250 ret += " ";
251 }
252 if (useHtml)
253 {
254 ret += "</p>";
255 }
256 ret += "\n\n";
257 return ret;
258 }
259
261 {
262 return std::string("\"") +
263 m_Source.random_word(m_Gen, m_Settings.lazy) + "\"";
264 }
265
267 {
268 constexpr int JSON_NUMBER_MIN = -1000;
269 constexpr int JSON_NUMBER_MAX = 1000;
270 return internal::ToString(
271 random_number(JSON_NUMBER_MIN, JSON_NUMBER_MAX));
272 }
273#else
274 std::string Generator::tld()
275 {
276 return ".com";
277 }
278
279 std::string Generator::single_sentence(const ArgVec2& wordArg,
280 const ArgVec2& frag)
281 {
282 std::string result;
283 int words;
284 int frags = frag.roll(m_Gen, m_Settings.lazy);
285 for (int i = 0; i < frags; ++i)
286 {
287 words = wordArg.roll(m_Gen, m_Settings.lazy);
288 result += word(words);
289 if (i != frags - 1)
290 {
291 result += ", ";
292 }
293 }
294 result += ".";
295 result.at(0) = LPSM_SAFE_CCTYPE(char, std::toupper, result.at(0));
296 return result;
297 }
298
299 // placeholder
300
301 std::string Generator::single_fmt_paragraph(bool, bool)
302 {
303 return "";
304 }
305
306 std::string Generator::json_string()
307 {
308 return "";
309 }
310
311 std::string Generator::json_number()
312 {
313 return "";
314 }
315#endif
316
317 std::string Generator::single_paragraph(bool useLipsum)
318 {
319 std::string result = "\t";
320 int sents = m_Settings.sent.roll(m_Gen, m_Settings.lazy);
321 for (int i = 0; i < sents; ++i)
322 {
323 if (i == 0 && useLipsum)
324 {
325 result += GenerateDefaultLipsumSentence() + " ";
326 }
327 else
328 {
329 result +=
330 single_sentence(m_Settings.word, m_Settings.frag) + " ";
331 }
332 }
333
334 if (!result.empty())
335 {
336 // remove trailing space
337 result.pop_back();
338 }
339 result += "\n";
340 return result;
341 }
342} // namespace lipsum
void change_setting(const std::string &setting, const ArgVec2 &value)
Change a setting.
Definition generatorcore.inl:131
std::string fmt_emphasis(bool isBold=true, bool useHtml=false)
Generate an emphasized Markdown or HTML sentence.
Definition generatorformats.inl:145
GeneratorSettings m_Settings
Settings for generation.
Definition generator.hpp:832
void load_seed(int seed)
Reload a seed.
Definition generatorcore.inl:125
void toggle_lazy()
Toggle the lazy flag.
Definition generatorcore.inl:114
std::string word(int num=1)
Generate words.
Definition generator.inl:67
std::string single_sentence(const ArgVec2 &wordArg, const ArgVec2 &frag)
Generate a single sentence.
Definition generatorcore.inl:181
std::string single_fmt_paragraph(bool useLipsum, bool useHtml)
Generate a single Markdown or HTML paragraph.
Definition generatorcore.inl:210
void load_source(const std::string &path)
Reload a source.
Definition generatorcore.inl:119
std::string json_string()
Generate a random JSON string.
Definition generatorcore.inl:260
Source m_Source
Source used for generation.
Definition generator.hpp:844
int weighted_random_idx(const std::vector< int > &weights)
Choose a random index based off weights.
Definition generatorcore.inl:160
Generator()
Default constructor for Generator.
Definition generatorcore.inl:80
T random_number(T min, T max)
Generate a random number.
Definition generator.hpp:755
std::string fmt_link(bool useHtml=false)
Generate a Markdown or HTML link.
Definition generatorformats.inl:173
std::string tld()
Pick a random TLD.
Definition generatorcore.inl:167
std::string single_paragraph(bool useLipsum)
Generate a single paragraph.
Definition generatorcore.inl:317
std::string json_number()
Generate a random JSON number.
Definition generatorcore.inl:266
std::mt19937 m_Gen
Random number generator.
Definition generator.hpp:834
~Generator()
Definition generatorcore.inl:86
#define LPSM_SAFE_CCTYPE(type, func, arg)
Simplify safe cctype usage.
Definition core.hpp:36
#define LPSM_FLIP_COIN
Choose between true or false.
Definition core.hpp:31
#define LPSM_VERBOSE_LOG(type,...)
Verbose log messages.
Definition core.hpp:49
Declaration of lipsum::internal.
void LogWarn(LogType type, const Args &... args)
Log a warning.
Definition internal.hpp:126
std::string ToString(const T &param)
Convert an object to a string.
Definition internal.hpp:242
This is the main namespace of lipsum-cpp.
LIPSUM_API std::string GenerateDefaultLipsumSentence()
Generate the beginning Lorem Ipsum sentence.
Definition misc.inl:38
Store data for inputting into functions.
Definition argvec2.hpp:29
int max
The maximum value.
Definition argvec2.hpp:56
int min
The minimum value.
Definition argvec2.hpp:55
int roll(std::mt19937 &gen, bool lazy=false) const
Return a number between min and max.
Definition argvec2.inl:39
bool lazy
Whether generation will be "lazy".
Definition generatorsettings.hpp:33
ArgVec2 csvCols
The minimum and maximum possible number of columns in CSV data.
Definition generatorsettings.hpp:130
void change_setting(const std::string &setting, const ArgVec2 &value)
Change a setting.
Definition generatorcore.inl:23
ArgVec2 fragFmt
The minimum and maximum possible number of sentence fragments in a formatted sentence.
Definition generatorsettings.hpp:99
ArgVec2 point
The minimum and maximum possible number of points in lists or statements in code blocks.
Definition generatorsettings.hpp:74
ArgVec2 sent
The minimum and maximum possible number of sentences in a paragraph.
Definition generatorsettings.hpp:57
ArgVec2 para
The minimum and maximum possible number of paragraphs in a text.
Definition generatorsettings.hpp:65
ArgVec2 frag
The minimum and maximum possible number of sentence fragments in a sentence.
Definition generatorsettings.hpp:49
ArgVec2 word
The minimum and maximum possible number of words in a sentence fragment.
Definition generatorsettings.hpp:41
ArgVec2 wordURL
The minimum and maximum possible number of words at the end of a URL, in a heading,...
Definition generatorsettings.hpp:83
ArgVec2 wordFmt
The minimum and maximum possible number of words in a sentence fragment in formatted sentences.
Definition generatorsettings.hpp:91
ArgVec2 jsonLength
The minimum and maximum possible number of items in JSON objects and arrays.
Definition generatorsettings.hpp:115
ArgVec2 level
The minimum and maximum possible heading levels, excluding the main heading.
Definition generatorsettings.hpp:107
ArgVec2 csvRows
The minimum and maximum possible number of rows in CSV data.
Definition generatorsettings.hpp:122
void toggle_lazy()
Toggle the lazy flag.
Definition generatorcore.inl:70