NLTK上下文无关的语法 [英] NLTK context-free grammars

查看:103
本文介绍了NLTK上下文无关的语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想知道您如何在规则中添加可选的语法?

I am just wondering how would you add an optional grammer in the rule

>>> import nltk
>>> nltk.app.rdparser()

例如,添加可选语法的常规方法是将其放在括号中:NP-> NP(PP)

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'

谢谢

推荐答案

NP -> NP | NP PP

但是请注意,使用此规则,您可以在解析树中无限期地堆叠NP个节点.

But note that, with this rule, you can stack NP nodes indefinitely in the parse tree.

这篇关于NLTK上下文无关的语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆