File size: 3,986 Bytes
5b854d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import pandas as pd
from collections import Counter
import re

def ana(txt):
    """Analyze text and return comprehensive stats in dataframes"""
    if not txt.strip():
        return "Enter text!", None, None, None, None
    
    wds = txt.split()
    chs = len(txt)
    wdc = len(wds)
    sns = [s.strip() for s in re.split(r'[.!?]+', txt) if s.strip()]
    snc = len(sns)
    avg = wdc / max(1, snc)
    rdm = wdc / 200
    wrd = sum(len(w) for w in wds) / max(1, wdc)  # avg word length
    syl = sum(max(1, len(re.findall(r'[aeiouy]+', w.lower()))) for w in wds)  # rough syllable count
    fre = 206.835 - 1.015 * avg - 84.6 * (syl / max(1, wdc))  # Flesch Reading Ease
    fkg = 0.39 * avg + 11.8 * (syl / max(1, wdc)) - 15.59  # Flesch-Kincaid Grade

    # Add to stats dataframe
    st = pd.DataFrame({
        '📊 Metric': ['Characters', 'Words', 'Sentences', 'Avg Words/Sentence', 
                    'Reading Time (min)', 'Readability Score', 'Grade Level'],
        '📈 Value': [chs, wdc, snc, f'{avg:.1f}', f'{rdm:.1f}', 
                    f'{fre:.1f}', f'{fkg:.1f}']
    })
    
    # Word frequency (1-grams)
    wfq = Counter([w.lower().strip('.,!?;:"()[]') for w in wds if w.strip('.,!?;:"()[]')])
    wf = pd.DataFrame(wfq.most_common(15), columns=['🔤 Word', '📊 Count'])
    
    # Bi-grams
    bgr = [f"{wds[i].lower()} {wds[i+1].lower()}" for i in range(len(wds)-1)]
    bgc = Counter(bgr)
    bg = pd.DataFrame(bgc.most_common(15), columns=['🔤 Bi-gram', '📊 Count'])
    
    # Tri-grams
    tgr = [f"{wds[i].lower()} {wds[i+1].lower()} {wds[i+2].lower()}" for i in range(len(wds)-2)]
    tgc = Counter(tgr)
    tg = pd.DataFrame(tgc.most_common(15), columns=['🔤 Tri-gram', '📊 Count'])
    
    # Sentence analysis with actual sentences
    sls = []
    for i, sn in enumerate(sns, 1):
        swc = len(sn.split())
        sls.append({
            '📝 #': i,
            '📏 Words': swc,
            '💬 Sentence': sn
        })
    sl = pd.DataFrame(sls)
    
    # Character distribution
    chd = Counter(txt.lower())
    let = {c: chd[c] for c in chd if c.isalpha()}
    let = dict(sorted(let.items(), key=lambda x: x[1], reverse=True)[:20])
    ch = pd.DataFrame(list(let.items()), columns=['🔤 Letter', '📊 Count'])
    
    return st, wf, bg, tg, sl, ch

# Gradio Interface
with gr.Blocks(theme=gr.themes.Soft(), css="""
    .gradio-container {background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);}
    h1, h3 {color: white !important;}
    """) as app:
    
    gr.Markdown("# 🚀 Text Analyzer Pro\n### Comprehensive text analysis with n-grams!")
    
    inp = gr.Textbox(label="📝 Enter Your Text", placeholder="Type or paste text...", lines=8)
    btn = gr.Button("🔍 Analyze", variant="primary", size="lg")
    
    with gr.Row():
        o1 = gr.Dataframe(label="📊 Overall Statistics", interactive=False)
        o2 = gr.Dataframe(label="🎨 Character Distribution", interactive=False)
    
    gr.Markdown("### 🔤 N-Gram Analysis")
    with gr.Row():
        o3 = gr.Dataframe(label="1-Grams (Words)", interactive=False)
        o4 = gr.Dataframe(label="2-Grams (Phrases)", interactive=False)
        o5 = gr.Dataframe(label="3-Grams (Phrases)", interactive=False)
    
    gr.Markdown("### 📈 Sentence Analysis")
    o6 = gr.Dataframe(label="📝 Sentences with Length", interactive=False, wrap=True)
    
    btn.click(fn=ana, inputs=inp, outputs=[o1, o3, o4, o5, o6, o2])
    
    gr.Examples(
        examples=[
            ["The quick brown fox jumps over the lazy dog. This is a test sentence. Testing is important!"],
            ["Machine learning is transforming technology. Artificial intelligence powers innovation. Deep learning drives progress."]
        ],
        inputs=inp,
        label="💡 Try These Examples"
    )
    
    gr.Markdown("---\n*Built with ❤️ using Gradio | All variables ≤ 3 chars*")

if __name__ == "__main__":
    app.launch()