Esempio n. 1
0
File: cfg.py Progetto: Geolem/nltk
    def reset_workspace(self):
        c = self._workspace.canvas()
        fontsize = int(self._size.get())
        node_font = ("helvetica", -(fontsize + 4), "bold")
        leaf_font = ("helvetica", -(fontsize + 2))

        # Remove the old tree
        if self._tree is not None:
            self._workspace.remove_widget(self._tree)

        # The root of the tree.
        start = self._grammar.start().symbol()
        rootnode = TextWidget(c, start, font=node_font, draggable=1)

        # The leaves of the tree.
        leaves = []
        for word in self._text:
            leaves.append(TextWidget(c, word, font=leaf_font, draggable=1))

        # Put it all together into one tree
        self._tree = TreeSegmentWidget(c, rootnode, leaves, color="white")

        # Add it to the workspace.
        self._workspace.add_widget(self._tree)

        # Move the leaves to the bottom of the workspace.
        for leaf in leaves:
            leaf.move(0, 100)
Esempio n. 2
0
def tree_to_widget(s, canvas):
    """
    Parse a tree string, and return a corresponding widget.  See the
    module docstring for the format of C{s}.
    """
    WORD = r'(\\\\|\\[^\\\n]|[^\\\s()<>])+'
    TOKEN_RE = re.compile(r'\(\s*%s|<\s*%s|\)|>|%s|\s+' % (WORD, WORD, WORD))
    stack = [[]]
    for tok in tokenize(s.strip(), TOKEN_RE):
        if tok.strip() == '':
            pass
        elif tok[:1] in '(<':
            label = word_to_widget(tok[1:].strip(),
                                   canvas,
                                   color='#004080',
                                   bold=True)
            roof = (tok[:1] == '<')
            stack[-1].append(dict(canvas=canvas, label=label, roof=roof))
            stack.append([])
        elif tok[:1] in ')>':
            subtrees = stack.pop()
            node_kwargs = stack[-1][-1]
            stack[-1][-1] = TreeSegmentWidget(subtrees=subtrees, **node_kwargs)
        else:
            leaf = word_to_widget(tok.strip(), canvas, color='#008040')
            stack[-1].append(leaf)

    if not len(stack) == 1 and len(stack[0]) == 1:
        raise ValueError('unbalanced parens?')
    return stack[0][0]