Created
April 3, 2013 13:21
-
-
Save anonymous/5301148 to your computer and use it in GitHub Desktop.
This file contains 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
from curses.ascii import isdigit | |
from nltk.corpus import cmudict | |
import numpy as np | |
from string import punctuation | |
from nltk.tokenize import sent_tokenize | |
d = cmudict.dict() | |
def nsyl(word): | |
try: | |
return [len(list(y for y in x if isdigit(y[-1]))) for x in d[word.strip(punctuation).lower()]] | |
except Exception, e: | |
return [99] | |
def haiku_or_not(sentence): | |
haiku=[] | |
sentence_words=sentence.split() | |
sentence_syllables=[nsyl(word)[0] for word in sentence_words] | |
syl_sum = np.cumsum(sentence_syllables) | |
if syl_sum[-1]==17 and 5 in syl_sum and 12 in syl_sum: | |
print '\n' | |
print article['cite'] | |
print article.get('DI',[''])[0] | |
line=[] | |
for idx,item in enumerate(syl_sum): | |
line.append(sentence_words[idx]) | |
if item in [5,12,17]: | |
line=' '.join(line) | |
print line | |
haiku.append(line) | |
line=[] | |
return haiku |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment