如果你正在寻找从括号内的解析创建的规则,你可以使用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'
请编辑您的问题,以便更清楚地了解您的需求:第一句说明您需要括号形式,第二句建议您要输出为该句子启动的基础语法部分。 – lenz
这里可能会有一些混淆,从你一贯输入的“formstring”而不是“fromstring”来判断。你正在寻找的格式不是一个分析树,而是一个语法规范(它是解析器的输入之一,而不是输出)。所以我怀疑已经有一个自动解决方案。 – lenz
是的,我纠正了混乱。这是来自于。我知道这是一个用于输入的语法规范。给出一个分析树,是否有解决方法来创建这样的规范?这将节省我一些时间和精力。不仅通过NLTK,而且通过任何其他方法。如果不存在这种现成的解决方案;是否有可能创建一个? –