Text classification, also called text categorization or document classification, is the task of assigning one or more predefined labels to a piece of text. The text might be an email, a news article, a customer message or a single sentence, and the labels might be topics, languages, intentions, levels of urgency or any other categories that matter to the application. Sebastiani’s survey defined the task as labeling natural language texts with categories from a predefined set [1]. It is one of the most common tasks in natural language processing and a building block for many other tools.
Overview
A text classifier takes raw text as input and returns a label, or a set of labels with scores. When each text receives exactly one label, the task is single-label or multiclass classification. When a text can belong to several categories at once, as with a news article tagged “politics” and “economy,” it is multi-label classification. The simplest case, with two possible labels such as spam and not spam, is binary classification.
Classification differs from clustering, where no labels are defined in advance and the algorithm finds groups on its own. Because the categories are fixed beforehand, text classification is a form of supervised learning, although unsupervised and semi-supervised variants exist. It is a special case of classification in machine learning, applied to language.
Common tasks
- Topic classification. Assigning subject labels such as sports, finance or health.
- Spam detection. Separating unwanted messages from legitimate ones.
- Intent detection. Identifying what a user wants, as part of natural language understanding.
- Sentiment classification. Labeling opinions as positive, negative or neutral; see sentiment analysis.
- Language identification. Determining which language a text is written in; see language detection.
- Content moderation. Flagging abusive, unsafe or policy-violating text.
- Routing and triage. Sending support tickets or documents to the right team or priority level.
How text classification works
- Define the labels. Decide on categories that are clear, mutually distinct where needed, and useful for the task.
- Collect labeled examples. Gather text that people have tagged with the right label. The quality and consistency of these labels strongly shape the result.
- Represent the text. Convert text to numbers using one of several methods, described below.
- Train a model. Fit a classifier to the labeled data.
- Evaluate. Test the model on examples it has not seen.
- Deploy and monitor. Apply it to new text and track performance, since language and topics change over time.
Text representation
Computers need numeric input, so text must be converted.
- Bag of words. Each text is represented by counts of the words it contains, ignoring order.
- TF-IDF. Counts are weighted so that words frequent in one text but rare overall count for more. This remains a strong baseline.
- Word embeddings. Words are mapped to dense vectors that capture similarity of meaning. See embeddings.
- Contextual embeddings. Transformer models produce a vector for each word that depends on its sentence, and a vector for the whole text.
Preprocessing steps such as tokenization, lowercasing and removing common words are used with the simpler methods, and are less needed with modern neural models.
Classification methods
### Classical algorithms
Naive Bayes is a simple probabilistic method that was used for spam filtering from the late 1990s [3]. Logistic regression and support vector machines are also strong choices when paired with TF-IDF features. Decision trees and random forests can be used as well. These methods are fast, cheap and often competitive on smaller data sets.
### Neural networks
Convolutional and recurrent networks learned features directly from word embeddings. Today, transformer models such as BERT are commonly fine-tuned on labeled examples and give high accuracy across many tasks.
### Language models and prompting
Large language models can classify text through instructions, with no training for the task. With few-shot prompting, a handful of labeled examples in the prompt can be enough. This lowers the cost of starting but may be slower and costlier per item than a small dedicated model. Developers often combine the approaches, using a language model to label data and then training a compact classifier on it, a technique related to model distillation.
Evaluation
Common measures include accuracy, precision, recall and the F1 score, which combines the last two. Precision is the share of predicted positives that are correct, and recall is the share of actual positives that were found. When one class is rare, accuracy can mislead: a model that always predicts the majority class may score high accuracy while being useless. Practitioners therefore look at a confusion matrix, per-class scores and, for multi-label tasks, averages across labels. Human agreement on the labels sets a practical ceiling.
Challenges
- Ambiguous categories. Labels that overlap or are subjective produce inconsistent training data.
- Class imbalance. Rare categories are hard to learn and need special handling.
- Domain and time shift. A model trained on old or different text may degrade, so monitoring and retraining are needed.
- Bias. Classifiers can reproduce prejudices in data, for example flagging dialects or identity terms as toxic more often than other text.
- Adversarial behavior. Spammers adapt their text to avoid filters.
- Explainability. It may be hard to tell why a neural model chose a label, which matters in regulated settings.
Hierarchical and extreme classification
Some applications have hundreds or thousands of categories organized in a tree, such as a product catalog or a subject index for a library. Hierarchical classification takes advantage of this structure, first choosing a broad category and then a narrower one, which reduces errors and lets a system stop at a higher level when it is uncertain. Extreme multi-label classification deals with very large label sets, for example assigning tags to scientific papers, and needs specialized methods that scale efficiently.
Building a reliable classifier
A few habits make a large difference in practice. Writing short annotation guidelines with examples helps labelers agree, and measuring their agreement reveals unclear categories before any model is trained. Splitting data so that near-duplicate texts do not appear in both training and test sets prevents inflated scores. A simple baseline, such as TF-IDF with logistic regression, shows how much a more complex model really adds. After deployment, teams review a sample of predictions regularly, add the mistakes to the training set and keep a confidence threshold that sends uncertain items to a person, as in human-in-the-loop AI.
FAQs
What is text classification? It is the task of assigning predefined labels, such as topic, sentiment or spam, to text using machine learning or rules.
What is the difference between classification and clustering? Classification uses labels defined in advance and learns from labeled examples. Clustering groups texts by similarity without predefined labels.
What algorithms are used for text classification? Common choices are Naive Bayes, logistic regression, support vector machines and fine-tuned transformer models, along with prompt-based use of large language models.
How much training data is needed? It depends on the task and method. Simple classifiers may work with hundreds of examples per class, while fine-tuned neural models often benefit from more, and prompting can start with very few.
Where is text classification used? It is used for spam filtering, topic tagging, support ticket routing, content moderation and sentiment analysis.
More in Natural Language Processing
Key facts
- Fabrizio Sebastiani's 2002 survey described text categorization as the labeling of natural language texts with thematic categories from a predefined set1.
- The 20 Newsgroups and Reuters-21578 collections have long served as standard benchmarks for text classification research2.
- Naive Bayes classifiers were applied to email spam filtering in the late 1990s3.
Related concepts
Quick checkWhat is text classification?Show answer
Text classification is a natural language processing task in which a model assigns one or more predefined labels, such as a topic, a language or a spam flag, to a piece of text.
Sources
- Fabrizio Sebastiani, Machine Learning in Automated Text Categorization, ACM Computing Surveys, https://doi.org/10.1145/505282.505283
- Christopher Manning, Prabhakar Raghavan and Hinrich Schutze, Introduction to Information Retrieval, Chapter 13, Stanford University, https://nlp.stanford.edu/IR-book/
- Mehran Sahami et al., A Bayesian Approach to Filtering Junk E-Mail, AAAI Workshop on Learning for Text Categorization, https://www.microsoft.com/en-us/research/publication/a-bayesian-approach-to-filtering-junk-e-mail/
Editorially reviewed by Specialty Digest Editorial TeamLast reviewed November 17, 2026Researched and drafted with AI assistanceReport an issue
