/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under both the BSD-style license (found in the
* LICENSE file in the root directory of this source tree) and the GPLv2 (found
* in the COPYING file in the root directory of this source tree).
* You may select, at your option, one of the above-listed licenses.
*/
/* Implementation notes:
*
* This is a very simple lorem ipsum generator
* which features a static list of words
* and print them one after another randomly
* with a fake sentence / paragraph structure.
*
* The goal is to generate a printable text
* that can be used to fake a text compression scenario.
* The resulting compression / ratio curve of the lorem ipsum generator
* is more satisfying than the previous statistical generator,
* which was initially designed for entropy compression,
* and lacks a regularity more representative of text.
*
* The compression ratio achievable on the generated lorem ipsum
* is still a bit too good, presumably because the dictionary is a bit too
* small. It would be possible to create some more complex scheme, notably by
* enlarging the dictionary with a word generator, and adding grammatical rules
* (composition) and syntax rules. But that's probably overkill for the intended
* goal.
*/
/* Function to generate a random sentence */
static void generateSentence(int nbWords)
{
int commaPos = about(9);
int comma2 = commaPos + about(7);
int qmark = (LOREM_rand(11) == 7);
const char* endSep = qmark ? "? " : ". ";
int i;
for (i = 0; i < nbWords; i++) {
int const wordID = g_distrib[LOREM_rand(g_distribCount)];
const char* const word = kWords[wordID];
const char* sep = " ";
if (i == commaPos)
sep = ", ";
if (i == comma2)
sep = ", ";
if (i == nbWords - 1)
sep = endSep;
generateWord(word, sep, i == 0);
}
}
static void generateParagraph(int nbSentences)
{
int i;
for (i = 0; i < nbSentences; i++) {
int wordsPerSentence = about(11);
generateSentence(wordsPerSentence);
}
if (g_nbChars < g_maxChars) {
g_ptr[g_nbChars++] = '\n';
}
if (g_nbChars < g_maxChars) {
g_ptr[g_nbChars++] = '\n';
}
}
/* It's "common" for lorem ipsum generators to start with the same first
* pre-defined sentence */
static void generateFirstSentence(void)
{
int i;
for (i = 0; i < 18; i++) {
const char* word = kWords[i];
const char* separator = " ";
if (i == 4)
separator = ", ";
if (i == 7)
separator = ", ";
generateWord(word, separator, i == 0);
}
generateWord(kWords[18], ". ", 0);
}
if (first) {
generateFirstSentence();
}
while (g_nbChars < g_maxChars) {
int sentencePerParagraph = about(7);
generateParagraph(sentencePerParagraph);
if (!fill)
break; /* only generate one paragraph in not-fill mode */
}
g_ptr = NULL;
return g_nbChars;
}