python – NLTK:如何列出解析树的所有相邻子树对(以特定的非终结符号为根)

如何有效地列出解析树的所有子树对(根据特定的非终结符)?例如,我有以下树:

(S (S (S (S (X (PRO pro))) (X (V v))) (X (ADJ adj))) (X (N n)))

你可以在this link上看到图像.

我想列出符号X的所有相邻实例,扩展到其他符号,i. Ë:

>(X(PRO pro))和(X(V v))
>(X(V v))和(X(ADJ adj))
>(X(ADJ adj))和(X(N n))

最佳答案 这是一个适用于您的示例的解决方案,但可能不适用于您可能遇到的其他树结构:

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

运行

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"))

给出输出:

[(('X', ('PRO', 'pro')), ('X', ('V', 'v'))),
 (('X', ('V', 'v')), ('X', ('ADJ', 'adj'))),
 (('X', ('ADJ', 'adj')), ('X', ('N', 'n')))]
点赞