NLTK. Detecting whether a sentence is Interrogative or Not?
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
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?
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
whatorhow - an auxiliary such as
do,is, orcan - 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.
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.
Related reading
- NLTK for Named Entity Recognition
- NotFittedError TfidfVectorizer - Vocabulary wasn't fitted
- OpenAI GPT-2 model use with TensorFlow JS
- optimizing byte-pair encoding
- No Module Named '_pywrap_tensorflow_internal
- No module named 'keras.wrappers
- Orange vs NLTK for Content Classification in Python
- Parsing one terabyte of text and efficiently counting the number of occurrences of each word
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.