NLTK Chart Parser无法打印 [英] NLTK Chart Parser is not printing
问题描述
这是代码:
groucho_grammar = nltk.CFG.fromstring("""
S -> V NP PP CONJ V NP PP
PP -> PRP NP
NP -> Det N | PRP N |DET ADJ CONJ ADJ N P
Det -> 'a' | 'every' | 'all'
N -> 'work' | 'Word Document' | 'results' | 'step'
ADJ -> 'intermediate' | 'final'
V -> 'Describe' | 'present'
P -> 'of' | 'in'
CONJ -> 'and'
PRP -> 'your'
""")
sent = ['Describe', 'every', 'step' ,'of', 'your', 'work', 'and' ,\
'present', 'all', 'intermediate' ,'and' ,'final', 'results', 'in' ,'a', 'Word Document']
parser = nltk.ChartParser(groucho_grammar)
for tree in parser.parse(sent):
print(tree)
当我这样做时,它可以正常运行,但不会打印任何语法树.我不确定在这里我做错了什么.我遵循了nltk书中的指导原则,但是并没有帮助.
When I do this, It runs without any errors but it doesn't print any grammar trees. I'm not sure what I am doing wrong here. I followed the guidelines in the nltk book but that hasnt helped.
推荐答案
Always write the CFG grammar in bite-size, see Python and NLTK: How to analyze sentence grammar?
让我们首先尝试处理describe your work
.
Let's try to handle describe your work
first.
import nltk
your_grammar = nltk.CFG.fromstring("""
S -> V NP
V -> 'describe' | 'present'
NP -> PRP N
PRP -> 'your'
N -> 'work'
""")
parser = nltk.ChartParser(your_grammar)
sent = 'describe your work'.split()
print (list(parser.parse(sent)))
[输出]:
[Tree('S', [Tree('V', ['describe']), Tree('NP', [Tree('PRP', ['your']), Tree('N', ['work'])])])]
现在让我们尝试describe every step of your work
:
Now let's try describe every step of your work
:
import nltk
your_grammar = nltk.CFG.fromstring("""
S -> V NP
V -> 'describe' | 'present'
NP -> PRP N | DT N PP
PRP -> 'your'
N -> 'work' | 'step'
PP -> P NP
P -> 'of'
DT -> 'every'
""")
parser = nltk.ChartParser(your_grammar)
sent = 'describe every step of your work'.split()
print (list(parser.parse(sent)))
[输出]:
[Tree('S', [Tree('V', ['describe']), Tree('NP', [Tree('DT', ['every']), Tree('N', ['step']), Tree('PP', [Tree('P', ['of']), Tree('NP', [Tree('PRP', ['your']), Tree('N', ['work'])])])])])]
现在让我们尝试present final results in a Word Document
:
Now let's try present final results in a Word Document
:
import nltk
your_grammar = nltk.CFG.fromstring("""
S -> V NP
V -> 'describe' | 'present'
NP -> PRP N | DT N PP | DT N | ADJ N PP
PRP -> 'your'
N -> 'work' | 'step' | 'results' | 'Word_Document'
PP -> P NP
P -> 'of' | 'in'
DT -> 'every' | 'a'
ADJ -> 'final'
""")
parser = nltk.ChartParser(your_grammar)
#sent = 'describe every step of your work'.split()
sent = 'present final results in a Word_Document'.split()
print (list(parser.parse(sent)))
[输出]:
[Tree('S', [Tree('V', ['present']), Tree('NP', [Tree('ADJ', ['final']), Tree('N', ['results']), Tree('PP', [Tree('P', ['in']), Tree('NP', [Tree('DT', ['a']), Tree('N', ['Word_Document'])])])])])]
现在,我们为present all final results in a Word Document
添加NP -> DT NP
:
Now, let's add NP -> DT NP
for present all final results in a Word Document
:
import nltk
your_grammar = nltk.CFG.fromstring("""
S -> V NP
V -> 'describe' | 'present'
NP -> PRP N | DT N PP | DT N | ADJ N PP | DT NP
PRP -> 'your'
N -> 'work' | 'step' | 'results' | 'Word_Document'
PP -> P NP
P -> 'of' | 'in'
DT -> 'every' | 'a' | 'all'
ADJ -> 'final'
""")
parser = nltk.ChartParser(your_grammar)
#sent = 'describe every step of your work'.split()
sent = 'present all final results in a Word_Document'.split()
print (list(parser.parse(sent)))
[输出]:
[Tree('S', [Tree('V', ['present']), Tree('NP', [Tree('DT', ['all']), Tree('NP', [Tree('ADJ', ['final']), Tree('N', ['results']), Tree('PP', [Tree('P', ['in']), Tree('NP', [Tree('DT', ['a']), Tree('N', ['Word_Document'])])])])])])]
现在让我们来看一下present all intermediate and final results in a Word_Document
的连词:
Now let's go for the conjunctions for present all intermediate and final results in a Word_Document
:
import nltk
your_grammar = nltk.CFG.fromstring("""
S -> V NP
V -> 'describe' | 'present'
NP -> PRP N | DT N PP | DT N | ADJ N PP | DT NP
PRP -> 'your'
N -> 'work' | 'step' | 'results' | 'Word_Document'
PP -> P NP
P -> 'of' | 'in'
DT -> 'every' | 'a' | 'all'
ADJ -> 'final' | 'intermediate' | ADJ CONJ ADJ
CONJ -> 'and'
""")
parser = nltk.ChartParser(your_grammar)
#sent = 'describe every step of your work'.split()
sent = 'present all intermediate and final results in a Word_Document'.split()
print (list(parser.parse(sent)))
[输出]:
[Tree('S', [Tree('V', ['present']), Tree('NP', [Tree('DT', ['all']), Tree('NP', [Tree('ADJ', [Tree('ADJ', ['intermediate']), Tree('CONJ', ['and']), Tree('ADJ', ['final'])]), Tree('N', ['results']), Tree('PP', [Tree('P', ['in']), Tree('NP', [Tree('DT', ['a']), Tree('N', ['Word_Document'])])])])])])]
但这只会给您一个阅读present all [(intermediate and final) (results) (in a Word_Document)]
.对于模棱两可的结果,我将留给您想象; P
But that only give you one reading present all [(intermediate and final) (results) (in a Word_Document)]
. For ambiguous results, I'll leave it to your imagination ;P
现在让我们继续并连接S -> S CONJ S
到describe your work and present all intermediate and final results in a Word_Document
:
Now let's move on and concatenate the S -> S CONJ S
for describe your work and present all intermediate and final results in a Word_Document
:
import nltk
your_grammar = nltk.CFG.fromstring("""
S -> V NP | S CONJ S
V -> 'describe' | 'present'
NP -> PRP N | DT N PP | DT N | ADJ N PP | DT NP
PRP -> 'your'
N -> 'work' | 'step' | 'results' | 'Word_Document'
PP -> P NP
P -> 'of' | 'in'
DT -> 'every' | 'a' | 'all'
ADJ -> 'final' | 'intermediate' | ADJ CONJ ADJ
CONJ -> 'and'
""")
parser = nltk.ChartParser(your_grammar)
sent1 = 'describe every step of your work'
sent2 = 'present all intermediate and final results in a Word_Document'
sent = ' and '.join([sent1, sent2]).split()
print (list(parser.parse(sent)))
[输出]:
[Tree('S', [Tree('S', [Tree('V', ['describe']), Tree('NP', [Tree('DT', ['every']), Tree('N', ['step']), Tree('PP', [Tree('P', ['of']), Tree('NP', [Tree('PRP', ['your']), Tree('N', ['work'])])])])]), Tree('CONJ', ['and']), Tree('S', [Tree('V', ['present']), Tree('NP', [Tree('DT', ['all']), Tree('NP', [Tree('ADJ', [Tree('ADJ', ['intermediate']), Tree('CONJ', ['and']), Tree('ADJ', ['final'])]), Tree('N', ['results']), Tree('PP', [Tree('P', ['in']), Tree('NP', [Tree('DT', ['a']), Tree('N', ['Word_Document'])])])])])])])]
肯定有其他方法可以编写适合您句子的CFG语法,这只是众多方法之一.但一般来说,将CFG语法写成bitesize.
There're surely other ways to write the CFG grammar to suit your sentence and this is just one of the many ways. But in general, write the CFG grammar in bitesize.
这篇关于NLTK Chart Parser无法打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!