Skip to content

Instantly share code, notes, and snippets.

@roneesh
Created March 3, 2017 03:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save roneesh/ba629414e6fedc1abf95885d43211a1f to your computer and use it in GitHub Desktop.
Save roneesh/ba629414e6fedc1abf95885d43211a1f to your computer and use it in GitHub Desktop.
Drake Poem Generator Using Markov Chains
function parseText(text) {
return text.split(' ').map(function(currentValue, index, array) {
return currentValue.toLowerCase().replace(/[.,']/g,"");
});
}
function generateWordPairs(text) {
var parsedText = parseText(text);
var wordPairs = {};
parsedText.forEach(function(currentValue, index, array) {
var nextWord = array[index + 1];
if (wordPairs[currentValue]) {
wordPairs[currentValue].push(nextWord);
} else {
wordPairs[currentValue] = [ nextWord ];
}
});
return wordPairs;
}
function writeLine(text, length) {
var parsedText = parseText(text);
var markovChain = generateWordPairs(text);
var line = '';
var randomNumber = Math.floor(Math.random() * parsedText.length);
var nextWord = parsedText[randomNumber];
line += nextWord + ' ';
length--;
while (length > 0) {
var possibleNextWords = markovChain[nextWord];
var nextWordIndex = Math.floor(Math.random() * possibleNextWords.length);
nextWord = possibleNextWords[nextWordIndex];
line += nextWord + ' ';
length--;
}
return line;
}
function writePoem(text, lines) {
var poem = '';
for (var i = 0; i < lines; i++) {
poem += writeLine(text, Math.floor(Math.random() * 10))
poem += '\n';
}
return poem;
}
var hotline = "You used to call me on my You used to, you used to Yeah You used to call me on my cell phone Late night when you need my love Call me on my cell phone Late night when you need my love And I know when that hotline bling That can only mean one thing I know when that hotline bling That can only mean one thing Ever since I left the city, you Got a reputation for yourself now Everybody knows and I feel left out Girl, you got me down, you got me stressed out Cause ever since I left the city, you Started wearing less and goin out more Glasses of champagne out on the dance floor Hangin' with some girls I never seen beforeYou used to call me on my cell phone Late night when you need my love Call me on my cell phone Late night when you need my love I know when that hotline bling That can only mean one thing I know when that hotline bling That can only mean one thing Ever since I left the city, you, you, you You and me, we just don't get along You make me feel like I did you wrong Going places where you don't belong Ever since I left the city, you You got exactly what you asked for Running out of pages in your passport Hangin' with some girls I've never seen before You used to call me on my cell phone Late night when you need my love Call me on my cell phone Late night when you need my love And I know when that hotline bling That can only mean one thing I know when that hotline bling That can only mean one thing These days, all I do is Wonder if you're bendin over backwards for someone else Wonder if you're rolling up a Backwoods for someone else Doing things I taught you, gettin' nasty for someone else You don't need no one else You don't need nobody else, no Why you never alone Why you always touching road Used to always stay at home Be a good girl, you was in the zone Yeah, you should just be yourself Right now, you're someone else You used to call me on my cell phone Late night when you need my love Call me on my cell phone Late night when you need my love And I know when that hotline bling That can only mean one thing I know when that hotline bling That can only mean one thing Ever since I left the city";
// console.log(generateWordPairs(hotline));
// console.log(generateWordPairs(hotline)['late']);
console.log(writeLine(hotline, 10));
console.log(writePoem(hotline, 10));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment