为nltk解析树生成语法规则
如果我有这样一句话 "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
1 个回答
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提到的,你想要的这个事情其实是相当不可能的 =)