Python nltk.compat.unicode_repr() Examples

The following are code examples for showing how to use nltk.compat.unicode_repr(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: razzy-spinner   Author: rafasashi   File: tree.py    GNU General Public License v3.0 6 votes vote down vote up
def _pformat_flat(self, nodesep, parens, quotes):
        childstrs = []
        for child in self:
            if isinstance(child, Tree):
                childstrs.append(child._pformat_flat(nodesep, parens, quotes))
            elif isinstance(child, tuple):
                childstrs.append("/".join(child))
            elif isinstance(child, string_types) and not quotes:
                childstrs.append('%s' % child)
            else:
                childstrs.append(unicode_repr(child))
        if isinstance(self._label, string_types):
            return '%s%s%s %s%s' % (parens[0], self._label, nodesep,
                                    " ".join(childstrs), parens[1])
        else:
            return '%s%s%s %s%s' % (parens[0], unicode_repr(self._label), nodesep,
                                    " ".join(childstrs), parens[1]) 
Example 2
Project: razzy-spinner   Author: rafasashi   File: regexp.py    GNU General Public License v3.0 6 votes vote down vote up
def _trace_apply(self, chunkstr, verbose):
        """
        Apply each rule of this ``RegexpChunkParser`` to ``chunkstr``, in
        turn.  Generate trace output between each rule.  If ``verbose``
        is true, then generate verbose output.

        :type chunkstr: ChunkString
        :param chunkstr: The chunk string to which each rule should be
            applied.
        :type verbose: bool
        :param verbose: Whether output should be verbose.
        :rtype: None
        """
        print('# Input:')
        print(chunkstr)
        for rule in self._rules:
            rule.apply(chunkstr)
            if verbose:
                print('#', rule.descr()+' ('+unicode_repr(rule)+'):')
            else:
                print('#', rule.descr()+':')
            print(chunkstr) 
Example 3
Project: razzy-spinner   Author: rafasashi   File: regexp.py    GNU General Public License v3.0 6 votes vote down vote up
def __str__(self):
        """
        :return: a verbose string representation of this ``RegexpChunkParser``.
        :rtype: str
        """
        s = "RegexpChunkParser with %d rules:\n" % len(self._rules)
        margin = 0
        for rule in self._rules:
            margin = max(margin, len(rule.descr()))
        if margin < 35:
            format = "    %" + repr(-(margin+3)) + "s%s\n"
        else:
            format = "    %s\n      %s\n"
        for rule in self._rules:
            s += format % (rule.descr(), unicode_repr(rule))
        return s[:-1]

##//////////////////////////////////////////////////////
##  Chunk Grammar
##////////////////////////////////////////////////////// 
Example 4
Project: razzy-spinner   Author: rafasashi   File: recursivedescent.py    GNU General Public License v3.0 6 votes vote down vote up
def _trace_fringe(self, tree, treeloc=None):
        """
        Print trace output displaying the fringe of ``tree``.  The
        fringe of ``tree`` consists of all of its leaves and all of
        its childless subtrees.

        :rtype: None
        """

        if treeloc == (): print("*", end=' ')
        if isinstance(tree, Tree):
            if len(tree) == 0:
                print(unicode_repr(Nonterminal(tree.label())), end=' ')
            for i in range(len(tree)):
                if treeloc is not None and i == treeloc[0]:
                    self._trace_fringe(tree[i], treeloc[1:])
                else:
                    self._trace_fringe(tree[i])
        else:
            print(unicode_repr(tree), end=' ') 
Example 5
Project: razzy-spinner   Author: rafasashi   File: rule.py    GNU General Public License v3.0 6 votes vote down vote up
def __repr__(self):
        # Cache the repr (justified by profiling -- this is used as
        # a sort key when deterministic=True.)
        try:
            return self.__repr
        except AttributeError:
            self.__repr = (
                "{0}('{1}', {2}, {3}, [{4}])".format(
                    self.__class__.__name__,
                    self.templateid,
                    unicode_repr(self.original_tag),
                    unicode_repr(self.replacement_tag),

                    # list(self._conditions) would be simpler but will not generate
                    # the same Rule.__repr__ in python 2 and 3 and thus break some tests
                    ', '.join("({0},{1})".format(f, unicode_repr(v)) for (f, v) in self._conditions)
                )
            )

            return self.__repr 
Example 6
Project: OpenBottle   Author: xiaozhuchacha   File: tree.py    MIT License 6 votes vote down vote up
def _pformat_flat(self, nodesep, parens, quotes):
        childstrs = []
        for child in self:
            if isinstance(child, Tree):
                childstrs.append(child._pformat_flat(nodesep, parens, quotes))
            elif isinstance(child, tuple):
                childstrs.append("/".join(child))
            elif isinstance(child, string_types) and not quotes:
                childstrs.append('%s' % child)
            else:
                childstrs.append(unicode_repr(child))
        if isinstance(self._label, string_types):
            return '%s%s%s %s%s' % (parens[0], self._label, nodesep,
                                    " ".join(childstrs), parens[1])
        else:
            return '%s%s%s %s%s' % (parens[0], unicode_repr(self._label), nodesep,
                                    " ".join(childstrs), parens[1]) 
Example 7
Project: OpenBottle   Author: xiaozhuchacha   File: regexp.py    MIT License 6 votes vote down vote up
def _trace_apply(self, chunkstr, verbose):
        """
        Apply each rule of this ``RegexpChunkParser`` to ``chunkstr``, in
        turn.  Generate trace output between each rule.  If ``verbose``
        is true, then generate verbose output.

        :type chunkstr: ChunkString
        :param chunkstr: The chunk string to which each rule should be
            applied.
        :type verbose: bool
        :param verbose: Whether output should be verbose.
        :rtype: None
        """
        print('# Input:')
        print(chunkstr)
        for rule in self._rules:
            rule.apply(chunkstr)
            if verbose:
                print('#', rule.descr()+' ('+unicode_repr(rule)+'):')
            else:
                print('#', rule.descr()+':')
            print(chunkstr) 
Example 8
Project: OpenBottle   Author: xiaozhuchacha   File: regexp.py    MIT License 6 votes vote down vote up
def __str__(self):
        """
        :return: a verbose string representation of this ``RegexpChunkParser``.
        :rtype: str
        """
        s = "RegexpChunkParser with %d rules:\n" % len(self._rules)
        margin = 0
        for rule in self._rules:
            margin = max(margin, len(rule.descr()))
        if margin < 35:
            format = "    %" + repr(-(margin+3)) + "s%s\n"
        else:
            format = "    %s\n      %s\n"
        for rule in self._rules:
            s += format % (rule.descr(), unicode_repr(rule))
        return s[:-1]

##//////////////////////////////////////////////////////
##  Chunk Grammar
##////////////////////////////////////////////////////// 
Example 9
Project: OpenBottle   Author: xiaozhuchacha   File: recursivedescent.py    MIT License 6 votes vote down vote up
def _trace_fringe(self, tree, treeloc=None):
        """
        Print trace output displaying the fringe of ``tree``.  The
        fringe of ``tree`` consists of all of its leaves and all of
        its childless subtrees.

        :rtype: None
        """

        if treeloc == (): print("*", end=' ')
        if isinstance(tree, Tree):
            if len(tree) == 0:
                print(unicode_repr(Nonterminal(tree.label())), end=' ')
            for i in range(len(tree)):
                if treeloc is not None and i == treeloc[0]:
                    self._trace_fringe(tree[i], treeloc[1:])
                else:
                    self._trace_fringe(tree[i])
        else:
            print(unicode_repr(tree), end=' ') 
Example 10
Project: OpenBottle   Author: xiaozhuchacha   File: rule.py    MIT License 6 votes vote down vote up
def __repr__(self):
        # Cache the repr (justified by profiling -- this is used as
        # a sort key when deterministic=True.)
        try:
            return self.__repr
        except AttributeError:
            self.__repr = (
                "{0}('{1}', {2}, {3}, [{4}])".format(
                    self.__class__.__name__,
                    self.templateid,
                    unicode_repr(self.original_tag),
                    unicode_repr(self.replacement_tag),

                    # list(self._conditions) would be simpler but will not generate
                    # the same Rule.__repr__ in python 2 and 3 and thus break some tests
                    ', '.join("({0},{1})".format(f, unicode_repr(v)) for (f, v) in self._conditions)
                )
            )

            return self.__repr 
Example 11
Project: OpenBottle   Author: xiaozhuchacha   File: tree.py    MIT License 6 votes vote down vote up
def _pformat_flat(self, nodesep, parens, quotes):
        childstrs = []
        for child in self:
            if isinstance(child, Tree):
                childstrs.append(child._pformat_flat(nodesep, parens, quotes))
            elif isinstance(child, tuple):
                childstrs.append("/".join(child))
            elif isinstance(child, string_types) and not quotes:
                childstrs.append('%s' % child)
            else:
                childstrs.append(unicode_repr(child))
        if isinstance(self._label, string_types):
            return '%s%s%s %s%s' % (parens[0], self._label, nodesep,
                                    " ".join(childstrs), parens[1])
        else:
            return '%s%s%s %s%s' % (parens[0], unicode_repr(self._label), nodesep,
                                    " ".join(childstrs), parens[1]) 
Example 12
Project: OpenBottle   Author: xiaozhuchacha   File: punkt.py    MIT License 6 votes vote down vote up
def __repr__(self):
        """
        A string representation of the token that can reproduce it
        with eval(), which lists all the token's non-default
        annotations.
        """
        typestr = (' type=%s,' % unicode_repr(self.type)
                   if self.type != self.tok else '')

        propvals = ', '.join(
            '%s=%s' % (p, unicode_repr(getattr(self, p)))
            for p in self._properties
            if getattr(self, p)
        )

        return '%s(%s,%s %s)' % (self.__class__.__name__,
            unicode_repr(self.tok), typestr, propvals) 
Example 13
Project: OpenBottle   Author: xiaozhuchacha   File: regexp.py    MIT License 6 votes vote down vote up
def __str__(self):
        """
        :return: a verbose string representation of this ``RegexpChunkParser``.
        :rtype: str
        """
        s = "RegexpChunkParser with %d rules:\n" % len(self._rules)
        margin = 0
        for rule in self._rules:
            margin = max(margin, len(rule.descr()))
        if margin < 35:
            format = "    %" + repr(-(margin+3)) + "s%s\n"
        else:
            format = "    %s\n      %s\n"
        for rule in self._rules:
            s += format % (rule.descr(), unicode_repr(rule))
        return s[:-1]

##//////////////////////////////////////////////////////
##  Chunk Grammar
##////////////////////////////////////////////////////// 
Example 14
Project: OpenBottle   Author: xiaozhuchacha   File: shiftreduce.py    MIT License 6 votes vote down vote up
def _trace_stack(self, stack, remaining_text, marker=' '):
        """
        Print trace output displaying the given stack and text.

        :rtype: None
        :param marker: A character that is printed to the left of the
            stack.  This is used with trace level 2 to print 'S'
            before shifted stacks and 'R' before reduced stacks.
        """
        s = '  '+marker+' [ '
        for elt in stack:
            if isinstance(elt, Tree):
                s += unicode_repr(Nonterminal(elt.label())) + ' '
            else:
                s += unicode_repr(elt) + ' '
        s += '* ' + ' '.join(remaining_text) + ']'
        print(s) 
Example 15
Project: OpenBottle   Author: xiaozhuchacha   File: recursivedescent.py    MIT License 6 votes vote down vote up
def _trace_fringe(self, tree, treeloc=None):
        """
        Print trace output displaying the fringe of ``tree``.  The
        fringe of ``tree`` consists of all of its leaves and all of
        its childless subtrees.

        :rtype: None
        """

        if treeloc == (): print("*", end=' ')
        if isinstance(tree, Tree):
            if len(tree) == 0:
                print(unicode_repr(Nonterminal(tree.label())), end=' ')
            for i in range(len(tree)):
                if treeloc is not None and i == treeloc[0]:
                    self._trace_fringe(tree[i], treeloc[1:])
                else:
                    self._trace_fringe(tree[i])
        else:
            print(unicode_repr(tree), end=' ') 
Example 16
Project: OpenBottle   Author: xiaozhuchacha   File: rule.py    MIT License 6 votes vote down vote up
def __repr__(self):
        # Cache the repr (justified by profiling -- this is used as
        # a sort key when deterministic=True.)
        try:
            return self.__repr
        except AttributeError:
            self.__repr = (
                "{0}('{1}', {2}, {3}, [{4}])".format(
                    self.__class__.__name__,
                    self.templateid,
                    unicode_repr(self.original_tag),
                    unicode_repr(self.replacement_tag),

                    # list(self._conditions) would be simpler but will not generate
                    # the same Rule.__repr__ in python 2 and 3 and thus break some tests
                    ', '.join("({0},{1})".format(f, unicode_repr(v)) for (f, v) in self._conditions)
                )
            )

            return self.__repr 
Example 17
Project: Health-Checker   Author: KriAga   File: tree.py    MIT License 6 votes vote down vote up
def _pformat_flat(self, nodesep, parens, quotes):
        childstrs = []
        for child in self:
            if isinstance(child, Tree):
                childstrs.append(child._pformat_flat(nodesep, parens, quotes))
            elif isinstance(child, tuple):
                childstrs.append("/".join(child))
            elif isinstance(child, string_types) and not quotes:
                childstrs.append('%s' % child)
            else:
                childstrs.append(unicode_repr(child))
        if isinstance(self._label, string_types):
            return '%s%s%s %s%s' % (parens[0], self._label, nodesep,
                                    " ".join(childstrs), parens[1])
        else:
            return '%s%s%s %s%s' % (parens[0], unicode_repr(self._label), nodesep,
                                    " ".join(childstrs), parens[1]) 
Example 18
Project: Health-Checker   Author: KriAga   File: regexp.py    MIT License 6 votes vote down vote up
def _trace_apply(self, chunkstr, verbose):
        """
        Apply each rule of this ``RegexpChunkParser`` to ``chunkstr``, in
        turn.  Generate trace output between each rule.  If ``verbose``
        is true, then generate verbose output.

        :type chunkstr: ChunkString
        :param chunkstr: The chunk string to which each rule should be
            applied.
        :type verbose: bool
        :param verbose: Whether output should be verbose.
        :rtype: None
        """
        print('# Input:')
        print(chunkstr)
        for rule in self._rules:
            rule.apply(chunkstr)
            if verbose:
                print('#', rule.descr()+' ('+unicode_repr(rule)+'):')
            else:
                print('#', rule.descr()+':')
            print(chunkstr) 
Example 19
Project: Health-Checker   Author: KriAga   File: regexp.py    MIT License 6 votes vote down vote up
def __str__(self):
        """
        :return: a verbose string representation of this ``RegexpChunkParser``.
        :rtype: str
        """
        s = "RegexpChunkParser with %d rules:\n" % len(self._rules)
        margin = 0
        for rule in self._rules:
            margin = max(margin, len(rule.descr()))
        if margin < 35:
            format = "    %" + repr(-(margin+3)) + "s%s\n"
        else:
            format = "    %s\n      %s\n"
        for rule in self._rules:
            s += format % (rule.descr(), unicode_repr(rule))
        return s[:-1]

##//////////////////////////////////////////////////////
##  Chunk Grammar
##////////////////////////////////////////////////////// 
Example 20
Project: Health-Checker   Author: KriAga   File: shiftreduce.py    MIT License 6 votes vote down vote up
def _trace_stack(self, stack, remaining_text, marker=' '):
        """
        Print trace output displaying the given stack and text.

        :rtype: None
        :param marker: A character that is printed to the left of the
            stack.  This is used with trace level 2 to print 'S'
            before shifted stacks and 'R' before reduced stacks.
        """
        s = '  '+marker+' [ '
        for elt in stack:
            if isinstance(elt, Tree):
                s += unicode_repr(Nonterminal(elt.label())) + ' '
            else:
                s += unicode_repr(elt) + ' '
        s += '* ' + ' '.join(remaining_text) + ']'
        print(s) 
Example 21
Project: Health-Checker   Author: KriAga   File: recursivedescent.py    MIT License 6 votes vote down vote up
def _trace_fringe(self, tree, treeloc=None):
        """
        Print trace output displaying the fringe of ``tree``.  The
        fringe of ``tree`` consists of all of its leaves and all of
        its childless subtrees.

        :rtype: None
        """

        if treeloc == (): print("*", end=' ')
        if isinstance(tree, Tree):
            if len(tree) == 0:
                print(unicode_repr(Nonterminal(tree.label())), end=' ')
            for i in range(len(tree)):
                if treeloc is not None and i == treeloc[0]:
                    self._trace_fringe(tree[i], treeloc[1:])
                else:
                    self._trace_fringe(tree[i])
        else:
            print(unicode_repr(tree), end=' ') 
Example 22
Project: rubric-sampling-public   Author: mhw32   File: tree.py    MIT License 6 votes vote down vote up
def _pformat_flat(self, nodesep, parens, quotes):
        childstrs = []
        for child in self:
            if isinstance(child, Tree):
                childstrs.append(child._pformat_flat(nodesep, parens, quotes))
            elif isinstance(child, tuple):
                childstrs.append("/".join(child))
            elif isinstance(child, string_types) and not quotes:
                childstrs.append('%s' % child)
            else:
                childstrs.append(unicode_repr(child))
        if isinstance(self._label, string_types):
            return '%s%s%s %s%s' % (parens[0], self._label, nodesep,
                                    " ".join(childstrs), parens[1])
        else:
            return '%s%s%s %s%s' % (parens[0], unicode_repr(self._label), nodesep,
                                    " ".join(childstrs), parens[1]) 
Example 23
Project: razzy-spinner   Author: rafasashi   File: tree.py    GNU General Public License v3.0 5 votes vote down vote up
def __repr__(self):
        childstr = ", ".join(unicode_repr(c) for c in self)
        return '%s(%s, [%s])' % (type(self).__name__, unicode_repr(self._label), childstr) 
Example 24
Project: razzy-spinner   Author: rafasashi   File: tree.py    GNU General Public License v3.0 5 votes vote down vote up
def pformat(self, margin=70, indent=0, nodesep='', parens='()', quotes=False):
        """
        :return: A pretty-printed string representation of this tree.
        :rtype: str
        :param margin: The right margin at which to do line-wrapping.
        :type margin: int
        :param indent: The indentation level at which printing
            begins.  This number is used to decide how far to indent
            subsequent lines.
        :type indent: int
        :param nodesep: A string that is used to separate the node
            from the children.  E.g., the default value ``':'`` gives
            trees like ``(S: (NP: I) (VP: (V: saw) (NP: it)))``.
        """

        # Try writing it on one line.
        s = self._pformat_flat(nodesep, parens, quotes)
        if len(s) + indent < margin:
            return s

        # If it doesn't fit on one line, then write it on multi-lines.
        if isinstance(self._label, string_types):
            s = '%s%s%s' % (parens[0], self._label, nodesep)
        else:
            s = '%s%s%s' % (parens[0], unicode_repr(self._label), nodesep)
        for child in self:
            if isinstance(child, Tree):
                s += '\n'+' '*(indent+2)+child.pformat(margin, indent+2,
                                                  nodesep, parens, quotes)
            elif isinstance(child, tuple):
                s += '\n'+' '*(indent+2)+ "/".join(child)
            elif isinstance(child, string_types) and not quotes:
                s += '\n'+' '*(indent+2)+ '%s' % child
            else:
                s += '\n'+' '*(indent+2)+ unicode_repr(child)
        return s+parens[1] 
Example 25
Project: razzy-spinner   Author: rafasashi   File: tree.py    GNU General Public License v3.0 5 votes vote down vote up
def __repr__(self):
        return '%s (p=%r)' % (Tree.unicode_repr(self), self.prob()) 
Example 26
Project: razzy-spinner   Author: rafasashi   File: tree.py    GNU General Public License v3.0 5 votes vote down vote up
def __repr__(self):
        return '%s [%s]' % (Tree.unicode_repr(self), self.prob()) 
Example 27
Project: razzy-spinner   Author: rafasashi   File: regexp.py    GNU General Public License v3.0 5 votes vote down vote up
def __repr__(self):
        """
        Return a string representation of this ``ChunkString``.
        It has the form::

            <ChunkString: '{<DT><JJ><NN>}<VBN><IN>{<DT><NN>}'>

        :rtype: str
        """
        return '<ChunkString: %s>' % unicode_repr(self._str) 
Example 28
Project: razzy-spinner   Author: rafasashi   File: regexp.py    GNU General Public License v3.0 5 votes vote down vote up
def __repr__(self):
        """
        Return a string representation of this rule.  It has the form::

            <RegexpChunkRule: '{<IN|VB.*>}'->'<IN>'>

        Note that this representation does not include the
        description string; that string can be accessed
        separately with the ``descr()`` method.

        :rtype: str
        """
        return ('<RegexpChunkRule: '+unicode_repr(self._regexp.pattern)+
                '->'+unicode_repr(self._repl)+'>') 
Example 29
Project: razzy-spinner   Author: rafasashi   File: regexp.py    GNU General Public License v3.0 5 votes vote down vote up
def __repr__(self):
        """
        Return a string representation of this rule.  It has the form::

            <ChunkRule: '<IN|VB.*>'>

        Note that this representation does not include the
        description string; that string can be accessed
        separately with the ``descr()`` method.

        :rtype: str
        """
        return '<ChunkRule: '+unicode_repr(self._pattern)+'>' 
Example 30
Project: razzy-spinner   Author: rafasashi   File: regexp.py    GNU General Public License v3.0 5 votes vote down vote up
def __repr__(self):
        """
        Return a string representation of this rule.  It has the form::

            <ChinkRule: '<IN|VB.*>'>

        Note that this representation does not include the
        description string; that string can be accessed
        separately with the ``descr()`` method.

        :rtype: str
        """
        return '<ChinkRule: '+unicode_repr(self._pattern)+'>' 
Example 31
Project: razzy-spinner   Author: rafasashi   File: regexp.py    GNU General Public License v3.0 5 votes vote down vote up
def __repr__(self):
        """
        Return a string representation of this rule.  It has the form::

            <UnChunkRule: '<IN|VB.*>'>

        Note that this representation does not include the
        description string; that string can be accessed
        separately with the ``descr()`` method.

        :rtype: str
        """
        return '<UnChunkRule: '+unicode_repr(self._pattern)+'>' 
Example 32
Project: razzy-spinner   Author: rafasashi   File: regexp.py    GNU General Public License v3.0 5 votes vote down vote up
def __repr__(self):
        """
        Return a string representation of this rule.  It has the form::

            <SplitRule: '<NN>', '<DT>'>

        Note that this representation does not include the
        description string; that string can be accessed
        separately with the ``descr()`` method.

       :rtype: str
        """
        return ('<SplitRule: '+unicode_repr(self._left_tag_pattern)+', '+
                unicode_repr(self._right_tag_pattern)+'>') 
Example 33
Project: razzy-spinner   Author: rafasashi   File: regexp.py    GNU General Public License v3.0 5 votes vote down vote up
def __repr__(self):
        """
        Return a string representation of this rule.  It has the form::

            <ExpandLeftRule: '<NN|DT|JJ>', '<NN|JJ>'>

        Note that this representation does not include the
        description string; that string can be accessed
        separately with the ``descr()`` method.

        :rtype: str
        """
        return ('<ExpandLeftRule: '+unicode_repr(self._left_tag_pattern)+', '+
                unicode_repr(self._right_tag_pattern)+'>') 
Example 34
Project: razzy-spinner   Author: rafasashi   File: regexp.py    GNU General Public License v3.0 5 votes vote down vote up
def __repr__(self):
        """
        Return a string representation of this rule.  It has the form::

            <ExpandRightRule: '<NN|DT|JJ>', '<NN|JJ>'>

        Note that this representation does not include the
        description string; that string can be accessed
        separately with the ``descr()`` method.

        :rtype: str
        """
        return ('<ExpandRightRule: '+unicode_repr(self._left_tag_pattern)+', '+
                unicode_repr(self._right_tag_pattern)+'>') 
Example 35
Project: OpenBottle   Author: xiaozhuchacha   File: tree.py    MIT License 5 votes vote down vote up
def __repr__(self):
        childstr = ", ".join(unicode_repr(c) for c in self)
        return '%s(%s, [%s])' % (type(self).__name__, unicode_repr(self._label), childstr) 
Example 36
Project: OpenBottle   Author: xiaozhuchacha   File: tree.py    MIT License 5 votes vote down vote up
def pformat(self, margin=70, indent=0, nodesep='', parens='()', quotes=False):
        """
        :return: A pretty-printed string representation of this tree.
        :rtype: str
        :param margin: The right margin at which to do line-wrapping.
        :type margin: int
        :param indent: The indentation level at which printing
            begins.  This number is used to decide how far to indent
            subsequent lines.
        :type indent: int
        :param nodesep: A string that is used to separate the node
            from the children.  E.g., the default value ``':'`` gives
            trees like ``(S: (NP: I) (VP: (V: saw) (NP: it)))``.
        """

        # Try writing it on one line.
        s = self._pformat_flat(nodesep, parens, quotes)
        if len(s) + indent < margin:
            return s

        # If it doesn't fit on one line, then write it on multi-lines.
        if isinstance(self._label, string_types):
            s = '%s%s%s' % (parens[0], self._label, nodesep)
        else:
            s = '%s%s%s' % (parens[0], unicode_repr(self._label), nodesep)
        for child in self:
            if isinstance(child, Tree):
                s += '\n'+' '*(indent+2)+child.pformat(margin, indent+2,
                                                  nodesep, parens, quotes)
            elif isinstance(child, tuple):
                s += '\n'+' '*(indent+2)+ "/".join(child)
            elif isinstance(child, string_types) and not quotes:
                s += '\n'+' '*(indent+2)+ '%s' % child
            else:
                s += '\n'+' '*(indent+2)+ unicode_repr(child)
        return s+parens[1] 
Example 37
Project: OpenBottle   Author: xiaozhuchacha   File: tree.py    MIT License 5 votes vote down vote up
def __repr__(self):
        return '%s (p=%r)' % (Tree.unicode_repr(self), self.prob()) 
Example 38
Project: OpenBottle   Author: xiaozhuchacha   File: tree.py    MIT License 5 votes vote down vote up
def __repr__(self):
        return '%s [%s]' % (Tree.unicode_repr(self), self.prob()) 
Example 39
Project: OpenBottle   Author: xiaozhuchacha   File: regexp.py    MIT License 5 votes vote down vote up
def __repr__(self):
        """
        Return a string representation of this ``ChunkString``.
        It has the form::

            <ChunkString: '{<DT><JJ><NN>}<VBN><IN>{<DT><NN>}'>

        :rtype: str
        """
        return '<ChunkString: %s>' % unicode_repr(self._str) 
Example 40
Project: OpenBottle   Author: xiaozhuchacha   File: regexp.py    MIT License 5 votes vote down vote up
def __repr__(self):
        """
        Return a string representation of this rule.  It has the form::

            <RegexpChunkRule: '{<IN|VB.*>}'->'<IN>'>

        Note that this representation does not include the
        description string; that string can be accessed
        separately with the ``descr()`` method.

        :rtype: str
        """
        return ('<RegexpChunkRule: '+unicode_repr(self._regexp.pattern)+
                '->'+unicode_repr(self._repl)+'>') 
Example 41
Project: OpenBottle   Author: xiaozhuchacha   File: regexp.py    MIT License 5 votes vote down vote up
def __repr__(self):
        """
        Return a string representation of this rule.  It has the form::

            <ChunkRule: '<IN|VB.*>'>

        Note that this representation does not include the
        description string; that string can be accessed
        separately with the ``descr()`` method.

        :rtype: str
        """
        return '<ChunkRule: '+unicode_repr(self._pattern)+'>' 
Example 42
Project: OpenBottle   Author: xiaozhuchacha   File: regexp.py    MIT License 5 votes vote down vote up
def __repr__(self):
        """
        Return a string representation of this rule.  It has the form::

            <ChinkRule: '<IN|VB.*>'>

        Note that this representation does not include the
        description string; that string can be accessed
        separately with the ``descr()`` method.

        :rtype: str
        """
        return '<ChinkRule: '+unicode_repr(self._pattern)+'>' 
Example 43
Project: OpenBottle   Author: xiaozhuchacha   File: regexp.py    MIT License 5 votes vote down vote up
def __repr__(self):
        """
        Return a string representation of this rule.  It has the form::

            <UnChunkRule: '<IN|VB.*>'>

        Note that this representation does not include the
        description string; that string can be accessed
        separately with the ``descr()`` method.

        :rtype: str
        """
        return '<UnChunkRule: '+unicode_repr(self._pattern)+'>' 
Example 44
Project: OpenBottle   Author: xiaozhuchacha   File: regexp.py    MIT License 5 votes vote down vote up
def __repr__(self):
        """
        Return a string representation of this rule.  It has the form::

            <SplitRule: '<NN>', '<DT>'>

        Note that this representation does not include the
        description string; that string can be accessed
        separately with the ``descr()`` method.

       :rtype: str
        """
        return ('<SplitRule: '+unicode_repr(self._left_tag_pattern)+', '+
                unicode_repr(self._right_tag_pattern)+'>') 
Example 45
Project: OpenBottle   Author: xiaozhuchacha   File: regexp.py    MIT License 5 votes vote down vote up
def __repr__(self):
        """
        Return a string representation of this rule.  It has the form::

            <ExpandLeftRule: '<NN|DT|JJ>', '<NN|JJ>'>

        Note that this representation does not include the
        description string; that string can be accessed
        separately with the ``descr()`` method.

        :rtype: str
        """
        return ('<ExpandLeftRule: '+unicode_repr(self._left_tag_pattern)+', '+
                unicode_repr(self._right_tag_pattern)+'>') 
Example 46
Project: OpenBottle   Author: xiaozhuchacha   File: regexp.py    MIT License 5 votes vote down vote up
def __repr__(self):
        """
        Return a string representation of this rule.  It has the form::

            <ExpandRightRule: '<NN|DT|JJ>', '<NN|JJ>'>

        Note that this representation does not include the
        description string; that string can be accessed
        separately with the ``descr()`` method.

        :rtype: str
        """
        return ('<ExpandRightRule: '+unicode_repr(self._left_tag_pattern)+', '+
                unicode_repr(self._right_tag_pattern)+'>') 
Example 47
Project: OpenBottle   Author: xiaozhuchacha   File: tree.py    MIT License 5 votes vote down vote up
def __repr__(self):
        childstr = ", ".join(unicode_repr(c) for c in self)
        return '%s(%s, [%s])' % (type(self).__name__, unicode_repr(self._label), childstr) 
Example 48
Project: OpenBottle   Author: xiaozhuchacha   File: tree.py    MIT License 5 votes vote down vote up
def pformat(self, margin=70, indent=0, nodesep='', parens='()', quotes=False):
        """
        :return: A pretty-printed string representation of this tree.
        :rtype: str
        :param margin: The right margin at which to do line-wrapping.
        :type margin: int
        :param indent: The indentation level at which printing
            begins.  This number is used to decide how far to indent
            subsequent lines.
        :type indent: int
        :param nodesep: A string that is used to separate the node
            from the children.  E.g., the default value ``':'`` gives
            trees like ``(S: (NP: I) (VP: (V: saw) (NP: it)))``.
        """

        # Try writing it on one line.
        s = self._pformat_flat(nodesep, parens, quotes)
        if len(s) + indent < margin:
            return s

        # If it doesn't fit on one line, then write it on multi-lines.
        if isinstance(self._label, string_types):
            s = '%s%s%s' % (parens[0], self._label, nodesep)
        else:
            s = '%s%s%s' % (parens[0], unicode_repr(self._label), nodesep)
        for child in self:
            if isinstance(child, Tree):
                s += '\n'+' '*(indent+2)+child.pformat(margin, indent+2,
                                                  nodesep, parens, quotes)
            elif isinstance(child, tuple):
                s += '\n'+' '*(indent+2)+ "/".join(child)
            elif isinstance(child, string_types) and not quotes:
                s += '\n'+' '*(indent+2)+ '%s' % child
            else:
                s += '\n'+' '*(indent+2)+ unicode_repr(child)
        return s+parens[1] 
Example 49
Project: OpenBottle   Author: xiaozhuchacha   File: tree.py    MIT License 5 votes vote down vote up
def __repr__(self):
        return '%s [%s]' % (Tree.unicode_repr(self), self.prob()) 
Example 50
Project: OpenBottle   Author: xiaozhuchacha   File: regexp.py    MIT License 5 votes vote down vote up
def __repr__(self):
        """
        Return a string representation of this ``ChunkString``.
        It has the form::

            <ChunkString: '{<DT><JJ><NN>}<VBN><IN>{<DT><NN>}'>

        :rtype: str
        """
        return '<ChunkString: %s>' % unicode_repr(self._str) 
Example 51
Project: OpenBottle   Author: xiaozhuchacha   File: regexp.py    MIT License 5 votes vote down vote up
def __repr__(self):
        """
        Return a string representation of this rule.  It has the form::

            <RegexpChunkRule: '{<IN|VB.*>}'->'<IN>'>

        Note that this representation does not include the
        description string; that string can be accessed
        separately with the ``descr()`` method.

        :rtype: str
        """
        return ('<RegexpChunkRule: '+unicode_repr(self._regexp.pattern)+
                '->'+unicode_repr(self._repl)+'>') 
Example 52
Project: OpenBottle   Author: xiaozhuchacha   File: regexp.py    MIT License 5 votes vote down vote up
def __repr__(self):
        """
        Return a string representation of this rule.  It has the form::

            <ChunkRule: '<IN|VB.*>'>

        Note that this representation does not include the
        description string; that string can be accessed
        separately with the ``descr()`` method.

        :rtype: str
        """
        return '<ChunkRule: '+unicode_repr(self._pattern)+'>' 
Example 53
Project: OpenBottle   Author: xiaozhuchacha   File: regexp.py    MIT License 5 votes vote down vote up
def __repr__(self):
        """
        Return a string representation of this rule.  It has the form::

            <UnChunkRule: '<IN|VB.*>'>

        Note that this representation does not include the
        description string; that string can be accessed
        separately with the ``descr()`` method.

        :rtype: str
        """
        return '<UnChunkRule: '+unicode_repr(self._pattern)+'>' 
Example 54
Project: OpenBottle   Author: xiaozhuchacha   File: regexp.py    MIT License 5 votes vote down vote up
def __repr__(self):
        """
        Return a string representation of this rule.  It has the form::

            <MergeRule: '<NN|DT|JJ>', '<NN|JJ>'>

        Note that this representation does not include the
        description string; that string can be accessed
        separately with the ``descr()`` method.

        :rtype: str
        """
        return ('<MergeRule: '+unicode_repr(self._left_tag_pattern)+', '+
                unicode_repr(self._right_tag_pattern)+'>') 
Example 55
Project: OpenBottle   Author: xiaozhuchacha   File: regexp.py    MIT License 5 votes vote down vote up
def __repr__(self):
        """
        Return a string representation of this rule.  It has the form::

            <SplitRule: '<NN>', '<DT>'>

        Note that this representation does not include the
        description string; that string can be accessed
        separately with the ``descr()`` method.

       :rtype: str
        """
        return ('<SplitRule: '+unicode_repr(self._left_tag_pattern)+', '+
                unicode_repr(self._right_tag_pattern)+'>') 
Example 56
Project: OpenBottle   Author: xiaozhuchacha   File: regexp.py    MIT License 5 votes vote down vote up
def __repr__(self):
        """
        Return a string representation of this rule.  It has the form::

            <ExpandLeftRule: '<NN|DT|JJ>', '<NN|JJ>'>

        Note that this representation does not include the
        description string; that string can be accessed
        separately with the ``descr()`` method.

        :rtype: str
        """
        return ('<ExpandLeftRule: '+unicode_repr(self._left_tag_pattern)+', '+
                unicode_repr(self._right_tag_pattern)+'>') 
Example 57
Project: OpenBottle   Author: xiaozhuchacha   File: regexp.py    MIT License 5 votes vote down vote up
def __repr__(self):
        """
        Return a string representation of this rule.  It has the form::

            <ExpandRightRule: '<NN|DT|JJ>', '<NN|JJ>'>

        Note that this representation does not include the
        description string; that string can be accessed
        separately with the ``descr()`` method.

        :rtype: str
        """
        return ('<ExpandRightRule: '+unicode_repr(self._left_tag_pattern)+', '+
                unicode_repr(self._right_tag_pattern)+'>') 
Example 58
Project: Health-Checker   Author: KriAga   File: tree.py    MIT License 5 votes vote down vote up
def __repr__(self):
        childstr = ", ".join(unicode_repr(c) for c in self)
        return '%s(%s, [%s])' % (type(self).__name__, unicode_repr(self._label), childstr) 
Example 59
Project: Health-Checker   Author: KriAga   File: tree.py    MIT License 5 votes vote down vote up
def pformat(self, margin=70, indent=0, nodesep='', parens='()', quotes=False):
        """
        :return: A pretty-printed string representation of this tree.
        :rtype: str
        :param margin: The right margin at which to do line-wrapping.
        :type margin: int
        :param indent: The indentation level at which printing
            begins.  This number is used to decide how far to indent
            subsequent lines.
        :type indent: int
        :param nodesep: A string that is used to separate the node
            from the children.  E.g., the default value ``':'`` gives
            trees like ``(S: (NP: I) (VP: (V: saw) (NP: it)))``.
        """

        # Try writing it on one line.
        s = self._pformat_flat(nodesep, parens, quotes)
        if len(s) + indent < margin:
            return s

        # If it doesn't fit on one line, then write it on multi-lines.
        if isinstance(self._label, string_types):
            s = '%s%s%s' % (parens[0], self._label, nodesep)
        else:
            s = '%s%s%s' % (parens[0], unicode_repr(self._label), nodesep)
        for child in self:
            if isinstance(child, Tree):
                s += '\n'+' '*(indent+2)+child.pformat(margin, indent+2,
                                                  nodesep, parens, quotes)
            elif isinstance(child, tuple):
                s += '\n'+' '*(indent+2)+ "/".join(child)
            elif isinstance(child, string_types) and not quotes:
                s += '\n'+' '*(indent+2)+ '%s' % child
            else:
                s += '\n'+' '*(indent+2)+ unicode_repr(child)
        return s+parens[1] 
Example 60
Project: Health-Checker   Author: KriAga   File: tree.py    MIT License 5 votes vote down vote up
def __repr__(self):
        return '%s (p=%r)' % (Tree.unicode_repr(self), self.prob()) 
Example 61
Project: Health-Checker   Author: KriAga   File: tree.py    MIT License 5 votes vote down vote up
def __repr__(self):
        return '%s [%s]' % (Tree.unicode_repr(self), self.prob()) 
Example 62
Project: Health-Checker   Author: KriAga   File: regexp.py    MIT License 5 votes vote down vote up
def __repr__(self):
        """
        Return a string representation of this ``ChunkString``.
        It has the form::

            <ChunkString: '{<DT><JJ><NN>}<VBN><IN>{<DT><NN>}'>

        :rtype: str
        """
        return '<ChunkString: %s>' % unicode_repr(self._str) 
Example 63
Project: Health-Checker   Author: KriAga   File: regexp.py    MIT License 5 votes vote down vote up
def __repr__(self):
        """
        Return a string representation of this rule.  It has the form::

            <RegexpChunkRule: '{<IN|VB.*>}'->'<IN>'>

        Note that this representation does not include the
        description string; that string can be accessed
        separately with the ``descr()`` method.

        :rtype: str
        """
        return ('<RegexpChunkRule: '+unicode_repr(self._regexp.pattern)+
                '->'+unicode_repr(self._repl)+'>') 
Example 64
Project: Health-Checker   Author: KriAga   File: regexp.py    MIT License 5 votes vote down vote up
def __repr__(self):
        """
        Return a string representation of this rule.  It has the form::

            <ChunkRule: '<IN|VB.*>'>

        Note that this representation does not include the
        description string; that string can be accessed
        separately with the ``descr()`` method.

        :rtype: str
        """
        return '<ChunkRule: '+unicode_repr(self._pattern)+'>' 
Example 65
Project: Health-Checker   Author: KriAga   File: regexp.py    MIT License 5 votes vote down vote up
def __repr__(self):
        """
        Return a string representation of this rule.  It has the form::

            <ChinkRule: '<IN|VB.*>'>

        Note that this representation does not include the
        description string; that string can be accessed
        separately with the ``descr()`` method.

        :rtype: str
        """
        return '<ChinkRule: '+unicode_repr(self._pattern)+'>' 
Example 66
Project: Health-Checker   Author: KriAga   File: regexp.py    MIT License 5 votes vote down vote up
def __repr__(self):
        """
        Return a string representation of this rule.  It has the form::

            <UnChunkRule: '<IN|VB.*>'>

        Note that this representation does not include the
        description string; that string can be accessed
        separately with the ``descr()`` method.

        :rtype: str
        """
        return '<UnChunkRule: '+unicode_repr(self._pattern)+'>' 
Example 67
Project: Health-Checker   Author: KriAga   File: regexp.py    MIT License 5 votes vote down vote up
def __repr__(self):
        """
        Return a string representation of this rule.  It has the form::

            <SplitRule: '<NN>', '<DT>'>

        Note that this representation does not include the
        description string; that string can be accessed
        separately with the ``descr()`` method.

       :rtype: str
        """
        return ('<SplitRule: '+unicode_repr(self._left_tag_pattern)+', '+
                unicode_repr(self._right_tag_pattern)+'>') 
Example 68
Project: Health-Checker   Author: KriAga   File: regexp.py    MIT License 5 votes vote down vote up
def __repr__(self):
        """
        Return a string representation of this rule.  It has the form::

            <ExpandLeftRule: '<NN|DT|JJ>', '<NN|JJ>'>

        Note that this representation does not include the
        description string; that string can be accessed
        separately with the ``descr()`` method.

        :rtype: str
        """
        return ('<ExpandLeftRule: '+unicode_repr(self._left_tag_pattern)+', '+
                unicode_repr(self._right_tag_pattern)+'>') 
Example 69
Project: Health-Checker   Author: KriAga   File: regexp.py    MIT License 5 votes vote down vote up
def __repr__(self):
        """
        Return a string representation of this rule.  It has the form::

            <ExpandRightRule: '<NN|DT|JJ>', '<NN|JJ>'>

        Note that this representation does not include the
        description string; that string can be accessed
        separately with the ``descr()`` method.

        :rtype: str
        """
        return ('<ExpandRightRule: '+unicode_repr(self._left_tag_pattern)+', '+
                unicode_repr(self._right_tag_pattern)+'>') 
Example 70
Project: Health-Checker   Author: KriAga   File: chart.py    MIT License 5 votes vote down vote up
def __str__(self):
        return '[%s:%s] %s' % (self._index, self._index+1, unicode_repr(self._leaf)) 
Example 71
Project: Health-Checker   Author: KriAga   File: featstruct.py    MIT License 5 votes vote down vote up
def _repr(self, reentrances, reentrance_ids):
        # If this is the first time we've seen a reentrant structure,
        # then assign it a unique identifier.
        if reentrances[id(self)]:
            assert id(self) not in reentrance_ids
            reentrance_ids[id(self)] = repr(len(reentrance_ids) + 1)
            prefix = '(%s)' % reentrance_ids[id(self)]
        else:
            prefix = ''

        segments = []
        for fval in self:
            if id(fval) in reentrance_ids:
                segments.append('->(%s)' % reentrance_ids[id(fval)])
            elif isinstance(fval, Variable):
                segments.append(fval.name)
            elif isinstance(fval, Expression):
                segments.append('%s' % fval)
            elif isinstance(fval, FeatStruct):
                segments.append(fval._repr(reentrances, reentrance_ids))
            else:
                segments.append('%s' % unicode_repr(fval))

        return '%s[%s]' % (prefix, ', '.join(segments))


######################################################################
# Variables & Bindings
###################################################################### 
Example 72
Project: Health-Checker   Author: KriAga   File: featstruct.py    MIT License 5 votes vote down vote up
def _trace_unify_succeed(path, fval1):
    # Print the result.
    print('  ' + '|   ' * len(path) + '|')
    print('  ' + '|   ' * len(path) + '+-->' + unicode_repr(fval1)) 
Example 73
Project: Health-Checker   Author: KriAga   File: featstruct.py    MIT License 5 votes vote down vote up
def _trace_valrepr(val):
    if isinstance(val, Variable):
        return '%s' % val
    else:
        return '%s' % unicode_repr(val) 
Example 74
Project: Health-Checker   Author: KriAga   File: grammar.py    MIT License 5 votes vote down vote up
def __repr__(self):
        """
        Return a string representation for this ``Nonterminal``.

        :rtype: str
        """
        if isinstance(self._symbol, string_types):
            return '%s' % self._symbol
        else:
            return '%s' % unicode_repr(self._symbol) 
Example 75
Project: Health-Checker   Author: KriAga   File: grammar.py    MIT License 5 votes vote down vote up
def __str__(self):
        """
        Return a string representation for this ``Nonterminal``.

        :rtype: str
        """
        if isinstance(self._symbol, string_types):
            return '%s' % self._symbol
        else:
            return '%s' % unicode_repr(self._symbol) 
Example 76
Project: Health-Checker   Author: KriAga   File: grammar.py    MIT License 5 votes vote down vote up
def __str__(self):
        """
        Return a verbose string representation of the ``Production``.

        :rtype: str
        """
        result = '%s -> ' % unicode_repr(self._lhs)
        result += " ".join(unicode_repr(el) for el in self._rhs)
        return result 
Example 77
Project: rubric-sampling-public   Author: mhw32   File: tree.py    MIT License 5 votes vote down vote up
def __repr__(self):
        childstr = ", ".join(unicode_repr(c) for c in self)
        return '%s(%s, [%s])' % (type(self).__name__, unicode_repr(self._label), childstr) 
Example 78
Project: rubric-sampling-public   Author: mhw32   File: tree.py    MIT License 5 votes vote down vote up
def pformat(self, margin=70, indent=0, nodesep='', parens='()', quotes=False):
        """
        :return: A pretty-printed string representation of this tree.
        :rtype: str
        :param margin: The right margin at which to do line-wrapping.
        :type margin: int
        :param indent: The indentation level at which printing
            begins.  This number is used to decide how far to indent
            subsequent lines.
        :type indent: int
        :param nodesep: A string that is used to separate the node
            from the children.  E.g., the default value ``':'`` gives
            trees like ``(S: (NP: I) (VP: (V: saw) (NP: it)))``.
        """

        # Try writing it on one line.
        s = self._pformat_flat(nodesep, parens, quotes)
        if len(s) + indent < margin:
            return s

        # If it doesn't fit on one line, then write it on multi-lines.
        if isinstance(self._label, string_types):
            s = '%s%s%s' % (parens[0], self._label, nodesep)
        else:
            s = '%s%s%s' % (parens[0], unicode_repr(self._label), nodesep)
        for child in self:
            if isinstance(child, Tree):
                s += '\n'+' '*(indent+2)+child.pformat(margin, indent+2,
                                                  nodesep, parens, quotes)
            elif isinstance(child, tuple):
                s += '\n'+' '*(indent+2)+ "/".join(child)
            elif isinstance(child, string_types) and not quotes:
                s += '\n'+' '*(indent+2)+ '%s' % child
            else:
                s += '\n'+' '*(indent+2)+ unicode_repr(child)
        return s+parens[1] 
Example 79
Project: rubric-sampling-public   Author: mhw32   File: tree.py    MIT License 5 votes vote down vote up
def __repr__(self):
        return '%s (p=%r)' % (Tree.unicode_repr(self), self.prob()) 
Example 80
Project: Health-Checker   Author: KriAga   File: featstruct.py    MIT License 4 votes vote down vote up
def _repr(self, reentrances, reentrance_ids):
        segments = []
        prefix = ''
        suffix = ''

        # If this is the first time we've seen a reentrant structure,
        # then assign it a unique identifier.
        if reentrances[id(self)]:
            assert id(self) not in reentrance_ids
            reentrance_ids[id(self)] = repr(len(reentrance_ids) + 1)

        # sorting note: keys are unique strings, so we'll never fall
        # through to comparing values.
        for (fname, fval) in sorted(self.items()):
            display = getattr(fname, 'display', None)
            if id(fval) in reentrance_ids:
                segments.append('%s->(%s)' %
                                (fname, reentrance_ids[id(fval)]))
            elif (display == 'prefix' and not prefix and
                  isinstance(fval, (Variable, string_types))):
                prefix = '%s' % fval
            elif display == 'slash' and not suffix:
                if isinstance(fval, Variable):
                    suffix = '/%s' % fval.name
                else:
                    suffix = '/%s' % unicode_repr(fval)
            elif isinstance(fval, Variable):
                segments.append('%s=%s' % (fname, fval.name))
            elif fval is True:
                segments.append('+%s' % fname)
            elif fval is False:
                segments.append('-%s' % fname)
            elif isinstance(fval, Expression):
                segments.append('%s=<%s>' % (fname, fval))
            elif not isinstance(fval, FeatStruct):
                segments.append('%s=%s' % (fname, unicode_repr(fval)))
            else:
                fval_repr = fval._repr(reentrances, reentrance_ids)
                segments.append('%s=%s' % (fname, fval_repr))
        # If it's reentrant, then add on an identifier tag.
        if reentrances[id(self)]:
            prefix = '(%s)%s' % (reentrance_ids[id(self)], prefix)
        return '%s[%s]%s' % (prefix, ', '.join(segments), suffix)