我有一个简单的Python程序,用于判断一句话是否是问题。
from nltk.tokenize import word_tokenize
from nltk.stem.wordnet import WordNetLemmatizer
a = ["what are you doing","are you mad?","how sad"]
question =["what","why","how","are","am","should","do","can","have","could","when","whose","shall","is","would","may","whoever","does"];
word_list =["i","me","he","she","you","it","that","this","many","someone","everybody","her","they","them","his","we","am","is","are","was","were","should","did","would","does","do"];
def f(paragraph):
sentences = paragraph.split(".")
result = []
for i in range(len(sentences)):
token = word_tokenize(sentences[i])
change_tense = [WordNetLemmatizer().lemmatize(word, 'v') for word in token]
input_sentences = [item.lower() for item in change_tense]
if input_sentences[-1]=='?':
result.append("question")
elif input_sentences[0] in question:
find_question = [input_sentences.index(qestion) for qestion in input_sentences if qestion in question]
if len(find_question) > 0:
for a in find_question:
if input_sentences[a + 1] in word_list:
result.append("question")
else:
result.append("not a question")
else:
result.append("not a quetion")
return result
my_result = [f(paragraph) for paragraph in a]
print my_result
但是它会产生以下错误。
if input_sentences[a + 1] in word_list:
IndexError: list index out of range
我认为问题出在找到a的下一个元素值上。有人能帮我解决这个问题吗?
a+1
不是用来访问word_list
的,而是用来访问input_sentences
的。 - Matti Lyra