Natural Language Processing and Text Classification: From Bag-of-Words to BERT

Photo: Bibek ghosh / Pexels
Every email you don't see in your inbox because spam filters caught it, every toxic comment automatically removed from social media before you encounter it, every customer support ticket automatically routed to the right team—these are NLP text classification systems operating at scale. Gmail's spam filter, trained on user feedback from over a billion accounts, achieves 99.9% accuracy while letting fewer than 0.1% of spam through. Twitter's content moderation classifiers process 500 million tweets per day, making millisecond decisions about whether content violates platform policies. Text classification may be the single most deployed ML application globally, operating invisibly across nearly every digital system that handles human language.
Key takeaways
- Text classification assigns predefined categories to text documents—from binary spam/not-spam to fine-grained 100+ category product taxonomy classification.
- The field progressed from feature engineering (TF-IDF, n-grams) to neural word embeddings (Word2Vec, GloVe) to contextual embeddings (BERT, RoBERTa) over roughly two decades.
- BERT-style fine-tuning—taking a pretrained language model and training a classification head on task-specific labeled data—is the current industry standard, achieving human-level performance on many benchmarks with 1,000–10,000 labeled examples.
- Few-shot and zero-shot classification using LLMs is reducing labeled data requirements to the point where NLP practitioners can deploy useful classifiers with dozens rather than thousands of examples.
The Evolution of Text Representation
The fundamental challenge in text classification is representation: how do you convert variable-length text (a tweet, an email, a medical note) into a fixed-dimensional numerical vector that a classifier can operate on?
Bag of words and TF-IDF: The earliest approach ignores word order and treats a document as a bag of word counts. TF-IDF (Term Frequency–Inverse Document Frequency) weights each word by how frequently it appears in the document (TF) divided by how commonly it appears across all documents (IDF)—rare words that appear often in one document get high weight, common words like "the" get low weight. A Naive Bayes classifier trained on TF-IDF features is still the first thing a practitioner should try: it's fast, interpretable, and surprisingly competitive with more complex methods when labeled data is plentiful.
Word embeddings: Word2Vec (Google, 2013) and GloVe (Stanford, 2014) learned dense vector representations for words from unlabeled text. The insight: words that appear in similar contexts have similar meanings and should have similar representations. Word2Vec's skip-gram model trains a neural network to predict context words from a target word; the learned hidden layer weights become the word embeddings. The resulting embeddings capture semantic relationships: king - man + woman ≈ queen in the embedding space. Document representations are computed by averaging or pooling word embeddings—a step up from bag-of-words but still losing word order information.
Contextual embeddings: Word2Vec gives every token a fixed representation regardless of context. "Bank" has the same vector whether in "river bank" or "savings bank." BERT (Bidirectional Encoder Representations from Transformers, Google, 2018) introduced contextual embeddings: the representation of each word depends on all surrounding words, computed via bidirectional transformer attention. BERT changed what the field considered possible for text classification.
BERT Fine-Tuning: The Current Standard
BERT is pretrained on massive unlabeled text corpora (English Wikipedia + BookCorpus, 3.3 billion words) using two self-supervised objectives: Masked Language Modeling (predict randomly masked tokens) and Next Sentence Prediction (predict whether two sentences are consecutive). This pretraining teaches BERT rich syntactic and semantic representations of English without any task-specific labels.
Fine-tuning for text classification takes the pretrained BERT model, adds a simple classification head (a linear layer) on top of BERT's [CLS] token representation, and trains on task-specific labeled examples. The entire model updates during fine-tuning, allowing BERT's representations to specialize for the task.
The results were dramatic: BERT fine-tuned on Stanford Sentiment Treebank (SST-2) achieved 93.5% accuracy on sentiment classification—compared to 91.3% for the previous state-of-the-art. On GLUE (General Language Understanding Evaluation), a benchmark of diverse NLP tasks including classification, BERT improved on every task and remains competitive with methods developed years later.
Why does BERT fine-tuning work so well with limited labeled data? Because BERT's pretraining provides general language understanding that fine-tuning adapts to the specific task. A practitioner needs perhaps 1,000 labeled examples to fine-tune BERT to 90% accuracy on a new classification task; a logistic regression on TF-IDF features might need 10,000+ examples to reach the same performance.
Google, Amazon, and Microsoft all use BERT-family models as the backbone for production text classifiers in their search, advertising, and customer service systems.
Real-World Applications at Scale
Spam and phishing detection: Google's Gmail spam filter uses a combination of rule-based filters, user feedback signals, and neural classifiers to achieve 99.9% spam detection accuracy. Modern spam detection models process full email content, attachment metadata, sending patterns, and URL characteristics. The arms race is ongoing: spam operators use adversarial techniques (inserting nonsense text to confuse classifiers, mimicking legitimate email patterns) that require continuous model updates.
Content moderation: Facebook/Meta processes billions of posts, comments, and images daily for policy violations—hate speech, misinformation, self-harm content. Their AI content moderation system uses transformer-based classifiers trained on human-labeled examples of violating content, with separate models for different violation types and different languages. Meta's 2024 Transparency Report noted that AI automated detection of violating content before user reports in 97.8% of cases for their highest-priority violation categories.
Clinical NLP: Text classification applied to medical records—identifying diagnostic codes from clinical notes, classifying patient condition severity from nursing documentation, detecting adverse drug event descriptions in electronic health records—is transforming healthcare informatics. The NHS in the UK uses BERT-based classifiers to extract structured information from unstructured clinical notes for epidemiological research. A 2023 study in NEJM AI showed that a fine-tuned clinical BERT model classified acute coronary syndrome severity from emergency department notes with 89% AUC, comparable to cardiologist assessment but requiring seconds rather than minutes.
Financial services: JPMorgan Chase's COIN (Contract Intelligence) platform uses text classification to extract key provisions from commercial loan agreements—a task that previously required 360,000 hours of lawyer time annually. COIN processes the same workload in seconds. Morgan Stanley, Goldman Sachs, and Bloomberg all use NLP classifiers for sentiment analysis of financial news and earnings call transcripts to inform trading signals.
Few-Shot and Zero-Shot Classification
The emerging frontier in text classification reduces labeled data requirements dramatically using large language models. Zero-shot classification presents a text and a list of candidate categories to an LLM in natural language, asking it to classify without any task-specific training:
"Classify the following review as positive, negative, or neutral: 'The battery life is terrible but the camera is exceptional.'"
GPT-4 and similar models handle many standard classification tasks in zero-shot with accuracy approaching fine-tuned BERT. For tasks where the category descriptions can be clearly expressed in natural language—sentiment, topic classification, relevance scoring—zero-shot performance is often 80–85% of fine-tuned baseline accuracy, achieved without any labeled examples.
Few-shot classification provides a small number of examples (3–10 per class) in the prompt. OpenAI's API pricing, combined with few-shot classification performance, now enables rapid prototyping of classifiers for new tasks in hours rather than weeks—the time previously required for data collection and fine-tuning.
The tradeoff: LLM-based zero/few-shot classification is slower, more expensive, and less controllable than fine-tuned models at high volume. Production systems at scale still use fine-tuned models for efficiency; LLMs are used for rapid prototyping, evaluation, and tasks where volume is low but diversity is high.
Evaluation Metrics and Common Pitfalls
Text classification evaluation requires understanding precision, recall, and F1 score—accuracy alone is misleading for imbalanced classes. If 99% of emails are legitimate, a classifier that always predicts "not spam" achieves 99% accuracy while being useless.
Precision: of predicted positive cases, what fraction are actually positive? High precision = few false alarms. Recall: of actual positive cases, what fraction did the model identify? High recall = few missed positives. F1 score: harmonic mean of precision and recall; summarizes the precision-recall tradeoff in a single number.
Common pitfalls in production NLP classification:
Distribution shift: a model trained on 2022 data classifies 2025 content poorly because language evolves—new slang, new product names, new geopolitical context. Models must be retrained or continuously updated.
Label ambiguity: classification tasks with subjective categories (toxicity, sentiment, relevance) have inherent inter-annotator disagreement. Models learn the average of annotator judgments, which may not match any individual user's expectations.
Demographic bias: if training data over-represents certain demographic groups, the model may perform worse on text from underrepresented groups—a significant concern for content moderation systems applied globally.
FAQ
Q: When should I use BERT fine-tuning versus zero-shot classification with GPT-4? A: Zero-shot with GPT-4 for: rapid prototyping, low-volume tasks (<10,000 classifications/day), and tasks where category descriptions can be clearly expressed in natural language. Fine-tuned BERT for: high-volume production (cost and latency), tasks with domain-specific vocabulary (medical, legal, financial), and tasks where you have 1,000+ labeled examples and need predictable, consistent behavior.
Q: How much labeled training data does BERT fine-tuning require? A: BERT fine-tuning with 100–500 examples per class typically achieves strong results for well-defined classification tasks, often 80%+ accuracy. With 1,000–10,000 examples per class, fine-tuned BERT approaches human-level performance on most standard tasks. For very fine-grained classifications (hundreds of categories) or highly ambiguous tasks, more data is needed.
Q: What is the difference between multi-class and multi-label text classification? A: Multi-class classification assigns one category from a mutually exclusive set (sentiment: positive OR negative OR neutral). Multi-label classification assigns one or more categories from a non-exclusive set (a news article can be tagged as both "finance" AND "technology"). Multi-label problems require sigmoid output activations (independent probability per label) rather than softmax, and different evaluation metrics (per-label precision/recall or Hamming loss).
Q: How do you handle text classification in languages other than English? A: Multilingual BERT (mBERT, Google, 2019) and XLM-RoBERTa (Meta AI, 2020) are pretrained on 100+ languages simultaneously. Fine-tuning on English examples only enables cross-lingual transfer—the model classifies non-English text with reasonable accuracy without any non-English training examples. For languages with significant training data, language-specific models (CamemBERT for French, BERTje for Dutch) outperform multilingual models.
Q: What tools are available for quick deployment of text classifiers?
A: Hugging Face's Transformers library is the standard for fine-tuning BERT-family models, with thousands of pretrained models available. SimpleTransformers provides a higher-level API for fine-tuning without deep framework knowledge. For zero-shot classification, the Hugging Face zero-shot-classification pipeline uses BART-based NLI models. For production deployment, ONNX Runtime and TorchScript provide optimized inference, while services like AWS Comprehend and Google Natural Language API provide managed classification without model management overhead.
Read next:
Follow new articles
Add SciHub101 to your feed reader to see new science and technology stories.
About the Author
SciHub101 is an independent science and technology publication founded and edited by Buğra Sözeri, a software engineer and app developer based in Türkiye. AI tools assist with drafting and research. Source documentation in the existing archive is uneven, and articles are being reviewed and corrected. The Editorial Team byline identifies editorial responsibility; it does not certify specialist or independent human review. Where we compare products, anything built by our own team is labelled as such — see the editorial guidelines and About page for how corrections and disclosures work.
Related Articles

Reinforcement Learning for Robotics: Teaching Machines to Move Through the World
Robots now learn walking, balancing, and some manipulation by trial and error, mostly in simulation. Here is how the training works, what has reached real hardware, and what still breaks.

AI Software Testing: How Machine Learning Finds Bugs Early
Machine learning now helps pick which tests to run, steer fuzzers toward crashes, draft unit tests, and flag anomalies in production. Here is what published results show, and where the tools still fail.

Neural Networks in Network Routing: How AI Optimizes 5G and Beyond
AI-driven network routing is replacing static algorithms in 5G—reducing latency, balancing load, and adapting to traffic in real time.

Machine Learning in Cybersecurity: How AI Detects Intrusions Before Humans Can
ML-powered intrusion detection systems now identify novel attacks in milliseconds—analyzing network patterns no human analyst could process.

How AI Designs Chips: Hardware-Aware Neural Architecture Search
AI now helps design the chips that run AI. Reinforcement-learning floorplanning, neural architecture search, and co-design are changing semiconductor R&D, and parts of the evidence are disputed.

Edge Computing and Federated Learning: Private AI Training
Federated learning lets phones, hospitals, and banks improve a shared AI model without sending raw data to a central server. Here is how the loop works, what it protects, and where it still leaks.