Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine if a sentence is an inquiry

Tags:

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?

like image 435
Joel Avatar asked Nov 02 '10 23:11

Joel


People also ask

How do you check if a sentence is a question?

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?

Is it a question or a statement?

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.

Is could you please a question?

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.

Can you ask a question without a question mark?

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.


2 Answers

See also: How to find out if a sentence is a question (interrogative)?

My answer from that question:

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.

like image 76
msbmsb Avatar answered Oct 16 '22 18:10

msbmsb


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.

like image 21
JohnFx Avatar answered Oct 16 '22 18:10

JohnFx