I'm new to programming in python and I have a challenge that I've been attempting for a few days now but I can't seem to figure out what is wrong with my code. My code take a text file and tells me how many sentences, words, and syllables are in the text. I have everything running fine except my code is counting a syllable containing consecutive vowels as multiple syllables and I can't seem to figure out how to fix it. Any help at all would be appreciated. For example if the file has this: "Or to take arms against a sea of troubles, And by opposing end them? To die: to sleep." It should come out as saying the text has 21 syllables but the program tells me it has 26 because it counts the consecutive vowels more than once.
fileName = input("Enter the file name: ")
inputFile = open(fileName, 'r')
text = inputFile.read()
# Count the sentences
sentences = text.count('.') + text.count('?') + \
text.count(':') + text.count(';') + \
text.count('!')
# Count the words
words = len(text.split())
# Count the syllables
syllables = 0
vowels = "aeiouAEIOU"
for word in text.split():
for vowel in vowels:
syllables += word.count(vowel)
for ending in ['es', 'ed', 'e']:
if word.endswith(ending):
syllables -= 1
if word.endswith('le'):
syllables += 1
# Compute the Flesch Index and Grade Level
index = 206.835 - 1.015 * (words / sentences) - \
84.6 * (syllables / words)
level = int(round(0.39 * (words / sentences) + 11.8 * \
(syllables / words) - 15.59))
# Output the results
print("The Flesch Index is", index)
print("The Grade Level Equivalent is", level)
print(sentences, "sentences")
print(words, "words")
print(syllables, "syllables")