Using NLTK Tree

牧云@^-^@ 提交于 2019-12-25 08:19:08

问题


 ['', ['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']]]], ['.', '.']]]

I want to generate production rules by traversing this grammar using NLTK Tree. Production rules would be as follows :-

S -> NP-SBJ VP
NP-SBJ -> NP VP

Until now i tried to do this :-

def traverseTree(tree):
    #print("tree:", tree)
    for subtree in tree:
        if type(subtree) == nltk.tree.Tree:
            print subtree
            traverseTree(subtree)

and also used the breadth search api of nltk trees without any success. Graphical view of this grammar is here :- http://nbviewer.jupyter.org/github/gmonce/nltk_parsing/blob/master/1.%20NLTK%20Syntax%20Trees.ipynb

Is there any way to just visit a node and its children and create this kind of production rules ? Also I don't want to use tree's productions api for generating this grammar rules.

来源:https://stackoverflow.com/questions/40791675/using-nltk-tree

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!