How can I detect if a search query is in the form of a question?
For example, a customer might search for "how do I track my order" (notice no question mark).
I'm guessing most direct questions would conform to a particular grammar.
Very simple guessing approach:
START WORDS = [who, what, when, where, why, how, is, can, does, do] isQuestion(sentence): sentence ends with '?' OR sentence starts with one of START WORDS
START WORDS list could be longer. The scope is a website search box, so I imagine the list shouldn't need to include too many words.
Is there a library that can do this better than my simple guessing approach? Any improvements on my approach?
The main purpose of a question mark, perhaps unsurprisingly, is to indicate that a sentence is a question. Direct questions often (but not always) begin with a wh- word (who, what, when, where, why). Why did the chicken cross the road?
A statement is a sentence that tells about something. It always ends with a period. I have an older brother and a younger sister. A question is a sentence that asks something.
6. Many polite requests or instructions are made in the form of a question. But because they are not really questions, they do not take a question mark: Could you please send me your catalogue.
Questions like these, which do not require or expect an answer, are called rhetorical questions. Because they are questions in form only, rhetorical questions may be written without question marks.
See also: How to find out if a sentence is a question (interrogative)?
In a syntactic parse of a question (obtained through a toolkit like nltk), the correct structure will be in the form of:
(SBARQ (WH+ (W+) ...) (SQ ...* (V+) ...*) (?))
So, using anyone of the syntactic parsers available, a tree with an SBARQ node having an embedded SQ (optionally) will be an indicator the input is a question. The WH+ node (WHNP/WHADVP/WHADJP) contains the question stem (who/what/when/where/why/how) and the SQ holds the inverted phrase.
i.e.:
(SBARQ (WHNP (WP What)) (SQ (VBZ is) (NP (DT the) (NN question))) (. ?))
Of course, having a lot of preceeding clauses will cause errors in the parse (that can be worked around), as will really poorly-written questions. For example, the title of this post "How to find out if a sentence is a question?" will have an SBARQ, but not an SQ.
You are going to need a much more advanced form of linguistic analysis to pull this off. Need Proof? Okay...
Does are female deer.
Where there is a will, there is a way.
When the time comes, I'll jump!
Why no. I do not have any pumpernickel.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With