2016-08-17 31 views
2

我有一个例句。 “打开门。”我解析了一个句子来得到括号内的解析输出,如下所示。如何提取给出了括号分析的语法生成规则?

(S(VP(VB打开)(NP(DT的)(NN门)))(。))

我需要提取产生经解析的输出的CFG语法规则。 我可以手动把它们写出来这样:

grammar = CFG.fromstring(""" 
S -> VP NP 
NP -> Det N 
VP -> V 
Det ->'the ' 
N -> 'door' 
V -> 'Open' 
""") 

但它耗费时间,我怎么给生产括号内的自动解析语法规则?

+0

请编辑您的问题,以便更清楚地了解您的需求:第一句说明您需要括号形式,第二句建议您要输出为该句子启动的基础语法部分。 – lenz

+0

这里可能会有一些混淆,从你一贯输入的“formstring”而不是“fromstring”来判断。你正在寻找的格式不是一个分析树,而是一个语法规范(它是解析器的输入之一,而不是输出)。所以我怀疑已经有一个自动解决方案。 – lenz

+0

是的,我纠正了混乱。这是来自于。我知道这是一个用于输入的语法规范。给出一个分析树,是否有解决方法来创建这样的规范?这将节省我一些时间和精力。不仅通过NLTK,而且通过任何其他方法。如果不存在这种现成的解决方案;是否有可能创建一个? –

回答

2

您可以使用Tree.productions()方法从树中获取CFG规则。

例子:

from nltk import Tree 

t = Tree.fromstring("(S (VP (VB open) (NP (DT the) (NN door))) (. .))") 
print t.productions() 

输出:

[S -> VP ., VP -> VB NP, VB -> 'open', NP -> DT NN, DT -> 'the', 
NN -> 'door', . -> '.'] 

欲了解更多信息检查 - NLTK Tree Productions

+0

感谢您的帮助。 –

2

如果你正在寻找从括号内的解析创建的规则,你可以使用Tree.productions()

>>> from nltk import Tree 
>>> t = Tree.fromstring("(S (NP (D the) (N dog)) (VP (V chased) (NP (D the) (N cat))))") 
>>> t.productions() 
[S -> NP VP, NP -> D N, D -> 'the', N -> 'dog', VP -> V NP, V -> 'chased', NP -> D N, D -> 'the', N -> 'cat'] 

Tree.productions()将返回nltk.grammar.Productions对象列表:

>>> type(t.productions()[0]) 
<class 'nltk.grammar.Production'> 

为了让规则转化为字符串的形式,使用Production.unicode_repr

>>> t.productions()[0].unicode_repr() 
u'S -> NP VP' 

向获得的字符串表示从括号内解析得到的语法:

>>> from nltk import Tree 
>>> t = Tree.fromstring("(S (NP (D the) (N dog)) (VP (V chased) (NP (D the) (N cat))))") 
>>> grammar_from_parse = "\n".join([rule.unicode_repr() for rule in t.productions()]) 
>>> print grammar_from_parse 
S -> NP VP 
NP -> D N 
D -> 'the' 
N -> 'dog' 
VP -> V NP 
V -> 'chased' 
NP -> D N 
D -> 'the' 
N -> 'cat' 
+0

现货上。像魔术一样解决了这个问题。 –