aboutsummaryrefslogtreecommitdiff
path: root/examples/redis-unstable/src/lolwut8.c
diff options
context:
space:
mode:
authorMitja Felicijan <mitja.felicijan@gmail.com>2026-01-21 22:52:54 +0100
committerMitja Felicijan <mitja.felicijan@gmail.com>2026-01-21 22:52:54 +0100
commitdcacc00e3750300617ba6e16eb346713f91a783a (patch)
tree38e2d4fb5ed9d119711d4295c6eda4b014af73fd /examples/redis-unstable/src/lolwut8.c
parent58dac10aeb8f5a041c46bddbeaf4c7966a99b998 (diff)
downloadcrep-dcacc00e3750300617ba6e16eb346713f91a783a.tar.gz
Remove testing data
Diffstat (limited to 'examples/redis-unstable/src/lolwut8.c')
-rw-r--r--examples/redis-unstable/src/lolwut8.c179
1 files changed, 0 insertions, 179 deletions
diff --git a/examples/redis-unstable/src/lolwut8.c b/examples/redis-unstable/src/lolwut8.c
deleted file mode 100644
index f01f09e..0000000
--- a/examples/redis-unstable/src/lolwut8.c
+++ /dev/null
@@ -1,179 +0,0 @@
1/*
2 * Copyright (c) 2025-Present, Redis Ltd.
3 * All rights reserved.
4 *
5 * Licensed under your choice of (a) the Redis Source Available License 2.0
6 * (RSALv2); or (b) the Server Side Public License v1 (SSPLv1); or (c) the
7 * GNU Affero General Public License v3 (AGPLv3).
8 *
9 * Originally authored by: Salvatore Sanfilippo.
10 * Algorithm based on the Almanacco Bompiani description and the Python
11 * code written by Emiliano Russo.
12 */
13
14#include "server.h"
15#include <ctype.h>
16
17/* The LOLWUT 8 command:
18 *
19 * LOLWUT [EN|IT]
20 *
21 * By default the command produces verses in English language, in order for
22 * the output to be more universally accessible. However, passing IT as argument
23 * it is possible to reproduce the original output, exactly like done by
24 * Nanni Balestrini in TAPE MARK I, and described in the Almanacco Letterario
25 * Bompiani, 1962.
26 */
27
28// Structure to represent a verse with its metrical characteristics.
29typedef struct {
30 char text_en[100]; // English verse text.
31 char text_it[100]; // Italian verse text.
32 char fraction1[5]; // First fraction (rhythm/meter indicator).
33 char fraction2[5]; // Second fraction (rhythm/meter indicator).
34 char group[2]; // Group number (1-3 representing different
35 // literary sources).
36} Verse;
37
38// Fisher-Yates shuffle algorithm to randomize verse order.
39static void shuffle(Verse *array, int size) {
40 for (int i = size - 1; i > 0; i--) {
41 int j = rand() % (i + 1);
42 Verse temp = array[j];
43 array[j] = array[i];
44 array[i] = temp;
45 }
46}
47
48void lolwut8Command(client *c) {
49 int en_lang = 1; // Default to English.
50
51 /* Parse the optional arguments if any. */
52 if (c->argc > 1 && !strcasecmp(c->argv[1]->ptr,"IT"))
53 en_lang = 0;
54
55 // Define verses from three literary sources with their metrical fractions:
56 // Group 1: Diary of Hiroshima by Michihito Hachiya.
57 // Group 2: The Mystery of the Elevator by Paul Goldwin.
58 // Group 3: Tao Te Ching by Lao Tse.
59 Verse verses[] = {
60 // Group 1: Hiroshima verses.
61 {" The blinding / globe / of fire ",
62 " l accecante / globo / di fuoco ", "1/4", "2/3", "1"},
63 {" It expands / rapidly ",
64 " si espande / rapidamente ", "1/2", "3/4", "1"},
65 {" Thirty times / brighter / than the sun ",
66 " trenta volte / piu luminoso / del sole ", "2/3", "2/4", "1"},
67 {" When it reaches / the stratosphere ",
68 " quando raggiunge / la stratosfera ", "3/4", "1/2", "1"},
69 {" The summit / of the cloud ",
70 " la sommita / della nuvola ", "1/3", "2/3", "1"},
71 {" Assumes / the well-known shape / of a mushroom ",
72 " assume / la ben nota forma / di fungo ", "2/4", "3/4", "1"},
73
74 // Group 2: Elevator mystery verses.
75 {" The head / pressed / upon the shoulder ",
76 " la testa / premuta / sulla spalla ", "1/4", "2/4", "2"},
77 {" The hair / between the lips ",
78 " i capelli / tra le labbra ", "1/4", "2/4", "2"},
79 {" They lay / motionless / without speaking ",
80 " giacquero / immobili / senza parlare ", "2/3", "2/3", "2"},
81 {" Till he moved / his fingers / slowly ",
82 " finche non mosse / le dita / lentamente ", "3/4", "1/3", "2"},
83 {" Trying / to grasp ",
84 " cercando / di afferrare ", "3/4", "1/2", "2"},
85
86 // Group 3: Tao Te Ching verses.
87 {" While the multitude / of things / comes into being ",
88 " mentre la moltitudine / delle cose / accade ", "1/2", "1/2", "3"},
89 {" I envisage / their return ",
90 " io contemplo / il loro ritorno ", "2/3", "3/4", "3"},
91 {" Although / things / flourish ",
92 " malgrado / che le cose / fioriscano ", "1/2", "2/3", "3"},
93 {" They all return / to / their roots ",
94 " esse tornano / tutte / alla loro radice ", "2/3", "1/4", "3"}
95 };
96
97 // Calculate the total number of verses.
98 int num_verses = sizeof(verses) / sizeof(verses[0]);
99
100 // Create a working copy of verses for manipulation.
101 Verse *working_verses = zmalloc(num_verses * sizeof(Verse));
102 memcpy(working_verses, verses, num_verses * sizeof(Verse));
103
104 // Step 1: Shuffle the verses randomly.
105 shuffle(working_verses, num_verses);
106
107 // Step 2: Build stanza by finding compatible verses
108 // Each subsequent verse must:
109 // - Have compatible metrical fractions (connecting criteria).
110 // - Belong to a different group than the previous verse.
111 Verse stanza[10];
112
113 int j; // At the end, it will contain the number of added stanzas.
114 for (j = 0; j < 10; j++) {
115 int i = 0;
116 int found = 0;
117
118 // Search for compatible verse among remaining verses.
119 while (i < num_verses) {
120 // Metrical compatibility check: this is used to select verses
121 // that go somewhat well together, if their fractions match.
122 // The algorithm checks if current verse's first fraction matches
123 // with previous verse's second fraction in various ways, and
124 // force successive verses to be of different groups.
125 if (j == 0 || // First stanza is always accepted.
126 ((working_verses[i].fraction1[0] == stanza[j-1].fraction2[0] ||
127 working_verses[i].fraction1[2] == stanza[j-1].fraction2[0] ||
128 working_verses[i].fraction1[2] == stanza[j-1].fraction2[2]) &&
129 strcmp(working_verses[i].group, stanza[j-1].group) != 0))
130 {
131
132 // Add compatible verse to stanza.
133 stanza[j] = working_verses[i];
134
135 // Remove selected verse from working set, to avoid reuse.
136 for (int k = i; k < num_verses - 1; k++)
137 working_verses[k] = working_verses[k + 1];
138 num_verses--;
139
140 found = 1;
141 break;
142 }
143 i++;
144 }
145
146 // Exit if there are no longer matching verses.
147 if (!found) break;
148 }
149 zfree(working_verses);
150
151 // Step 3: Combine all stanza verses into single SDS string.
152 sds combined = sdsempty();
153 for (int i = 0; i < j; i++) {
154 if (en_lang) {
155 combined = sdscat(combined, stanza[i].text_en);
156 } else {
157 combined = sdscat(combined, stanza[i].text_it);
158 }
159 combined = sdscat(combined, "\n");
160 }
161
162 // Step 4: Make uppercase, and strip the "/".
163 for (size_t j = 0; j < sdslen(combined); j++) {
164 combined[j] = toupper(combined[j]);
165 if (combined[j] == '/') combined[j] = ' ';
166 }
167
168 // Step 5: Add background info about what the user just saw.
169 combined = sdscat(combined,
170 "\nIn 1961, Nanni Balestrini created one of the first computer-generated poems, TAPE MARK I, using an IBM 7090 mainframe. Each execution combined verses from three literary sources following algorithmic rules based on metrical compatibility and group constraints. This LOLWUT command reproduces Balestrini's original algorithm, generating new stanzas through the same computational poetry process described in Almanacco Letterario Bompiani, 1962.\n\n"
171 "https://en.wikipedia.org/wiki/Digital_poetry\n"
172 "https://www.youtube.com/watch?v=8i7uFCK7G0o (English subs)\n\n"
173 "Use: LOLWUT IT for the original Italian output. Redis ver. ");
174 combined = sdscat(combined,REDIS_VERSION);
175 combined = sdscatlen(combined,"\n",1);
176
177 addReplyVerbatim(c,combined,sdslen(combined),"txt");
178 sdsfree(combined);
179}