-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhaiku.py
79 lines (65 loc) · 3.27 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import re
from syllables import countSyllables
def removePunct(sentence):
puncts = ["'"]
for punct in puncts:
sentence = sentence.replace(punct, "")
return sentence
# Checks whether a sentence (String) follows a syllabic structure (int list), eg. structure = [5, 7, 5] for a haiku
# a sentence follows a syllabic strucuture if it can be partitioned by words such that the nth partition has a total syllable count of the nth strucure value
def checkSyllabicStructure(sentence, structure, debug=False):
words = re.split("\\b", removePunct(sentence))
currentSyllabicStructure = [0 for i in structure]
syllabicWordIndex = [[] for i in structure]
syllabicIndex = 0
badOutput = [False, []]
for wordIndex in range(len(words)):
word = words[wordIndex]
if(debug):
print("current word: {}".format(word))
if syllabicIndex >= len(structure):
if(debug):
print("index too large")
return badOutput
currSyllables = countSyllables(word)
# if current word fits into this structure, add the wordIndex to the list of indices and update syllable total
if currSyllables + currentSyllabicStructure[syllabicIndex] <= structure[syllabicIndex]:
syllabicWordIndex[syllabicIndex].append(wordIndex)
currentSyllabicStructure[syllabicIndex] += currSyllables
if(debug):
print("able to be fit")
print("new syllabicWordIndex: {}".format(syllabicWordIndex))
print("new currentSyllabicStructure: {}".format(currentSyllabicStructure))
else: # current word too big for current group
#check if this is the final group
if syllabicIndex >= len(structure)-1:
if(debug):
print("no next group to place word in")
return badOutput
elif currentSyllabicStructure[syllabicIndex] < structure[syllabicIndex]: #check if current group is not fulfilled
if(debug):
print("last group not fullfilled")
return badOutput
elif currSyllables > structure[syllabicIndex + 1]:# check if current word is too big for next group
if(debug):
print("current word too big for next group")
return badOutput
else: # Everything seems fine
if(debug):
print("word added to next index")
syllabicIndex += 1
#print("new syllabicIndex: {}".format(syllabicIndex))
syllabicWordIndex[syllabicIndex].append(wordIndex)
currentSyllabicStructure[syllabicIndex] += currSyllables
if(debug):
print(syllabicWordIndex)
if currentSyllabicStructure != structure:
return badOutput
else:
wordPartitions = []
for i in range(len(syllabicWordIndex)):
wordPartitions.append("".join([words[j] for j in syllabicWordIndex[i]]))
return [True, wordPartitions]
# checks whether a sentence follows a haiku structure, if so returns the sentence partitioned with the structure
def checkHaiku(sentence):
return checkSyllabicStructure(sentence, [5,7,5])