NLTK context-free grammars

倖福魔咒の 提交于 2019-12-12 17:04:50

问题


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

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