nltk:来自生产的语法

2024-10-01 07:12:21 发布

您现在位置:Python中文网/ 问答频道 /正文

This link展示了从树到结果的可能性,现在我只需要知道如何从结果到语法。在

def trees2productions(trees):
""" Transform list of Trees to a list of productions """
productions = []
for t in trees:
    productions += t.productions()
return productions

This page演示了如何从预定义的语法中获取语法的结果,但它没有说明如何从结果到语法。有人知道我怎么做吗?在

^{pr2}$

Tags: oftoinfordef语法linktransform
1条回答
网友
1楼 · 发布于 2024-10-01 07:12:21

我发现了如何解决我的问题,遵循代码here并对其进行了一点更新。这里是:

import nltk
from nltk.grammar import CFG, Nonterminal
productions = [S -> NP VP, PP -> P NP, NP -> Det N, NP -> NP PP, VP -> V NP, VP -> VP PP, Det -> 'a', Det -> 'the', N -> 'dog', N -> 'cat', V -> 'chased', V -> 'sat', P -> 'on', P -> 'in']
grammar = CFG(Nonterminal('S'), productions)

相关问题 更多 >