10

我试图检测一个句子是一个问题还是一个陈述。除了在句尾寻找问号之外,还有其他方法可以检测到这一点吗?我正在处理 Twitter 帖子,人们不一定会遵循 Twitter 上问号之类的良好做法。

如果nltk现在可以工作,我也可以参考其他库。

4

2 回答 2

17

一种简单的方法是解析一个句子并寻找分配给它的标签。例如,解析句子“有没有办法做到这一点?” 斯坦福解析器将返回:

(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))))))
    (. ?)))

whereSQ表示“在 SBARQ 中的 wh 短语之后,倒置的是/否问题,或 wh 问题的主要子句”。另一个例子:

(ROOT
  (SBARQ
    (WHNP (WP What))
    (SQ (VBZ is)
      (NP
        (NP (DT the) (NN capital))
        (PP (IN of)
          (NP (NNP Scotland)))))
    (. ?)))

其中 SBARQ 表示“由 wh 词或 wh 短语引入的直接问题”。从 Python 调用外部解析器并处理其输出非常简单,例如检查这个 Python 接口到斯坦福 NLP 工具。

于 2013-07-26T16:35:06.700 回答
-2

您可以检查问题可能的关键字,并将示例问题列表与您要检查的输入进行比较。

Sample_Questions = ["what is the weather like","where are we today","why did you do that","where is the dog","when are we going to leave","why do you hate me","what is the Answer to question 8",
                    "what is a dinosour","what do i do in an hour","why do we have to leave at 6.00", "When is the apointment","where did you go","why did you do that","how did he win","why won’t you help me",
                    "when did he find you","how do you get it","who does all the shipping","where do you buy stuff","why don’t you just find it in the target","why don't you buy stuff at target","where did you say it was",
                    "when did he grab the phone","what happened at seven am","did you take my phone","do you like me","do you know what happened yesterday","did it break when it dropped","does it hurt everyday",
                    "does the car break down often","can you drive me home","where did you find me"
                    "can it fly from here to target","could you find it for me"]


 def Question_Sentence_Match():
                for Ran_Question in Sample_Questions:
                    Question_Matcher = SequenceMatcher(None, Ran_Question, what_person_said_l).ratio()
                    if Question_Matcher > 0.5:
                        print (Question_Matcher)
                        print ("Similar to Question: "+Ran_Question)
                        print ("likely a Question")
                        return True
于 2017-06-27T16:17:42.513 回答