Spaces:
Sleeping
Sleeping
Commit ·
58ef530
1
Parent(s): 401d95e
change all
Browse files
app.py
CHANGED
|
@@ -1,18 +1,27 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
| 2 |
|
| 3 |
def word_and_char_counter(text):
|
| 4 |
-
|
|
|
|
| 5 |
num_words = len(words)
|
| 6 |
-
num_chars = len(
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
# Define your interface
|
| 10 |
interface = gr.Interface(
|
| 11 |
fn=word_and_char_counter,
|
| 12 |
-
inputs=gr.Textbox(lines=
|
| 13 |
outputs="text",
|
| 14 |
title="Word and Character Counter",
|
| 15 |
-
description="Counts the words and
|
| 16 |
)
|
| 17 |
|
| 18 |
# Launch the app
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from collections import Counter
|
| 3 |
+
import re
|
| 4 |
|
| 5 |
def word_and_char_counter(text):
|
| 6 |
+
# Clean and split text into words
|
| 7 |
+
words = re.findall(r'\w+', text.lower()) # Remove punctuation and make lowercase
|
| 8 |
num_words = len(words)
|
| 9 |
+
num_chars = len("".join(words)) # Count characters excluding spaces and punctuation
|
| 10 |
+
|
| 11 |
+
# Count the frequency of each word
|
| 12 |
+
word_freq = Counter(words).most_common(3)
|
| 13 |
+
|
| 14 |
+
# Format the word frequency results
|
| 15 |
+
freq_results = ', '.join([f"'{word}': {count}" for word, count in word_freq])
|
| 16 |
+
return f"{num_words} words, {num_chars} characters. Most common words: {freq_results}"
|
| 17 |
|
| 18 |
# Define your interface
|
| 19 |
interface = gr.Interface(
|
| 20 |
fn=word_and_char_counter,
|
| 21 |
+
inputs=gr.Textbox(lines=4, placeholder="Type something here..."),
|
| 22 |
outputs="text",
|
| 23 |
title="Word and Character Counter",
|
| 24 |
+
description="Counts the words and characters in your text and shows the most common words."
|
| 25 |
)
|
| 26 |
|
| 27 |
# Launch the app
|