diff options
Diffstat (limited to 'js')
-rw-r--r-- | js/bird-words/beak.js | 47 |
1 files changed, 24 insertions, 23 deletions
diff --git a/js/bird-words/beak.js b/js/bird-words/beak.js index 12c6a79..a87bc0d 100644 --- a/js/bird-words/beak.js +++ b/js/bird-words/beak.js @@ -43,38 +43,39 @@ for (let i = 0; i < storyLength; i++) { story += " " + randomPair.split(' ')[1]; } -// console.log(wrap(prettyItUp(story), 40)); - - - let nouns = fs.readFileSync('nouns.txt', 'utf8').split('\n'); let adjectives = fs.readFileSync('adjectives.txt', 'utf8').split('\n'); let places = fs.readFileSync('places.txt', 'utf8').split('\n'); -function fillGaps(script) { - let parts = script.split('@'); - for (let i = 0; i < parts.length; i++) { - if (parts[i].startsWith('NOUN')) { - parts[i] = nouns[Math.floor(Math.random() * nouns.length)]; - } else if (parts[i].startsWith('ADJECTIVE')) { - parts[i] = adjectives[Math.floor(Math.random() * adjectives.length)]; - } else if (parts[i].startsWith('PLACE')) { - parts[i] = places[Math.floor(Math.random() * places.length)]; - } else if (parts[i].startsWith('MARKOV')) { - let length = parseInt(parts[i].split(' ')[1]); +const fillGaps = (script, nouns, adjectives, places, markovChain, pairs) => { + return script.split('@').map(part => { + if (part.startsWith('NOUN')) { + return nouns[Math.floor(Math.random() * nouns.length)] + ' '; + } else if (part.startsWith('ADJECTIVE')) { + return adjectives[Math.floor(Math.random() * adjectives.length)] + ' '; + } else if (part.startsWith('PLACE')) { + return places[Math.floor(Math.random() * places.length)]; + } else if (part.startsWith('MARKOV')) { + const length = parseInt(part.split(' ')[1]); let chain = ''; let pair = pairs[Math.floor(Math.random() * pairs.length)]; for (let j = 0; j < length; j++) { - let nextWords = markovChain.get(pair); - let nextWord = nextWords[Math.floor(Math.random() * nextWords.length)]; + const nextWords = markovChain.get(pair); + const nextWord = nextWords[Math.floor(Math.random() * nextWords.length)]; chain += ' ' + nextWord; pair = pair.split(' ')[1] + ' ' + nextWord; } - parts[i] = chain; + return chain; + } else { + return part; } - } - return parts.join(''); -} + }).join(''); +}; + +const script = 'The @NOUN@ of @ADJECTIVE@ @NOUN@ was found in @PLACE@ where there was a partial inscription that read, @MARKOV 22@'; + +// example of how to use fillGaps to generate a story +console.log(wrap(prettyItUp(fillGaps(script, nouns, adjectives, places, markovChain, pairs)), 40)); -let script = 'The @NOUN@ of @ADJECTIVE@ @NOUN@ was found in @PLACE@, where there was a partial inscription that read, @MARKOV 22@'; -console.log(fillGaps(script)); \ No newline at end of file +// example of how to just use markov chain to generate a story +console.log(wrap(prettyItUp(story), 40)); \ No newline at end of file |