-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoemDetector.py
264 lines (233 loc) · 7.31 KB
/
poemDetector.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
import utils
def detectHaiku(poem):
"""Detect if poem is a haiku."""
if len(poem) != 3:
print("NOT A HAIKU")
return False
else:
sent_syllables = [0, 0, 0]
for i in range(len(poem)):
for word in poem[i].split():
sent_syllables[i] += utils.count_syllables(word)
print(f"Sent Syllables: {sent_syllables}")
correct_syllables = [5, 7, 5]
tol = 1
if sent_syllables == correct_syllables:
print("IS A HAIKU")
return True
else:
for i in range(len(sent_syllables)):
if abs(sent_syllables[i] - correct_syllables[i]) <= tol:
continue
else:
print("NOT A HAKU")
return False
print("CLOSE TO A HAIKU")
return False
def detectLimerick(poem):
"""Detect if poem is a limerick."""
if len(poem) != 5:
print("NOT A LIMERICK")
return False
sent_syllables = [0, 0, 0, 0, 0]
for i in range(len(poem)):
for word in poem[i].split():
sent_syllables[i] += utils.count_syllables(word)
print(f"Sent Syllables: {sent_syllables}")
part_1 = [0, 1, 4]
part_2 = [2, 3]
tol = 1.5
# First two lines contain seven to ten syllables
# Final line contains seven to ten syllables
lim = [0, 0, 0, 0, 0]
for num in part_1:
if abs(sent_syllables[num] - 8.5) <= tol:
continue
elif abs(sent_syllables[num] - 8.5) <= tol + 1:
print("CLOSE TO A LIMERICK, line: ", num+1)
lim[num] = 1
else:
print("NOT A LIMERICK, line: ", num+1)
return False
# Third and fourth lines contain five to seven syllables
tol = 1
for num in part_2:
if abs(sent_syllables[num] - 6) <= tol:
continue
elif abs(sent_syllables[num] - 6) <= tol + 1:
print("CLOSE TO A LIMERICK, line: ", num+1)
lim[num] = 1
else:
print("NOT A LIMERICK, line: ", num+1)
return False
close = False
for num in lim:
if num == 1:
close = True
# AABBA rhyme scheme
correct_rs = [1,1,2,2,1]
tol = 1
manual = [2, 2, 1, 1, 0]
rs = utils.word_rhyme(poem)
print(f"Rhymescheme: {rs}")
if rs == correct_rs and close == False:
print("IS A LIMERICK")
return True
else:
close = True
for i in range(len(rs)):
if rs[i] == None:
rs[i] = 0
if abs(rs[i] - correct_rs[i]) > tol:
close == False
break
if close == True:
print("CLOSE TO A LIMERICK")
return False
print("NOT A LIMERICK")
return False
def detectBallad(poem):
"""Detect if poem is a ballad."""
if len(poem) % 4 != 0:
print("NOT A BALLAD")
return False
correct_rs = [[1,2,3,2], [1,2,1,2]]
print("HEREEE", correct_rs)
rs = utils.word_rhyme(poem)
print(f"Rhymescheme: {rs}")
tol = len(poem) * 0.125
for c in correct_rs:
ballad = True
close = True
wrong = 0
for i in range(len(rs)):
if rs[i] != c[i % 4]:
ballad = False
break
if ballad == True:
print("IS A BALLAD")
return True
else:
i = 0
while i in range(len(rs) - 4):
quartrain = [rs[i], rs[i+1], rs[i+2], rs[i+3]]
if c == [1,2,3,2]:
if quartrain[1] == None or quartrain[3] == None:
quartrain[1] = 0
quartrain[3] = 0
if quartrain[1] != quartrain[3] or quartrain[0] == quartrain[2]:
print("a", i)
wrong += 1
else:
if quartrain[0] == None or quartrain[2] == None:
quartrain[0] = 0
quartrain[2] = 0
if quartrain[1] == None or quartrain[3] == None:
quartrain[1] = 0
quartrain[3] = 0
if quartrain[0] != quartrain[2] or quartrain[1] != quartrain[3]:
print("b", i)
wrong += 1
if wrong > tol:
close = False
break
i += 4
if close == True:
print("CLOSE TO A BALLAD", c)
return False
print("NOT A BALLAD")
return False
def detectSonnet(poem):
"""Detect if poem is a Shakespearean or Petrarchan sonnet."""
if len(poem) != 14:
print("NOT A SONNET")
return False
shakespearean_rs = [1,2,1,2,3,4,3,4,5,6,5,6,7,7]
petrarchan_rs = [[1,2,2,1,3,4,4,3,5,6,5,6,5,6],
[1,2,2,1,3,4,4,3,5,6,7,5,6,7]]
rs = utils.word_rhyme(poem)
print(f"Rhymescheme: {rs}")
tol = 1
close = True
if rs == shakespearean_rs:
print("IS A SHAKESPEAREAN SONNET")
return True
else:
for i in range(len(rs)):
if rs[i] == None:
rs[i] = 0
if abs(rs[i] - shakespearean_rs[i]) > tol:
close == False
break
if close == True:
print("CLOSE TO A SHAKESPEAREAN SONNET")
return False
close_1 = False
for p_rs in petrarchan_rs:
close = True
if rs == p_rs:
print("IS A PETRARCHAN SONNET")
return True
else:
for i in range(len(rs)):
if rs[i] == None:
rs[i] = 0
if abs(rs[i] - p_rs[i]) > tol:
close == False
break
if close:
close_1 = True
if close_1:
print("CLOSE TO A PETRARCHAN SONNET")
return False
print("NOT A SONNET")
return False
def detectVillanelle(poem):
"""Detect if poem is a villanelle."""
if len(poem) != 19:
print("NOT A VILLANELLE")
return False
first = poem[0]
third = poem[2]
if first != poem[5] or first != poem[11] or first != poem[17]:
print("NOT A VILLANELLE")
return False
if third != poem[8] or third != poem[14] or third != poem[18]:
print("NOT A VILLANELLE")
return False
correct_rs = [1,2,1,1,2,1,1,2,1,1,2,1,1,2,1,1,2,1,1]
rs = utils.word_rhyme(poem)
print(f"Rhymescheme: {rs}")
tol = 1
close = True
if rs == correct_rs:
print("IS A VILLANELLE")
return True
else:
for i in range(len(rs)):
if rs[i] == None:
rs[i] = 0
if abs(rs[i] - correct_rs[i]) > tol:
close == False
break
if close == True:
print("CLOSE TO A VILLANELLE")
return False
print("IS NOT A VILLANELLE")
return False
def detectBlankVerse(poem):
"""Detect if poem is a blank verse."""
meter = utils.meter_detector(poem)
if meter != '':
print("IS A BLANK VERSE")
return True
else:
return False
def detectFreeVerse(poem):
"""Detect if poem is a free verse."""
meter = utils.meter_detector(poem)
if meter == '':
print("IS A FREE VERSE")
return True
else:
return False