-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSimpleSyllableCounter.py
46 lines (34 loc) · 1.46 KB
/
SimpleSyllableCounter.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
# NOTE: words ending with es, ed, and ing are not counted towards the total number of syllables
def syllables(word):
# y can usually be considered as a syllable due to its pronunciation
vowels = "aeiouy"
# always separate syllables
multi_syllables = ["ia"]
# following are separate syllables unless they're at the end of the word
multi_syllable_except_at_end = ["io", "ua", "eo", "ui", "uo", "ya", "es", "ed"]
word = word.lower()
syllable_count = 0
ends_with_vowel_flag = False
last_letter = ""
# removing ing from the end of the word
if 'ing' == word[-3:]:
word = word[:-3]
for letter in word:
if letter in vowels:
letter_combination = last_letter+letter
if ends_with_vowel_flag and letter_combination not in multi_syllables \
and letter_combination not in multi_syllable_except_at_end:
ends_with_vowel_flag = True
else:
syllable_count += 1
ends_with_vowel_flag = True
else:
ends_with_vowel_flag = False
last_letter = letter
# es and ed are usually silent, so removing them
if len(word) > 2 and word[-2:] in multi_syllable_except_at_end:
syllable_count -= 1
# disregard single e at the end, but not ee since it was counted previously
elif len(word) > 2 and word[-1:] == "e" and word[-2:] != "ee":
syllable_count -= 1
return syllable_count