如果我有这句话"Mary saw a dog"
以及以下内容:
pos_tags = ['NNP', 'VBD', 'DT', 'NN']
是否可以生成这句话的语法规则,以便可以生成解析树(下面的语法是使用的语法规则nltk.parse_cfg
)
sent = "Mary saw a dog".split()
rd_parser = nltk.RecursiveDescentParser(grammar)
for tree in rd_parser.nbest_parse(sent):
print tree
你可以试试:
import nltk
# Define the cfg grammar.
grammar = nltk.parse_cfg("""
S -> NP VP
NP -> 'DT' 'NN'
VP -> 'VB'
VP -> 'VB' 'NN'
""")
# Make your POS sentence into a list of tokens.
sentence = "DT NN VB NN".split(" ")
# Load the grammar into the ChartParser.
cp = nltk.ChartParser(grammar)
# Generate and print the nbest_parse from the grammar given the sentence tokens.
for tree in cp.nbest_parse(sentence):
print tree
但正如 @alexis 强调的那样,你所要求的是不可能的 =)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)