Last active
September 16, 2015 18:56
-
-
Save majman/2123ee0574428f61c1b8 to your computer and use it in GitHub Desktop.
random-playlist-title.js
This file contains hidden or 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
// 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