简体   繁体   中英

CFG top down parsing in nltk with python 36

I'm beginner in learning NLP. I read about the CFG and I want to apply it in top-down parsing and bottom-up parsing. 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. 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. I don't know where you got it from, but get rid of it. Second, CFG grammars do their own POS tagging. Pass the plain word list words to the parser.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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