Redability Score
Python code to Calculate readability score for text in CSV file, using TEXTSTAT
import osimport pandas as pdimport textstat
# Function to calculate readability metrics for a given textdef calculate_readability(text): return { "flesch_reading_ease": textstat.flesch_reading_ease(text), "flesch_kincaid_grade": textstat.flesch_kincaid_grade(text), "smog_index": textstat.smog_index(text), "coleman_liau_index": textstat.coleman_liau_index(text), "automated_readability_index": textstat.automated_readability_index(text), "dale_chall_readability_score": textstat.dale_chall_readability_score(text), "difficult_words": textstat.difficult_words(text), "linsear_write_formula": textstat.linsear_write_formula(text), "gunning_fog": textstat.gunning_fog(text), "text_standard": textstat.text_standard(text, float_output=True) }
# Read the CSV file with appropriate encoding or fallback to 'latin-1'try: df = pd.read_csv('filename.csv', encoding='utf-8-sig')except UnicodeDecodeError: df = pd.read_csv('filename.csv', encoding='latin-1')
# Calculate readability metrics for each textdf['readability_metrics'] = df['Coloum name'].apply(calculate_readability)
# Save the results to the desktop directorydesktop_path = os.path.join(os.path.join(os.environ['USERPROFILE']), 'Desktop')output_file = os.path.join(desktop_path, 'readability_results.csv')df.to_csv(output_file, index=False)
print(f"Readability results saved to '{output_file}'.")
# Function to calculate readability metrics for a given textdef calculate_readability(text): return { "flesch_reading_ease": textstat.flesch_reading_ease(text), "flesch_kincaid_grade": textstat.flesch_kincaid_grade(text), "smog_index": textstat.smog_index(text), "coleman_liau_index": textstat.coleman_liau_index(text), "automated_readability_index": textstat.automated_readability_index(text), "dale_chall_readability_score": textstat.dale_chall_readability_score(text), "difficult_words": textstat.difficult_words(text), "linsear_write_formula": textstat.linsear_write_formula(text), "gunning_fog": textstat.gunning_fog(text), "text_standard": textstat.text_standard(text, float_output=True) }
# Read the CSV file with appropriate encoding or fallback to 'latin-1'try: df = pd.read_csv('filename.csv', encoding='utf-8-sig')except UnicodeDecodeError: df = pd.read_csv('filename.csv', encoding='latin-1')
# Calculate readability metrics for each textdf['readability_metrics'] = df['Coloum name'].apply(calculate_readability)
# Save the results to the desktop directorydesktop_path = os.path.join(os.path.join(os.environ['USERPROFILE']), 'Desktop')output_file = os.path.join(desktop_path, 'readability_results.csv')df.to_csv(output_file, index=False)
print(f"Readability results saved to '{output_file}'.")