Syllable Counter in Python Fails with Silent E in Word
I've Been Trying to Make a Piece of Code That Will Successfully Count Syllables for All Words, but Words Like "Announcement" or Brand Names Like "Facebook" Are...
I've been trying to make a piece of code that will successfully count syllables for all words, but words like "announcement" or brand names like "Facebook" are counted incorrectly because they have silent E's in the middle of the word. The code ends up adding one more syllable than there is supposed to be, and I'm not sure how to fix it without ruining other words.
def syllable_count(word):
word = word.lower()
count = 0
vowels = "aeiouy"
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 word.endswith("ia"):
count += 1
if count == 0:
count += 1
return count
print(syllable_count('ANNOUNCEMENT'))
This outputs 4 instead of 3, the correct amount of syllables. Anyone know what to do?
1 Answer
Your application is related with NLP. May be the libraries like NLTK or Spacy may have models for this task. If they don't , you will have to build a model for this, which will exactly involve machine learning , Which is a heavy work fro you if you are a beginner.
Why do I say that machine learning (ML) is involved here?
To determine whether there is a syllable or not, It has to learn patterns in words/letters. Just like: what are the leading letters, what are the trailing letters, is it at the end or at the beginning. So in this case you have to create a method to identify patterns by studying sample of already known data. This is where you need machine learning.
So to do this you need domain knowledge in English languages and a dataset to train your model with. And also you must have knowledge in ML to build a suitable model for this. Something advanced.
And if you have got all you need, Domain knowledge , Proper Model and Suitable Dataset, you will get a (almost) PERFECT syllable counter.