Skip to content

Instantly share code, notes, and snippets.

@joelalejandro
Created October 17, 2016 06:16
Show Gist options
  • Save joelalejandro/4bdedda883cbe2ef4529bedaedceb73d to your computer and use it in GitHub Desktop.
Save joelalejandro/4bdedda883cbe2ef4529bedaedceb73d to your computer and use it in GitHub Desktop.
function compose(tweets = this.tweets, verses = this.settings.poem_rules.verse_count) {
let blockA = [], blockB = [], poem = [];
let i = 0;
let processed = 0;
let authors = [];
let titleBlocks = [];
while (processed < verses) {
const tweet = tweets[i];
if (!tweet) {
tweets.shuffle();
blockA = [];
blockB = [];
i = 0;
processed = 0;
continue;
}
let text = this.sanitize(tweet);
let lastWord = text.split(' ').pop();
let rhyme = this.getRhymes(lastWord, tweets.filter(t => { return t.id_str !== tweet.id_str }));
if (rhyme.length > 0) {
blockA.push(text);
let tweetB = rhyme.shuffle()[Math.floor(Math.random() * rhyme.length)];
blockB.push(this.sanitize(tweetB));
authors.push({ name: tweet.user.name, alias: tweet.user.screen_name });
authors.push({ name: tweetB.user.name, alias: tweetB.user.screen_name });
titleBlocks.push(tweet.id_str.substr(-4));
titleBlocks.push(tweetB.id_str.substr(-4));
processed += 2;
}
i += 1;
}
for (i = 0; i < blockA.length; i++) {
poem.push(blockA[i]);
}
for (i = 0; i < blockB.length; i++) {
poem.push(blockB[i]);
}
const title = `Twoem ${titleBlocks.join(' ')}`;
return {
title: title,
authors: authors,
verses: poem
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment