Skip to content

Instantly share code, notes, and snippets.

@butsugiri
Created December 11, 2016 02:49
Show Gist options
  • Save butsugiri/6b97c3e9e328f3cfc47e2b4bc513bde9 to your computer and use it in GitHub Desktop.
Save butsugiri/6b97c3e9e328f3cfc47e2b4bc513bde9 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
from nltk.tree import Tree
from nltk.tree import ParentedTree
from nltk.tree import ImmutableParentedTree
def main():
tree = ImmutableParentedTree.fromstring("(ROOT (S (NP (NNP Dog)) (VP (VBZ likes) (NP (NN cat))) (. .)))")
t = tree.leaf_treeposition(1)
x = tree.leaf_treeposition(2)
print([tree[t[:i]].parent() for i in range(1, len(t))])
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment