['', ['S', ['NP-SBJ', ['NP', ['NNP', 'Pierre'], ['NNP', 'Vinken']], [',', ','], ['ADJP', ['NP', ['CD', '61'], ['NNS', 'years']], ['JJ', 'old']], [',', ',']], ['VP', ['MD', 'will'], ['VP', ['VB', 'join'], ['NP', ['DT', 'the'], ['NN', 'board']], ['PP-CLR', ['IN', 'as'], ['NP', ['DT', 'a'], ['JJ', 'nonexecutive'], ['NN', 'director']]], ['NP-TMP', ['NNP', 'Nov.'], ['CD', '29']]]], ['.', '.']]]
我想通过使用NLTK Tree遍历此语法来生成生产规则。 制作规则如下: -
S -> NP-SBJ VP
NP-SBJ -> NP VP
直到现在我试图这样做: -
def traverseTree(tree):
#print("tree:", tree)
for subtree in tree:
if type(subtree) == nltk.tree.Tree:
print subtree
traverseTree(subtree)
并且还使用了nltk树的广度搜索api而没有任何成功。此语法的图形视图如下: - http://nbviewer.jupyter.org/github/gmonce/nltk_parsing/blob/master/1.%20NLTK%20Syntax%20Trees.ipynb
有没有办法只访问一个节点及其子节点并创建这种生产规则?另外,我不想使用树的制作api来生成这个语法规则。