Ejemplo n.º 1
0
def _is_leaf(tree: ParentedTree):
    """
    Checks whether the given tree is a leaf.
    :param tree: a ParentedTree instance
    :return: true if it is a leaf
    """
    return tree.height() == 2
    def traverse_and_store(self, tree: ParentedTree,
                           parse_tree_stored: List[Dict]):

        label = tree.label()
        words = [x.split('_')[0] for x in tree.leaves()]
        indices = [int(x.split('_')[-1]) for x in tree.leaves()]
        ngram_info = len(words)
        words = " ".join(words)

        if tree.height() > self.TREE_HEIGHT and ngram_info < self.NGRAM_LIMIT:
            parse_tree_stored.append({
                'phrase_label': label,
                'phrase': words,
                'ngram': ngram_info,
                'indices': indices
            })
        for subtree in tree:
            if type(subtree) == ParentedTree:
                self.traverse_and_store(tree=subtree,
                                        parse_tree_stored=parse_tree_stored)

        return parse_tree_stored