Example #1
0
def parent_map(element_tree: etree._ElementTree) -> dict:
    """Considerando que a estrutura etree._Element não quarda ponteiro para parent, esta função
    retorna um dicionário com a estrutura {child : parent , child2 : parent2, ...}, onde 'child'
    e 'parent' são objetos do tipo lxml.etree._Element. 

    Args:
        element_tree (etree._ElementTree): Arvore xml gerada no "parser" da biblioteca lxml.

    Returns:
        dict: Dicionário que relaciona os elementos da arvore do tipo _ElementTree com seus nós
        pais.
    """
    parent_map = dict((c, p) for p in element_tree.iter() for c in p)
    return parent_map
Example #2
0
def get_u_pos(tree: etree._ElementTree) -> ty.Dict[str, int]:
    """Return a dict mapping utterance nodes to their position in the tree."""
    return {xmlid(u): i for i, u in enumerate(tree.iter(f"{TEI}u"))}
Example #3
0
def get_w_pos(tree: etree._ElementTree) -> ty.Dict[str, int]:
    """Return a dict mapping token nodes to their position in the tree."""
    return {xmlid(w): i for i, w in enumerate(tree.iter(*TOKEN_TAGS))}