-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhaiku.py
49 lines (35 loc) · 1.66 KB
/
haiku.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from haikufinder import *
from nltk.util import ngrams
import random, re
f = open('hobbylobby.txt', 'r')
theText = f.read()
theHaiku = []
n = 2
thengram = ngrams(theText.split(), n)
# for rando in range(1, len(thengram) - 1):
# rando = random.randrange(0, len(thengram), 1)
# if count_syllables(' '.join(thengram[rando])) == 5 and ' '.join(thengram[rando]).upper() != ' '.join(thengram[rando]) and ' '.join(thengram[rando])[0] == ' '.join(thengram[rando])[0].upper() and thengram[rando][0].find(r'\d') != -1:
# theHaiku.append(' '.join(thengram[rando]))
# break
for rando in range(1, len(thengram) - 1):
rando = random.randrange(0, len(thengram), 1)
if count_syllables(' '.join(thengram[rando])) == 5 and ' '.join(thengram[rando]).upper() != ' '.join(thengram[rando]):
theHaiku.append(' '.join(thengram[rando]))
break
for rando in range(1, len(thengram) - 1):
rando = random.randrange(0, len(thengram), 1)
if count_syllables(' '.join(thengram[rando])) == 7 and ' '.join(thengram[rando]).upper() != ' '.join(thengram[rando]):
theHaiku.append(' '.join(thengram[rando]))
break
for rando in range(1, len(thengram) - 1):
rando = random.randrange(0, len(thengram), 1)
if count_syllables(' '.join(thengram[rando])) == 5 and ' '.join(thengram[rando]).upper() != ' '.join(thengram[rando]):
theHaiku.append(' '.join(thengram[rando]))
break
print ' '.join(theHaiku)
#print count_syllables("testing")
# Consider on haiku line breaks generating a bigram:
# http://nltk.googlecode.com/svn/trunk/doc/book/ch02.html#chap-corpora
# to make sure the lines flow.
# Could generate a bi-gram for ending of a line then find an instance of the
# randomly generated word for the next line.