Created
April 12, 2016 05:30
-
-
Save coblezc/e2f60f26823593287e2f9e9b31010909 to your computer and use it in GitHub Desktop.
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
# rwet hw 3 | |
import random | |
from string import punctuation | |
# strip first ~50 lines of metadata | |
lyrics_file = "/path/to/file" | |
lyrics = [line.strip() for line in \ | |
open(lyrics_file).readlines() | |
if len(line.strip()) > 0] | |
def not_with_semicolon(lines): | |
if not line.startswith(';;;') and not line[0] in punctuation: | |
return True | |
else: | |
return False | |
# load and clean cmu dictionary | |
def cmu_dictionary(lyrics): | |
with open('cmudict-0.7b') as infile: | |
lines = filter(not_with_semicolon, infile.read().strip().split('\n')) | |
#get rid of alternate pronunciations | |
lines = filter(lambda x: not '(' in x, lines) | |
#put the words in the dictionary | |
cmu_dict = dict() | |
for l in lines: | |
word, phonemes = l.split(' ', 1) | |
cmu_dict[word.lower()] = phonemes.split(' ') | |
return cmu_dict | |
# get phones and lines for last words | |
def last_word_phons(lyrics): | |
last_words = list() | |
for line in lyrics: | |
words = line.split() | |
last_word = words[-1].strip('.,?!;:') # .strip() to remove any punctuation | |
last_word_lower = last_word.lower() | |
# if last word is in cmu dictionary (ie no slang, abbreviations, etc) | |
if last_word_lower in cmu_dict: | |
line_with_part = [cmu_dict[last_word_lower], line] | |
last_words.append(line_with_part) | |
return last_words | |
# generate alliterative lines | |
def get_alliteration_couplet(last_words): | |
alliteration_lines = list() | |
# grab random line to start | |
first = random.choice(last_word_phons.last_words) | |
# create a list of lines that rhymes with the line randomly selected above | |
for line in last_words: | |
matching_phons = list() | |
for p1, p2 in zip(first[0], line[0]): | |
if p1 == p2: | |
matching_phons.append(p1) | |
else: | |
break | |
if len(matching_phons) > 1: | |
alliteration_lines.append(first[1]) | |
alliteration_lines.append(line[1]) | |
break | |
second = random.choice(alliteration_lines) | |
return [first[1], second] | |
# generate chorus | |
def generate_chorus(num_lines): | |
chorus_lines = list() | |
for line in range(num_lines/2): | |
couplet = get_alliteration_couplet(last_word_phons) | |
chorus_lines.append(couplet1[0]) | |
chorus_lines.append(couplet1[1]) | |
return chorus_lines | |
print generate_chorus(4) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment