now that i've determined what the subject, action and location/time are using this code:
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)
How can i check if the action == 'whatever' do something, else do something else.
ive tried this when typing "what time is it" as the input but it always prints failure:
if actions[0] == '(Action time/NN is/VBZ)':
print('success')
else:
print('failure')
please help
The result from a parser will be at least one Tree
object, which you will have to recursively evaluate. See this answer for an example of using a parser and the result it returns.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.