[英]generating grammar rules for nltk parse trees
如果我有句子"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強調的那樣,您要的是幾乎不可能的=)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.