-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsyllables.py
67 lines (55 loc) · 1.57 KB
/
syllables.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
import nltk
import ssl
try:
_create_unverified_https_context = ssl._create_unverified_context
except AttributeError:
pass
else:
ssl._create_default_https_context = _create_unverified_https_context
nltk.download('cmudict')
from nltk.corpus import cmudict
d = cmudict.dict()
import re
dict_filename = 'cmudict.rep'
def syllable_num(word):
if is_underscore(word):
return handle_underscore(word)
if is_NOP(word):
return 0
try:
return [len(list(y for y in x if y[-1].isdigit())) for x in d[word.lower()]][0]
except KeyError:
return count_syllables_manually(word)
def is_NOP(symbol):
return bool(re.search(r"[.,\/#!$%\^&\*\"\';:{}=\-_`~()+-><]",symbol)) or symbol==''
def is_underscore(symbol):
return bool(re.search(r"_", symbol))
def handle_underscore(symbol):
words = re.split(r"_",symbol)
num_syllables = 0
for word in words:
num_syllables += syllable_num(word)
return num_syllables
def is_special(symbol):
return is_camel_case(symbol)
def count_syllables_manually(word):
count = 0
vowels = 'aeiouy'
word = word.lower()
if word[0] in vowels:
count +=1
for index in range(1,len(word)):
if word[index] in vowels and word[index-1] not in vowels:
count +=1
if word.endswith('e'):
count -= 1
if word.endswith('le'):
count+=1
if count == 0:
count +=1
return count
def get_syllable_list(word):
# TODO : implement this
print( syllable_dict['seriously'])
if __name__ == '__main__':
print(syllable_num('const'))