File size: 4,258 Bytes
0eb97a6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import json

import gradio as gr


def analyze_text(text: str) -> str:
    """Analyze text and return statistics.

    Args:
        text: The input text to analyze

    Returns:
        JSON string with analysis results
    """
    words = text.split()
    chars = len(text)
    chars_no_spaces = len(text.replace(" ", ""))
    sentences = text.count(".") + text.count("!") + text.count("?")

    avg_word_length = round(chars_no_spaces / len(words), 2) if words else 0
    avg_sentence_length = round(len(words) / max(sentences, 1), 2)

    return json.dumps(
        {
            "total_characters": chars,
            "characters_without_spaces": chars_no_spaces,
            "total_words": len(words),
            "total_sentences": max(sentences, 1),
            "average_word_length": avg_word_length,
            "average_sentence_length": avg_sentence_length,
            "unique_words": len(set(word.lower() for word in words)),
        }
    )


def extract_keywords(text: str, count: int = 5) -> str:
    """Extract keywords (most common words) from text.

    Args:
        text: The input text
        count: Number of keywords to return (default 5)

    Returns:
        JSON string with keywords and frequencies
    """
    # Remove common words
    stopwords = {
        "the",
        "a",
        "an",
        "and",
        "or",
        "but",
        "in",
        "on",
        "at",
        "to",
        "for",
        "of",
        "with",
        "is",
        "are",
        "was",
        "were",
        "be",
        "been",
        "by",
        "from",
    }

    words = text.lower().split()
    filtered = [w.strip(".,!?;:") for w in words if w.lower() not in stopwords]

    from collections import Counter

    word_freq = Counter(filtered)
    top_words = word_freq.most_common(count)

    return json.dumps({"keywords": [{"word": w, "frequency": f} for w, f in top_words]})


def check_reading_level(text: str) -> str:
    """Estimate reading difficulty level.

    Args:
        text: The input text

    Returns:
        JSON string with reading level estimate
    """
    sentences = max(
        text.count(".") + text.count(",") + text.count("!") + text.count("?"), 1
    )
    words = len(text.split())
    syllables = (
        text.count("a")
        + text.count("e")
        + text.count("i")
        + text.count("o")
        + text.count("u")
    )

    if words == 0:
        return json.dumps({"error": "No text to analyze"})

    # Flesh Kincaid Grade
    grade = (0.39 * (words / sentences)) + (11.8 * (syllables / words)) - 15.59
    grade = max(0, round(grade, 1))

    if grade < 6:
        level = "Elementary School"
    elif grade < 9:
        level = "Middle School"
    elif grade < 13:
        level = "High School"
    else:
        level = "College/Academic"

    return json.dumps({"grade_level": grade, "reading_level": level})


# Create web UI
with gr.Blocks(title="Text Processor") as demo:
    gr.Markdown("# Text Processing Tools")
    gr.Markdown(
        "Analyze text statistics, extract keywords, and check reading difficulty."
    )

    with gr.Tab("Text Analysis"):
        text_input1 = gr.Textbox(
            label="Enter text",
            lines=8,
            placeholder="Paste your text here...",
        )
        analysis_output = gr.Textbox(label="Analysis Result", lines=8)
        gr.Button("Analyze", size="lg").click(
            analyze_text, inputs=text_input1, outputs=analysis_output
        )

    with gr.Tab("Extract Keywords"):
        text_input2 = gr.Textbox(label="Enter text", lines=8)
        count_input = gr.Slider(1, 20, value=5, step=1, label="Number of keywords")
        keywords_output = gr.Textbox(label="Keywords ", lines=8)
        gr.Button("Extract", size="lg").click(
            extract_keywords, inputs=[text_input2, count_input], outputs=keywords_output
        )

    with gr.Tab("Reading Level"):
        text_input3 = gr.Textbox(label="Enter text", lines=8)
        level_output = gr.Textbox(label="Reading Level Analysis", lines=5)
        gr.Button("Check Level", size="lg").click(
            check_reading_level, inputs=text_input3, outputs=level_output
        )


if __name__ == "__main__":
    demo.launch(mcp_server=True)