Text Classification
Text classification assigns a category to a piece of text. Sentiment analysis is one type. Other types include spam detection, topic categorization, and language identification.
10 min•By Priygop Team•Updated 2026
Types of Text Classification
- Spam detection: classify emails as spam or not spam
- Sentiment analysis: positive, negative, or neutral
- Topic classification: technology, sports, politics, entertainment
- Language detection: is this English, French, Spanish, or Arabic?
- Intent detection: is the user asking a question, making a complaint, or placing an order?
- Content moderation: does this post violate community guidelines?
Text Classification Pipeline
Text Classification Pipeline
# Text classification pipeline illustration
def classify_topic(text):
"""Simple keyword-based topic classifier."""
text_lower = text.lower()
topics = {
"Technology": ["ai", "software", "computer", "algorithm", "data", "programming", "machine learning"],
"Sports": ["football", "basketball", "cricket", "match", "player", "team", "score", "tournament"],
"Finance": ["stock", "market", "investment", "price", "economic", "revenue", "profit", "bank"],
"Health": ["disease", "treatment", "doctor", "patient", "medicine", "hospital", "health", "study"],
}
scores = {}
for topic, keywords in topics.items():
scores[topic] = sum(1 for kw in keywords if kw in text_lower)
best_topic = max(scores, key=scores.get)
best_score = scores[best_topic]
if best_score == 0:
return "Uncategorized", 0
return best_topic, best_score
# Test on different texts
articles = [
"The new machine learning model from Google achieves record accuracy on image data",
"The cricket team won the tournament after a fantastic last-over match yesterday",
"Stock markets fell sharply after the economic report showed higher inflation than expected",
"Researchers published a study on the effectiveness of the new treatment for the disease",
]
print("Text Classification Results:")
print()
for article in articles:
topic, score = classify_topic(article)
print(f" '{article[:60]}...'")
print(f" Topic: {topic} (matched {score} keyword(s))")
print()Diagram
Loading diagram…
Modern NLP = Transformer-based. Pre-train, then fine-tune.
Key Takeaways
- Text classification assigns a category to a piece of text.
- Spam detection: classify emails as spam or not spam
- Sentiment analysis: positive, negative, or neutral
- Topic classification: technology, sports, politics, entertainment