Last active
October 11, 2015 14:18
-
-
Save drsm79/3871401 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
silly little script to test that a haiku matches the syllable pattern. Call test with your haiku, split() on new lines. |
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
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