Classifying Concord

© 2025 Richard Anton. All rights reserved.

Introduction

This article is a walkthrough of using machine learning classification techniques combined with some preprocessing to distinguish between the writing of Emerson and Thoreau.

I wanted to show methods for classifying text using a novel dataset rather than one of the toy ones so often used for tutorials. Eventually I settled on classifying text to predict the original author. Since I wanted this to be primarily about actual writing style rather than the changes to language between regions or over time I chose two authors from the same era and location, Emerson and Thoreau. For background on computational authorship attribution see Koppel et al. (2009) .

Walden has always been one of my favorite books and had a big influence on me when I first read it in high school, but Emerson was a different story. I found his writing style wordy and frustrating. Recently I revisited the context of these works when I read The Transcendentalists and Their World (Gross, 2021) which gave me the idea to use their writings as the basis for this project.

The dataset was created from two public domain works, one by each author. For Emerson, I used Essays: First Series (1841), and for Thoreau, there was really no choice but Walden, and On The Duty Of Civil Disobedience (1854).

The texts were segmented into passages of 3-5 sentences each, creating a collection of labeled examples for training and testing classification models.

To explore how different machine learning approaches perform on this task I compared multiple methods including classical models like logistic regression, random forests, and support vector machines, along with newer transformer-based models.

The accompanying code, but not this article itself, is licensed under the MIT License. See the LICENSE file for details.

The Evolution of Machine Learning Classification

Text classification has developed substantially over the years. Early methods in the 1960s relied on hand-crafted rules, for example Rocchio (1971), then statistical techniques took over. Advanced neural networks joined simpler statistical models in the following years, notably recurrent neural networks (Sebastiani, 2002). More recently, transformer-based models have gained prominence in this and most other natural language processing (NLP) use cases from 2018 onward.

  1. Rule-based systems (1960s-1980s): Manually crafted rules and decision trees.

  2. Statistical methods (1990s-2000s): Naïve Bayes, Support Vector Machines, and Logistic Regression

  3. Neural network approaches (2010s): Recurrent neural networks (RNNs) including LSTMs (Hochreiter & Schmidhuber, 1997) and convolutional neural networks (CNNs) (LeCun et al. 1998).

  4. Transformer models (2018-present): BERT, GPT, and their derivatives following the original "Attention is All You Need" paper on transformers Vaswani, A. et al., 2017).

Each era brought improvements to accuracy and capability. The field also expanded from simple categorization to include other use cases like sentiment analysis, authorship attribution (Stamatatos, 2009), and stylometry—the quantitative study of writing style (Burrows, 2002).

We compare methods across this range, first with TF-IDF representation with various classifiers then using a pretrained transformer DistilBERT model first as a feature extractor, then directly as a classifier with fine-tuning.

Creating a Unique Literary Dataset

Data Acquisition

First, we selected two representative texts from Project Gutenberg's public domain collection. (Project Gutenberg, n.d.).

This programmatic approach ensures reproducibility and automation eliminating the need for manual file handling.

Text Preprocessing

Both works contain Project Gutenberg boilerplate text that needs removal. We identify the start of the actual content by searching for the standard "START OF THE PROJECT GUTENBERG EBOOK" marker:

Text Segmentation

We segment the texts into manageable chunks of 3-5 sentences each. This creates a dataset with many examples for training and testing while preserving enough context to capture authorial style. We did not directly experiment with different sample lengths, but having the variability allows us to still do some comparison on how the length affects model performance.

This function creates randomly-sized windows of 3-5 sentences, providing natural text chunks while introducing variation in the dataset.

 

Dataset Creation

We create pandas DataFrames from these segments and label them with their respective authors:

The result is a dataset of 1,911 text segments (1,064 from Emerson and 847 from Thoreau), each labeled with its author. This is relatively balancd which helps prevent bias in the models.

 

Overview of Methods

At a high level there are two versions of the ML pipeline whether we are using a deep learning/transformers based approach or using a more traditional path.

 

Traditional path (blue): Raw Text → Preprocessing → TF-IDF → Classical ML models → Classification

Transformer path (green): Raw Text → Preprocessing → BERT embeddings → Fine-tuned model → Classification

📝 Raw Text
Emerson: 'Essays: First Series'
Thoreau: 'Walden & Civil Disobedience'
1,911 text segments
🔧 Preprocessing
• Remove Project Gutenberg boilerplate
• Segment into 3-5 sentences
• Remove stopwords & punctuation
• Lemmatization with spaCy
🔀 Feature Extraction Split
📊 Traditional Path:
TF-IDF Vectorization
🧠 Transformer Path:
DistilBERT Embeddings
🎯 Classical ML Models
• Logistic Regression
• Random Forest
• Support Vector Machine
🔧 DistilBERT Feature Extraction
🎯 Fine-tuned DistilBERT
🎯 Classical ML + BERT Features
• Logistic Regression
• SVM
📈 Results: 83-86% Accuracy
🏆 Best: SVM (86%)
📈 Results: 89-90% Accuracy
🏆 Best: Logistic Regression (90%)
📈 Results: 92% Accuracy
🏆 Best Overall Performance
🎯 Final Classification
Emerson vs Thoreau

 

Text Representation Techniques

Visualization with Word Clouds

Before diving into classification, we visualize the most frequent words used by each author through word clouds, excluding common stopwords:

emerson_word_cloud

thoreau_word_cloud

These visualizations already reveal interesting differences in vocabulary. Emerson's text prominently features abstract terms like "soul," "nature," "truth," and "power," reflecting his focus on philosophical concepts. Thoreau's cloud emphasizes more concrete words like "house," "wood," "water," and "life," mirroring his practical observations at Walden Pond.

Text Preprocessing for ML

We preprocess the text using spaCy (Honnibal & Montani, 2017) to remove stopwords and perform lemmatization, which reduces words to their base forms:

TF-IDF Vectorization

For our traditional machine learning models, we convert text to numerical features using Term Frequency-Inverse Document Frequency (TF-IDF) vectorization:

TF-IDF converts text into numerical vectors by calculating two components for each word: how frequently it appears in a specific document (Term Frequency), and how unique it is across all documents (Inverse Document Frequency) (Jones, 1972). The value combines how frequently the term appears in the document (TF) and how unique it is across all documents (IDF). This balances common words against distinctive ones that might better differentiate the authors (Salton & Buckley, 1988).

Traditional ML Classification Models

We split our dataset into training (80%) and test (20%) sets to evaluate model performance, as typically needed for a supervised ML workflow:

Logistic Regression

We begin with logistic regression, a simple but effective linear classifier (Cox, D.R., 1958).

And here we have our results for this first model.

If you are not already familiar with precision, accuracy, recall, and F1 metrics for model evaluation or want a review, then I recommend the article Performance Metrics: Confusion matrix, Precision, Recall, and F1 Score (Jayaswal, V. , 2020).

logistic_regression_confusion

Logistic regression achieves an impressive 85% accuracy, correctly identifying the author in most cases.

Here we have included the confusion matrix. For space we will omit this for most of the models, but they are available in the GitHub repo in the original notebook.

Random Forest

Next, we implement a Random Forest classifier, which creates an ensemble of decision trees (Breiman, 2001).

Results:

The Random Forest model achieves 83% accuracy, slightly lower than logistic regression.

Support Vector Machine

We also implement a Support Vector Machine (SVM) with a radial basis function kernel. SVM's are often a strong model compared to traditional neural networks (pre-transformer,etc) that can train more quickly and often converge more reliabily (Cortes & Vapnik 1995). If you are not familiar with SVMs and want to understand the theory behind them a good place to start is the KDnuggets article A gentle introduction to support vector machines (Priya, 2023).

Results:

svm_confusion

The SVM achieves our best traditional model performance at 86% accuracy. I think SVM's are somewhat underappreciated amid all the neural network hype, for reference on this see "Do we Need Hundreds of Classifiers to Solve Real World Classification Problems?" by Fernández-Delgado et al. (2014). SVMs can be trained in an amount of time that is quite zippy compared to more complex models that do not always beat them without a lot of training data and with less risk of overfitting.

Deep Learning and Transformer Models

Transformer models revolutionized NLP tasks by capturing complex contextual relationships in text (Wolf et al., 2020). Here we use DistilBERT (Sanh, Debut, Chaumond, & Wolf, 2019), a lightweight version of BERT (Bidirectional Encoder Representations from Transformers) (Devlin, Chang, Lee, & Toutanova, 2019). We use the popular Tranformers open source library (Wolf et al., 2020) from Hugging Face for loading, training, and running the model.

Unlike traditional methods that treat words as independent units, transformer models like BERT understand context by analyzing how words relate to all other words in a sentence simultaneously. This 'attention mechanism' allows the model to capture subtle stylistic patterns that might be missed by simpler approaches.

In 2025, DistilBERT is not exactly state of the art, however you can train or fine-tune it easily with very modest resources. You can replicate this entire project in Google Colab for free.

We employ two different strategies using DistilBERT:

  1. Feature Extraction: We freeze DistilBERT's pre-trained weights and use its internal representations as sophisticated features for a traditional classifier.

  2. Fine-tuning: We allow DistilBERT's weights to update during training on our dataset, adapting the entire model specifically for our Emerson vs. Thoreau classification task.

Feature Extraction Approach

First, we use DistilBERT as a feature extractor, employing its hidden states as input to traditional classifiers. This method is useful in situations where you may not have enough data to effectively train a model from scratch, but you want something more powerful than simple feature engineering techniques.

There is a fair bit of code we are leaving out of the article to get things in a form to make the Transformers library happy here since everything before this point was setup with scikit learn.

 

We then use these features with our traditional classifiers:

Logistic Regression on DistilBERT hidden states:

lr_dbert_confusion

SVM on DistilBERT hidden states:

This hybrid approach shows a significant improvement, with accuracy increasing to 90%.

Fine-tuning DistilBERT

And finally, we fine-tune DistilBERT specifically for our classification task using the training split of our dataset.

Here we have left out a bunch of preprocessing code and other boiler plate for brevity which you can find in the repo.

Results:

fine_tuned_dbert_confusion

The fine-tuned DistilBERT achieves our best performance at 92% accuracy, demonstrating how powerful transformer models are for capturing subtle stylistic differences.

Analysis of Results

Performance Comparison

Looking at our results across models:

  1. Traditional ML models (TF-IDF + classifiers): 83-86% accuracy

  2. DistilBERT features + traditional classifiers: 89-90% accuracy

  3. Fine-tuned DistilBERT: 92% accuracy

This progression demonstrates the advantages of modern transformer models, which capture contextual information and semantic relationships beyond what bag-of-words approaches can represent.

The 6-9% accuracy improvement from traditional ML to transformers suggests that author style involves subtle contextual patterns beyond simple word frequency that require more sophisticated language understanding to detect.

ML Approach PerformanceRand. ForestLog. Reg.SVMdBERT+RFdBERT+SVMdBERT+LRF.Tuned dBERT9492908886848280Accuracy (%)

 

Misclassification Analysis

Examining misclassified examples provides some insights on model performance relative to different aspects of input samples:

Several patterns emerge in the 30 misclassified examples (out of 383):

  1. Project Gutenberg boilerplate: Many misclassifications involve similar license text that appears in both works

  2. Short segments: Brief text snippets provide insufficient stylistic markers

  3. Universal themes: When both authors discuss similar philosophical concepts, the distinction blurs

  4. Quotations: When either author quotes someone else, their personal style is diminished.

One interesting observation is that Thoreau's more practical descriptions seem to rarely be misclassified, while his philosophical musings more frequently get attributed to Emerson. This aligns with our understanding of their writing styles-Thoreau's concrete observations of nature are distinctive, while both authors share transcendentalist philosophical language.

It is also interesting to look at the length of passages versus the author prediction correctness.

density_plots

Word Count (Median/Average)

ClassificationCorrectly ClassifiedIncorrectly Classified
all78.00/90.5968.50/77.50
thoreau100.00/111.7978.50/74.38
emerson62.00/71.5265.50/79.06

We can see that the median and average length correleates significantly with correctness for passages by Thoreau but seems to have less relation for passages by Emerson.

 

Model Interpretability

While transformer models achieve the highest accuracy, they function as "black boxes" compared to traditional models. With logistic regression, we can examine the coefficients to identify the most influential words for classification:

The most strongly Emerson-associated terms include abstract nouns and adjectives like "soul," "divine," "intellect," and "universal." Thoreau's distinctive vocabulary includes more concrete terms like "pond," "woods," "house," and action verbs reflecting his practical experiences at Walden.

Conclusion

The results show how well even simpler ML models can distinguish between the literary style of these two authors. The fine-tuned DistilBERT model achieved 92% accuracy on the test set, while traditional machine learning approaches reached 83–86% accuracy.

The performance progression from traditional to transformer-based models reflects broader developments in natural language processing. TF-IDF vectorization with classic algorithms provided a functional baseline (83–86% accuracy), while transformer models improved on these results (92% accuracy) by capturing contextual relationships between words.

The misclassification analysis revealed some specific patterns in the 30 misclassified examples (from 383 total test examples). These conclusions are qualitative and somewhat subjective, but indicate a tendency for quotations, shorter samples, and topic to affect the accuracy of the model on particular samples.

The classification results highlight the distinct writing styles of that Emerson and Thoreau despite their shared historical context, philosophy, interests, and proximity.

If you made it here thanks for reading, and I hope you found this helpful and interesting.

Project Repository

All code is available at https://github.com/ranton256/classifying_concord

References

Literary Works and Sources

  1. Emerson, R.W. (1841). Essays: First Series. James Munroe and Company. Retrieved from Project Gutenberg: https://www.gutenberg.org/ebooks/16643

  2. Gross, R. A. (2021). The transcendentalists and their world. Farrar, Straus and Giroux.

  3. Thoreau, H.D. (1854). Walden, and On The Duty Of Civil Disobedience. Ticknor and Fields. Retrieved from Project Gutenberg: https://www.gutenberg.org/ebooks/205

  4. Richardson, R.D. (1995). Emerson: The Mind on Fire. University of California Press.

  5. Walls, L.D. (2017). Henry David Thoreau: A Life. University of Chicago Press.

  6. Buell, L. (2003). Emerson. Harvard University Press.

Machine Learning and NLP References

  1. Breiman, L. (2001). Random Forests. Machine Learning, 45, 5-32. https://doi.org/10.1023/A:1010933404324

  2. Cortes, C., & Vapnik, V.N. (1995). Support-Vector Networks. Machine Learning, 20, 273-297.https://doi.org/10.1007/BF00994018

  3. Cox, David R. (1958). "The regression analysis of binary sequences (with discussion)". J R Stat Soc B. 20 (2): 215–242. https://doi.org/10.1111%2Fj.2517-6161.1958.tb00292.x. JSTOR 2983890.

  4. Devlin, J., Chang, M.W., Lee, K., & Toutanova, K. (2019). BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding. Proceedings of NAACL-HLT 2019, 4171–4186. https://doi.org/10.18653/v1/N19-1423

  5. Hastie, T., Tibshirani, R., & Friedman, J. (2009). The Elements of Statistical Learning: Data Mining, Inference, and Prediction (2nd ed.). Springer.

  6. Honnibal, M., & Montani, I. (2017). spaCy 2: Natural language understanding with Bloom embeddings, convolutional neural networks and incremental parsing.

  7. Jayaswal, V. (2020, September 14). Performance metrics: Confusion matrix, precision, recall, and F1 score. Towards Data Science. https://towardsdatascience.com/performance-metrics-confusion-matrix-precision-recall-and-f1-score-a8fe076a2262/

  8. Pedregosa, F., Varoquaux, G., Gramfort, A., Michel, V., Thirion, B., Grisel, O., ... & Duchesnay, É. (2011). Scikit-learn: Machine Learning in Python. Journal of Machine Learning Research, 12, 2825-2830.

  9. Priya, B. C. (2023, July 10). A gentle introduction to support vector machines. KDnuggets. https://www.kdnuggets.com/2023/07/gentle-introduction-support-vector-machines.html

  10. Sanh, V., Debut, L., Chaumond, J., & Wolf, T. (2019). DistilBERT, a distilled version of BERT: smaller, faster, cheaper and lighter. ArXiv, abs/1910.01108. https://doi.org/10.48550/arXiv.1910.01108

  11. Sebastiani, F. (2002). Machine learning in automated text categorization. ACM Computing Surveys, 34(1), 1–47. https://doi.org/10.1145/505282.505283

  12. Spärck Jones, K. (1973). Index term weighting. Inf. Storage Retr., 9, 619-633.

  13. Vaswani, A., Shazeer, N.M., Parmar, N., Uszkoreit, J., Jones, L., Gomez, A.N., Kaiser, L., & Polosukhin, I. (2017). Attention is All you Need. Neural Information Processing Systems.

  14. Wolf, T., Debut, L., Sanh, V., Chaumond, J., Delangue, C., Moi, A., ... & Rush, A. M. (2020). Transformers: State-of-the-Art Natural Language Processing. Proceedings of the 2020 Conference on Empirical Methods in Natural Language Processing: System Demonstrations, 38–45.

Computational Stylometry and Literary Analysis

  1. Burrows, J. (2002). 'Delta': a Measure of Stylistic Difference and a Guide to Likely Authorship. Literary and Linguistic Computing, 17(3), 267–287. https://doi.org/10.1093/llc/17.3.267

  2. Jockers, M.L. (2013). Macroanalysis: Digital Methods and Literary History. University of Illinois Press.

  3. Stamatatos, E. (2009). A survey of modern authorship attribution methods. Journal of the American Society for Information Science and Technology, 60(3), 538-556. https://doi.org/10.1002/asi.21001

  4. Underwood, T. (2019). Distant Horizons: Digital Evidence and Literary Change. University of Chicago Press.

Python Libraries and Tools

  1. Pandas Development Team (2020). pandas-dev/pandas: Pandas. Zenodo. https://doi.org/10.5281/zenodo.3509134

  2. Harris, C.R., Millman, K.J., van der Walt, S.J. et al. (2020). Array programming with NumPy. Nature, 585, 357–362. https://doi.org/10.1038/s41586-020-2649-2

  3. Hunter, J. D. (2007). Matplotlib: A 2D Graphics Environment. Computing in Science & Engineering, 9(3), 90-95. https://doi.org/10.1109/MCSE.2007.55

  4. Pedregosa et al. (2011). Scikit-learn: Machine Learning in Python. Journal of Machine Learning Research, 12, 2825–2830.

  5. Waskom, M.L. (2021). seaborn: statistical data visualization. Journal of Open Source Software, 6(60), 3021. https://doi.org/10.21105/joss.03021

  6. Paszke, A., Gross, S., Massa, F., Lerer, A., Bradbury, J., Chanan, G., ... & Chintala, S. (2019). PyTorch: An Imperative Style, High-Performance Deep Learning Library. Advances in Neural Information Processing Systems, 32, 8026-8037.

Core NLP and Text Classification