问题
['', ['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