Skip to content

Instantly share code, notes, and snippets.

@mrdrozdov
Created February 21, 2019 19:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrdrozdov/e0d370fd13a0eb913b1c9d6ed748b9eb to your computer and use it in GitHub Desktop.
Save mrdrozdov/e0d370fd13a0eb913b1c9d6ed748b9eb to your computer and use it in GitHub Desktop.
detect-seq.py
mytree = """(ROOT
(S
(NP (NN Trouble))
(VP
(VBD was)
(, ,)
(SBAR (S (NP (NN nobody)) (VP (VBD thought) (SBAR (S (NP (PRP they)) (VP (VBD looked) (ADJP (RB right)))))))))
(. .)
)
)"""
tree = nltk.tree.Tree(mytree)
def recursive_tree_search(tr):
assert len(tr) == 3
seq = ['NP', 'VP', 'NP']
for tt, ss in zip(tr, seq):
assert tt == ss
for node in tr:
print(node.label())
recursive_tree_search(node)
recursive_tree_search(tree)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment