I am trying to detect if a sentence is a question or a statement. Apart from looking for a question mark at the end of the sentence, is there another way to detect this? I am processing Twitter posts and people are not necessarily following good practises like question marks on Twitter.
Reference to other libraries is also ok with me if nltk does now work.
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? Who wants to know?
Next, to check if a sentence is a question or not, we need to check if any word from the list is present at the beginning of the sentence. If it is present, then the sentence is a question, and if it is not present, then the sentence is not a question.
It follows subject-verb-object model. To mark the subject, write a rule set with POS tags. Tag the sentence I[NOUN] shot[VERB] an elephant[NOUN] . If you see the first noun is subject, then there is a verb and then there is an object.
One simple way to do this is to parse a sentence and look for the tag assigned to it. For example, parsing the sentence "Is there any way to do this?" with Stanford parser will return:
(ROOT
(SQ (VBZ Is)
(NP (EX there))
(NP
(NP (DT any) (JJ other) (NN way))
(S
(VP (TO to)
(VP (VB do)
(NP (DT this))))))
(. ?)))
where SQ
denotes "Inverted yes/no question, or main clause of a wh-question, following the wh-phrase in SBARQ". Another example:
(ROOT
(SBARQ
(WHNP (WP What))
(SQ (VBZ is)
(NP
(NP (DT the) (NN capital))
(PP (IN of)
(NP (NNP Scotland)))))
(. ?)))
where SBARQ denotes "Direct question introduced by a wh-word or a wh-phrase". It's pretty straightforward to call an external parser from Python and process its output, for example check this Python interface to Stanford NLP tools.
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