finished
This commit is contained in:
parent
140495fe26
commit
6ead849bd7
@ -1,6 +1,7 @@
|
|||||||
# Readability is King Codewars Kata https://www.codewars.com/kata/52b2cf1386b31630870005d4
|
# Readability is King Codewars Kata https://www.codewars.com/kata/52b2cf1386b31630870005d4
|
||||||
|
|
||||||
def avarage_words_sentence(text):
|
def avarage_words_sentence(text):
|
||||||
|
text = text.replace("!", ".").replace("?", ".")
|
||||||
sentences = text.split(".")
|
sentences = text.split(".")
|
||||||
sentences.pop(-1)
|
sentences.pop(-1)
|
||||||
|
|
||||||
@ -12,13 +13,49 @@ def avarage_words_sentence(text):
|
|||||||
|
|
||||||
return sum(average_words_sentence) / len(average_words_sentence)
|
return sum(average_words_sentence) / len(average_words_sentence)
|
||||||
|
|
||||||
def avarage_syllables(text):
|
def avarage_count_syllables(text):
|
||||||
vowels = {"a", "e", "i", "o", "u"}
|
vowels = {"a", "e", "i", "o", "u"}
|
||||||
|
|
||||||
|
sentences = text.split(".")
|
||||||
|
sentences.pop(-1)
|
||||||
|
|
||||||
|
count_words = {}
|
||||||
|
|
||||||
|
for sentence in sentences:
|
||||||
|
sentence = sentence.split()
|
||||||
|
for word in sentence:
|
||||||
|
word = word.lower()
|
||||||
|
if word not in count_words:
|
||||||
|
count_words[word] = {"count": 1, "vowels": {"count": 0, "position": {}}}
|
||||||
|
else:
|
||||||
|
count_words[word]["count"] += 1
|
||||||
|
|
||||||
|
for word in count_words:
|
||||||
|
for i, char in enumerate(word):
|
||||||
|
if char in vowels:
|
||||||
|
count_words[word]["vowels"]["count"] += 1
|
||||||
|
count_words[word]["vowels"]["position"][i] = i
|
||||||
|
|
||||||
|
for word in count_words:
|
||||||
|
if len(count_words[word]["vowels"]["position"]) > 1:
|
||||||
|
for position in count_words[word]["vowels"]["position"]:
|
||||||
|
if position + 1 in count_words[word]["vowels"]["position"]:
|
||||||
|
count_words[word]["vowels"]["count"] -= 1
|
||||||
|
sum_vowels = 0
|
||||||
|
for word in count_words:
|
||||||
|
sum_vowels += count_words[word]["vowels"]["count"] * count_words[word]["count"]
|
||||||
|
|
||||||
|
sum_words = 0
|
||||||
|
for word in count_words:
|
||||||
|
sum_words += count_words[word]["count"]
|
||||||
|
return sum_vowels / sum_words
|
||||||
|
|
||||||
def flesch_kincaid(text):
|
def flesch_kincaid(text):
|
||||||
average_words_sentence = avarage_words_sentence(text)
|
average_sentence = avarage_words_sentence(text)
|
||||||
avaraage_syllables = avaraage_syllables(text)
|
print(average_sentence)
|
||||||
|
avarage_syllables = avarage_count_syllables(text)
|
||||||
|
print(avarage_syllables)
|
||||||
|
|
||||||
return average_words_sentence
|
return round((0.39 * average_sentence) + (11.8 * avarage_syllables) - 15.59, 2)
|
||||||
|
|
||||||
print(flesch_kincaid("To be or not to be. That is the question."))
|
print(flesch_kincaid("Sweet active! Active cover happiness the sun jumps jumps happiness cute! Cuddles cover cover walks! Fun sweet."))
|
||||||
Loading…
x
Reference in New Issue
Block a user