Sub topic 15
Building a simple rule-based intent classifier
This chapter assumes you know the four intent types, can read basic Python, and have seen how query wording signals intent. You will build a small classifier, run it, and learn where it fails.
What a rule-based classifier does
A rule-based classifier labels a keyword by checking it against fixed word lists. It has no model and no training data. You write the rules, so you can always explain why a keyword got its label.
That makes it useful for a first pass over a large keyword list. It is not a replacement for checking the search results page, which is the stronger evidence.
The rules
The classifier uses three modifier lists and one brand list:
- Commercial words, such as "best", "review", "vs", and "alternatives".
- Transactional words, such as "buy", "coupon", "download", and "near me".
- Informational words, such as "what", "how", "guide", and "tutorial".
- Brand names, which signal navigational intent.
Matching uses a word boundary (\b) so that "top" does not match inside "laptop". The order of the checks matters, and the next sections explain why.
import re
COMMERCIAL = ["best", "top", "review", "reviews", "vs", "compare", "comparison", "alternative", "alternatives"]
TRANSACTIONAL = ["buy", "order", "coupon", "discount", "download", "sign up", "free trial", "near me"]
INFORMATIONAL = ["what", "why", "how", "guide", "tutorial", "examples", "meaning", "definition"]
def has_term(query, terms):
return any(re.search(r"\b" + re.escape(t) + r"\b", query) for t in terms)
def classify(query, brands):
q = query.lower().strip()
if has_term(q, COMMERCIAL):
return "commercial"
if has_term(q, TRANSACTIONAL):
return "transactional"
if has_term(q, INFORMATIONAL):
return "informational"
if has_term(q, brands):
return "navigational"
return "unclear"
if __name__ == "__main__":
brands = ["slack", "github"]
for q in [
"what is search intent",
"slack login",
"best project management software",
"buy standing desk",
"standing desk",
]:
print(q, "->", classify(q, brands))Running it prints informational, navigational, commercial, transactional, and unclear for the five sample queries, in that order.
Why the order matters
A query can match several lists. The first match wins, so the order is a decision about which signal you trust most.
The code checks modifiers before brands. A query like "github vs gitlab" contains a brand, but the word "vs" shows that the searcher is comparing options. Checking brands first would label it navigational, which is wrong.
Checking brands last has a cost. "slack tutorial" is labeled informational, which is probably right. A bare brand name still reaches the brand check and is labeled navigational.
Limits and common errors
The classifier reads words, not meaning. Expect these failures:
- Conflicting signals. "how to buy a standing desk" matches "buy" first and is labeled transactional, though the searcher may want a guide.
- No signal. "standing desk" returns
unclear. That label is correct, because the real intent is mixed. Send these keywords to a manual SERP check. - Incomplete lists. A brand missing from the list is never labeled navigational.
- Substring traps. Without
\b, "top" would match "laptop". The boundary check prevents this.
Extending it
Improve it by adding words from your own niche and by logging every unclear result. Review those results each week and add the rules they suggest. If you reorder the checks, rerun a fixed set of test queries so you notice any label that changes.
Exercise
What label does classify("github vs gitlab", ["github", "gitlab"]) return, and why?
Show answer
It returns "commercial". The word "vs" matches the commercial list, which is checked before the brand list, so the brand names are never reached.