Python(nltk.tokenize)-Detect a Question














































Python(nltk.tokenize)-Detect a Question



In this article we need to find out if the input of the sentence is questions or not. We can use ML to find questions, but since we know all kinds of sentences we see in the questionnaire, we can write python text to find out if the sentence is a question or not.

Before we write a Python program to find out if a sentence is asking or not, we need to make a list of words that we see at the beginning of the question. For example, who are you? where are you from ?, in these two questions, "who" and "where" are the types of words we need to keep in the python list. Next, to check whether the sentence is a question or not, we need to check that any word in the list is present at the beginning of the sentence. If so, a sentence is a question, and if not, a sentence is not a question.


The Code implementation is given below:


from nltk.tokenize import word_tokenize question_words = ["what", "why", "when", "where", "name", "is", "how", "do", "does", "which", "are", "could", "would", "should", "has", "have", "whom", "whose", "don't"] question = input("Input a sentence: ") question = question.lower() question = word_tokenize(question) if any(x in question[0] for x in question_words): print("This is a question!") else: print("This is not a question!")





OUTPUT-






Comments