Analyzing Large Batches of Documents with Textstat

Analyzing Large Batches of Documents with Textstat

Whether you’re working on a research project or processing text from multiple sources, Textstat can help you analyze the readability and complexity of large sets of documents at once.

In earlier articles, we learned how to process a handful of files. This time, we’ll go deeper into batch processing at a larger scale and how to glean insights quickly using Python’s data manipulation libraries.

Why Batch Analysis Matters

Analyzing documents in bulk is essential for:

  • Research Studies: Collecting large corpora of journal articles or historical texts for comparative analysis
  • Content Audits: Checking brand communications, blog posts, or news articles for consistent readability levels
  • Educational Use: Evaluating multiple pieces of student writing to identify common patterns

Step 1: Organize Your Data

Let’s assume you have a folder called datasets/ that contains numerous .txt files. The file structure might look like this:

datasets/
   file_01.txt
   file_02.txt
   file_03.txt
   ...
   file_50.txt

Step 2: Define a Function to Read and Analyze Files

We’ll leverage Python’s glob module to find files matching a certain pattern, read them, and then compute textstat metrics of interest. Here’s a simple example:

import textstat
import pandas as pd
import glob
import os

def analyze_documents_in_folder(folder_path='datasets/*.txt'):
    """Analyze all text files in a specified folder and return DataFrame of metrics."""
    data = []
    
    for filepath in glob.glob(folder_path):
        with open(filepath, 'r', encoding='utf-8') as f:
            content = f.read()
        
        metrics = {
            'filename': os.path.basename(filepath),
            'word_count': textstat.lexicon_count(content, removepunct=True),
            'sentence_count': textstat.sentence_count(content),
            'flesch_ease': textstat.flesch_reading_ease(content),
            'flesch_grade': textstat.flesch_kincaid_grade(content),
        }
        data.append(metrics)
    
    return pd.DataFrame(data)

In this function:

  • glob.glob(folder_path) locates all matching text files
  • each file is read in UTF-8 encoding
  • a few core metrics are computed with textstat (you can add or remove metrics depending on your needs)
  • everything is stored in a list of dictionaries, which we then convert into a Pandas DataFrame and return

Step 3: Running the Analysis

Let’s run the function and see what we get. Suppose our datasets/ folder contains 5 text files for demonstration.

results_df = analyze_documents_in_folder('datasets/*.txt')
print(results_df)

And a possible output:

      filename  word_count  sentence_count  flesch_ease  flesch_grade
0  file_05.txt         906              77        67.96           6.7
1  file_04.txt         743              30        46.30          13.0
2  file_01.txt         541              11        13.08          23.7
3  file_03.txt         969              32        32.26          16.3
4  file_02.txt         637              33        60.35           9.6

Step 4: Summarizing Your Results

With the Pandas DataFrame in hand, you can compute descriptive statistics easily, or export your results to a CSV file for further processing.

summary_stats = results_df[['word_count','sentence_count','flesch_ease','flesch_grade']].describe()
print(summary_stats)

# If you want to export to CSV:
results_df.to_csv('analysis_results.csv', index=False)

Possible output:

       word_count  sentence_count  flesch_ease  flesch_grade
count    5.000000        5.000000     5.000000      5.000000
mean   759.200000       36.600000    43.990000     13.860000
std    179.145751       24.316661    22.025937      6.575181
min    541.000000       11.000000    13.080000      6.700000
25%    637.000000       30.000000    32.260000      9.600000
50%    743.000000       32.000000    46.300000     13.000000
75%    906.000000       33.000000    60.350000     16.300000
max    969.000000       77.000000    67.960000     23.700000

Now you’ve got a single file — analysis_results.csv — that contains the readability metrics for each document. This makes it straightforward to:

  • run additional analysis in Python, R, or any tool that reads CSV files
  • use spreadsheets to filter, sort, or visualize the data quickly

Step 5: Basic Visualization

We could utilize a variety of simple visualizations for deeper analysis. For example, we can create a quick bar chart or scatterplot of document word counts versus Flesch reading ease. Here’s a short snippet:

import matplotlib.pyplot as plt

# Scatter plot
plt.figure(figsize=(6,4))
plt.scatter(results_df['word_count'], results_df['flesch_ease'], color='green')
plt.xlabel("Word Count")
plt.ylabel("Flesch Reading Ease")
plt.title("Word Count vs. Flesch Reading Ease")
plt.grid(True)
plt.tight_layout()
plt.show()

Sample output visualization:

Sample output visualization

This gives you a quick visual sense of whether longer documents (higher word count) tend to have higher or lower readability scores.

Wrapping Up

And that’s it — now you can run textstat over an entire folder of documents with just a few lines of Python. This approach is perfect for auditing large content collections, conducting academic research, or simply exploring writing patterns across multiple files. Once you have your results in a single CSV or DataFrame, the possibilities for analysis, visualization, and deeper insights become endless.

Leave a Reply

Your email address will not be published. Required fields are marked *