NLTK. Find if a sentence is in a questioning form

前端 未结 2 1903
余生分开走
余生分开走 2020-12-16 05:03

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 a

2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-16 05:46

    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.

提交回复
热议问题