summaryrefslogtreecommitdiff
path: root/amaryllis.cgi
blob: 12d2c150d330807a60ea54088143240da7d9feb6 (plain)
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
#!/usr/bin/lua5.1

local marigold = require 'marigold'
local lang = require 'language'

local h = marigold.h
local metavars = marigold.get_metavars()
local settings = marigold.decode_query(metavars.query_string or '')


local body
if not settings.help then
	settings.randomize_seed = settings.randomize_seed or false
	if settings.randomize_seed then
		settings.seed = os.time()
	else
		settings.seed = tonumber(settings.seed) or os.time()
	end
	math.randomseed(settings.seed)
	
	settings.phonemes = settings.phonemes or [[
C=mnbdTLshzwrly
V=aeiou
T=1324
	]]
	
	settings.syllables = settings.syllables or [[
CVT
CVTl
CVTn
	]]
	
	settings.orthography = settings.orthography or [[
T=th
L=lh
z=zh
(%V)1=%1́
(%V)2=%1%1́
(%V)3=%1
(%V)4=%1́%1
	]]
	
	settings.phonemes_decay = settings.phonemes_decay or 'Q'
	settings.syllables_decay = settings.syllables_decay or 'Q'
	
	settings.len_min = settings.len_min or "1"
	settings.len_max = settings.len_max or "3"
	settings.count = settings.count or "20"
	
	
	local distributions = {
		U=lang.uniform,
		Q=lang.quadratic,
	}
	
	local p_dist = distributions[settings.phonemes_decay]
	local s_dist = distributions[settings.syllables_decay]
	
	
	local l = lang.Language(
		settings.phonemes,
		settings.syllables,
		settings.orthography,
		tonumber(settings.len_min),
		tonumber(settings.len_max),
		p_dist,
		s_dist
	)
	
	
	local syllable_html = {}
	for i=1,tonumber(settings.count) do
		table.insert(
			syllable_html,
			h('li', l:romanize(l:word()))
		)
	end
	syllable_html = h('ol', syllable_html)
	
	
	function radiobutton(label, name, value, checked)
		local radio = h('input', {
			type="radio",
			name=name,
			id = name .. value,
			value = value,
			checked = (checked and "true") or nil
		})
		local lbl = {}
		lbl['for'] = name .. value
		local label = h('label', label, lbl)
	
		return h('div', { radio, label })
	end
	
	
	function textarea(name, text, decay)
		local children = { class="noborder",
			h('legend', name),
			h('textarea', text, { name=name, rows="6", spellcheck="false" }),
		}
	
		if decay then table.insert(children, h('fieldset', {
				h('legend', name .. ' decay'),
				radiobutton("uniform", name .. '_decay', 'U', settings[name .. '_decay'] == 'U'),
				radiobutton("quadratic", name .. '_decay', 'Q', settings[name .. '_decay'] == 'Q'),
			})
		) end
	
		return h('fieldset', children)
	end
	
	
	local form = h('form', {
		h('label', 'seed', {
			h('input', { name="seed", type="number", value=tostring(settings.seed) }),
		}),
	
		h('input', { value="update", type="Submit" } ),
		h('br'),
		h('label', 'randomize seed', {
			h('input', { 
				name="randomize_seed", 
				type="checkbox", 
				checked=(settings.randomize_seed and "true") or nil,
				value=(settings.randomize_seed and "on") or nil,
			}),
		}),
	
		h('br'),
		h('div', { class="form-flex", 
			textarea('phonemes', settings.phonemes, true),
			textarea('syllables', settings.syllables, true),
			textarea('orthography', settings.orthography, false),
		}),
		h('br'),
	
		h('label', 'min syllables', {
			h('input', { name="len_min", type="number", min="1", value=settings.len_min }),
		}),
		h('br'),
		h('label', 'max syllables', {
			h('input', { name="len_max", type="number", min="1", value=settings.len_max }),
		}),
		h('br'),
		h('label', '# to generate', {
			h('input', { name="count", type="number", min="1", value=settings.count }),
		}),
	
		h('input', { value="update", type="Submit" } ),
	})


	if settings.hide then
		form = h('div', {
			h('form', { h('input', { value="Show details", type="Submit" })}),
			h('form', { 
				h('input', { name="hide", value="1", type="hidden" }),
				h('input', { value="Refresh", type="Submit" })
			})
		})
	end
	
	
	local self_path = metavars.path_info .. '/' .. metavars.script_name
	self_path = string.gsub(self_path, '^//', '/')
	
	body = h('body', { h('div', { id="content", 
		h('a', '<- projects', { href='https://sanine.net/projects/' }),
		h('br'), h('br'),
		form,
		h('hr'),
		syllable_html,
		h('a', 'share this link!', { 
			href=self_path .. '?' .. metavars.query_string 
		}),
		h('a', 'what the heck is this?', { href=self_path .. '?help=1' }),
	})})
else
	-- help page!
	body = h('body', { h('div', { id="content", 
		h('a', '<- projects', { href='https://sanine.net/projects/' }),
		h('p', 'This is a tool for generating words in invented languages. You can set up your basic phonology (int the phonemes tab), your phonotactics (in the syllables tab), and romanization/orthography issues (in the orthography tab).'),
		h('p', "The 'seed' value, at the top, is the mathematical value used for all of the other random generation. Change it to get new words, or leave it alone to tinker with the settings and see how they change the words you get."),
		h('p', "In the phonemes tab you should pick out the phonemes for your language. Because of how the syllables work, you should sort them into classes. Each line, you write something like <code>V=aeiou</code>, where V is the class and a, e, i, o, and u are the phones that are members of the class. (In this case the class letter V was selected to represent the vowels). The class name can only be one character long, but it can be any character. Take care not to make it a character that occurs within your phones; this may lead to strange results!"),
		h('p', "In the syllables tab, you define which syllables are possible for your language, using the phoneme classes defined earlier. Each line defines one syllable type. For instance, if we have a C and V class, then <code>CVC</code> would be a traditional CVC syllable. If you use letters in your syllable definitions which are <i>not</i> phoneme classes, they will simply be output as part of that syllable type verbatim."),
		h('p', "In the orthography tab, you define rewrite rules for text. Each rule looks like [pattern]=[replacement]. Anything that matches the pattern will be replaced with replacement. The special sequence (%x), where x is one of your phoneme classes, will match any one of the phones in that class. The special sequence %1 will then be replaced in the replacement with whichever phone matched the (%x) pattern. This can be useful to avoid writing a distinct rule for each element in the class."),
		h('p', "Phonemes and syllables each have an additional parameter called 'decay' which is essentially the distribution of the probabilities. Uniform means that each element has an equal probability of being selected. Quadratic means that earlier elements have a higher probability of being selected than later elements, making the sequencing important."),
		h('p', "Finally, min/max syllables is simply the minimum/maximum number of syllables to generate per word, and '# to generate' is the number of words to generate."),
		h('p', "Have fun!!!"),
	})})

end


local head = h('head', {
	h('meta', { charset="utf-8" }),
	h('meta', { name="viewport", content="width=device-width, initial-scale=1" }),
	h('title', 'amaryllis | sanine.net'),
	h('style', [[

			:root {
			    --light: #eee;
			    --dark: #1c1c1c;
			    --highlight: #f5ae2e;
			}
			
			body {
			    color: var(--light);
			    background: var(--dark);
			    font: 1.3em monospace;
			    text-size-adjust: auto;
			}

			#content {
				max-width: 40em;
				margin: auto;
			}

			.noborder {
				border: none;
				padding: 0px;
			}
			
			a {
			    color: var(--highlight);
			}
			
			a:hover {
			    color: var(--dark);
			    background: var(--highlight);
			    text-decoration: none;
			}

			.form-flex {
				display: flex;
				flex-wrap: wrap;
				wrap: 100%;
			}

			textarea {
				color: var(--light);
				background: #191919;
				border: 1px solid var(--highlight);
			}

			input {
				font-family: monospace;
				color: var(--highlight);
				background: var(--dark);
				border: 1px solid var(--highlight);
			}
		]]
	),
})


print('Content-type: text/html\n')
print(marigold.html(h('html', { head, body })))