Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NLTK. Find if a sentence is in a questioning form

Tags:

python

nlp

nltk

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.

like image 777
rishi Avatar asked Jul 26 '13 10:07

rishi


People also ask

How do you detect 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? Who wants to know?

How do you determine if a sentence is a question python?

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.

How do you find the subject of a sentence in Python?

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.


1 Answers

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.

like image 100
dkar Avatar answered Oct 15 '22 16:10

dkar