def searchTree(labels, regex, tree): if tb.is_terminal(tree): return else: label = tb.tree_label(tree).split('#')[0] if regex.match(label): if not label in labels: labels[label] = {} word = ''.join(tb.terminals(tree)).replace("\\", "") incr(word, labels[label]) for subtree in tb.tree_subtrees(tree): searchTree(labels, regex, subtree)
def visit(node, wordssofar, segssofar): """Does a preorder visit of the nodes in the tree""" if tb.is_terminal(node): if not ignore_terminal_rex.match(node): segssofar.append(simplify_terminal(node)) return wordssofar,segssofar for child in tb.tree_children(node): wordssofar,segssofar = visit(child, wordssofar, segssofar) if word_rex.match(tb.tree_label(node)): if segssofar != []: wordssofar.append(''.join(segssofar)) segssofar = [] return wordssofar,segssofar
def visit(node, wordssofar, segssofar): """Does a preorder visit of the nodes in the tree""" if tb.is_terminal(node): if not ignore_terminal_rex.match(node): segssofar.append(simplify_terminal(node)) return wordssofar, segssofar for child in tb.tree_children(node): wordssofar, segssofar = visit(child, wordssofar, segssofar) if word_rex.match(tb.tree_label(node)): if segssofar != []: wordssofar.append(''.join(segssofar)) segssofar = [] return wordssofar, segssofar
def visit(node, wordssofar, segssofar): """Does a preorder visit of the nodes in the tree""" if tb.is_terminal(node): if not ignore_terminal_rex.match(node): segssofar.append(node) return wordssofar,segssofar for child in tb.tree_children(node): wordssofar,segssofar = visit(child, wordssofar, segssofar) mo = score_cat_rex.match(tb.tree_label(node)) if mo: if segssofar != []: word = ''.join(segssofar) segssofar = [] try: topic = mo.group('topic') if topic != None: wordssofar.append((word,topic)) else: wordssofar.append(word) except IndexError: wordssofar.append(word) return wordssofar,segssofar