Skip to content

Instantly share code, notes, and snippets.

@drsm79
Last active October 11, 2015 14:18
Show Gist options
  • Save drsm79/3871401 to your computer and use it in GitHub Desktop.
Save drsm79/3871401 to your computer and use it in GitHub Desktop.
silly little script to test that a haiku matches the syllable pattern. Call test with your haiku, split() on new lines.
import nltk
from nltk.corpus import cmudict
d = cmudict.dict()
def nsyl(word):
return [len(list(y for y in x if y[-1].isdigit())) for x in d[word.lower()]]
def test(haiku):
for line in haiku:
length = 0
expected = 5 + 2 * (haiku.index(line) % 2)
for word in line.split():
length += nsyl(word)[0]
if length == expected:
print "'%s' is a good haiku line" % line
else:
print "'%s' is a bad haiku line (%s syllables not %s)" % (line, length, expected)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment