[英]CFG top down parsing in nltk with python 36
我是学习NLP的初学者。 我读到有关CFG的文章,我想将其应用于自上而下的解析和自下而上的解析。 我从上而下的解析开始。 我想用nltk和python 36绘制自上而下的解析树。我编写了以下代码,但是它不起作用。 怎么了 有没有人可以帮助我增强代码?
import nltk
from nltk.tag import pos_tag
from nltk.tokenize import word_tokenize
from nltk.tree import *
from nltk.draw import tree
from nltk import Nonterminal, nonterminals, Production, CFG
from nltk.parse import RecursiveDescentParser
text = input('Please enter a sentence: ')
words = text.split()
sentence = pos_tag(words)
grammar1 = nltk.CFG.fromstring("""
S -> NP VP
S -> VP
VP -> V NP | V NP PP
NP -> Det N | Det N PP
PP -> P NP
V -> "saw" | "ate" | "walked" | "book" | "prefer" | "sleeps"
Det -> "a" | "an" | "the" | "my" | "that"
N -> "man" | "dog" | "cat" | "telescope" | "park" | "flight" | "apple"
P -> "in" | "on" | "by" | "with"
""")
rd = nltk.RecursiveDescentParser(grammar1, "Input")
result = rd.parse(sentence)
result.draw()
我输入此文本用于解析“预订该航班”。
下次您问一个问题时,不要只说“它不起作用”。 解释失败的地方和发生的情况(包括堆栈跟踪和错误消息,如果失败并显示错误)。
您的代码有两个问题:参数"Input"
不属于解析器构造函数。 我不知道你从哪里得到的,但要摆脱它。 其次,CFG语法执行自己的POS标记。 将普通单词列表words
传递给解析器。
rd = nltk.RecursiveDescentParser(grammar1)
result = rd.parse(words)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.