CoolFace
Apppublic

MK-316/textanalysis01

sourceHugging Faceupdated 3y agoView on Hugging Face
0likes
app.py100 linesDownload Raw Back to root
1import gradio as gr2import re3import math4 5# Define the TTR, MTLD, and Flesch Reading Ease functions as previously described6# ... (functions go here)7 8# TTR9 10def calculate_ttr(text):11    words = re.findall(r'\b[a-zA-Z]+\b', text.lower())12    unique_words = len(set(words))13    total_words = len(words)14    ttr = unique_words / total_words if total_words != 0 else 015    return math.ceil(ttr * 100) / 100  # Rounded up to 2 decimal places16 17# MTLD18 19def calculate_mtld(text, ttr_threshold=0.72):20    def mtld_calculation(word_list, threshold):21        token_count = 022        factor_count = 023        for i in range(len(word_list)):24            token_count += 125            unique_words = len(set(word_list[:i + 1]))26            current_ttr = unique_words / token_count27            if current_ttr < threshold:28                factor_count += 129                token_count = 030        if token_count > 0:31            factor_count += token_count / len(word_list)32        return len(word_list) / factor_count if factor_count != 0 else 033 34    words = re.findall(r'\b[a-zA-Z]+\b', text.lower())35    mtld_forward = mtld_calculation(words, ttr_threshold)36    mtld_backward = mtld_calculation(words[::-1], ttr_threshold)37    mtld = (mtld_forward + mtld_backward) / 238    return math.ceil(mtld * 100) / 100  # Rounded up to 2 decimal places39 40# Flesch Reading Ease41 42def count_syllables(word):43    syllable_count = 044    vowels = "aeiouy"45    if word[0] in vowels:46        syllable_count += 147    for index in range(1, len(word)):48        if word[index] in vowels and word[index - 1] not in vowels:49            syllable_count += 150    if word.endswith("e"):51        syllable_count -= 152    if syllable_count == 0:53        syllable_count += 154    return syllable_count55 56def calculate_flesch_reading_ease(text):57    sentences = re.split(r'[.!?]+', text)58    words = re.findall(r'\b[a-zA-Z]+\b', text)59    total_sentences = len(sentences) - 1 if sentences[-1] == '' else len(sentences) # Adjusting for the case when text ends with a punctuation mark60    total_words = len(words)61    total_syllables = sum(count_syllables(word) for word in words)62    if total_sentences == 0 or total_words == 0: # Prevent division by zero63        return 064    flesch_score = 206.835 - 1.015 * (total_words / total_sentences) - 84.6 * (total_syllables / total_words)65    return math.ceil(flesch_score * 100) / 100  # Rounded up to 2 decimal places66 67def calculate_flesch_kincaid_grade_level(text):68    sentences = re.split(r'[.!?]+', text)69    words = re.findall(r'\b[a-zA-Z]+\b', text)70    total_sentences = len(sentences) - 1 if sentences[-1] == '' else len(sentences)71    total_words = len(words)72    total_syllables = sum(count_syllables(word) for word in words)73    if total_sentences == 0 or total_words == 0: # Prevent division by zero74        return 075    fk_grade_level = 0.39 * (total_words / total_sentences) + 11.8 * (total_syllables / total_words) - 15.5976    return math.ceil(fk_grade_level * 100) / 100  # Rounded up to 2 decimal places77 78def analyze_text(text):79    word_count = len(re.findall(r'\b[a-zA-Z]+\b', text))80    ttr = calculate_ttr(text)81    mtld = calculate_mtld(text)82    flesch_score = calculate_flesch_reading_ease(text)83    fk_grade_level = calculate_flesch_kincaid_grade_level(text)84    return word_count, round(ttr, 2), round(mtld, 2), round(flesch_score, 2), round(fk_grade_level, 2)85 86interface = gr.Interface(87    fn=analyze_text,88    inputs=gr.Textbox(lines=10, label="Input Text"),89    outputs=[90        gr.Textbox(label="Word Count"),91        gr.Textbox(label="Type-Token Ratio (TTR)"),92        gr.Textbox(label="Measure of Textual Lexical Diversity (MTLD)"),93        gr.Textbox(label="Flesch Reading Ease (Readability measure)"),94        gr.Textbox(label="Flesch-Kincaid Grade Level (Readability index)")95    ],96    title="Text Analysis Tool",97    description="Enter text to analyze its word count, Type-Token Ratio (TTR), Measure of Textual Lexical Diversity (MTLD), Flesch Reading Ease, and Flesch-Kincaid Grade Level. Note: The Flesch-Kincaid Grade Level indicates the U.S. school grade level needed to understand the text."98)99 100interface.launch()