如果我有句子"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
答案 0 :(得分:0)
您可以尝试:
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所强调的那样,你要求的东西是不可能的=)