Created
February 17, 2017 15:56
-
-
Save egorvinogradov/f806cc54cbadc6e2794a6fef792b6f79 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
var names = [ | |
'Catherine', | |
'Brenda', | |
'Cecilia', | |
'Grazy', | |
'Artem', | |
'Sage', | |
'Vanessa', | |
'Hoon', | |
'Dannisa', | |
'Nya', | |
'Patty', | |
'Gabe', | |
'Monique', | |
'Ozlem' | |
] | |
var nouns = [ | |
'action', | |
'work', | |
'noise', | |
'desolation', | |
'death', | |
'life', | |
'love', | |
'faith', | |
'anger', | |
'exhaustion', | |
'street', | |
'sidewalk', | |
'corner', | |
'door', | |
'window', | |
'hood', | |
'slum', | |
'skyscraper', | |
'car', | |
'truck', | |
'guy', | |
'girl', | |
'job', | |
'flower', | |
'light', | |
'cigarette', | |
'rain', | |
'jackhammer', | |
'driver', | |
'worker', | |
] | |
var verbs = [ | |
'get', | |
'grab', | |
'shove', | |
'love', | |
'desire', | |
'buy', | |
'sell', | |
'fight', | |
'hustle', | |
'drive', | |
] | |
var adjectives = [ | |
'big', | |
'small', | |
'old', | |
'fast', | |
'cold', | |
'hot', | |
'dark', | |
'dusty', | |
'grimy', | |
'dry', | |
'rainy', | |
'misty', | |
'noisy', | |
'faceless', | |
'dead', | |
]; | |
function lazyGenerateRhymes (words, rhymesObj) { | |
const API_URL = 'http://rhymebrain.com/talk?function=getRhymes&word='; | |
words.forEach(function(word){ | |
fetch(API_URL + word).then(function(rhyme){ | |
rhyme.text().then(function(rhymeBody){ | |
var index = Math.round( Math.random() * 5 ); | |
rhymesObj[word] = JSON.parse(rhymeBody)[index].word; | |
}); | |
}); | |
}); | |
} | |
function chooseRandomWord(list){ | |
var index = Math.round(Math.random() * list.length - 1); | |
return list[index]; | |
} | |
function generatePoem(names, nouns, verbs, adjectives) { | |
var rows = []; | |
var prevNoun = chooseRandomWord(nouns); | |
names.forEach(function(name, i){ | |
if (i === 0) { | |
return ''; | |
} | |
if (i % 2) { | |
prevNoun = chooseRandomWord(nouns); | |
rows.push([ | |
name, | |
chooseRandomWord(verbs), | |
chooseRandomWord(adjectives), | |
prevNoun, | |
].join(' ')); | |
} | |
else { | |
rows.push([ | |
name, | |
chooseRandomWord(verbs), | |
chooseRandomWord(adjectives), | |
rhymesObj[prevNoun], | |
].join(' ')); | |
} | |
}); | |
return rows.join('\n'); | |
}; | |
var rhymesObj = {}; | |
lazyGenerateRhymes(nouns, rhymesObj); | |
generatePoem(names, nouns, verbs, adjectives); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment