wordcounter / app.py
rioanggara's picture
change all
58ef530
raw
history blame
1.01 kB
import gradio as gr
from collections import Counter
import re
def word_and_char_counter(text):
# Clean and split text into words
words = re.findall(r'\w+', text.lower()) # Remove punctuation and make lowercase
num_words = len(words)
num_chars = len("".join(words)) # Count characters excluding spaces and punctuation
# Count the frequency of each word
word_freq = Counter(words).most_common(3)
# Format the word frequency results
freq_results = ', '.join([f"'{word}': {count}" for word, count in word_freq])
return f"{num_words} words, {num_chars} characters. Most common words: {freq_results}"
# Define your interface
interface = gr.Interface(
fn=word_and_char_counter,
inputs=gr.Textbox(lines=4, placeholder="Type something here..."),
outputs="text",
title="Word and Character Counter",
description="Counts the words and characters in your text and shows the most common words."
)
# Launch the app
if __name__ == "__main__":
interface.launch()