keepcalculator

06 Numbers and Powers

Categorize Text with Keywords: A Practical Guide

Card No. 06·35620 · Updated · Standard formula, shown below

Yes, you can categorize text with keywords by defining a set of rules that map specific words or phrases to predefined categories, then applying those rules to new text. This approach, often called rule-based or keyword-based classification, is simple, transparent, and works well when your categories are clearly distinguished by vocabulary.

What Is Keyword-Based Text Categorization?

Keyword-based text categorization is a method of automatically assigning a label (category) to a piece of text by checking whether it contains certain keywords or phrases. For example, an email containing the word “refund” might be categorized as “Billing,” while one with “reset password” goes to “Account Support.” This is distinct from machine learning classification, which learns patterns from labeled examples rather than relying on hand-crafted rules.

The method has been around since the early days of text processing and remains popular because it is easy to implement, debug, and explain. It is especially useful when you have a small number of categories, a limited vocabulary, or when you need to comply with strict audit requirements (e.g., “why was this document labeled as sensitive?”).

How Keyword Categorization Works

The core process involves three steps: defining categories, building keyword lists, and applying a matching rule. Here’s a typical workflow:

  1. Define categories – List the labels you want to assign (e.g., “Complaint,” “Question,” “Praise”).
  2. Create keyword lists – For each category, write down words and phrases that are strong indicators. For “Complaint,” you might use “broken,” “not working,” “disappointed.”
  3. Apply a matching rule – Decide how to score a text. The simplest rule is: if the text contains any keyword from a category’s list, assign that category. More sophisticated rules use counts, weights, or priorities.

For example, with a simple “first match wins” rule, the text “My order is broken and I want a refund” would match “Complaint” (if “broken” is on that list) before checking other categories.

Choosing Keywords: What Makes a Good Keyword?

Not all words are equally useful for categorization. A good keyword should be:

  • Specific – “refund” is better than “money” because “money” appears in many contexts.
  • Discriminative – It should appear often in the target category and rarely in others. “password reset” is discriminative for account support, while “help” is not.
  • Not ambiguous – Avoid words like “cool” (temperature vs. approval) unless you add context (e.g., “cooling system”).

Start by brainstorming, but also look at real examples from your data. A common mistake is using words that are too generic, like “problem” or “issue,” which appear in almost every category. Instead, use phrases: “problem with my order” is more specific than “problem.”

Common Problems and How to Avoid Them

Keyword categorization has several well-known pitfalls:

  • False positives – A text matches a keyword but is actually about something else. For example, “The software is not broken, it’s working as designed” contains “broken” but is not a complaint. Solution: use negative keywords (e.g., “not broken”) or require multiple positive keywords.
  • False negatives – A text should be categorized but uses synonyms not in your list. “My laptop died” might not match “battery issue” unless you include “died.” Solution: expand your list with common synonyms and misspellings.
  • Overlapping categories – When two categories share many keywords, the rule becomes ambiguous. For instance, “I want to return a broken item” could be both “Returns” and “Complaint.” Solution: assign priority or use a weighted score.

These issues are why many systems move to machine learning when the volume is high and categories are fuzzy. But for small, well-defined problems, keyword rules are often sufficient and much easier to maintain.

Comparing Keyword Rules vs. Machine Learning

It’s worth understanding the trade-offs before choosing a method. Here’s a quick comparison:

  • Transparency – Keyword rules are fully explainable; you can show exactly why a text was categorized. Machine learning models (especially deep learning) are often black boxes.
  • Maintenance – Keyword lists are easy to update when you discover a new synonym. Machine learning requires retraining and validation.
  • Accuracy on complex language – Machine learning handles sarcasm, context, and rephrasing far better. Keyword rules struggle with anything beyond direct vocabulary.
  • Data requirements – Machine learning needs a large labeled dataset (hundreds or thousands of examples per category). Keyword rules need only your domain knowledge.

In practice, many systems use a hybrid: keyword rules for initial filtering, then machine learning for the remaining ambiguous cases.

Tools and Implementation Options

You don’t need a heavy platform to start. Here are common ways to implement keyword categorization:

  • Spreadsheet formulas – In Excel or Google Sheets, you can use IF(COUNTIF(A1,"*refund*"),"Billing","Other") for simple cases.
  • Programming languages – Python’s re module or even simple if “keyword” in text.lower() works for prototypes. For more robust systems, libraries like spaCy or scikit-learn offer tokenization and matching utilities.
  • No-code automation tools – Many workflow platforms (like Zapier or Make) have built-in “if text contains” conditions that let you route emails or form submissions to different folders or teams.
  • Dedicated classification services – Some cloud providers offer text classification APIs that let you provide your own keyword lists as rules, but these are often overkill for small projects.

For a one-off task, a simple script is usually enough. For ongoing use, consider building a small web service or a spreadsheet template that your team can maintain without coding.

When Not to Use Keyword Categorization

Keyword rules are not a universal solution. Avoid them when:

  • Your categories are abstract – For example, “positive sentiment” cannot be reliably detected by a few words because sentiment depends heavily on context and tone.
  • Your text is highly variable – Social media posts, for instance, use slang, hashtags, and emojis that make keyword lists quickly outdated.
  • You need high accuracy on a large scale – If you’re processing millions of documents, even a 5% error rate means a lot of mistakes. Machine learning can often reduce that error rate if you have enough data.

In those cases, consider a supervised machine learning approach, or at least combine keywords with other features like text length, metadata, or user ratings.

Frequently Asked Questions

How many keywords do I need per category?

There’s no fixed number, but a good starting point is 10–20 keywords per category for a small project. You’ll refine based on testing. If you find many false negatives, add more synonyms; if many false positives, remove or add negative keywords.

Can keyword categorization handle multiple categories per text?

Yes. Instead of assigning a single label, you can allow multiple matches. For example, a support ticket might be both “Billing” and “Technical.” Use a rule that returns all categories that meet a threshold (e.g., at least one keyword match).

What is the difference between keyword matching and regex?

Regex (regular expressions) is a more powerful pattern-matching language. While keyword matching looks for exact substrings, regex allows patterns like “order\s+status” (with any whitespace) or “(?:refund|return)”. Regex is more flexible but harder to write and debug.

How do I evaluate the accuracy of my keyword rules?

Take a sample of texts (e.g., 100 per category) and manually label them. Run your rules and compare the results. Calculate precision (how many of the assigned labels are correct) and recall (how many of the true labels were found). Adjust your keywords based on errors.

Do I need to preprocess the text (lowercase, remove punctuation)?

Yes, it’s essential. Convert to lowercase so “Refund” and “refund” match the same keyword. Remove punctuation or replace it with spaces so “refund,” and “refund.” don’t cause mismatches. Also consider stemming (e.g., “refunded” → “refund”) to catch variations.

Can I use keyword categorization for languages other than English?

Yes, the same principles apply. The main challenge is that some languages have more complex word forms (e.g., German compounds, Arabic prefixes). You may need to use stemming or lemmatization specific to that language, or simply include more inflected forms in your keyword list.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →