wordcounter / app.py
rioanggara's picture
new
378cc86
raw
history blame
2.16 kB
import gradio as gr
from collections import Counter
import re
import textstat
from transformers import pipeline
from langdetect import detect
# Load a summarization model
summarizer = pipeline("summarization")
def word_and_char_counter(text):
# Clean and split text into words and sentences
words = re.findall(r'\w+', text.lower()) # Remove punctuation and make lowercase
sentences = re.split(r'[.!?]+', text) # Split text into sentences
num_sentences = len(sentences) - 1 # Adjusting for the last split being empty if text ends with a punctuation
num_words = len(words)
num_chars = len("".join(words)) # Count characters excluding spaces and punctuation
# Count the frequency of each word
word_freq = Counter(words)
most_common_words = word_freq.most_common(3)
unique_words = [word for word, count in word_freq.items() if count == 1]
# Calculate Flesch Reading Ease score
reading_ease = textstat.flesch_reading_ease(text)
# Detect language of the text
language = detect(text)
# Generate summary of the text using the transformer model
summary = summarizer(text, max_length=130, min_length=30, do_sample=False)[0]['summary_text']
# Format the results
freq_results = ', '.join([f"'{word}': {count}" for word, count in most_common_words])
unique_words_result = ', '.join(unique_words)
return f"Language: {language}. {num_sentences} sentences, {num_words} words, {num_chars} characters. Most common words: {freq_results}. Unique words: {unique_words_result}. Readability (Flesch Reading Ease): {reading_ease:.2f}. Summary: {summary}"
# Define your interface
interface = gr.Interface(
fn=word_and_char_counter,
inputs=gr.Textbox(lines=4, placeholder="Type something here..."),
outputs="text",
title="Comprehensive Text Analysis with Language Detection",
description="This app provides detailed analysis including language detection, sentence, word, and character counts, lists the most common and unique words, calculates readability, and provides a concise summary of the text."
)
# Launch the app
if __name__ == "__main__":
interface.launch()