Skip to content

Instantly share code, notes, and snippets.

/haikus.py

Created Apr 3, 2013
Embed
What would you like to do?
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