NLTK图表分析器未打印

2024-03-29 10:43:55 发布

您现在位置:Python中文网/ 问答频道 /正文

代码如下:

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书中的指导原则,但这没有帮助。在


Tags: andinnpallppwordworkdet
1条回答
网友
1楼 · 发布于 2024-03-29 10:43:55

总是以字节大小编写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)))

[出来]:

^{pr2}$

现在让我们试试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)]。对于模棱两可的结果,我将留给你想象

现在让我们继续并连接S -> S CONJ Sdescribe 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语法来适应你的句子,这只是众多方法中的一种。但一般来说,用bitesize编写CFG语法。在

相关问题 更多 >