Spec doesn't state what Unicode normalisation is allowed. Examples use NFC, but NFD is cheaper.
s=>s.replace(/\S+/g,w=>w.replace(x=/[aeiou]/gi,c=>v--?c:c+'̀́̂'[3*r()|0],v=(r=Math.random)()*w.match(x)?.length|0))
Attempt This Online! Note: My test does 1000 trials and checks to see if it got the example output. Occasionally it misses for the long ones.
This is basically the same as the old solution, just swapped out the vowel replacement function.
Old solution (NFC), 159 bytes
s=>s.replace(/\S+/g,w=>w.replace(x=/[aeiou]/gi,c=>v--?c:Buffer(c+c).map((n,j)=>j?n+68-4*!(n&12)-!(n&16)+3*r()|0:195),v=(r=Math.random)()*w.match(x)?.length|0))
Attempt This Online 2!
Ungolfed and somewhat explained
In UTF-8, all the accented character are 2 bytes, the first of which is always 195. The second can be calculated from the base letter.
char: A E I O U a e i o u
code: 65 69 73 79 85 97 101 105 111 117
&12: 0 4 8 12 4 0 4 8 12 4
&16: 0 0 0 0 16 0 0 0 0 16
with \: 128 136 140 146 153 160 168 172 178 185
with /: 129 137 141 147 154 161 169 173 179 186
with ^: 130 138 142 148 155 162 170 174 180 187
code to \: 63 67 67 67 68 63 67 67 67 68
code to /: 64 68 68 68 69 64 68 68 68 69
code to ^: 65 69 69 69 70 65 69 69 69 70
|---same---| |---same---|
For most letters, we just increase character code by 67 (for Aa
, increase by 63, for Uu
increase by 68), then offset by 0, 1, or 2 to add the hat.
s => s.replace( // replace each word
/\S+/g,
w => w.replace( // replace each vowel
x = /[aeiou]/gi,
c => v-- ? // if this is not the vowel to replace
c : // don't replace
Buffer(c + c).map( // otherwise create a Buffer of 2 bytes
(n, j) => j ? // map (replace) each byte; if it's the 2nd byte
n // start at n
+ 68 // add 68
- 4 * !(n & 12) // subtract 4 if it's 'A' or 'a'
- !(n & 16) // subtract 1 if it's not 'U' or 'u'
+ 3 * r() | 0 : // add randomly 0, 1, or 2 (and floor it)
195 // set the first byte to 195
),
v = (r = Math.random)() * w.match(x)?.length | 0 // choose vowel
)
)
get the current time
random? \$\endgroup\$tschüss
as having an accentedu
, but an NFD word liketschüss
as having a non-accentedu
, and yet the two look identical. Likewise, are there constraints on Unicode normalization of the ouptut? (I'd imagine NFD output would be shorter in bytes to generate.) \$\endgroup\$