From 7d6afc221a04f1e3e953737dc4651307297cab48 Mon Sep 17 00:00:00 2001 From: Garkatron <145294535+Garkatron@users.noreply.github.com> Date: Tue, 12 Dec 2023 23:59:26 +0100 Subject: [PATCH] Reto #[19] [Python] --- .../python/Garkatron.py" | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 "Retos/Reto #19 - AN\303\201LISIS DE TEXTO [Media]/python/Garkatron.py" diff --git "a/Retos/Reto #19 - AN\303\201LISIS DE TEXTO [Media]/python/Garkatron.py" "b/Retos/Reto #19 - AN\303\201LISIS DE TEXTO [Media]/python/Garkatron.py" new file mode 100644 index 0000000000..7b8d1b3f80 --- /dev/null +++ "b/Retos/Reto #19 - AN\303\201LISIS DE TEXTO [Media]/python/Garkatron.py" @@ -0,0 +1,57 @@ +import re + +# ! Coded by Garkatron +# ! https://github.com/Garkatron + +# ? -------------------------------------------------------------------------------------- ? # + +# * Crea un programa que analice texto y obtenga: +# ! Número total de palabras. V +# ! Longitud media de las palabras. V +# ! Número de oraciones del texto (cada vez que aparecen un punto). V +# ! Encuentre la palabra más larga. V +# Todo esto utilizando un único bucle. + +# ? -------------------------------------------------------------------------------------- ? # + +# ! Functions + +def get_words(text:str)->[]: + words=re.split(r'[\s,;.:]+',text) + return words + +def max_length_word(words:list)->int: + return max(words, key=lambda v: len(v)) + +def get_sentences(text:str)->int: + return text.split(".") + +def word_mean(words:list)->int: + return sum(map(lambda w: len(w),words))/len(words) + + +# ! To execute + +if __name__=="__main__": + + # ! Getting the values + text=input("Insert text: ") + words=get_words(text) + words_count=len(words) + mean_word_lenght=word_mean(words) + sentences=get_sentences(text) + sentences_number=len(sentences) + max_length_word=max_length_word(words) + + # ! Printing values + print("----------------------------------------") + print("Analisyng text: ", text) + print("----------------------------------------", "\n") + + print("Words: ", words) + print("Words count: ", words_count) + print("Max length word: ", max_length_word) + print("Mean word length: " ,mean_word_lenght, "\n") + + print("Sentences: ", sentences) + print("Sentences count: ", sentences_number, "\n")