Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# main
|
| 2 |
+
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import textstat
|
| 5 |
+
import matplotlib.pyplot as plt
|
| 6 |
+
|
| 7 |
+
def evaluate_text_details(text):
|
| 8 |
+
details = {
|
| 9 |
+
"Number of Sentences": textstat.sentence_count(text),
|
| 10 |
+
"Number of Words": textstat.lexicon_count(text, removepunct=True),
|
| 11 |
+
"Number of Syllables": textstat.syllable_count(text),
|
| 12 |
+
"Number of Characters": sum(len(word) for word in text.split()),
|
| 13 |
+
"Number of Complex Words": textstat.difficult_words(text),
|
| 14 |
+
"Percentage of Complex Words": round((textstat.difficult_words(text) / max(textstat.lexicon_count(text, removepunct=True), 1)) * 100, 3),
|
| 15 |
+
"Average Syllables per Word": round(textstat.syllable_count(text) / max(textstat.lexicon_count(text, removepunct=True), 1), 3),
|
| 16 |
+
"Average Words per Sentence": round(textstat.lexicon_count(text, removepunct=True) / max(textstat.sentence_count(text), 1),3),
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
readability_scores = {
|
| 20 |
+
"Flesch Reading Ease": textstat.flesch_reading_ease(text),
|
| 21 |
+
"Flesch-Kincaid Grade Level": textstat.flesch_kincaid_grade(text),
|
| 22 |
+
"Gunning Fog Index": textstat.gunning_fog(text),
|
| 23 |
+
"Automated Readability Index (ARI)": textstat.automated_readability_index(text),
|
| 24 |
+
"SMOG Index": textstat.smog_index(text),
|
| 25 |
+
"Coleman-Liau Index": textstat.coleman_liau_index(text),
|
| 26 |
+
"Dale-Chall Readability Score": textstat.dale_chall_readability_score(text)
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
return details, readability_scores
|
| 30 |
+
|
| 31 |
+
def plot_bar_chart(data, title, ylabel):
|
| 32 |
+
plt.figure(figsize=(6, 4))
|
| 33 |
+
plt.barh(list(data.keys()), list(data.values()), color='skyblue')
|
| 34 |
+
plt.xlabel(ylabel)
|
| 35 |
+
plt.title(title)
|
| 36 |
+
plt.grid(axis='x', linestyle='--', alpha=0.6)
|
| 37 |
+
plt.tight_layout()
|
| 38 |
+
|
| 39 |
+
plot_filename = f"{title.replace(' ', '_')}.png"
|
| 40 |
+
plt.savefig(plot_filename)
|
| 41 |
+
plt.close()
|
| 42 |
+
return plot_filename
|
| 43 |
+
|
| 44 |
+
def analyze_text(text):
|
| 45 |
+
details, readability_scores = evaluate_text_details(text)
|
| 46 |
+
|
| 47 |
+
stats_chart = plot_bar_chart(details, "Text Statistics", "Count")
|
| 48 |
+
readability_chart = plot_bar_chart(readability_scores, "Readability Scores", "Score")
|
| 49 |
+
|
| 50 |
+
return details, readability_scores, stats_chart, readability_chart
|
| 51 |
+
|
| 52 |
+
# Explanation text to be displayed above the input box
|
| 53 |
+
explanation_text = """
|
| 54 |
+
### Readability Score Descriptions:
|
| 55 |
+
- **Flesch Reading Ease**: A higher score means the text is easier to read.
|
| 56 |
+
- **Flesch-Kincaid Grade Level**: Indicates the US school grade required to understand the text.
|
| 57 |
+
- **Gunning Fog Index**: Estimates the number of years of formal education needed.
|
| 58 |
+
- **Automated Readability Index (ARI)**: Similar to Flesch-Kincaid but uses a different formula.
|
| 59 |
+
- **SMOG Index**: Designed for healthcare and scientific texts.
|
| 60 |
+
- **Coleman-Liau Index**: Uses character count instead of syllables.
|
| 61 |
+
- **Dale-Chall Readability Score**: Considers familiar words to determine readability.
|
| 62 |
+
"""
|
| 63 |
+
|
| 64 |
+
# Sample text
|
| 65 |
+
sample_text = """This is an example text. It is used to demonstrate how readability scores work.
|
| 66 |
+
The quick brown fox jumps over the lazy dog. Readability metrics help in understanding
|
| 67 |
+
how easy or difficult a text is to read."""
|
| 68 |
+
|
| 69 |
+
# Create Gradio Blocks Interface
|
| 70 |
+
with gr.Blocks(title="Text Readability Analyzer") as app:
|
| 71 |
+
gr.Markdown("## Text Readability Analyzer")
|
| 72 |
+
|
| 73 |
+
with gr.Row():
|
| 74 |
+
with gr.Column(scale=1): # Left Panel (Input & Explanation)
|
| 75 |
+
gr.Markdown(explanation_text)
|
| 76 |
+
text_input = gr.Textbox(lines=5, placeholder="Enter your text here...")
|
| 77 |
+
gr.Markdown("### Example Text")
|
| 78 |
+
example_btn = gr.Button("Use Example Text")
|
| 79 |
+
|
| 80 |
+
with gr.Column(scale=1): # Right Panel (Output Results)
|
| 81 |
+
text_details_output = gr.JSON(label="Text Details")
|
| 82 |
+
readability_scores_output = gr.JSON(label="Readability Scores")
|
| 83 |
+
stats_chart_output = gr.Image(label="Text Statistics Chart")
|
| 84 |
+
readability_chart_output = gr.Image(label="Readability Scores Chart")
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
# Function to handle button click
|
| 88 |
+
def load_example():
|
| 89 |
+
return sample_text
|
| 90 |
+
|
| 91 |
+
example_btn.click(load_example, outputs=text_input)
|
| 92 |
+
|
| 93 |
+
# Link input to function outputs
|
| 94 |
+
text_input.change(
|
| 95 |
+
analyze_text,
|
| 96 |
+
inputs=text_input,
|
| 97 |
+
outputs=[text_details_output, readability_scores_output, stats_chart_output, readability_chart_output]
|
| 98 |
+
)
|
| 99 |
+
|
| 100 |
+
# Launch the app
|
| 101 |
+
app.launch()
|