Last active
December 14, 2016 20:21
-
-
Save Kuzcoo/3d48bf1ea4da286b601451d5f8246a88 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Array.prototype.pickRand = function () { | |
return this[Math.floor(Math.random()*this.length)]; | |
}; | |
// grammar from Daniel Howe | |
// http://rednoise.org/rita/examples/p5js/HaikuGrammar/#source2 | |
let grammar = { | |
"start": "<5L> <7L> <5L>", | |
"<5L>": "<1> <4>|<1> <3> <1>|<1> <1> <3>|<1> <2> <2>|<1> <2> <1> <1>|<1> <1> <2> <1>|<1> <1> <1> <2>|<1> <1> <1> <1> <1>|<2> <3>|<2> <2> <1>|<2> <1> <2>|<2> <1> <1> <1>|<3> <2>|<3> <1> <1>|<4> <1>|<5>", | |
"<7L>": "<1> <1> <5L>|<2> <5L>|<5L> <1> <1>|<5L> <2>", | |
"<1>": "red|white|black|sky|dawns|breaks|falls|leaf|rain|pool|my|your|sun|clouds|blue|green|night|day|dawn|dusk|birds|fly|grass|tree|branch|through|hell|zen|smile|gray|wave|sea|through|sound|mind|smoke|cranes|fish", | |
"<2>": "drifting|purple|mountains|skyline|city|faces|toward|empty|buddhist|temple|japan|under|ocean|thinking|zooming|rushing|over|rice field|rising|falling|sparkling|snowflake", | |
"<3>": "sunrises|pheasant farms|people farms|samurai|juniper|fishing boats|far away|kimonos|evenings|peasant rain|sad snow fall", | |
"<4>": "aluminum|yakitori|the east village|west of the sun| chrysanthemums|cherry blossoms", | |
"<5>":"resolutional|non-elemental|rolling foothills rise|toward mountains higher|out over this country|in the springtime again" | |
}; | |
// | |
class Grammar { | |
constructor() { | |
this.grammar = null; | |
} | |
load(src) { | |
if (src && | |
src instanceof Object) { | |
return this.grammar = src; | |
} | |
// else get the grammar file | |
} | |
expand(entry) { | |
let start = entry|| this.grammar["start"], | |
pick = start.split('|').pickRand(); | |
if (!/<[A-Z0-9]+>/.test(pick)) { | |
return pick; | |
} | |
return pick.split(' ').map(word => { | |
if (/^<[A-Z0-9]+>$/.test(word)) { | |
return this.expand(this.grammar[word]); | |
} | |
return pick; | |
}).join(' '); | |
} | |
} | |
let g = new Grammar(); | |
g.load(grammar); | |
console.log(g.expand()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment