This repository has been archived by the owner on Aug 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
script.js
368 lines (338 loc) · 11 KB
/
script.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
// Ideas:
// -rewrite boxes in the correct way after forms are submitted
// -max possible words counter
// -Remove spaces from final word
// -night mode
// -add seperate IPA chart for mobile, dont add table head just make it a group of characters
// -add titles to IPA symbols
// -made comments italicized
// To Add:
// -add LITERAL notation ("C")V(N), C"V"N
// -make phonetic groups possible in replacemenets and filters (VV: V)
// -floating point weights (fix the equation in general)
// -favicon
// To Fix:
// -Affricate buttons put cursor after the first letter of the affricate
function generate(
_min,
_max,
_count,
_patterns,
_filters,
_rewrites,
_characters,
_duplicates,
_charWeights,
_writeWeights
) {
// Variables
const min = _min
const max = _max
const count = _count
const patterns = unique(_patterns)
const filters = unique(_filters)
const rewrites = new Map(_rewrites)
const characters = new Map(_characters)
const duplicates = _duplicates
const charWeights = new Map(_charWeights)
const writeWeights = new Map(_writeWeights)
let lexicon = []
// Test if patterns are valid
if (patterns.length < 1) {return lexicon}
// Functions
function unique(list) {
return Array.from(new Set(list))
}
function random(min, max) {
return Math.floor(Math.random() * (max - min)) + min
}
function weightedRandom(items, weights) {
function float(min, max) {
return Math.random() * (max - min) + min
}
const sum = weights.reduce((a, b) => a + b, 0)
let ran = float(0, sum)
for (let i = 0; i < items.length; i++) {
if (ran < weights[i]) {
return [items[i]]
}
else {
ran -= weights[i]
}
}
}
// Loop
while (lexicon.length < count) {
// Create the template
let syllableCount = random(min, max + 1)
let template = ''
for (let i = 0; i < syllableCount; i++) {
template += patterns[random(0, patterns.length)]
}
// Pick random letter(s) in brackets
const bRegex = /\[([^\[]*?)\]/
while (bRegex.test(template)) {
let matches = bRegex.exec(template)[1].split(/\s+/)
template = template.replace(bRegex, matches[random(0, matches.length)])
}
// Make word from template
let word = ''
for (let key of template) {
let letters = characters.get(key)
let weights = charWeights.get(key)
word += letters && letters.length > 0 ? weightedRandom(letters, weights) : key
}
// Randomly keep or delete letters in parentheses
const pRegex = /\(([^\(]*?)\)/
while (pRegex.test(word)) {
word = word.replace(pRegex, random(0, 2) === 1 ? pRegex.exec(word)[1] : '')
}
// Rewrites
for (let key of rewrites.keys()) {
let letters = rewrites.get(key)
let weights = writeWeights.get(key)
while (word.includes(key)) {
word = word.replace(key, letters.length > 0 ? weightedRandom(letters, weights) : '')
}
}
// Filters
let push = true
for (let filter of filters) {
if (word.includes(filter)) {
push = false
}
}
// Add word to lexicon
if (push) {
lexicon.push(word)
}
}
// Remove duplicates
if (!duplicates) {
lexicon = unique(lexicon)
}
// Return
console.log('Characters:', characters, '\n')
console.log('Character Weights:', charWeights, '\n')
console.log('Rewrites:', rewrites, '\n')
console.log('Rewrite Weights:', writeWeights, '\n')
console.log('Filters:', filters, '\n')
console.log('Patterns:', patterns, '\n')
return lexicon
}
// Format inputs and display list to the page
function takeInput() {
// Functions
function format(element, regex) {
const array = []
// Remove comments
element = element.replace(/\/[^\s,]*/g, '')
// Create the arrays
const matches = element.match(regex)
if (matches !== null) {
for (match of matches) {
let items = []
match = match.replace(/\(|\)|\[|\]|"|:/g, ' ')
match = match.match(/\S+/g)
let name = match[0]
let contents = match.slice(1, match.length)
array.push([name, contents])
}
}
return array
}
function listFormat(element) {
// Remove comments
element = element.replace(/\/[^\s,]*/g, '')
// Split list
const regex = /[^\s,][^,]*[^\s,]|[^\s,]/g
return element.match(regex)
}
function makeWeights(input) {
const array = []
const regex = /(.+)\*((\d+\.*\d*)|(\.\d+))/
for (item of input) {
let pair = []
let weights = []
let contents = item[1]
pair.push(item[0])
for (i in contents) {
if (regex.test(contents[i])) {
weights.push(Number(regex.exec(contents[i])[2]))
item[1][i] = regex.exec(contents[i])[1]
} else {
weights.push(1)
}
}
pair.push(weights)
array.push(pair)
}
return array
}
// Variables
const myMin = Number(document.getElementById("min").value)
const myMax = Number(document.getElementById("max").value)
const myCount = Number(document.getElementById("words").value)
const myPatterns = listFormat(document.getElementById("patterns").value)
const myFilters = listFormat(document.getElementById("filters").value)
const myRewrites = format(document.getElementById("rewrites").value, /[^\s,]+\s*:[^,]*/g)
const myCharacters = format(document.getElementById("characters").value, /[^\s,]\s*:[^,]*/g)
const myDuplicate = document.getElementById("duplicates").checked
const myCharWeights = makeWeights(myCharacters)
const myWriteWeights = makeWeights(myRewrites)
const newLine = document.getElementById("new").checked
const myLexicon = generate(
myMin,
myMax,
myCount,
myPatterns,
myFilters,
myRewrites,
myCharacters,
myDuplicate,
myCharWeights,
myWriteWeights
)
// Fix form inputs
myMax < myMin ? document.getElementById("max").value = myMin : null
// Clear words
const list = document.getElementById("lexicon")
list.innerHTML = ''
// Display words
for (item of myLexicon) {
list.innerHTML += item + (newLine ? '\n' : ' ')
}
// Information
const divider = document.getElementById("divider")
divider.style.display = "block"
const info = document.getElementById("info")
info.innerHTML = '~ ' + myLexicon.length + ' words generated ~'
}
// Copy list
function copyToClipboard() {
const copy = document.getElementById("copy")
const list = document.getElementById("lexicon").innerHTML
let info = document.getElementById("info")
if (list !== '') {
navigator.clipboard.writeText(list)
info.innerHTML = "~ Copied to clipboard! ~"
}
}
// Test if user is on mobile device
const onMobile = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)
// Show IPA chart
function showIPA() {
const ipa = !onMobile ? document.getElementById("ipaChart") : document.getElementById("ipaMobile")
const button = document.getElementById("ipa")
if (ipa.style.display !== "block") {
ipa.style.display = "block"
button.innerHTML = "IPA<i class='fas fa-caret-up'></i>"
} else {
ipa.style.display = "none"
button.innerHTML = "IPA<i class='fas fa-caret-down'>"
}
}
// If number is greater than max number, set number to max
const inputs = document.getElementsByTagName("input")
const numbers = []
for (let input of inputs) {
if (input.type === "number") {
numbers.push(input)
}
}
for (let number of numbers) {
number.addEventListener("input", () => {
let max = number.max
if (Number(number.value) > Number(max)) {
number.value = max
}
})
}
// When symbol is clicked, add it to the current focused input
const letters = document.getElementsByClassName("letter")
for (let letter of letters) {
letter.addEventListener("mousedown", () => {
let active = document.activeElement
if (active.id === "characters" || active.id === "rewrites" || active.id === "patterns" || active.id === "filters") {
let start = active.selectionStart;
let end = active.selectionEnd;
if (start || start <= '0') {
active.value = active.value.substring(0, start)
+ letter.innerHTML
+ active.value.substring(end, active.value.length);
}
let newStart = start + letter.innerHTML.length
active.setSelectionRange(newStart, newStart)
}
event.preventDefault()
})
}
const puncts = document.getElementsByClassName("punct")
for (let punct of puncts) {
punct.addEventListener("mousedown", () => {
let active = document.activeElement
if (active.id === "characters" || active.id === "rewrites" || active.id === "patterns" || active.id === "filters") {
let start = active.selectionStart;
let end = active.selectionEnd;
console.log(start)
if (start || start <= '0') {
active.value = active.value.substring(0, start)
+ punct.getAttribute("value")
+ active.value.substring(end, active.value.length);
}
active.setSelectionRange(start + 1, start + 1)
}
event.preventDefault()
})
}
// Fix input's position if scrolled too far down
if (!onMobile) {
function resetInputs () {
document.getElementById("characters").style.position = "initial"
document.getElementById("characters").style.minWidth = "initial"
document.getElementById("characters").style.left = "initial"
document.getElementById("characters").style.top = "initial"
document.getElementById("rewrites").style.position = "initial"
document.getElementById("rewrites").style.minWidth = "initial"
document.getElementById("rewrites").style.left = "initial"
document.getElementById("rewrites").style.top = "initial"
document.getElementById("patterns").style.position = "initial"
document.getElementById("patterns").style.minWidth = "initial"
document.getElementById("patterns").style.maxWidth = "initial"
document.getElementById("patterns").style.left = "initial"
document.getElementById("patterns").style.top = "initial"
document.getElementById("filters").style.position = "initial"
document.getElementById("filters").style.minWidth = "initial"
document.getElementById("filters").style.maxWidth = "initial"
document.getElementById("filters").style.left = "initial"
document.getElementById("filters").style.top = "initial"
}
function fixInput() {
let active = document.activeElement
if (
active.id === "characters"
|| active.id === "rewrites"
|| active.id === "patterns"
|| active.id === "filters"
) {
if (document.getElementById("ipaChart").style.display === "block") {
if (window.scrollY > 300 && window.scrollY < 1200) {
active.style.position = "initial"
let calculate = window.innerWidth/2 - (active.id === "rewrites" ? active.offsetWidth : active.innerWidth)/2
active.style.left = calculate + 'px'
active.style.top = "16px"
active.style.minWidth = "480px"
active.style.maxWidth = "480px"
active.style.position = "fixed"
} else {
resetInputs()
}
} else {
resetInputs()
}
} else {
resetInputs()
}
}
window.setInterval(fixInput, 250)
}