私はPythonでのプログラミングが初めてで、数日間試みてきた課題がありますが、コードの何が問題なのかわかりません。私のコードはテキスト ファイルを受け取り、テキストに含まれる文、単語、および音節の数を教えてくれます。コードが連続した母音を含む音節を複数の音節としてカウントしていることを除いて、すべて正常に動作しており、それを修正する方法がわかりません。どんな助けでも大歓迎です。たとえば、ファイルに次のようなものがあるとします。テキストには 21 の音節があると表示されるはずですが、連続する母音を 2 回以上カウントするため、プログラムは 26 の音節であると教えてくれます。
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")