|
|
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) |
|
|
syl = sum(max(1, len(re.findall(r'[aeiouy]+', w.lower()))) for w in wds) |
|
|
fre = 206.835 - 1.015 * avg - 84.6 * (syl / max(1, wdc)) |
|
|
fkg = 0.39 * avg + 11.8 * (syl / max(1, wdc)) - 15.59 |
|
|
|
|
|
|
|
|
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}'] |
|
|
}) |
|
|
|
|
|
|
|
|
wfq = Counter([w.lower().strip('.,!?;:"()[]') for w in wds if w.strip('.,!?;:"()[]')]) |
|
|
wf = pd.DataFrame(wfq.most_common(15), columns=['🔤 Word', '📊 Count']) |
|
|
|
|
|
|
|
|
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']) |
|
|
|
|
|
|
|
|
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']) |
|
|
|
|
|
|
|
|
sls = [] |
|
|
for i, sn in enumerate(sns, 1): |
|
|
swc = len(sn.split()) |
|
|
sls.append({ |
|
|
'📝 #': i, |
|
|
'📏 Words': swc, |
|
|
'💬 Sentence': sn |
|
|
}) |
|
|
sl = pd.DataFrame(sls) |
|
|
|
|
|
|
|
|
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 |
|
|
|
|
|
|
|
|
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() |
|
|
|