Reputation: 2554
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.
Upvotes: 11
Views: 12799
Reputation: 480
You can check for question likely keywords and compare sample questions lists with the input you want to check.
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
Upvotes: -2
Reputation: 2123
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.
Upvotes: 17