# Buggy code
def __init__(self, tree, sentence=None, highlight=()):
    if sentence is None:
        leaves = tree.leaves()
        if (
            leaves
            and not any(len(a) == 0 for a in tree.subtrees())
            and all(isinstance(a, int) for a in leaves)
        ):
            sentence = [str(a) for a in leaves]
        else:
            # this deals with empty nodes (frontier non-terminals)
            # and multiple/mixed terminals under non-terminals.
            tree = tree.copy(True)
            sentence = []
            for a in tree.subtrees():
                if len(a) == 0:
                    a.append(len(sentence))
                    sentence.append(None)
                elif any(not isinstance(b, Tree) for b in a):
                    for n, b in enumerate(a):
                        if not isinstance(b, Tree):
                            a[n] = len(sentence)
                            sentence.append("%s" % b)
    self.nodes, self.coords, self.edges, self.highlight = self.nodecoords(
        tree, sentence, highlight
    )


# Traceback (TypeError)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-32-306ca23c9095> in <module>()
7 NPChunker = nltk.RegexpParser(pattern)
8 result = NPChunker.parse(sentence)
----> 9 result.pretty_print()

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/nltk/tree.py in pretty_print(self, sentence, highlight, stream, **kwargs)
697         """
698         from nltk.treeprettyprinter import TreePrettyPrinter
--> 699         print(TreePrettyPrinter(self, sentence, highlight).text(**kwargs),
700               file=stream)
701

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/nltk/treeprettyprinter.py in __init__(self, tree, sentence, highlight)
95                             if not isinstance(b, Tree):
96                                 a[n] = len(sentence)
---> 97                                 sentence.append('%s' % b)
98         self.nodes, self.coords, self.edges, self.highlight = self.nodecoords(
99                 tree, sentence, highlight)

TypeError: not all arguments converted during string formatting

# Fixed code
def __init__(self, tree, sentence=None, highlight=()):
    if sentence is None:
        leaves = tree.leaves()
        if (
            leaves
            and not any(len(a) == 0 for a in tree.subtrees())
            and all(isinstance(a, int) for a in leaves)
        ):
            sentence = [str(a) for a in leaves]
        else:
            # this deals with empty nodes (frontier non-terminals)
            # and multiple/mixed terminals under non-terminals.
            tree = tree.copy(True)
            sentence = []
            for a in tree.subtrees():
                if len(a) == 0:
                    a.append(len(sentence))
                    sentence.append(None)
                elif any(not isinstance(b, Tree) for b in a):
                    for n, b in enumerate(a):
                        if not isinstance(b, Tree):
                            a[n] = len(sentence)
                            if type(b) == tuple:
                                b = "/".join(b)
                            sentence.append("%s" % b)
    self.nodes, self.coords, self.edges, self.highlight = self.nodecoords(
        tree, sentence, highlight
    )