Skip to content

Instantly share code, notes, and snippets.

@majman
Last active September 16, 2015 18:56
Show Gist options
  • Save majman/2123ee0574428f61c1b8 to your computer and use it in GitHub Desktop.
Save majman/2123ee0574428f61c1b8 to your computer and use it in GitHub Desktop.
random-playlist-title.js
// tracks that make up a given playlist
var playlistTracks = [...];
// store possible words in an array
var dictionary = [];
// loop though tracks and add words to dictionary
for(var i = 0; i < playlistTracks.length; i++){
var track = playlistTracks[i];
// grab Track Title, Album Title, Artist name, and Genre; split by spaces
var trackTitleWords = track.name.split(' ');
var artistNameWords = track.artist.name.split(' ');
var albumTitleWords = track.album.name.split(' ');
var tagWords = track.genre.name.split(' ');
// add to dictionary array
dictionary = dictionary.concat(trackTitleWords, artistNameWords, albumTitleWords, tagWords);
}
// shuffle dictionary (I use underscore.js for this)
dictionary = _.shuffle(dictionary);
// random number of words (using 2-4 currently)
var minWords = 2;
var maxWords = 4;
var wordCount = Math.floor(Math.random()*(maxWords - minWords + 1) + minWords);
// get first n words
var playlistWords = dictionary.slice(0, wordCount);
// join words to create title
var playlistTitle = dictionary.join(' ');
// remove/replace unwanted words/characters
// *TODO* create list of common words to remove/replace/reorder (ie, don't end title with "the")
// regex to remove certain punctuation and replace certain characters with spaces
var punctRE = /[\u2000-\u206F\u2E00-\u2E7F\"():;<=>?@\[\]^_`{|}]/g;
var spaceRE = /[\/_]/g;
playlistTitle = playlistTitle.replace('feat.', '').replace(punctRE, '').replace(spaceRE, ' ');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment