NLTK Chart Parser is not printing [duplicate]

余生长醉 提交于 2019-12-23 04:28:53

问题


Here is the code:

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)

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.


回答1:


Always write the CFG grammar in bite-size, see Python and NLTK: How to analyze sentence grammar?

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

[out]:

[Tree('S', [Tree('V', ['describe']), Tree('NP', [Tree('PRP', ['your']), Tree('N', ['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)))

[out]:

[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'])])])])])]

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

[out]:

[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'])])])])])]

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

[out]:

[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'])])])])])])]

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

[out]:

[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'])])])])])])]

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

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

[out]:

[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'])])])])])])])]

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.



来源:https://stackoverflow.com/questions/42966067/nltk-chart-parser-is-not-printing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!