NLTK
natural language processing
sentence classification
interrogative sentences
question detection

NLTK. Detecting whether a sentence is Interrogative or Not?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Detecting whether a sentence is interrogative sounds simple until you leave perfectly punctuated textbook English. In practice, a good solution combines obvious signals such as a trailing question mark with linguistic clues such as question words, auxiliary inversion, and part-of-speech patterns.

Start with a Rule-Based Baseline

If you are using NLTK, the fastest useful baseline is a rules-based detector. That works well for direct questions such as Where are you going? and Did she call?

python
1import nltk
2from nltk import word_tokenize, pos_tag
3
4QUESTION_WORDS = {"who", "what", "when", "where", "why", "how", "which", "whom", "whose"}
5AUXILIARIES = {
6    "is", "are", "am", "was", "were",
7    "do", "does", "did",
8    "can", "could", "will", "would",
9    "shall", "should", "has", "have", "had",
10    "may", "might", "must"
11}
12
13def is_interrogative(sentence: str) -> bool:
14    text = sentence.strip()
15    if not text:
16        return False
17
18    if text.endswith("?"):
19        return True
20
21    tokens = word_tokenize(text.lower())
22    if not tokens:
23        return False
24
25    first = tokens[0]
26    if first in QUESTION_WORDS or first in AUXILIARIES:
27        return True
28
29    tagged = pos_tag(tokens)
30    if len(tagged) >= 2 and tagged[0][1] in {"VBZ", "VBP", "VBD", "MD"}:
31        return True
32
33    return False
34
35
36examples = [
37    "Where are the keys",
38    "Did you finish the report",
39    "I wonder where the keys are",
40    "Please close the door",
41]
42
43for sentence in examples:
44    print(sentence, "->", is_interrogative(sentence))

This catches many direct questions even when the writer forgot the final ?.

Why Tokenization and POS Tagging Help

A plain punctuation check misses questions such as Can you help me or Why is this failing. NLTK helps because it can tokenize the sentence and assign part-of-speech tags, which makes it easier to notice question-like structure.

For English, direct questions often start with:

  • a wh-word such as what or how
  • an auxiliary such as do, is, or can
  • verb-first or modal-first word order

NLTK does not magically "know" a sentence is a question, but it provides the building blocks for heuristics that are much better than checking only the final character.

Direct Questions Versus Indirect Questions

One subtle issue is that not every sentence containing question words is interrogative.

I wonder where the keys are. contains where, but it is a declarative sentence. The speaker is making a statement, not asking the reader a question.

That is why pure keyword matching is not enough. A sentence classifier must distinguish:

  • direct questions: Where are the keys?
  • indirect statements about questions: I wonder where the keys are.

Rule-based detection can get you part of the way, but if you need higher accuracy across messy text, a trained classifier is more robust.

Build a Small NLTK Classifier

For a supervised approach, extract simple features and train a classifier. Even a lightweight Naive Bayes model can outperform rigid heuristics when you have labeled examples.

python
1import nltk
2from nltk.classify import NaiveBayesClassifier
3
4def question_features(sentence: str) -> dict[str, object]:
5    tokens = nltk.word_tokenize(sentence.lower())
6    return {
7        "ends_with_qmark": sentence.strip().endswith("?"),
8        "first_word": tokens[0] if tokens else "",
9        "contains_wh_word": any(t in QUESTION_WORDS for t in tokens),
10        "length": len(tokens),
11    }
12
13
14training_data = [
15    ("Where are you going?", "question"),
16    ("Can you help me", "question"),
17    ("I am going home.", "statement"),
18    ("She asked whether you called.", "statement"),
19]
20
21train_set = [(question_features(text), label) for text, label in training_data]
22classifier = NaiveBayesClassifier.train(train_set)
23
24test_sentence = "Why did this break"
25print(classifier.classify(question_features(test_sentence)))

This is still a small model, but it shows the path from hand-written heuristics to data-driven classification.

Practical Limits of NLTK for This Task

NLTK is useful for prototyping, but it will not solve every edge case by itself. Informal chat, sarcasm, missing punctuation, and domain-specific language can all confuse a simple detector.

If the stakes are high, treat the problem as sentence classification rather than a punctuation trick. Create labeled examples from your domain and evaluate precision and recall, especially if false positives are expensive.

Common Pitfalls

The most common mistake is checking only whether the sentence ends with ?. That misses many real questions and accepts some malformed text without understanding structure.

Another mistake is assuming that any sentence starting with a wh-word is interrogative. Indirect questions and quoted text break that assumption quickly.

Developers also sometimes forget that NLTK tokenizers and taggers may need the appropriate language models installed before running. If those resources are missing, the code fails before classification even starts.

Finally, avoid treating this as a perfect binary problem without evaluation. Natural language is noisy, and even a good detector should be measured on realistic examples.

Summary

  • A basic interrogative detector can be built with NLTK tokenization, POS tagging, and simple heuristics.
  • Trailing punctuation helps, but it is not enough on its own.
  • Question words and auxiliary-first structure are strong signals for direct questions.
  • Indirect questions require more than simple keyword checks.
  • For better accuracy, train a lightweight classifier on labeled examples from your domain.

Course illustration
Course illustration

All Rights Reserved.