问题
I am just wondering how would you add an optional grammer in the rule
>>> import nltk
>>> nltk.app.rdparser()
For example, the normal way to add a optional grammer is by putting it in parentheses: NP -> NP (PP)
But in the program how would you do it? parentheses doesnt work.
S Þ NP VP
NP Þ NP PP | Det N
VP Þ V NP PP
PP Þ P NP
Det Þ 'the' | 'a'
N Þ 'man' | 'park' | 'dog' | 'boy' | 'girl'
V Þ 'was' | 'saw'
P Þ 'in' | 'under' | 'with'
Thanks,
Ray
回答1:
NP -> NP | NP PP
But note that, with this rule, you can stack NP
nodes indefinitely in the parse tree.
来源:https://stackoverflow.com/questions/8591562/nltk-context-free-grammars