Contagem de Palavras de um Texto

from tkinter import *

root = Tk()
root.geometry("700x400")
root.resizable(0, 0)
root.config(bg="#103030")
root.title("Contagem de Palavras de um Texto")

# Título
titulo = Label(text="Contagem de Palavras de um Texto",
font=("Arial", "28", "bold"), bg="#103030", fg="#49e3e3")
titulo.place(relx=0.06, rely=0.05)

texto_sub1 = Label(text="Texto",
font=("Arial", "20", "bold"), bg="#103030", fg="#49e3e3")
texto_sub1.place(relx=0.45, rely=0.25)

Texto_entrada = Text(root, font=("Arial", "12", "bold"), bg="white", fg="blue", wrap="word", height=6, width=70)
Texto_entrada.place(relx=0.15, rely=0.33, relwidth=0.7, relheight=0.2)

scrollbar_entrada = Scrollbar(root, orient="vertical", command=Texto_entrada.yview)
scrollbar_entrada.place(relx=0.85, rely=0.33, relwidth=0.05, relheight=0.2)
Texto_entrada.config(yscrollcommand=scrollbar_entrada.set)

Texto_entrada.focus()


def limpar():
Texto_entrada.delete(1.0, END)
resultado_texto.configure(state=NORMAL)
resultado_texto.delete(1.0, END)
resultado_texto.configure(state=DISABLED)


def app():
t = Texto_entrada.get(1.0, END).strip()
texto = t.lower()
texto = texto.replace(".", "").replace(",", "")
palavras = texto.split()
contagem = {}
for palavra in palavras:
if palavra in contagem:
contagem[palavra] += 1
else:
contagem[palavra] = 1
resultado = ""
for palavra, quantidade in contagem.items():
resultado += f"A palavra '{palavra}' apareceu {quantidade} vez(es).\n"

resultado_texto.configure(state=NORMAL)
resultado_texto.delete(1.0, END)
resultado_texto.insert(END, resultado)
resultado_texto.configure(state=DISABLED)

but1 = Button(text="Calcular", bd=2, bg='#107db2', fg='white',
font=('verdana', 12, 'bold'), command=app)
but1.place(relx=0.1, rely=0.65, relwidth=0.25, relheight=0.1)

but_limpar = Button(text="Limpar", bd=2, bg='#107db2', fg='white',
font=('verdana', 12, 'bold'), command=limpar)
but_limpar.place(relx=0.4, rely=0.65, relwidth=0.25, relheight=0.1)

but_sair = Button(text="Sair", bd=2, bg='#107db2', fg='white',
font=('verdana', 12, 'bold'), command=root.destroy)
but_sair.place(relx=0.7, rely=0.65, relwidth=0.25, relheight=0.1)

resultado_texto = Text(root, wrap="word", height=8, width=70, font=("Arial", 12, "bold"), bg="#cfe2f3", bd=0,
fg="black", state=DISABLED)
resultado_texto.place(relx=0.05, rely=0.8, relwidth=0.9, relheight=0.15)

scrollbar_resultado = Scrollbar(root, orient="vertical", command=resultado_texto.yview)
scrollbar_resultado.place(relx=0.94, rely=0.8, relwidth=0.05, relheight=0.15)

resultado_texto.config(yscrollcommand=scrollbar_resultado.set)
scrollbar_resultado.config(command=resultado_texto.yview)

root.mainloop()

Comentários

Mensagens populares deste blogue

Criar Cartões de Visita

12 signos egípcios

Calcular a percentagem de ocupação