0

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") 
Aces Radix
  • 55
  • 3
  • I guess one solution is to check if any of 2 vowels combination is in the word and substract that number to the number of vowels in the word. You can probably also use a regex but i'm not good with it – Mayeul sgc Sep 30 '21 at 03:11
  • You may refer to this question to count the number of syllables. [click here](https://stackoverflow.com/questions/46759492/syllable-count-in-python) – abhira0 Sep 30 '21 at 03:17
  • 1
    This will still not be correct: `"reupload"` has 3 syllables, not 2 and not 4. It is not a simple problem; doing it yourself is not recommended. See [Detecting syllables in a word](https://stackoverflow.com/questions/405161/detecting-syllables-in-a-word). – Amadan Sep 30 '21 at 06:44

1 Answers1

1

Instead of counting the number of occurrences of each vowel for each word, we can iterate through the characters of the word, and only count a vowel if it isn't preceded by another vowel:

# Count the syllables
syllables = 0
vowels = "aeiou"
for word in (x.lower() for x in text.split()):
    syllables += word[0] in vowels
    for i in range(1, len(word)):
        syllables += word[i] in vowels and word[i - 1] not in vowels
    for ending in {'es', 'ed', 'e'}:
        if word.endswith(ending):
            syllables -= 1
    if word.endswith('le'):
        syllables += 1
Will Da Silva
  • 6,386
  • 2
  • 27
  • 52