0
私はNLPの初心者です。私はCFGについて読んで、トップダウン解析とボトムアップ解析でそれを適用したいと考えています。私はトップダウン解析を始めました。私はnltkとpython 36でトップダウン構文解析ツリーを描きたいと思います。次のコードを書いたが、うまくいきません。何が間違っていますか?コードを強化するのに役立つ人はいますか?CFGトップダウン解析、Pythonでnltkで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()
"本の飛行"を解析するためにこのテキストを入力しました。