Treebank-style Tree Parser Python
Recently i have been trying to parse syntactic trees returned by the stanford parser in python. I have been trying to do that with nltk tree = Tree.parse(result['sentences'][0]['pa
Solution 1:
Unfortunately, PyInputTree
is no longer maintained. However, the InputTree
class from the Charniak
parser lives on in wrapped form as the Tree
class in BLLIP Parser. It doesn't implement isPrePreTerminal()
but here's one way to do it:
import bllipparser
defis_prepreterminal(node):
"""Returns True iff all children of this node are preterminals."""
subtrees = node.subtrees()
returnlen(subtrees) > 0and \
all(subtree.is_preterminal() for subtree in subtrees)
# testing code
tree = bllipparser.Tree('(S1 (S (NP (DT This)) (VP (VBZ is) (NP (DT a) (ADJP (RB fairly) (JJ simple)) (NN parse) (NN tree))) (. .)))')
for subtree in tree.all_subtrees():
print subtree, is_prepreterminal(subtree)
See bllipparser
on PyPI for more information.
Post a Comment for "Treebank-style Tree Parser Python"