2021-07-31 17:10:28 +04:00

91 lines
2.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import ch1text
def count_syllables_in_word(word):
count = 0
endings = '.,;!?:'
last_char = word[-1]
if last_char in endings:
processed_word = word[:-1]
else:
processed_word = word
if len(processed_word) <= 3:
return 1
if processed_word[-1] in 'eE':
processed_word = processed_word[:-1]
vowels = 'aeiouAEIOU'
prev_char_was_vowel = False
for char in processed_word:
if char in vowels:
if not prev_char_was_vowel:
count += 1
prev_char_was_vowel = True
else:
prev_char_was_vowel = False
if processed_word[-1] in 'yY':
count += 1
return count
def count_syllables(words):
count = 0
for word in words:
word_count = count_syllables_in_word(word)
count = count + word_count
return count
def count_sentences(text):
count = 0
for char in text:
if char in ('.', '?', '!'):
count += 1
return count
def output_results(score):
if score >= 90:
return 'Уровень 5-го класса'
elif score >= 80:
return 'Уровень 6-го класса'
elif score >= 70:
return 'Уровень 7-го класса'
elif score >= 60:
return 'Уровень 8-9-го класса'
elif score >= 50:
return 'Уровень 10-11-го класса'
elif score >= 30:
return 'Уровень студента вуза'
else:
return 'Уровень выпускнинка вуза'
def compute_readability(text):
total_words = 0
total_sentences = 0
total_syllables = 0
score = 0
words = text.split()
total_words = len(words)
total_sentences = count_sentences(text)
total_syllables = count_syllables(words)
score = (206.835 - 1.015 * (total_words / total_sentences) - 84.6 * (total_syllables / total_words))
score_result = output_results(score)
# print(words)
print(total_words, 'слов')
print(total_sentences, 'предложений')
print(total_syllables, 'слогов')
print(score, ' - удобочитаемость')
print(score_result)
# print(text)
compute_readability(ch1text.text)