Created
March 2, 2016 14:27
-
-
Save dmyates/102e2c42054b299c765f 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
#!/usr/bin/python | |
# | |
# Count and compare the syllables for each set of two lines in the provided file. Bombs out with numbers and mispelt words. | |
# Uses Python 2.7 and nltk (pip install nltk) | |
import sys | |
from nltk.corpus import cmudict | |
d = cmudict.dict() | |
def syllable_count(word): | |
word = word.strip(",.") | |
return [len(list(y for y in x if y[-1].isdigit())) for x in d[word.lower()]] | |
def count_in_line(line): | |
return sum([item for sublist in map(syllable_count, line.split()) for item in sublist]) | |
def describe_couplet(couplet): | |
print "".join(couplet) | |
print "==First line: {0} syllables; Second line: {1} syllables==\n".format(count_in_line(couplet[0]), count_in_line(couplet[1])) | |
lines = [] | |
with open('couplets', 'r') as f: | |
lines = f.readlines() | |
couplets = [lines[i:i+2] for i in xrange(0, len(lines), 2)] #split into list of couplets (as two-element lists) | |
map(describe_couplet, couplets) | |
describe_couplet(couplets[0]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment