-
Notifications
You must be signed in to change notification settings - Fork 0
/
random.js
397 lines (361 loc) · 8.87 KB
/
random.js
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
window.RANDOM =
{
endToken: String.fromCharCode(3),
waitingDependencies: 0
};
window.RANDOM.generators =
{
"name":
{
"fallout4":
{
"name": "Fallout 4 Names",
"description": "Generates random names based on Codsworth's name list."
},
"eternity":
{
"name": "Pillars of Eternity Names",
"description": "Generates random names based on character names in Pillars of Eternity."
},
"english":
{
"name": "English First Names",
"description": "Generates random English(ish) first names (male or female)."
},
"mineral":
{
"name": "Mineral Names (Realistic)",
"description": "Generates random names for realistic-sounding minerals. Seed data from Wikipedia:List of minerals."
},
"town_british":
{
"name": "Town Names (British)",
"description": "Generates random names for British-sounding towns. Seed data from Wikipedia:List of towns in England."
}
},
"title":
{
"videogame":
{
"name": "Video Game Titles",
"description": "Generates random names for video games. Seed data collected from multiple Wikipedia categories."
}
}
};
window.RANDOM.loadGenerator = function(data)
{
/*File format:
type
0: markov letters
1: markov words
NaN: equally-weighted list
0/1:
title
desc
order
minTokens
maxTokens
startTokenCount
{
token
nextTokenCount
{
token(s)
frequency
}
}
end
*/
var stream = {
tokens: data.split(/\r?\n/),
streamPos: 0,
read: function()
{
if (this.eof())
{
console.error("Tried to read past end of token stream.");
return "";
}
else
return this.tokens[this.streamPos++];
},
eof: function()
{
return this.streamPos >= this.tokens.length;
}
};
//Load data
var generator = {};
var first = stream.read();
generator.type = parseInt(first);
if (generator.type === 0 || generator.type === 1)
{
generator.title = stream.read();
generator.description = stream.read();
//Markov model
generator.order = parseInt(stream.read());
generator.minTokens = parseInt(stream.read());
generator.maxTokens = parseInt(stream.read());
generator.chain = [];
var startTokens = parseInt(stream.read());
for (var c = 0; c < startTokens; c++)
{
var cObj = {};
var tokenc = stream.read();
generator.chain[tokenc] = cObj;
cObj.next = [];
var nextTokens = parseInt(stream.read());
for (var d = 0; d < nextTokens; d++)
{
var dObj = {};
cObj.next[d] = dObj;
dObj.token = stream.read();
dObj.freq = parseInt(stream.read());
}
}
//Create dependent data
for (var c in generator.chain)
{
var cObj = generator.chain[c];
//Sum frequencies
cObj.totalFreq = 0;
for (var i in cObj.next)
cObj.totalFreq += cObj.next[i].freq;
}
generator.generate = window.RANDOM.markovGenerate;
}
else if (!generator.type)
{
//Random list, equal
generator = [];
generator.push(first);
while (!stream.eof())
generator.push(stream.read());
generator.generate = window.RANDOM.listGenerate;
}
return generator;
}
window.RANDOM.loadDependencies = function(generator, callback)
{
//HACK: identifying type by function
if (generator && generator.generate == window.RANDOM.listGenerate)
{
for (var i = 0; i < generator.length; i++)
{
var split = generator[i].split("{{insert|");
for (var e = 1; e < split.length; e++)
{
var split2 = split[e].split("}}");
if (!this.isGeneratorLoaded(split2[0]))
{
console.log("got dep '" + split2[0]);
window.RANDOM.waitingDependencies++;
window.RANDOM.loadGeneratorFromUrl(split2[0], false, notifyDependencyFinished);
}
}
}
}
callback();
}
window.RANDOM.getGenerator = function(id)
{
var split = id.split("/");
if (this.generators[split[0]])
{
return this.generators[split[0]][split[1]].generator;
}
else
{
return null;
}
}
window.RANDOM.isGeneratorLoaded = function(id)
{
return !!this.getGenerator(id);
}
var notifyDependencyFinished = function()
{
console.log("finished dep");
window.RANDOM.waitingDependencies--;
if (window.RANDOM.waitingDependencies === 0)
{
onGenerate();
}
}
window.RANDOM.loadGeneratorFromUrl = function(id, setAsPrimary, callback)
{
var url = "data/" + id + ".txt";
var idSplit = id.split("/");
var request = new window.XMLHttpRequest();
request.onreadystatechange=function()
{
if (request.readyState == 4)
{
if (request.status != 200)
{
document.getElementById("content-results").innerHTML = request.statusText;
console.error("AJAX Error Response: " + request.statusText);
}
else
{
var generator = window.RANDOM.loadGenerator(request.responseText);
if (!window.RANDOM.generators[idSplit[0]])
{
window.RANDOM.generators[idSplit[0]] = {};
}
if (!window.RANDOM.generators[idSplit[0]][idSplit[1]])
{
window.RANDOM.generators[idSplit[0]][idSplit[1]] = {};
}
var content = window.RANDOM.generators[idSplit[0]][idSplit[1]];
content.generator = generator;
if (setAsPrimary)
{
window.RANDOM.current = generator;
var element = document.getElementById("generator-info");
if (element)
{
element.innerHTML = "<b>" + content.name + "</b>: " + content.description;
}
}
window.RANDOM.loadDependencies(generator, callback);
}
}
};
request.open("GET", url, true);
request.setRequestHeader("content-type", "application/x-www-form-urlencoded")
request.send();
document.getElementById("content-results").innerHTML = "Loading...";
}
window.RANDOM.markovGenerate = function()
{
var attemptsAllowed = 10;
do
{
attemptsAllowed--;
var result = "";
var precedingTokens = [];
do
{
//Create leading-up key
var key = "";
for (var d = 0; d < this.order && d < precedingTokens.length; d++)
{
if (this.type === 1 && d > 0)
key = " " + key;
key = precedingTokens[precedingTokens.length-d-1] + key;
}
//Select a succeeding character
var cObj = this.chain[key];
if (cObj === undefined)
{
console.error("markovGenerate: Undefined entry for key '" + key + "'.");
return "*Error*";
}
var rnd = Math.floor(Math.random() * cObj.totalFreq);
var token = undefined;
for (var d = 0; d < cObj.next.length; d++)
{
if (rnd < cObj.next[d].freq)
{
token = cObj.next[d].token;
break;
}
rnd -= cObj.next[d].freq;
}
//Append it
if (token !== window.RANDOM.endToken)
{
if (this.type === 1)
result += " ";
result += token;
precedingTokens.push(token);
}
} while (token !== window.RANDOM.endToken);
} while (attemptsAllowed > 0 && (precedingTokens.length < this.minTokens || precedingTokens.length > this.maxTokens))
//console.log("Range: " + this.minTokens + "-" + this.maxTokens);
//console.log("Attempts Left: " + attemptsAllowed);
return result;
}
window.RANDOM.listGenerate = function()
{
var string = this[Math.floor(Math.random() * this.length)];
// replace insert macros
var split = string.split("{{insert|");
string = split[0];
for (var e = 1; e < split.length; e++)
{
var split2 = split[e].split("}}");
var generator = window.RANDOM.getGenerator(split2[0]);
console.log("generate off of " + split2[0]);
string += generator.generate();
string += split2[1];
}
return string;
}
var handleFileSelect = function(evt)
{
var files = evt.target.files;
if (files.length > 0)
{
var reader = new FileReader();
reader.onload = function(e)
{
window.RANDOM.current = window.RANDOM.loadGenerator(e.target.result);
onGenerate();
}
reader.readAsText(files[0]);
}
}
var onGenerate = function()
{
if (window.RANDOM.waitingDependencies > 0)
{
return;
}
if (!window.RANDOM.current)
{
document.getElementById("content-results").innerHTML = "Choose a generator below.";
return;
}
var quantity = document.getElementById("genQty").value;
var result = "";
for (var d = 0; d < quantity; d++)
{
if (d > 0)
result += "<br/>";
result += window.RANDOM.current.generate();
}
document.getElementById("content-results").innerHTML = result;
}
var onAttachDom = function()
{
document.getElementById("fileChooser").addEventListener('change', handleFileSelect, false);
//Populate generator selections
var selections = document.getElementById("generator-selections");
for (var folder in window.RANDOM.generators)
{
for (var generator in window.RANDOM.generators[folder])
{
var meta = window.RANDOM.generators[folder][generator];
var child = document.createElement("div");
child.className = "generator-selection-item";
child.innerHTML = "<b><a href=\"?id=" + folder + "/" + generator + "\">" + meta.name + "</a></b>: " + meta.description;
selections.appendChild(child);
}
selections.appendChild(document.createElement("br"));
}
//Load generator requested by params
var id;
var params = window.location.search.substr(1).split("&");
for (var i in params)
{
var split = params[i].split("=");
if (split[0] == "id")
{
id = decodeURIComponent(split[1]);
window.RANDOM.loadGeneratorFromUrl(id, true, onGenerate);
break;
}
}
}