NLTK: how can I list all pairs of neighboring subtrees (embedded in specific nonterminal) parsing trees

How can I effectively list all pairs of subtrees (embedded in a specific nonterminal) of a parse tree? For example, I have the following tree:

(X (Nn))) (X (N n))) (X (N n)))

You can see the image of this link .

I want to list all contiguous instances of the character X expanding to other characters, i.e. e :.

  • (X (PRO pro)) and (X (V v))
  • (X (V v)) and (X (ADJ adj))
  • (X (ADJ adj)) and (X (N n))
+4
source share
1 answer

Here is one solution that works for your example, but may not work for other tree structures that you may encounter:

from itertools import groupby
from nltk.tree import Tree

def get_pairs(t, cat):
    pairs = sorted(_get_pairs(t, cat), key=lambda x:x[0])
    for is_none, _adjacents in groupby(pairs, lambda x:x[1] is None):
        if is_none:
            continue
        adjacents = list(_adjacents)
        for (_, p1), (_, p2) in zip(adjacents, adjacents[1:]):
            yield p1, p2

def _get_pairs(t, cat, path=(), idx=(), has_root_cat=False):
    if isinstance(t, str):
        if has_root_cat:
            yield idx, path[:-1] + ((path[-1], t,),)
        else:
            yield idx, None
        return
    for i, ch in enumerate(t):
        found_cat = has_root_cat or t.node == cat
        new_path = path + (t.node,) if found_cat else path
        new_idx = idx + (i,)
        get_pairs_children = _get_pairs(ch, cat, new_path, new_idx, found_cat)
        for pair in get_pairs_children:
            yield pair

Launch

t = Tree.parse("(S (S (S (S (X (PRO pro))) (X (V v))) (X (ADJ adj))) (X (N n)))")
print list(get_pairs(t, "X"))

gives the result:

[(('X', ('PRO', 'pro')), ('X', ('V', 'v'))),
 (('X', ('V', 'v')), ('X', ('ADJ', 'adj'))),
 (('X', ('ADJ', 'adj')), ('X', ('N', 'n')))]
+2
source

All Articles