简体   繁体   English

使用python 36在nltk中进行CFG自上而下的解析

[英]CFG top down parsing in nltk with python 36

I'm beginner in learning NLP. 我是学习NLP的初学者。 I read about the CFG and I want to apply it in top-down parsing and bottom-up parsing. 我读到有关CFG的文章,我想将其应用于自上而下的解析和自下而上的解析。 I started with top-down parsing. 我从上而下的解析开始。 I want to draw the top-down parsing tree with nltk and python 36. I wrote the following code, but it does not work. 我想用nltk和python 36绘制自上而下的解析树。我编写了以下代码,但是它不起作用。 what is the wrong? 怎么了 Is there anyone could help me enhance the code? 有没有人可以帮助我增强代码?

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()

I entered this text for parsing "book that flight". 我输入此文本用于解析“预订该航班”。

Next time you ask a question, don't just say "it doesn't work". 下次您问一个问题时,不要只说“它不起作用”。 Explain where it fails and what happens (include the stack trace and error message, if it fails with an error). 解释失败的地方和发生的情况(包括堆栈跟踪和错误消息,如果失败并显示错误)。

There are two problems with your code: The argument "Input" doesn't belong in the parser constructor. 您的代码有两个问题:参数"Input"不属于解析器构造函数。 I don't know where you got it from, but get rid of it. 我不知道你从哪里得到的,但要摆脱它。 Second, CFG grammars do their own POS tagging. 其次,CFG语法执行自己的POS标记。 Pass the plain word list words to the parser. 将普通单词列表words传递给解析器。

rd = nltk.RecursiveDescentParser(grammar1)
result = rd.parse(words)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM