Text classification

Text classification is the task of automatically assigning a piece of text to one or more predefined categories, such as spam or not spam, a topic, a language or a sentiment.

Text classification sorts text into categories decided in advance. Google's guide to the subject describes it as using algorithms to categorise text, the technology behind email spam filters and the moderation of comments on forums. It names two broad kinds: topic classification, which assigns a document to one of a set of subjects, and sentiment analysis, which identifies the opinion a text expresses.

Most text classifiers are built with supervised learning. People label a set of examples, such as thousands of emails marked spam or not spam. The text is turned into numbers the model can work with, from simple word counts to embeddings that capture meaning, and a model learns which features go with which category. Methods range from naive Bayes and support vector machines to decision trees, nearest-neighbour methods and neural networks. Google's guide sets out the workflow in seven steps: gather data, explore it, choose a model, prepare the data, build, train and evaluate the model, tune its settings, and deploy it.

Applications are everywhere: spam filtering, routing emails to the right department, identifying the language of a text, sorting documents by genre, assessing readability and triaging scientific articles. Content moderation on large platforms depends on classifiers that flag posts for removal or human review. Google's glossary makes a useful point here: many models that evaluate text, including text classification and sentiment models, are not language models, because they label text rather than predict the next word.

Large language models can now classify text without any task-specific training, simply by being told the categories in a prompt. That makes it quick to try out a classifier. Dedicated small models are still widely used for high-volume work because they are cheaper, faster and give the same answer every time for the same input.

Every classifier makes two kinds of mistake: it misses things it should have caught and flags things it should have left alone. Tightening it to catch more spam also catches more real email. Good evaluation checks performance on each category separately and on text unlike the training examples, such as new slang or a different language, where accuracy often drops.

Common questions

What is text classification?

Text classification is the automatic sorting of text into predefined categories. Common examples are deciding whether an email is spam, which department a support ticket should go to, what topic an article covers or whether a review is positive or negative.

What are examples of text classification?

Spam filtering, sentiment analysis of reviews, language identification, topic tagging of news articles, routing customer emails and flagging harmful posts for content moderation are all text classification tasks.

Can large language models do text classification?

Yes. A large language model can classify text when given the list of categories and some instructions, with no special training. For large volumes, many organisations still use smaller dedicated classifiers, which are cheaper, faster and more consistent.

What is the difference between text classification and sentiment analysis?

Sentiment analysis is one type of text classification, in which the categories are opinions such as positive, negative or neutral. Text classification covers any set of categories, including topics, languages, intent or whether a message is spam.

Finding more

Search for "text classification", "document classification" or "zero-shot classification" to learn methods and tools. Reliable sources include machine learning guides from major providers, documentation for open-source libraries and research papers in the ACL Anthology. Treat headline accuracy figures with care unless they show results for each category and on data the model had not seen.

Working out what this means for your business?

Aileron Shift builds the systems behind this site: AI newsrooms that research, write and publish on their own, marketing automation and WordPress hosting for companies that need to move faster than their competitors.