[Question]
  [
# Article Spinning with Spintax
[Article spinning](https://www.google.ca/url?sa=t&rct=j&q=&esrc=s&source=web&cd=2&cad=rja&uact=8&ved=0CCQQFjAB&url=https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FArticle_spinning&ei=-C2EVYfsLoj1-QHrq7zwAQ&usg=AFQjCNGREvbaE6Y8WeFsVc73xXEWAndlkA&sig2=j5rz3g_FG9bqHZ8YeEAqAg) is a search engine optimization technique by which search engine optimizers post a unique version of relevant content on article directories, web 2.0 sites, or other sources for backlinks. 
It is occasionally used by website authors on their own sites but usually avoided because the quality of spun content will be lower than hand written content. Spinning works by rewriting existing articles, or parts of articles, and replacing specific words, phrases, sentences, or even entire paragraphs with any number of alternate versions to provide a slightly different variation with each spin. 
This process can be completely automated or rewritten manually. Your job today will be to create a program or function which interprets a string containing **Spintax**, the syntax by which a program would read the different options available. Below is an example string that uses Spintax:
```
{Hello|Hi there|Bonjour}, user! {What's up?|How are you?|Feeling alright?|I caught a fish!}
```
As you can see, sections of the string that are to be 'spun' are enclosed in curly braces, where individual options are split by the `|` character. Here are some example sentences this could produce.
```
Hi there, user! What's up?
Hello, user! How are you?
Bonjour, user! Feeling alright?
Hello, user! What's up?
Hi there, user! I caught a fish!
```
# Your Job
Your mission, should you choose to accept it, is to create a program or function which takes an input string that may or may not contain **Spintax**, then print 5 versions of the string, [randomly](http://meta.codegolf.stackexchange.com/questions/1324/standard-definitions-of-terms-within-specifications/1325#1325) selecting one of the choices from each block of options. You can make a few assumptions:
1. You will not need to handle nested Spintax (`{This|is {an|one}|example}`).
2. The `|`,`{` and `}` characters will not occur anywhere else other than within a Spintax block.
3. You do not need to check if the 5 output sentences are distinct, as long as the code selects options using your language's random number function.
4. You may assume the input ends with a newline.
# Rules
1. [No standard loopholes.](http://meta.codegolf.stackexchange.com/questions/1061/loopholes-that-are-forbidden-by-default)
# Tests
```
1. {A|The|One} {quick|magnificent|charming|lucky|fast|super|cool} {gold|tan|yellow|white|brown|silver} {beaver|packrat|cat|lion|tiger|fox|dog|wolf} {consumed|ate|pilfered} my {pastry|strudel|cake}.
-------------------------------------------
One cool brown wolf consumed my pastry.
...(3 more here)...
The charming yellow beaver ate my cake.
-------------------------------------------
2. This sentence is valid, and happens to have no Spintax!
-------------------------------------------
This sentence is valid, and happens to have no Spintax!
...(3 more here)...
This sentence is valid, and happens to have no Spintax!
-------------------------------------------
3. {beg|get|ton|sun|bun|can|top|big}{2|3|4|5}{A|B|C|D|E|F|G}{2|3|4|5|6|7}{at|as|be|ex|hi|}{!|#|%}
-------------------------------------------
top2F2hi%
...(3 more here)...
big3E4ex#
```
A fourth test can be found [here.](https://gist.github.com/anonymous/2fb5f68817a67e0ca6dd)
---
This is [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'"), so fewest number of bytes wins.
  
[Answer]
# Pyth, 18
```
V5sOMcR\|scR\{cz\}
```
[Try it online here](https://pyth.herokuapp.com/?code=V5sOMcR%5C%7CscR%5C%7Bcz%5C%7D&input=%7BA%7CThe%7COne%7D+%7Bquick%7Cmagnificent%7Ccharming%7Clucky%7Cfast%7Csuper%7Ccool%7D+%7Bgold%7Ctan%7Cyellow%7Cwhite%7Cbrown%7Csilver%7D+%7Bbeaver%7Cpackrat%7Ccat%7Clion%7Ctiger%7Cfox%7Cdog%7Cwolf%7D+%7Bconsumed%7Cate%7Cpilfered%7D+my+%7Bpastry%7Cstrudel%7Ccake%7D.&debug=0)
### Explanation:
```
V5sOMcR\|scR\{cz\}                     : z = input()
V5                                     : do this 5 times
              cz\}                     : split z on the } character
          cR\{                         : split each element of the resulting list on {
         s                             : join the list of lists back into a list of strings
     cR\|                              : split these strings on the | character
   OM                                  : Choose a random element from each inner list
  s                                    : join those choices, and print them
```
[Answer]
# Ruby, 46
With command-line flag `-p`, run
```
($_*=5).gsub!(/{(.+?)}/){$1.split(?|).sample}
```
Read in a string with a trailing newline. Concatenate it to itself 5 times, mutate it in place, and output the result. `/{(.+?)}/` is a regular expression with a lazy quantifier: without the `?`, it would match the leftmost `{` to the rightmost `}` instead of the nearest one. `$1` is a magic variable referring to the first capture group in each match, while the `?|` literal refers to the `|` character. `sample` is an array method returning a random element.
[Answer]
# CJam, ~~25~~ ~~22~~ ~~19~~ 18 bytes
```
q5*'}/{'{/~'|/mR}/
```
This code requires the input to contain a trailing newline.
Try it online in the [CJam interpreter](http://cjam.aditsu.net/#code=q5*'%7D%2F%7B'%7B%2F~'%7C%2FmR%7D%2F&input=%7BA%7CThe%7COne%7D%20%7Bquick%7Cmagnificent%7Ccharming%7Clucky%7Cfast%7Csuper%7Ccool%7D%20%7Bgold%7Ctan%7Cyellow%7Cwhite%7Cbrown%7Csilver%7D%20%7Bbeaver%7Cpackrat%7Ccat%7Clion%7Ctiger%7Cfox%7Cdog%7Cwolf%7D%20%7Bconsumed%7Cate%7Cpilfered%7D%20my%20%7Bpastry%7Cstrudel%7Ccake%7D.%0A).
### How it works
```
q     e# Read from STDIN.
5*    e# Repeat the input five times.
'}/   e# Split at right curly brackets.
{     e# For each chunk:
  '{/ e#   Split at left curly brackets.
  ~   e#   Dump both chunks on the stack.
  '|/ e#   Split the right chunk at vertical bars.
  mR  e#   Select a chunk, pseudo-randomly.
}/    e#
```
In each iteration, two chunks (one constant, one variable) are left on the stack and will be printed when the program exits.
[Answer]
# Python 3, 110 bytes
```
import re,random
exec('print("".join(random.choice(s.split("|"))for s in re.split("{(.*?)}",%r)));'%input()*5)
```
Regex splitting, like the others.
# Python 3, ~~121~~ ~~116~~ 114 bytes
```
from random import*
exec(("print(%r[:-1]);"%(input()+'"')).translate({123:"'+choice(['",124:"','",125:"'])+'"})*5)
```
This one performs the appropriate replacements then evaluates it. If it wasn't for escaping backslashes and apostrophes, this would have turned out shorter.
For example, suppose we have the Spintax
```
I {like|hate} {Python|C}.
```
The `.translate` performs a few conversions to give:
```
print('I '+choice(['like','hate'])+' '+choice(['Python','C'])+'.')
```
[Answer]
# Perl, 50
```
$_=$_ x5;s/{(.*?)}/@r=split"\\|",$1;$r[rand@r]/ge
```
`49` characters `+1` for `-p`.
It works by first setting `$_` to the 5 sentences. Then it does a global search and replace, where we search for each `{ | | }` group. It splits the group on `|`, and the picks a random element for the replacement.
Run with:
```
echo '{A|The|One} {quick|magnificent|charming|lucky|fast|super|cool} {gold|tan|yellow|white|brown|silver} {beaver|packrat|cat|lion|tiger|fox|dog|wolf} {consumed|ate|pilfered} my {pastry|strudel|cake}.' | perl -pe'$_=$_ x5;s/{(.*?)}/@r=split"\\|",$1;$r[rand@r]/ge'
```
[Answer]
# [Ostrich v0.7.0](https://github.com/KeyboardFire/ostrich-lang/releases/tag/v0.7.0-alpha), 27 characters
```
{`{.*?}`{);(;"|/.,R*F=}X}5*
```
Explanation:
```
{...}5*  repeat 5 times
  `{.*?}`  regex to match "spintax" sections
  {...}X   replace with...
    );(;     remove first and last characters (the curly brackets)
    "|/      split on |
    .,R*     multiply length of that array by a random number
    F=       take the floor of that and get the nth element of the array
```
(note: the 0.7.0 release was made after this question was posted, but the answer is still valid as all the commits that made up that release were all pushed before this questions was posted.)
[Answer]
# Pip, ~~25~~ ~~22~~ 20 bytes
Late to the party, but here's mine. Takes input as a command-line argument, which may need to be quoted.
```
L5P_@RR#_Ma^`}|{`^'|
```
Explanation:
```
L5                    Loop 5 times:
          a^`}|{`     Split cmdline arg on curly braces using regex
                 ^'|  Split each item of the result on pipe (if there isn't one,
                        the result is a single-item list)
         M            Map this function to each item a in the result:
   _@RR#_             Calculate a random number between 0 and len(item)-1; use it to
                        index into item
  P                   Print the resulting list, concatenating elements together
```
## 17 bytes (but not a valid submission)
```
L5PRC_Ma^`}|{`^'|
```
Requires the latest version of Pip, updated after this question was posted. The RC (random choice) operator was planned for some time, but I never got around to implementing it until just now. :^(
[Learn more about Pip](http://github.com/dloscutoff/pip)
[Answer]
# JavaScript ES6, ~~86~~ 84 bytes
```
f=s=>s.repeat(5).replace(/{(.+?)}/g,(_,e)=>(p=e.split`|`)[Math.random()*p.length|0])
```
This expects the input to have a trailing newline. It starts by repeating the input 5 times, and then replacing each Spintax string with a random word from it, which is obtained by splitting out the `|` characters and then choosing a random number between 0 and the length of the resulting array minus 1. In this case, `|0` is just a shorter way of doing `Math.floor`. Thanks to vihan1086 for reminding me about tagged template strings.
The Stack Snippet below contains ungolfed and easily testable code.
```
f=function(s){
  return s.repeat(5).replace(/{(.+?)}/g,function(_,e){
    return (p=e.split('|'))[Math.random()*p.length|0]
  })
}
run=function(){document.getElementById('output').innerHTML=f(document.getElementById('input').value+'\n')};document.getElementById('run').onclick=run;run()
```
```
<input type="text" id="input" value="{A|The|One} {quick|magnificent|charming|lucky|fast|super|cool} {gold|tan|yellow|white|brown|silver} {beaver|packrat|cat|lion|tiger|fox|dog|wolf} {consumed|ate|pilfered} my {pastry|strudel|cake}." style="width: 400px;" /><button id="run">Run</button><br />
<pre id="output"></pre>
```
[Answer]
# Perl, 82 bytes
```
while($o=<>){for(0..4){$_=$o;s/{(.*?)}/@x=split\/\|\/,$1 and$x[rand@x]/ge;print;}}
```
[Answer]
# Python 2, 139 bytes
Added two bytes for quotes around input string. If these are not needed let me know.
[**Try it here**](http://ideone.com/vj3MLk)
```
import re,random
s=input()
exec"print''.join(t*(t!=''and(t[0]!='{'))or random.choice(t[1:].split('|'))for t in re.split('({.*?)}',s));"*5
```
[Answer]
# Java, 243 215 242 234 bytes
```
int i,k;void f(String s){String a,b[],c=s;for(;k++<5;System.out.println(c),c=s)while((i=c.indexOf("{"))>=0){a=c.substring(i,c.indexOf("}")+1);b=a.replaceAll("\\{|\\}","").split("\\|");c=c.replace(a,b[(int)(Math.random()*b.length)]);}}
```
Finds strings within braces `{}` and replaces them with a random element from the array of strings created by splitting by the pipe character. (I realized a little too late that five sentences had to be printed :P)
[Answer]
# Bash: ~~144~~ 138 characters
```
IFS={} read -ap
w()(for o in "${p[@]}";{
[[ $o =~ \| ]]&&{
IFS=\| read -aa<<<"$o"
o=${a[RANDOM%${#a[@]}]}
}
echo -n "$o"
}
echo)
w
w
w
w
w
```
Sample run:
```
bash-4.3$ bash spintax.sh <<< "Look {ma'|daddy|mr. president}! No {bin|core|doc|find|mail}utils tools nor {Awk|Sed|jq|XML Starlet}!"
Look ma'! No docutils tools nor Awk!
Look daddy! No binutils tools nor XML Starlet!
Look mr. president! No docutils tools nor XML Starlet!
Look ma'! No docutils tools nor Awk!
Look mr. president! No binutils tools nor Sed!
```
[Answer]
# Javascript, ~~143~~ 142 bytes
```
a=prompt(b=5);for(c=[];b--;c.push(a.replace(/{(.+?)}/g,function(_,j){return (d=j.split("|"))[d.length*Math.random()|0]})));alert(c.join("\n"))
```
[Answer]
# Python 3, 97 bytes
Using a lambda function in regex substitution. This is somewhat similar to how I did it in my Spintax module <https://github.com/AceLewis/spintax> however it does not deal with nested Spintax or escaping the characters.
```
import re,random
print(re.sub("{(.*?)}",lambda x:random.choice(x.group(1).split('|')),input()*5))
```
If you do not assume the input ends with a newline then it will be 104 bytes.
```
import re,random
print(re.sub("{(.*?)}",lambda x:random.choice(x.group(1).split('|')),(input()+'\n')*5))
```
[Answer]
# [Japt](https://github.com/ETHproductions/japt) [`-R`](https://codegolf.meta.stackexchange.com/a/14339/), 18 [bytes](https://en.wikipedia.org/wiki/ISO/IEC_8859-1)
```
5Ær"\{(.+?)}"Ïq| ö
```
[Try it](https://petershaggynoble.github.io/Japt-Interpreter/?v=1.4.6&flags=LVI&code=NcZyIlx7KC4rPyl9Is9xfCD2&input=IntBfFRoZXxPbmV9IHtxdWlja3xtYWduaWZpY2VudHxjaGFybWluZ3xsdWNreXxmYXN0fHN1cGVyfGNvb2x9IHtnb2xkfHRhbnx5ZWxsb3d8d2hpdGV8YnJvd258c2lsdmVyfSB7YmVhdmVyfHBhY2tyYXR8Y2F0fGxpb258dGlnZXJ8Zm94fGRvZ3x3b2xmfSB7Y29uc3VtZWR8YXRlfHBpbGZlcmVkfSBteSB7cGFzdHJ5fHN0cnVkZWx8Y2FrZX0i)
]