-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrhymes.py
49 lines (44 loc) · 1.52 KB
/
rhymes.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
#rhymes.py
#Based on code by Danielle Sucher
#https://github.com/DanielleSucher/Nantucket/blob/master/poetry.py
#Updated by Michael Kaminsky
from curses.ascii import isdigit
from nltk.corpus import cmudict
d = cmudict.dict()
def approx_nsyl(word):
digraphs = {"ai", "au", "ay", "ea", "ee", "ei", "ey", "oa", "oe", "oi", "oo", "ou", "oy", "ua", "ue", "ui"}
# Ambiguous, currently split: ie, io
# Ambiguous, currently kept together: ui
count = 0
array = re.split("[^aeiouy]+", word.lower())
for i, v in enumerate(array):
if len(v) > 1 and v not in digraphs:
count += 1
if v == '':
del array[i]
count += len(array)
if re.search("(?<=\w)(ion|ious|(?<!t)ed|es|[^lr]e)(?![a-z']+)", word.lower()):
count -= 1
if re.search("'ve|n't", word.lower()):
count += 1
return count
def nsyl(word):
# return the min syllable count in the case of multiple pronunciations
if not word.lower() in d:
return approx_nsyl(word)
return min([len([y for y in x if isdigit(str(y[-1]))]) for x in d[word.lower()]])
def rhymesyls(word):
if word.lower() in d:
list1 = min(d[word.lower()], key=len)
outlist = str()
i = -1
while i >= 0 - len(list1):
if isdigit(str(list1[i][-1])):
outlist = list1[i][:-1]
if i != -1:
outlist = outlist + ' ' + list1[i + 1:][0]
return outlist
i -= 1
return outlist
else:
return "NORHYME"