NLTK 图表解析器不打印

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 书中的指南,但这并没有帮助。

始终将 CFG 语法写成一口大小,参见 Python and NLTK: How to analyze sentence grammar?

我们先尝试处理describe your work

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:

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:

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

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 的连词:

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

现在让我们继续并连接 describe your work and present all intermediate and final results in a Word_DocumentS -> S CONJ S

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。