Skip to content

Instantly share code, notes, and snippets.

@skairunner
Created December 1, 2016 15:05
Show Gist options
  • Save skairunner/9386fba4ae52c84e7670a0bc7cc3a857 to your computer and use it in GitHub Desktop.
Save skairunner/9386fba4ae52c84e7670a0bc7cc3a857 to your computer and use it in GitHub Desktop.

How it's supposed to work:

lyrics/ has lyrics in it, snippets/ are the word clips from songs, labeled in format WORD - SONGNAME - ARTIST.wav

Running unique_all-lyrics.py uses unique_words.py as a module to take all lyrics in the lyrics directory and produce a unique set of words per song. Then, findword.py WORD will find all songs that have that exact word in it!

generatemetadata.py takes all the snippets in snippets/ and makes a JSON file of format

[
  {
    "WORD": [
      { "song": "SONG", "artist": "ARIST" },
      ....
    ]
  },
  ...
]

final.js does the word assembling logic.

<html>
<head>
<meta charset="UTF-8">
<script language="javascript" type="text/javascript" src="tone.js"></script>
<script language="javascript" type="text/javascript" src="final.js"></script>
<script language="javascript" type="text/javascript" src="https://d3js.org/d3.v4.js"></script>
</head>
<body>
<svg width="100%" height="100%" viewBox="100 100 600 600"></svg>
</body>
</html>
"use strict";
document.addEventListener("DOMContentLoaded", init);
let example = "Two roads diverged in a wood, and I-\nI took the one less traveled by,\nAnd that made all the difference."
let sanitize = /[^A-Za-z' \n]/g; // remove any character not in A-z or apostrophe or space.
let splitter = / +/g; // split by 1 or more spaces
let newline2dot = /\n/g;
// if multiple files, randomly select one
function selectFile(word, metadata) {
let files = metadata[word];
if (files.length == 1) {
return files[0];
}
let n = Math.floor(Math.random() * files.length);
return files[n];
}
function initMultiplayer(words, metadata) {
words = words.toLowerCase();
words = words.replace(sanitize, " ");
words = words.replace(newline2dot, " . ");
words = words.split(splitter);
let uniqwords = new Set(words);
let wordfiles = [];
let it = uniqwords.keys();
let done = false;
while (!done) {
let result = it.next();
done = result.done;
if (!done) {
let word = result.value;
if (word == "" || word == " ") continue;
console.log(word)
let file = selectFile(word, metadata);
let filename = word + " - " + file.song + " - " + file.artist + ".wav";
wordfiles.append([word, filename]);
}
};
}
function init() {
d3.json("wordmetadata.json", function(err, metadata){
initMultiplayer(example,metadata)
});
}
import sys
import os
search = sys.argv[1].split(",")
words = {}
for filename in os.listdir("words"):
with open("words/" + filename) as f:
contents = f.read()
words[filename] = contents.split("\n")
for word in search:
print(word + "\n-----")
for songname in words:
if word in words[songname]:
print(songname)
import json
import os
# import inflect
# inflector = inflect.engine()
def generate():
files = {}
for filename in os.listdir("snippets"):
filename = filename[:-4]
word, song, artist = filename.split(" - ")
if word in files:
files[word].append({
"song": song,
"artist": artist
})
else:
files[word] = [{
"song": song,
"artist": artist
}]
# # next, make another pass to cover for basic verb forms and plurali/sing
# newwords = {}
# for word in files:
# plural = inflector.plural(word)
# singul = inflector.singular_noun(word)
# if plural not in files:
# newwords[plural] = files[word]
# if singul not in files:
# newwords[singul] = files[word]
# for word in newwords:
# files[word] = newwords[word]
with open("wordmetadata.json", "w") as f:
json.dump(files, f, indent=2)
if __name__=="__main__":
generate()
#like unique-words.py but does it for all lyrics that haven't been converted.
import os
import unique_words
songs = set()
for song in os.listdir("lyrics"):
songs.add(song)
for song in os.listdir("words"):
if song in songs:
songs.remove(song)
for song in songs:
o = unique_words.uniquewords(song)
with open("words/" + song, "w") as f:
for word in o:
f.write(word + "\n")
#extract unique words from lyrics and save to file
import re
import sys
pattern = re.compile(r"[\w']+")
def uniquewords(filename):
with open("lyrics/" + filename) as f:
lyrics = f.read()
results = pattern.findall(lyrics)
out = []
for word in results:
out.append(word.lower())
return list(set(out))
if __name__=="__main__":
o = uniquewords("lyrics/" + sys.argv[1])
o = sorted(o)
with open("words/" + sys.argv[1], "w") as f:
for word in o:
print(word)
f.write(word + "\n")
{
"crazy": [
{
"song": "crazy",
"artist": "gnarls barkley"
}
],
"one": [
{
"song": "boulevard of dreams",
"artist": "green day"
}
],
"when": [
{
"song": "crazy",
"artist": "gnarls barkley"
}
],
"wood": [
{
"song": "no more walks in the wood",
"artist": "eagles"
}
],
"thinking": [
{
"song": "such great heights",
"artist": "postal service"
}
],
"roads": [
{
"song": "such great heights",
"artist": "postal service"
}
],
"less": [
{
"song": "road less traveled",
"artist": "lauren alaina"
}
],
"made": [
{
"song": "a better place a better time",
"artist": "streetlight manifesto"
}
],
"would": [
{
"song": "monster",
"artist": "imagine dragon"
}
],
"sign": [
{
"song": "such great heights",
"artist": "postal service"
}
],
"remember": [
{
"song": "crazy",
"artist": "gnarls barkley"
}
],
"diverge": [
{
"song": "can't let go",
"artist": "landon pigg"
}
],
"difference": [
{
"song": "cross the ocean",
"artist": "bora yoon"
}
],
"two": [
{
"song": "the big sleep",
"artist": "streetlight manifesto"
}
],
"lost": [
{
"song": "crazy",
"artist": "gnarls barkley"
}
],
"the": [
{
"song": "kill your heroes",
"artist": "awolnation"
}
],
"by": [
{
"song": "stand by me",
"artist": "imagine dragons"
}
],
"a": [
{
"song": "d.a.n.c.e",
"artist": "justice"
}
],
"took": [
{
"song": "Safer in the Forest & Love Song for Poor Michigan",
"artist": "La Dispute"
}
],
"in": [
{
"song": "such great heights",
"artist": "postal service"
}
],
"and": [
{
"song": "fix you",
"artist": "coldplay"
}
],
"road": [
{
"song": "such great heights",
"artist": "postal service"
}
],
".": [
{
"song": "a moment of silence",
"artist": "a brief resting stop"
}
],
"that": [
{
"song": "the big sleep",
"artist": "streetlight manifesto"
}
],
"all": [
{
"song": "kill your heroes",
"artist": "awolnation"
}
],
"mind": [
{
"song": "crazy",
"artist": "gnarls barkley"
}
],
"diverged": [
{
"song": "can't let go",
"artist": "landon pigg"
}
],
"traveled": [
{
"song": "road less traveled",
"artist": "lauren alaina"
}
],
"i": [
{
"song": "crazy",
"artist": "gnarls barkley"
},
{
"song": "such great heights",
"artist": "postal service"
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment