|
|
|
|
|
import gradio as gr |
|
import textstat |
|
import matplotlib.pyplot as plt |
|
|
|
def evaluate_text_details(text): |
|
details = { |
|
"Number of Sentences": textstat.sentence_count(text), |
|
"Number of Words": textstat.lexicon_count(text, removepunct=True), |
|
"Number of Syllables": textstat.syllable_count(text), |
|
"Number of Characters": sum(len(word) for word in text.split()), |
|
"Number of Complex Words": textstat.difficult_words(text), |
|
"Percentage of Complex Words": round((textstat.difficult_words(text) / max(textstat.lexicon_count(text, removepunct=True), 1)) * 100, 3), |
|
"Average Syllables per Word": round(textstat.syllable_count(text) / max(textstat.lexicon_count(text, removepunct=True), 1), 3), |
|
"Average Words per Sentence": round(textstat.lexicon_count(text, removepunct=True) / max(textstat.sentence_count(text), 1),3), |
|
} |
|
|
|
readability_scores = { |
|
"Flesch Reading Ease": textstat.flesch_reading_ease(text), |
|
"Flesch-Kincaid Grade Level": textstat.flesch_kincaid_grade(text), |
|
"Gunning Fog Index": textstat.gunning_fog(text), |
|
"Automated Readability Index (ARI)": textstat.automated_readability_index(text), |
|
"SMOG Index": textstat.smog_index(text), |
|
"Coleman-Liau Index": textstat.coleman_liau_index(text), |
|
"Dale-Chall Readability Score": textstat.dale_chall_readability_score(text) |
|
} |
|
|
|
return details, readability_scores |
|
|
|
def plot_bar_chart(data, title, ylabel): |
|
plt.figure(figsize=(6, 4)) |
|
plt.barh(list(data.keys()), list(data.values()), color='skyblue') |
|
plt.xlabel(ylabel) |
|
plt.title(title) |
|
plt.grid(axis='x', linestyle='--', alpha=0.6) |
|
plt.tight_layout() |
|
|
|
plot_filename = f"{title.replace(' ', '_')}.png" |
|
plt.savefig(plot_filename) |
|
plt.close() |
|
return plot_filename |
|
|
|
def analyze_text(text): |
|
details, readability_scores = evaluate_text_details(text) |
|
|
|
stats_chart = plot_bar_chart(details, "Text Statistics", "Count") |
|
readability_chart = plot_bar_chart(readability_scores, "Readability Scores", "Score") |
|
|
|
return details, readability_scores, stats_chart, readability_chart |
|
|
|
|
|
explanation_text = """ |
|
### Readability Score Descriptions: |
|
- **Flesch Reading Ease**: A higher score means the text is easier to read. |
|
- **Flesch-Kincaid Grade Level**: Indicates the US school grade required to understand the text. |
|
- **Gunning Fog Index**: Estimates the number of years of formal education needed. |
|
- **Automated Readability Index (ARI)**: Similar to Flesch-Kincaid but uses a different formula. |
|
- **SMOG Index**: Designed for healthcare and scientific texts. |
|
- **Coleman-Liau Index**: Uses character count instead of syllables. |
|
- **Dale-Chall Readability Score**: Considers familiar words to determine readability. |
|
""" |
|
|
|
|
|
sample_text = """This is an example text. It is used to demonstrate how readability scores work. |
|
The quick brown fox jumps over the lazy dog. Readability metrics help in understanding |
|
how easy or difficult a text is to read.""" |
|
|
|
|
|
with gr.Blocks(title="Text Readability Analyzer") as app: |
|
gr.Markdown("## Text Readability Analyzer") |
|
|
|
with gr.Row(): |
|
with gr.Column(scale=1): |
|
gr.Markdown(explanation_text) |
|
text_input = gr.Textbox(lines=5, placeholder="Enter your text here...") |
|
gr.Markdown("### Example Text") |
|
example_btn = gr.Button("Use Example Text") |
|
|
|
with gr.Column(scale=1): |
|
text_details_output = gr.JSON(label="Text Details") |
|
readability_scores_output = gr.JSON(label="Readability Scores") |
|
stats_chart_output = gr.Image(label="Text Statistics Chart") |
|
readability_chart_output = gr.Image(label="Readability Scores Chart") |
|
|
|
|
|
|
|
def load_example(): |
|
return sample_text |
|
|
|
example_btn.click(load_example, outputs=text_input) |
|
|
|
|
|
text_input.change( |
|
analyze_text, |
|
inputs=text_input, |
|
outputs=[text_details_output, readability_scores_output, stats_chart_output, readability_chart_output] |
|
) |
|
|
|
|
|
app.launch() |
|
|