Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 检查句子中的动作_Python_Python 3.x_Nltk - Fatal编程技术网

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')