Python 检查句子中的动作
现在,我已经确定了主题、动作和地点/时间使用的代码:Python 检查句子中的动作,python,python-3.x,nltk,Python,Python 3.x,Nltk,现在,我已经确定了主题、动作和地点/时间使用的代码: from nltk.tag import pos_tag from nltk.tokenize import word_tokenize import nltk text = input('Please enter a sentence: ') words = text.split() sent = [w for w in words if not w == 'computer'] sentence = pos_tag(sent) gra
from nltk.tag import pos_tag
from nltk.tokenize import word_tokenize
import nltk
text = input('Please enter a sentence: ')
words = text.split()
sent = [w for w in words if not w == 'computer']
sentence = pos_tag(sent)
grammar = '''
Action: {<NN.*>?<VB.*><RB.*>?}
Location/Time: {<IN><NN.*>+}
Subject: {<DT>?<JJ>*<NN.*>}
'''
cp = nltk.RegexpParser(grammar, "Input")
result = cp.parse(sentence)
subjects = []
actions = []
locations_times = []
for word in result:
if isinstance(word, nltk.tree.Tree):
if word.label() == 'Subject':
subjects.append(word)
for word in result:
if isinstance(word, nltk.tree.Tree):
if word.label() == 'Action':
actions.append(word)
for word in result:
if isinstance(word, nltk.tree.Tree):
if word.label() == 'Location/Time':
locations_times.append(word)
请帮助解析器的结果将至少是一个
树
对象,您必须对其进行递归计算。有关使用解析器及其返回结果的示例,请参见。您输入了什么?
if actions[0] == '(Action time/NN is/VBZ)':
print('success')
else:
print('failure')