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 3 |
+
import gradio as gr
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
def analyze_text(text: str) -> str:
|
| 7 |
+
"""Analyze text and return statistics.
|
| 8 |
+
|
| 9 |
+
Args:
|
| 10 |
+
text: The input text to analyze
|
| 11 |
+
|
| 12 |
+
Returns:
|
| 13 |
+
JSON string with analysis results
|
| 14 |
+
"""
|
| 15 |
+
words = text.split()
|
| 16 |
+
chars = len(text)
|
| 17 |
+
chars_no_spaces = len(text.replace(" ", ""))
|
| 18 |
+
sentences = text.count(".") + text.count("!") + text.count("?")
|
| 19 |
+
|
| 20 |
+
avg_word_length = round(chars_no_spaces / len(words), 2) if words else 0
|
| 21 |
+
avg_sentence_length = round(len(words) / max(sentences, 1), 2)
|
| 22 |
+
|
| 23 |
+
return json.dumps(
|
| 24 |
+
{
|
| 25 |
+
"total_characters": chars,
|
| 26 |
+
"characters_without_spaces": chars_no_spaces,
|
| 27 |
+
"total_words": len(words),
|
| 28 |
+
"total_sentences": max(sentences, 1),
|
| 29 |
+
"average_word_length": avg_word_length,
|
| 30 |
+
"average_sentence_length": avg_sentence_length,
|
| 31 |
+
"unique_words": len(set(word.lower() for word in words)),
|
| 32 |
+
}
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
def extract_keywords(text: str, count: int = 5) -> str:
|
| 37 |
+
"""Extract keywords (most common words) from text.
|
| 38 |
+
|
| 39 |
+
Args:
|
| 40 |
+
text: The input text
|
| 41 |
+
count: Number of keywords to return (default 5)
|
| 42 |
+
|
| 43 |
+
Returns:
|
| 44 |
+
JSON string with keywords and frequencies
|
| 45 |
+
"""
|
| 46 |
+
# Remove common words
|
| 47 |
+
stopwords = {
|
| 48 |
+
"the",
|
| 49 |
+
"a",
|
| 50 |
+
"an",
|
| 51 |
+
"and",
|
| 52 |
+
"or",
|
| 53 |
+
"but",
|
| 54 |
+
"in",
|
| 55 |
+
"on",
|
| 56 |
+
"at",
|
| 57 |
+
"to",
|
| 58 |
+
"for",
|
| 59 |
+
"of",
|
| 60 |
+
"with",
|
| 61 |
+
"is",
|
| 62 |
+
"are",
|
| 63 |
+
"was",
|
| 64 |
+
"were",
|
| 65 |
+
"be",
|
| 66 |
+
"been",
|
| 67 |
+
"by",
|
| 68 |
+
"from",
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
words = text.lower().split()
|
| 72 |
+
filtered = [w.strip(".,!?;:") for w in words if w.lower() not in stopwords]
|
| 73 |
+
|
| 74 |
+
from collections import Counter
|
| 75 |
+
|
| 76 |
+
word_freq = Counter(filtered)
|
| 77 |
+
top_words = word_freq.most_common(count)
|
| 78 |
+
|
| 79 |
+
return json.dumps({"keywords": [{"word": w, "frequency": f} for w, f in top_words]})
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
def check_reading_level(text: str) -> str:
|
| 83 |
+
"""Estimate reading difficulty level.
|
| 84 |
+
|
| 85 |
+
Args:
|
| 86 |
+
text: The input text
|
| 87 |
+
|
| 88 |
+
Returns:
|
| 89 |
+
JSON string with reading level estimate
|
| 90 |
+
"""
|
| 91 |
+
sentences = max(
|
| 92 |
+
text.count(".") + text.count(",") + text.count("!") + text.count("?"), 1
|
| 93 |
+
)
|
| 94 |
+
words = len(text.split())
|
| 95 |
+
syllables = (
|
| 96 |
+
text.count("a")
|
| 97 |
+
+ text.count("e")
|
| 98 |
+
+ text.count("i")
|
| 99 |
+
+ text.count("o")
|
| 100 |
+
+ text.count("u")
|
| 101 |
+
)
|
| 102 |
+
|
| 103 |
+
if words == 0:
|
| 104 |
+
return json.dumps({"error": "No text to analyze"})
|
| 105 |
+
|
| 106 |
+
# Flesh Kincaid Grade
|
| 107 |
+
grade = (0.39 * (words / sentences)) + (11.8 * (syllables / words)) - 15.59
|
| 108 |
+
grade = max(0, round(grade, 1))
|
| 109 |
+
|
| 110 |
+
if grade < 6:
|
| 111 |
+
level = "Elementary School"
|
| 112 |
+
elif grade < 9:
|
| 113 |
+
level = "Middle School"
|
| 114 |
+
elif grade < 13:
|
| 115 |
+
level = "High School"
|
| 116 |
+
else:
|
| 117 |
+
level = "College/Academic"
|
| 118 |
+
|
| 119 |
+
return json.dumps({"grade_level": grade, "reading_level": level})
|
| 120 |
+
|
| 121 |
+
|
| 122 |
+
# Create web UI
|
| 123 |
+
with gr.Blocks(title="Text Processor") as demo:
|
| 124 |
+
gr.Markdown("# Text Processing Tools")
|
| 125 |
+
gr.Markdown(
|
| 126 |
+
"Analyze text statistics, extract keywords, and check reading difficulty."
|
| 127 |
+
)
|
| 128 |
+
|
| 129 |
+
with gr.Tab("Text Analysis"):
|
| 130 |
+
text_input1 = gr.Textbox(
|
| 131 |
+
label="Enter text",
|
| 132 |
+
lines=8,
|
| 133 |
+
placeholder="Paste your text here...",
|
| 134 |
+
)
|
| 135 |
+
analysis_output = gr.Textbox(label="Analysis Result", lines=8)
|
| 136 |
+
gr.Button("Analyze", size="lg").click(
|
| 137 |
+
analyze_text, inputs=text_input1, outputs=analysis_output
|
| 138 |
+
)
|
| 139 |
+
|
| 140 |
+
with gr.Tab("Extract Keywords"):
|
| 141 |
+
text_input2 = gr.Textbox(label="Enter text", lines=8)
|
| 142 |
+
count_input = gr.Slider(1, 20, value=5, step=1, label="Number of keywords")
|
| 143 |
+
keywords_output = gr.Textbox(label="Keywords ", lines=8)
|
| 144 |
+
gr.Button("Extract", size="lg").click(
|
| 145 |
+
extract_keywords, inputs=[text_input2, count_input], outputs=keywords_output
|
| 146 |
+
)
|
| 147 |
+
|
| 148 |
+
with gr.Tab("Reading Level"):
|
| 149 |
+
text_input3 = gr.Textbox(label="Enter text", lines=8)
|
| 150 |
+
level_output = gr.Textbox(label="Reading Level Analysis", lines=5)
|
| 151 |
+
gr.Button("Check Level", size="lg").click(
|
| 152 |
+
check_reading_level, inputs=text_input3, outputs=level_output
|
| 153 |
+
)
|
| 154 |
+
|
| 155 |
+
|
| 156 |
+
if __name__ == "__main__":
|
| 157 |
+
demo.launch(mcp_server=True)
|