
`]c           @  s  d  Z  d d l m Z m Z d d l Z d d l Z d d l m Z m Z d d l	 m
 Z
 m Z d d l m Z m Z d d l m Z d d l m Z d d	 l m Z m Z d d
 l m Z e d e f d     Y Z d e f d     YZ e e  d e f d     Y Z d e f d     YZ d e f d     YZ d e e f d     YZ d e e f d     YZ e d e e f d     Y Z  e d e e f d     Y Z! d   Z" d   Z# d   Z$ d    Z% d! d" d# d$ d% d& d' d( d) d* d+ g Z& d S(,   uh   
Class for representing hierarchical language structures, such as
syntax trees and morphological trees.
i(   t   print_functiont   unicode_literalsN(   t   ABCMetat   abstractmethod(   t   string_typest   add_metaclass(   t
   Productiont   Nonterminal(   t   ProbabilisticMixIn(   t   slice_bounds(   t   python_2_unicode_compatiblet   unicode_repr(   t   raise_unorderable_typest   Treec           B  s3  e  Z d  Z d9 d  Z d   Z d   Z d   Z d   Z d   Z	 d   Z
 d   Z d	   Z d
   Z d   Z d   Z d   Z d   Z d   Z d   Z e e e  Z d   Z d   Z d   Z d   Z d   Z d d  Z d9 d  Z d   Z d   Z d   Z d   Z  d d9 d d d  d!  Z! e" d d  d" d#  Z# e$ e$ d" d$  Z% e& d%    Z' d&   Z( d'   Z) e$ d(  Z* d)   Z+ d9 d*  Z, e& d+ d9 d9 d9 d9 e$ d,   Z- e& d-    Z. d.   Z/ d9 d: d9 d/  Z0 d0   Z1 d1   Z2 d2   Z3 d3   Z4 d4 d d5 d+ e$ d6  Z5 d7   Z6 d8   Z7 RS(;   ui  
    A Tree represents a hierarchical grouping of leaves and subtrees.
    For example, each constituent in a syntax tree is represented by a single Tree.

    A tree's children are encoded as a list of leaves and subtrees,
    where a leaf is a basic (non-tree) value; and a subtree is a
    nested Tree.

        >>> from nltk.tree import Tree
        >>> print(Tree(1, [2, Tree(3, [4]), 5]))
        (1 2 (3 4) 5)
        >>> vp = Tree('VP', [Tree('V', ['saw']),
        ...                  Tree('NP', ['him'])])
        >>> s = Tree('S', [Tree('NP', ['I']), vp])
        >>> print(s)
        (S (NP I) (VP (V saw) (NP him)))
        >>> print(s[1])
        (VP (V saw) (NP him))
        >>> print(s[1,1])
        (NP him)
        >>> t = Tree.fromstring("(S (NP I) (VP (V saw) (NP him)))")
        >>> s == t
        True
        >>> t[1][1].set_label('X')
        >>> t[1][1].label()
        'X'
        >>> print(t)
        (S (NP I) (VP (V saw) (X him)))
        >>> t[0], t[1,1] = t[1,1], t[0]
        >>> print(t)
        (S (X him) (VP (V saw) (NP I)))

    The length of a tree is the number of children it has.

        >>> len(t)
        2

    The set_label() and label() methods allow individual constituents
    to be labeled.  For example, syntax trees use this label to specify
    phrase tags, such as "NP" and "VP".

    Several Tree methods use "tree positions" to specify
    children or descendants of a tree.  Tree positions are defined as
    follows:

      - The tree position *i* specifies a Tree's *i*\ th child.
      - The tree position ``()`` specifies the Tree itself.
      - If *p* is the tree position of descendant *d*, then
        *p+i* specifies the *i*\ th child of *d*.

    I.e., every tree position is either a single index *i*,
    specifying ``tree[i]``; or a sequence *i1, i2, ..., iN*,
    specifying ``tree[i1][i2]...[iN]``.

    Construct a new tree.  This constructor can be called in one
    of two ways:

    - ``Tree(label, children)`` constructs a new tree with the
        specified label and list of children.

    - ``Tree.fromstring(s)`` constructs a new tree by parsing the string ``s``.
    c         C  sp   | d  k r( t d t |   j   nD t | t  rS t d t |   j   n t j |  |  | |  _ d  S(   Nu)   %s: Expected a node value and child list u.   %s() argument 2 should be a list, not a string(	   t   Nonet	   TypeErrort   typet   __name__t
   isinstanceR   t   listt   __init__t   _label(   t   selft   nodet   children(    (    s(   lib/python2.7/site-packages/nltk/tree.pyR   f   s    c         C  s:   |  j  | j  k o9 |  j t |   f | j t |  f k S(   N(   t	   __class__R   R   (   R   t   other(    (    s(   lib/python2.7/site-packages/nltk/tree.pyt   __eq__x   s    $c         C  sy   t  | t  s% |  j j | j j k  S|  j | j k r_ |  j t |   f | j t |  f k  S|  j j | j j k  Sd  S(   N(   R   R   R   R   R   R   (   R   R   (    (    s(   lib/python2.7/site-packages/nltk/tree.pyt   __lt__~   s
    (c         C  s   |  | k S(   N(    (   R   R   (    (    s(   lib/python2.7/site-packages/nltk/tree.pyt   <lambda>   t    c         C  s   |  | k  p |  | k S(   N(    (   R   R   (    (    s(   lib/python2.7/site-packages/nltk/tree.pyR      R   c         C  s   |  | k  p |  | k S(   N(    (   R   R   (    (    s(   lib/python2.7/site-packages/nltk/tree.pyR      R   c         C  s   |  | k  S(   N(    (   R   R   (    (    s(   lib/python2.7/site-packages/nltk/tree.pyR      R   c         C  s   t  d   d  S(   Nu$   Tree does not support multiplication(   R   (   R   t   v(    (    s(   lib/python2.7/site-packages/nltk/tree.pyt   __mul__   s    c         C  s   t  d   d  S(   Nu$   Tree does not support multiplication(   R   (   R   R   (    (    s(   lib/python2.7/site-packages/nltk/tree.pyt   __rmul__   s    c         C  s   t  d   d  S(   Nu   Tree does not support addition(   R   (   R   R   (    (    s(   lib/python2.7/site-packages/nltk/tree.pyt   __add__   s    c         C  s   t  d   d  S(   Nu   Tree does not support addition(   R   (   R   R   (    (    s(   lib/python2.7/site-packages/nltk/tree.pyt   __radd__   s    c         C  s   t  | t t f  r% t j |  |  St  | t t f  r t |  d k rP |  St |  d k rn |  | d S|  | d | d Sn( t d t |   j	 t |  j	 f   d  S(   Ni    i   u#   %s indices must be integers, not %s(
   R   t   intt   sliceR   t   __getitem__t   tuplet   lenR   R   R   (   R   t   index(    (    s(   lib/python2.7/site-packages/nltk/tree.pyR&      s    c         C  s   t  | t t f  r( t j |  | |  St  | t t f  r t |  d k r^ t d   q t |  d k r | |  | d <q | |  | d | d <n( t d t	 |   j
 t	 |  j
 f   d  S(   Ni    u,   The tree position () may not be assigned to.i   u#   %s indices must be integers, not %s(   R   R$   R%   R   t   __setitem__R'   R(   t
   IndexErrorR   R   R   (   R   R)   t   value(    (    s(   lib/python2.7/site-packages/nltk/tree.pyR*      s    c         C  s   t  | t t f  r% t j |  |  St  | t t f  r t |  d k r[ t d   q t |  d k r{ |  | d =q |  | d | d =n( t d t	 |   j
 t	 |  j
 f   d  S(   Ni    u(   The tree position () may not be deleted.i   u#   %s indices must be integers, not %s(   R   R$   R%   R   t   __delitem__R'   R(   R+   R   R   R   (   R   R)   (    (    s(   lib/python2.7/site-packages/nltk/tree.pyR-      s    c         C  s   t  d   d S(   uI   Outdated method to access the node value; use the label() method instead.u#   Use label() to access a node label.N(   t   NotImplementedError(   R   (    (    s(   lib/python2.7/site-packages/nltk/tree.pyt	   _get_node   s    c         C  s   t  d   d S(   uJ   Outdated method to set the node value; use the set_label() method instead.u+   Use set_label() method to set a node label.N(   R.   (   R   R,   (    (    s(   lib/python2.7/site-packages/nltk/tree.pyt	   _set_node   s    c         C  s   |  j  S(   u  
        Return the node label of the tree.

            >>> t = Tree.fromstring('(S (NP (D the) (N dog)) (VP (V chased) (NP (D the) (N cat))))')
            >>> t.label()
            'S'

        :return: the node label (typically a string)
        :rtype: any
        (   R   (   R   (    (    s(   lib/python2.7/site-packages/nltk/tree.pyt   label   s    c         C  s   | |  _  d S(   uo  
        Set the node label of the tree.

            >>> t = Tree.fromstring("(S (NP (D the) (N dog)) (VP (V chased) (NP (D the) (N cat))))")
            >>> t.set_label("T")
            >>> print(t)
            (T (NP (D the) (N dog)) (VP (V chased) (NP (D the) (N cat))))

        :param label: the node label (typically a string)
        :type label: any
        N(   R   (   R   R1   (    (    s(   lib/python2.7/site-packages/nltk/tree.pyt	   set_label   s    c         C  sM   g  } x@ |  D]8 } t  | t  r8 | j | j    q | j |  q W| S(   u  
        Return the leaves of the tree.

            >>> t = Tree.fromstring("(S (NP (D the) (N dog)) (VP (V chased) (NP (D the) (N cat))))")
            >>> t.leaves()
            ['the', 'dog', 'chased', 'the', 'cat']

        :return: a list containing this tree's leaves.
            The order reflects the order of the
            leaves in the tree's hierarchical structure.
        :rtype: list
        (   R   R   t   extendt   leavest   append(   R   R4   t   child(    (    s(   lib/python2.7/site-packages/nltk/tree.pyR4      s    c         C  s   t  |  j   |  j    S(   u  
        Return a flat version of the tree, with all non-root non-terminals removed.

            >>> t = Tree.fromstring("(S (NP (D the) (N dog)) (VP (V chased) (NP (D the) (N cat))))")
            >>> print(t.flatten())
            (S the dog chased the cat)

        :return: a tree consisting of this tree's root connected directly to
            its leaves, omitting all intervening non-terminal nodes.
        :rtype: Tree
        (   R   R1   R4   (   R   (    (    s(   lib/python2.7/site-packages/nltk/tree.pyt   flatten  s    c         C  sU   d } xD |  D]< } t  | t  r: t | | j    } q t | d  } q Wd | S(   uG  
        Return the height of the tree.

            >>> t = Tree.fromstring("(S (NP (D the) (N dog)) (VP (V chased) (NP (D the) (N cat))))")
            >>> t.height()
            5
            >>> print(t[0,0])
            (D the)
            >>> t[0,0].height()
            2

        :return: The height of this tree.  The height of a tree
            containing no children is 1; the height of a tree
            containing only leaves is 2; and the height of any other
            tree is one plus the maximum of its children's
            heights.
        :rtype: int
        i    i   (   R   R   t   maxt   height(   R   t   max_child_heightR6   (    (    s(   lib/python2.7/site-packages/nltk/tree.pyR9     s    u   preorderc           s   g  } | d k r" | j  d  n  xh t |   D]Z \   } t | t  ry | j |  } | j   f d   | D  q/ | j    f  q/ W| d k r | j  d  n  | S(	   u.  
            >>> t = Tree.fromstring("(S (NP (D the) (N dog)) (VP (V chased) (NP (D the) (N cat))))")
            >>> t.treepositions() # doctest: +ELLIPSIS
            [(), (0,), (0, 0), (0, 0, 0), (0, 1), (0, 1, 0), (1,), (1, 0), (1, 0, 0), ...]
            >>> for pos in t.treepositions('leaves'):
            ...     t[pos] = t[pos][::-1].upper()
            >>> print(t)
            (S (NP (D EHT) (N GOD)) (VP (V DESAHC) (NP (D EHT) (N TAC))))

        :param order: One of: ``preorder``, ``postorder``, ``bothorder``,
            ``leaves``.
        u   preorderu	   bothorderc         3  s   |  ] }   f | Vq d  S(   N(    (   t   .0t   p(   t   i(    s(   lib/python2.7/site-packages/nltk/tree.pys	   <genexpr>M  s    u	   postorder(   u   preorderu	   bothorder(    (   u	   postorderu	   bothorder(    (   R5   t	   enumerateR   R   t   treepositionsR3   (   R   t   ordert	   positionsR6   t   childpos(    (   R=   s(   lib/python2.7/site-packages/nltk/tree.pyR?   :  s     c         c  sa   | s | |   r |  Vn  x? |  D]7 } t  | t  r" x | j |  D] } | VqG Wq" q" Wd S(   u  
        Generate all the subtrees of this tree, optionally restricted
        to trees matching the filter function.

            >>> t = Tree.fromstring("(S (NP (D the) (N dog)) (VP (V chased) (NP (D the) (N cat))))")
            >>> for s in t.subtrees(lambda t: t.height() == 2):
            ...     print(s)
            (D the)
            (N dog)
            (V chased)
            (D the)
            (N cat)

        :type filter: function
        :param filter: the function to filter all local trees
        N(   R   R   t   subtrees(   R   t   filterR6   t   subtree(    (    s(   lib/python2.7/site-packages/nltk/tree.pyRC   T  s    c         C  sy   t  |  j t  s! t d   n  t t |  j  t |    g } x0 |  D]( } t  | t  rI | | j   7} qI qI W| S(   u  
        Generate the productions that correspond to the non-terminal nodes of the tree.
        For each subtree of the form (P: C1 C2 ... Cn) this produces a production of the
        form P -> C1 C2 ... Cn.

            >>> t = Tree.fromstring("(S (NP (D the) (N dog)) (VP (V chased) (NP (D the) (N cat))))")
            >>> t.productions()
            [S -> NP VP, NP -> D N, D -> 'the', N -> 'dog', VP -> V NP, V -> 'chased',
            NP -> D N, D -> 'the', N -> 'cat']

        :rtype: list(Production)
        uP   Productions can only be generated from trees having node labels that are strings(	   R   R   R   R   R   R   t   _child_namesR   t   productions(   R   t   prodsR6   (    (    s(   lib/python2.7/site-packages/nltk/tree.pyRG   l  s    !c         C  sV   g  } xI |  D]A } t  | t  r8 | j | j    q | j | |  j f  q W| S(   u  
        Return a sequence of pos-tagged words extracted from the tree.

            >>> t = Tree.fromstring("(S (NP (D the) (N dog)) (VP (V chased) (NP (D the) (N cat))))")
            >>> t.pos()
            [('the', 'D'), ('dog', 'N'), ('chased', 'V'), ('the', 'D'), ('cat', 'N')]

        :return: a list of tuples containing leaves and pre-terminals (part-of-speech tags).
            The order reflects the order of the leaves in the tree's hierarchical structure.
        :rtype: list(tuple)
        (   R   R   R3   t   posR5   R   (   R   RI   R6   (    (    s(   lib/python2.7/site-packages/nltk/tree.pyRI     s    c         C  s   | d k  r t  d   n  |  d f g } x | r | j   \ } } t | t  sq | d k rd | S| d 8} q- xB t t |  d d d  D]$ } | j | | | | f f  q Wq- Wt  d   d S(   u,  
        :return: The tree position of the ``index``-th leaf in this
            tree.  I.e., if ``tp=self.leaf_treeposition(i)``, then
            ``self[tp]==self.leaves()[i]``.

        :raise IndexError: If this tree contains fewer than ``index+1``
            leaves, or if ``index<0``.
        i    u   index must be non-negativei   iu-   index must be less than or equal to len(self)N(    (   R+   t   popR   R   t   rangeR(   R5   (   R   R)   t   stackR,   t   treeposR=   (    (    s(   lib/python2.7/site-packages/nltk/tree.pyt   leaf_treeposition  s    		#&c         C  s   | | k r t  d   n  |  j |  } |  j | d  } xH t t |   D]4 } | t |  k s| | | | | k rP | |  SqP W| S(   u   
        :return: The tree position of the lowest descendant of this
            tree that dominates ``self.leaves()[start:end]``.
        :raise ValueError: if ``end <= start``
        u   end must be greater than starti   (   t
   ValueErrorRN   RK   R(   (   R   t   startt   endt   start_treepost   end_treeposR=   (    (    s(   lib/python2.7/site-packages/nltk/tree.pyt   treeposition_spanning_leaves  s    &u   righti    u   |u   ^c         C  s-   d d l  m } | |  | | | | |  d S(   u  
        This method can modify a tree in three ways:

          1. Convert a tree into its Chomsky Normal Form (CNF)
             equivalent -- Every subtree has either two non-terminals
             or one terminal as its children.  This process requires
             the creation of more"artificial" non-terminal nodes.
          2. Markov (vertical) smoothing of children in new artificial
             nodes
          3. Horizontal (parent) annotation of nodes

        :param factor: Right or left factoring method (default = "right")
        :type  factor: str = [left|right]
        :param horzMarkov: Markov order for sibling smoothing in artificial nodes (None (default) = include all siblings)
        :type  horzMarkov: int | None
        :param vertMarkov: Markov order for parent smoothing (0 (default) = no vertical annotation)
        :type  vertMarkov: int | None
        :param childChar: A string used in construction of the artificial nodes, separating the head of the
                          original subtree from the child nodes that have yet to be expanded (default = "|")
        :type  childChar: str
        :param parentChar: A string used to separate the node representation from its vertical annotation
        :type  parentChar: str
        i(   t   chomsky_normal_formN(   t   nltk.treetransformsRU   (   R   t   factort
   horzMarkovt
   vertMarkovt	   childChart
   parentCharRU   (    (    s(   lib/python2.7/site-packages/nltk/tree.pyRU     s    u   +c         C  s*   d d l  m } | |  | | | |  d S(   ul  
        This method modifies the tree in three ways:

          1. Transforms a tree in Chomsky Normal Form back to its
             original structure (branching greater than two)
          2. Removes any parent annotation (if it exists)
          3. (optional) expands unary subtrees (if previously
             collapsed with collapseUnary(...) )

        :param expandUnary: Flag to expand unary or not (default = True)
        :type  expandUnary: bool
        :param childChar: A string separating the head node from its children in an artificial node (default = "|")
        :type  childChar: str
        :param parentChar: A sting separating the node label from its parent annotation (default = "^")
        :type  parentChar: str
        :param unaryChar: A string joining two non-terminals in a unary production (default = "+")
        :type  unaryChar: str
        i(   t   un_chomsky_normal_formN(   RV   R\   (   R   t   expandUnaryRZ   R[   t	   unaryCharR\   (    (    s(   lib/python2.7/site-packages/nltk/tree.pyR\     s    c         C  s'   d d l  m } | |  | | |  d S(   u  
        Collapse subtrees with a single child (ie. unary productions)
        into a new non-terminal (Tree node) joined by 'joinChar'.
        This is useful when working with algorithms that do not allow
        unary productions, and completely removing the unary productions
        would require loss of useful information.  The Tree is modified
        directly (since it is passed by reference) and no value is returned.

        :param collapsePOS: 'False' (default) will not collapse the parent of leaf nodes (ie.
                            Part-of-Speech tags) since they are always unary productions
        :type  collapsePOS: bool
        :param collapseRoot: 'False' (default) will not modify the root production
                             if it is unary.  For the Penn WSJ treebank corpus, this corresponds
                             to the TOP -> productions.
        :type collapseRoot: bool
        :param joinChar: A string used to connect collapsed node values (default = "+")
        :type  joinChar: str
        i(   t   collapse_unaryN(   RV   R_   (   R   t   collapsePOSt   collapseRoott   joinCharR_   (    (    s(   lib/python2.7/site-packages/nltk/tree.pyR_     s    c         C  sI   t  | t  rA g  | D] } |  j |  ^ q } |  | j |  S| Sd S(   u  
        Convert a tree between different subtypes of Tree.  ``cls`` determines
        which class will be used to encode the new tree.

        :type tree: Tree
        :param tree: The tree that should be converted.
        :return: The new Tree.
        N(   R   R   t   convertR   (   t   clst   treeR6   R   (    (    s(   lib/python2.7/site-packages/nltk/tree.pyRc      s    
"c         C  s
   |  j    S(   N(   t   copy(   R   (    (    s(   lib/python2.7/site-packages/nltk/tree.pyt   __copy__0  s    c         C  s   |  j  d t  S(   Nt   deep(   Rf   t   True(   R   t   memo(    (    s(   lib/python2.7/site-packages/nltk/tree.pyt   __deepcopy__3  s    c         C  s3   | s t  |   |  j |   St  |   j |   Sd  S(   N(   R   R   Rc   (   R   Rh   (    (    s(   lib/python2.7/site-packages/nltk/tree.pyRf   6  s    c         C  s   t  S(   N(   t   ImmutableTree(   R   (    (    s(   lib/python2.7/site-packages/nltk/tree.pyt   _frozen_class=  s    c         C  s   |  j    } | d  k r* | j |   } nO |  j d t  } x+ | j d  D] } | | |  | | <qL W| j |  } t |  | S(   NRh   u   leaves(   Rm   R   Rc   Rf   Ri   R?   t   hash(   R   t   leaf_freezert   frozen_classt   newcopyRI   (    (    s(   lib/python2.7/site-packages/nltk/tree.pyt   freeze@  s    
u   ()c         C  sm  t  | t  s" t |  d k r1 t d   n  t j d |  rR t d   n  | \ } }	 t j |  t j |	  }
 } | d k r d |
 | f } n  | d k r d |
 | f } n  t j d |
 | | | f  } d g  f g } x| j	 |  D]} | j
   } | d | k rt |  d k r]t | d d  d k r]|  j | | d	  n  | d j   } | d k	 r| |  } n  | j | g  f  q | |	 k r5t |  d k rt | d d  d k r|  j | | |  q|  j | | d	  n  | j   \ } } | d
 d j |  | |   q t |  d k r]|  j | | |  n  | d k	 rx| |  } n  | d
 d j |  q Wt |  d k r|  j | d	 |	  nj t | d d  d k r|  j | d	 |  n: | d d d k st  t | d d  d k s#t  | d d d } | ri| j d k rit |  d k ri| d } n  | S(   u'  
        Read a bracketed tree string and return the resulting tree.
        Trees are represented as nested brackettings, such as::

          (S (NP (NNP John)) (VP (V runs)))

        :type s: str
        :param s: The string to read

        :type brackets: str (length=2)
        :param brackets: The bracket characters used to mark the
            beginning and end of trees and subtrees.

        :type read_node: function
        :type read_leaf: function
        :param read_node, read_leaf: If specified, these functions
            are applied to the substrings of ``s`` corresponding to
            nodes and leaves (respectively) to obtain the values for
            those nodes and leaves.  They should have the following
            signature:

               read_node(str) -> value

            For example, these functions could be used to process nodes
            and leaves whose values should be some type other than
            string (such as ``FeatStruct``).
            Note that by default, node strings and leaf strings are
            delimited by whitespace and brackets; to override this
            default, use the ``node_pattern`` and ``leaf_pattern``
            arguments.

        :type node_pattern: str
        :type leaf_pattern: str
        :param node_pattern, leaf_pattern: Regular expression patterns
            used to find node and leaf substrings in ``s``.  By
            default, both nodes patterns are defined to match any
            sequence of non-whitespace non-bracket characters.

        :type remove_empty_top_bracketing: bool
        :param remove_empty_top_bracketing: If the resulting tree has
            an empty node label, and is length one, then return its
            single child instead.  This is useful for treebank trees,
            which sometimes contain an extra level of bracketing.

        :return: A tree corresponding to the string representation ``s``.
            If this class method is called using a subclass of Tree,
            then it will return a tree of that type.
        :rtype: Tree
        i   u"   brackets must be a length-2 stringu   \su   whitespace brackets not allowedu
   [^\s%s%s]+u   %s\s*(%s)?|%s|(%s)i    i   u   end-of-stringiu    N(   R   R   R(   R   t   ret   searcht   escapeR   t   compilet   finditert   groupt   _parse_errort   lstripR5   RJ   t   AssertionErrorR   (   Rd   t   st   bracketst	   read_nodet	   read_leaft   node_patternt   leaf_patternt   remove_empty_top_bracketingt   open_bt   close_bt   open_patternt   close_patternt   token_reRL   t   matcht   tokenR1   R   Re   (    (    s(   lib/python2.7/site-packages/nltk/tree.pyt
   fromstringP  sZ    <",! 'c         C  s   | d k r" t  |  d } } n | j   | j   } } d |  j | | d | f } | j d d  j d d  } | } t  |  | d k r | | d  d } n  | d k r d | | d } d	 } n  | d
 d | d d | f 7} t |   d S(   u   
        Display a friendly error message when parsing a tree string fails.
        :param s: The string we're parsing.
        :param match: regexp match of the problem token.
        :param expecting: what we expected to see instead.
        u   end-of-stringu0   %s.read(): expected %r but got %r
%sat index %d.u    i   u   
u   	i
   u   ...i   u   
%s"%s"
%s^i   i   Nu               u                   (   R(   RP   Rx   R   t   replaceRO   (   Rd   R|   R   t	   expectingRI   R   t   msgt   offset(    (    s(   lib/python2.7/site-packages/nltk/tree.pyRy     s$    	
	c         C  s   d d l  m } | |   d S(   uP   
        Open a new window containing a graphical diagram of this tree.
        i(   t
   draw_treesN(   t   nltk.draw.treeR   (   R   R   (    (    s(   lib/python2.7/site-packages/nltk/tree.pyt   draw  s    c         K  s9   d d l  m } t | |  | |  j |   d | d S(   u   
        Pretty-print this tree as ASCII or Unicode art.
        For explanation of the arguments, see the documentation for
        `nltk.treeprettyprinter.TreePrettyPrinter`.
        i(   t   TreePrettyPrintert   fileN(   t   nltk.treeprettyprinterR   t   printt   text(   R   t   sentencet	   highlightt   streamt   kwargsR   (    (    s(   lib/python2.7/site-packages/nltk/tree.pyt   pretty_print  s    c         C  s<   d j  d   |  D  } d t |   j t |  j  | f S(   Nu   , c         s  s   |  ] } t  |  Vq d  S(   N(   R   (   R;   t   c(    (    s(   lib/python2.7/site-packages/nltk/tree.pys	   <genexpr>  s    u   %s(%s, [%s])(   t   joinR   R   R   R   (   R   t   childstr(    (    s(   lib/python2.7/site-packages/nltk/tree.pyt   __repr__  s
    c         C  s  d d l  } d d l } d d l } d d l } d d l m } d d l m } d d l m	 } |   } | | j
   |   }	 | j |	  |	 j   \ }
 } } } d d | | f | j
   d <| j   } d j | j  } d	 j | j  } | j |  | j |	  yK | j | d
 d d d g d d g d t g d j | |  j    Wn6 t k
 rt d  } t | d t j t  n Xt | d   } | j   } Wd QX| j |  | j |  | j |  j   SWd QXd S(   u   
        Draws and outputs in PNG for ipython.
        PNG is used instead of PDF, since it can be displayed in the qt console and
        has wider browser support.
        iN(   t   tree_to_treesegment(   t   CanvasFrame(   t   find_binaryi    u   scrollregionu   {0:}.psu   {0:}.pngu   gst   binary_namesu   gswin32c.exeu   gswin64c.exet   env_varsu   PATHt   verboseuz   -q -dEPSCrop -sDEVICE=png16m -r90 -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -dSAFER -dBATCH -dNOPAUSE -sOutputFile={0:} {1:}u   The Ghostscript executable isn't found.
See http://web.mit.edu/ghostscript/www/Install.htm
If you're using a Mac, you can try installing
https://docs.brew.sh/Installation then `brew install ghostscript`R   u   rb(   t   ost   base64t
   subprocesst   tempfileR   R   t   nltk.draw.utilR   t   nltk.internalsR   t   canvast
   add_widgett   bboxt   NamedTemporaryFilet   formatt   namet   print_to_filet   destroy_widgett   callt   Falset   splitt   LookupErrort   strR   t   syst   stderrt   opent   readt   removet	   b64encodet   decode(   R   R   R   R   R   R   R   R   t   _canvas_framet   widgett   xt   yt   wt   hR   t   in_patht   out_patht   pre_error_messaget   srt   res(    (    s(   lib/python2.7/site-packages/nltk/tree.pyt
   _repr_png_  sF    			
c         C  s
   |  j    S(   N(   t   pformat(   R   (    (    s(   lib/python2.7/site-packages/nltk/tree.pyt   __str__;  s    c         K  sC   d | k r  | d } | d =n d } t |  j |   d | d S(   uH   
        Print a string representation of this Tree to 'stream'
        u   streamR   N(   R   R   R   (   R   R   R   (    (    s(   lib/python2.7/site-packages/nltk/tree.pyt   pprint>  s
    

iF   u    c      	   C  sl  |  j  | | |  } t |  | | k  r/ | St |  j t  r^ d | d |  j | f } n  d | d t |  j  | f } x |  D] } t | t  r | d d | d | j | | d | | |  7} q t | t  r| d d | d d j	 |  7} q t | t  r<| r<| d d | d d | 7} q | d d | d t |  7} q W| | d S(	   uI  
        :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)))``.
        u   %s%s%si    u   
u    i   u   /u   %si   (
   t   _pformat_flatR(   R   R   R   R   R   R   R'   R   (   R   t   margint   indentt   nodesept   parenst   quotesR|   R6   (    (    s(   lib/python2.7/site-packages/nltk/tree.pyR   J  s      3&!$c         C  sD   t  j d  } |  j d d d d d d  } d	 t  j | d
 |  S(   u  
        Returns a representation of the tree compatible with the
        LaTeX qtree package. This consists of the string ``\Tree``
        followed by the tree represented in bracketed notation.

        For example, the following result was generated from a parse tree of
        the sentence ``The announcement astounded us``::

          \Tree [.I'' [.N'' [.D The ] [.N' [.N announcement ] ] ]
              [.I' [.V'' [.V' [.V astounded ] [.N'' [.N' [.N us ] ] ] ] ] ] ]

        See http://www.ling.upenn.edu/advice/latex.html for the LaTeX
        style file for the qtree package.

        :return: A latex qtree representation of this tree.
        :rtype: str
        u   ([#\$%&~_\{\}])R   i   R   u    R   u   [.u    ]u   \Tree u   \\\1(   u   [.u    ](   Rs   Rv   R   t   sub(   R   t   reserved_charsR   (    (    s(   lib/python2.7/site-packages/nltk/tree.pyt   pformat_latex_qtreer  s    c         C  s  g  } x |  D] } t  | t  rA | j | j | | |   q t  | t  ri | j d j |   q t  | t  r | r | j d |  q | j t |   q Wt  |  j t  r d | d |  j | d j |  | d f Sd | d t |  j  | d j |  | d f Sd  S(   Nu   /u   %su   %s%s%s %s%si    u    i   (	   R   R   R5   R   R'   R   R   R   R   (   R   R   R   R   t	   childstrsR6   (    (    s(   lib/python2.7/site-packages/nltk/tree.pyR     s,    N(    (8   R   t
   __module__t   __doc__R   R   R   R   t   __ne__t   __gt__t   __le__t   __ge__R    R!   R"   R#   R&   R*   R-   R/   R0   t   propertyR   R1   R2   R4   R7   R9   R?   RC   RG   RI   RN   RT   RU   Ri   R\   R   R_   t   classmethodRc   Rg   Rk   Rf   Rm   Rr   R   Ry   R   R   R   R   R   R   R   R   R   (    (    (    s(   lib/python2.7/site-packages/nltk/tree.pyR   %   sv   ?																											p#	
		5		(	Rl   c           B  s   e  Z d d   Z d   Z d   Z d   Z d   Z d   Z d   Z	 d   Z
 d   Z d d	  Z d
   Z d   Z d   Z d   Z d   Z RS(   c         C  sr   t  t |   j | |  y" t |  j t |   f  |  _ Wn0 t t f k
 rm t d t	 |   j
   n Xd  S(   Nu-   %s: node value and children must be immutable(   t   superRl   R   Rn   R   R'   t   _hashR   RO   R   R   (   R   R   R   (    (    s(   lib/python2.7/site-packages/nltk/tree.pyR     s    "c         C  s   t  d t |   j   d  S(   Nu   %s may not be modified(   RO   R   R   (   R   R)   R,   (    (    s(   lib/python2.7/site-packages/nltk/tree.pyR*     s    c         C  s   t  d t |   j   d  S(   Nu   %s may not be modified(   RO   R   R   (   R   R=   t   jR,   (    (    s(   lib/python2.7/site-packages/nltk/tree.pyt   __setslice__  s    c         C  s   t  d t |   j   d  S(   Nu   %s may not be modified(   RO   R   R   (   R   R)   (    (    s(   lib/python2.7/site-packages/nltk/tree.pyR-     s    c         C  s   t  d t |   j   d  S(   Nu   %s may not be modified(   RO   R   R   (   R   R=   R   (    (    s(   lib/python2.7/site-packages/nltk/tree.pyt   __delslice__  s    c         C  s   t  d t |   j   d  S(   Nu   %s may not be modified(   RO   R   R   (   R   R   (    (    s(   lib/python2.7/site-packages/nltk/tree.pyt   __iadd__  s    c         C  s   t  d t |   j   d  S(   Nu   %s may not be modified(   RO   R   R   (   R   R   (    (    s(   lib/python2.7/site-packages/nltk/tree.pyt   __imul__  s    c         C  s   t  d t |   j   d  S(   Nu   %s may not be modified(   RO   R   R   (   R   R   (    (    s(   lib/python2.7/site-packages/nltk/tree.pyR5     s    c         C  s   t  d t |   j   d  S(   Nu   %s may not be modified(   RO   R   R   (   R   R   (    (    s(   lib/python2.7/site-packages/nltk/tree.pyR3     s    c         C  s   t  d t |   j   d  S(   Nu   %s may not be modified(   RO   R   R   (   R   R   (    (    s(   lib/python2.7/site-packages/nltk/tree.pyRJ     s    c         C  s   t  d t |   j   d  S(   Nu   %s may not be modified(   RO   R   R   (   R   R   (    (    s(   lib/python2.7/site-packages/nltk/tree.pyR     s    c         C  s   t  d t |   j   d  S(   Nu   %s may not be modified(   RO   R   R   (   R   (    (    s(   lib/python2.7/site-packages/nltk/tree.pyt   reverse  s    c         C  s   t  d t |   j   d  S(   Nu   %s may not be modified(   RO   R   R   (   R   (    (    s(   lib/python2.7/site-packages/nltk/tree.pyt   sort  s    c         C  s   |  j  S(   N(   R   (   R   (    (    s(   lib/python2.7/site-packages/nltk/tree.pyt   __hash__  s    c         C  s8   t  |  d  r+ t d t |   j   n  | |  _ d S(   u   
        Set the node label.  This will only succeed the first time the
        node label is set, which should occur in ImmutableTree.__init__().
        u   _labelu   %s may not be modifiedN(   t   hasattrRO   R   R   R   (   R   R,   (    (    s(   lib/python2.7/site-packages/nltk/tree.pyR2     s    N(   R   R   R   R   R*   R   R-   R   R   R   R5   R3   RJ   R   R   R   R   R2   (    (    (    s(   lib/python2.7/site-packages/nltk/tree.pyRl     s   												t   AbstractParentedTreec           B  s   e  Z d  Z d d  Z e e d   Z e d    Z d   Z	 d   Z
 d   Z d   Z d   Z d	 d
  Z d   Z e e d  r d   Z d   Z d   Z n  RS(   u  
    An abstract base class for a ``Tree`` that automatically maintains
    pointers to parent nodes.  These parent pointers are updated
    whenever any change is made to a tree's structure.  Two subclasses
    are currently defined:

      - ``ParentedTree`` is used for tree structures where each subtree
        has at most one parent.  This class should be used in cases
        where there is no"sharing" of subtrees.

      - ``MultiParentedTree`` is used for tree structures where a
        subtree may have zero or more parents.  This class should be
        used in cases where subtrees may be shared.

    Subclassing
    ===========
    The ``AbstractParentedTree`` class redefines all operations that
    modify a tree's structure to call two methods, which are used by
    subclasses to update parent information:

      - ``_setparent()`` is called whenever a new child is added.
      - ``_delparent()`` is called whenever a child is removed.
    c         C  s   t  t |   j | |  | d  k	 r xB t |   D]4 \ } } t | t  r2 |  j | | d t q2 q2 Wx? t |   D]. \ } } t | t  rw |  j | |  qw qw Wn  d  S(   Nt   dry_run(	   R   R   R   R   R>   R   R   t
   _setparentRi   (   R   R   R   R=   R6   (    (    s(   lib/python2.7/site-packages/nltk/tree.pyR      s    c         C  s   d S(   u  
        Update the parent pointer of ``child`` to point to ``self``.  This
        method is only called if the type of ``child`` is ``Tree``;
        i.e., it is not called when adding a leaf to a tree.  This method
        is always called before the child is actually added to the
        child list of ``self``.

        :type child: Tree
        :type index: int
        :param index: The index of ``child`` in ``self``.
        :raise TypeError: If ``child`` is a tree with an impropriate
            type.  Typically, if ``child`` is a tree, then its type needs
            to match the type of ``self``.  This prevents mixing of
            different tree types (single-parented, multi-parented, and
            non-parented).
        :param dry_run: If true, the don't actually set the child's
            parent pointer; just check for any error conditions, and
            raise an exception if one is found.
        N(    (   R   R6   R)   R   (    (    s(   lib/python2.7/site-packages/nltk/tree.pyR     R   c         C  s   d S(   u  
        Update the parent pointer of ``child`` to not point to self.  This
        method is only called if the type of ``child`` is ``Tree``; i.e., it
        is not called when removing a leaf from a tree.  This method
        is always called before the child is actually removed from the
        child list of ``self``.

        :type child: Tree
        :type index: int
        :param index: The index of ``child`` in ``self``.
        N(    (   R   R6   R)   (    (    s(   lib/python2.7/site-packages/nltk/tree.pyt
   _delparent(  R   c         C  s  t  | t  r t |  | d t \ } } } xD t | | |  D]0 } t  |  | t  r@ |  j |  | |  q@ q@ Wt t |   j	 |  n t  | t
  r| d k  r | t |   7} n  | d k  r t d   n  t  |  | t  r |  j |  | |  n  t t |   j	 |  n t  | t t f  rt |  d k rOt d   qt |  d k ro|  | d =q|  | d | d =n( t d t |   j t |  j f   d  S(   Nt
   allow_stepi    u   index out of rangeu(   The tree position () may not be deleted.i   u#   %s indices must be integers, not %s(   R   R%   R	   Ri   RK   R   R   R   R   R-   R$   R(   R+   R   R'   R   R   R   (   R   R)   RP   t   stopt   stepR=   (    (    s(   lib/python2.7/site-packages/nltk/tree.pyR-   <  s.    c         C  s  t  | t  rHt |  | d t \ } } } t  | t t f  sQ t |  } n  xJ t |  D]< \ } } t  | t  r^ |  j | | | | d t q^ q^ WxD t	 | | |  D]0 } t  |  | t  r |  j
 |  | |  q q WxD t |  D]6 \ } } t  | t  r |  j | | | |  q q Wt t |   j | |  n_t  | t  r| d k  rv| t |   7} n  | d k  rt d   n  | |  | k rd  St  | t  r|  j | |  n  t  |  | t  r|  j
 |  | |  n  t t |   j | |  n t  | t t f  rt |  d k rCt d   qt |  d k rf| |  | d <q| |  | d | d <n( t d t |   j t |  j f   d  S(   NR   R   i    u   index out of rangeu,   The tree position () may not be assigned to.i   u#   %s indices must be integers, not %s(   R   R%   R	   Ri   R   R'   R>   R   R   RK   R   R   R   R*   R$   R(   R+   R   R   R   (   R   R)   R,   RP   R   R   R=   R6   (    (    s(   lib/python2.7/site-packages/nltk/tree.pyR*   d  sF    %c         C  sB   t  | t  r( |  j | t |    n  t t |   j |  d  S(   N(   R   R   R   R(   R   R   R5   (   R   R6   (    (    s(   lib/python2.7/site-packages/nltk/tree.pyR5     s    c         C  sS   xL | D]D } t  | t  r5 |  j | t |    n  t t |   j |  q Wd  S(   N(   R   R   R   R(   R   R   R5   (   R   R   R6   (    (    s(   lib/python2.7/site-packages/nltk/tree.pyR3     s    c         C  ss   | d k  r | t  |   7} n  | d k  r4 d } n  t | t  rV |  j | |  n  t t |   j | |  d  S(   Ni    (   R(   R   R   R   R   R   t   insert(   R   R)   R6   (    (    s(   lib/python2.7/site-packages/nltk/tree.pyR     s    	ic         C  sz   | d k  r | t  |   7} n  | d k  r: t d   n  t |  | t  rd |  j |  | |  n  t t |   j |  S(   Ni    u   index out of range(   R(   R+   R   R   R   R   R   RJ   (   R   R)   (    (    s(   lib/python2.7/site-packages/nltk/tree.pyRJ     s    c         C  sS   |  j  |  } t |  | t  r9 |  j |  | |  n  t t |   j |  d  S(   N(   R)   R   R   R   R   R   R   (   R   R6   R)   (    (    s(   lib/python2.7/site-packages/nltk/tree.pyR     s    u   __getslice__c         C  s(   |  j  t t d |  t d |    S(   Ni    (   R&   R%   R8   (   R   RP   R   (    (    s(   lib/python2.7/site-packages/nltk/tree.pyt   __getslice__  s    c         C  s(   |  j  t t d |  t d |    S(   Ni    (   R-   R%   R8   (   R   RP   R   (    (    s(   lib/python2.7/site-packages/nltk/tree.pyR     s    c         C  s+   |  j  t t d |  t d |   |  S(   Ni    (   R*   R%   R8   (   R   RP   R   R,   (    (    s(   lib/python2.7/site-packages/nltk/tree.pyR     s    N(   R   R   R   R   R   R   R   R   R   R-   R*   R5   R3   R   RJ   R   R   R   R   R   R   (    (    (    s(   lib/python2.7/site-packages/nltk/tree.pyR     s    	(	<						t   ParentedTreec           B  sn   e  Z d  Z d d  Z d   Z d   Z d   Z d   Z d   Z	 d   Z
 d   Z d	   Z e d
  Z RS(   u  
    A ``Tree`` that automatically maintains parent pointers for
    single-parented trees.  The following are methods for querying
    the structure of a parented tree: ``parent``, ``parent_index``,
    ``left_sibling``, ``right_sibling``, ``root``, ``treeposition``.

    Each ``ParentedTree`` may have at most one parent.  In
    particular, subtrees may not be shared.  Any attempt to reuse a
    single ``ParentedTree`` as a child of more than one parent (or
    as multiple children of the same parent) will cause a
    ``ValueError`` exception to be raised.

    ``ParentedTrees`` should never be used in the same tree as ``Trees``
    or ``MultiParentedTrees``.  Mixing tree implementations may result
    in incorrect parent pointers and in ``TypeError`` exceptions.
    c         C  s}   d  |  _ t t |   j | |  | d  k ry xH t |   D]7 \ } } t | t  r; d  | _ |  j | |  q; q; Wn  d  S(   N(	   R   t   _parentR   R   R   R>   R   R   R   (   R   R   R   R=   R6   (    (    s(   lib/python2.7/site-packages/nltk/tree.pyR     s    		c         C  s   t  S(   N(   t   ImmutableParentedTree(   R   (    (    s(   lib/python2.7/site-packages/nltk/tree.pyRm     s    c         C  s   |  j  S(   u5   The parent of this tree, or None if it has no parent.(   R   (   R   (    (    s(   lib/python2.7/site-packages/nltk/tree.pyt   parent  s    c         C  sY   |  j  d k r d Sx- t |  j   D] \ } } | |  k r# | Sq# Wt sU t d   d S(   uD  
        The index of this tree in its parent.  I.e.,
        ``ptree.parent()[ptree.parent_index()] is ptree``.  Note that
        ``ptree.parent_index()`` is not necessarily equal to
        ``ptree.parent.index(ptree)``, since the ``index()`` method
        returns the first child that is equal to its argument.
        u&   expected to find self in self._parent!N(   R   R   R>   R   R{   (   R   R=   R6   (    (    s(   lib/python2.7/site-packages/nltk/tree.pyt   parent_index  s    c         C  s4   |  j    } |  j r0 | d k r0 |  j | d Sd S(   u6   The left sibling of this tree, or None if it has none.i    i   N(   R   R   R   (   R   R   (    (    s(   lib/python2.7/site-packages/nltk/tree.pyt   left_sibling  s    c         C  sA   |  j    } |  j r= | t |  j  d k  r= |  j | d Sd S(   u7   The right sibling of this tree, or None if it has none.i   N(   R   R   R(   R   (   R   R   (    (    s(   lib/python2.7/site-packages/nltk/tree.pyt   right_sibling  s    "c         C  s/   |  } x" | j    d k	 r* | j    } q	 W| S(   u   
        The root of this tree.  I.e., the unique ancestor of this tree
        whose parent is None.  If ``ptree.parent()`` is None, then
        ``ptree`` is its own root.
        N(   R   R   (   R   t   root(    (    s(   lib/python2.7/site-packages/nltk/tree.pyR   #  s    c         C  s7   |  j    d k r d S|  j    j   |  j   f Sd S(   u   
        The tree position of this tree, relative to the root of the
        tree.  I.e., ``ptree.root[ptree.treeposition] is ptree``.
        N(    (   R   R   t   treepositionR   (   R   (    (    s(   lib/python2.7/site-packages/nltk/tree.pyR   .  s    c         C  sM   t  | t  s t  |  | | k s+ t  | j |  k s@ t  d  | _ d  S(   N(   R   R   R{   R   R   (   R   R6   R)   (    (    s(   lib/python2.7/site-packages/nltk/tree.pyR   <  s    c         C  sV   t  | t  s" t d d   n  | j d  k	 r@ t d   n  | sR |  | _ n  d  S(   Nu"   Can not insert a non-ParentedTree u   into a ParentedTreeu3   Can not insert a subtree that already has a parent.(   R   R   R   R   R   RO   (   R   R6   R)   R   (    (    s(   lib/python2.7/site-packages/nltk/tree.pyR   E  s    N(   R   R   R   R   R   Rm   R   R   R   R   R   R   R   R   R   (    (    (    s(   lib/python2.7/site-packages/nltk/tree.pyR     s   									t   MultiParentedTreec           B  s   e  Z d  Z d d  Z d   Z d   Z d   Z d   Z d   Z	 d   Z
 d   Z d	   Z d
   Z d   Z e d  Z RS(   u  
    A ``Tree`` that automatically maintains parent pointers for
    multi-parented trees.  The following are methods for querying the
    structure of a multi-parented tree: ``parents()``, ``parent_indices()``,
    ``left_siblings()``, ``right_siblings()``, ``roots``, ``treepositions``.

    Each ``MultiParentedTree`` may have zero or more parents.  In
    particular, subtrees may be shared.  If a single
    ``MultiParentedTree`` is used as multiple children of the same
    parent, then that parent will appear multiple times in its
    ``parents()`` method.

    ``MultiParentedTrees`` should never be used in the same tree as
    ``Trees`` or ``ParentedTrees``.  Mixing tree implementations may
    result in incorrect parent pointers and in ``TypeError`` exceptions.
    c         C  s}   g  |  _  t t |   j | |  | d  k ry xH t |   D]7 \ } } t | t  r; g  | _  |  j | |  q; q; Wn  d  S(   N(	   t   _parentsR   R   R   R   R>   R   R   R   (   R   R   R   R=   R6   (    (    s(   lib/python2.7/site-packages/nltk/tree.pyR   g  s    		c         C  s   t  S(   N(   t   ImmutableMultiParentedTree(   R   (    (    s(   lib/python2.7/site-packages/nltk/tree.pyRm   w  s    c         C  s   t  |  j  S(   u  
        The set of parents of this tree.  If this tree has no parents,
        then ``parents`` is the empty set.  To check if a tree is used
        as multiple children of the same parent, use the
        ``parent_indices()`` method.

        :type: list(MultiParentedTree)
        (   R   R   (   R   (    (    s(   lib/python2.7/site-packages/nltk/tree.pyt   parents~  s    	c         C  s7   g  |  j    D]& \ } } | d k r | | d ^ q S(   u}  
        A list of all left siblings of this tree, in any of its parent
        trees.  A tree may be its own left sibling if it is used as
        multiple contiguous children of the same parent.  A tree may
        appear multiple times in this list if it is the left sibling
        of this tree with respect to multiple parents.

        :type: list(MultiParentedTree)
        i    i   (   t   _get_parent_indices(   R   R   R)   (    (    s(   lib/python2.7/site-packages/nltk/tree.pyt   left_siblings  s    c         C  sA   g  |  j    D]0 \ } } | t |  d k  r | | d ^ q S(   u  
        A list of all right siblings of this tree, in any of its parent
        trees.  A tree may be its own right sibling if it is used as
        multiple contiguous children of the same parent.  A tree may
        appear multiple times in this list if it is the right sibling
        of this tree with respect to multiple parents.

        :type: list(MultiParentedTree)
        i   (   R  R(   (   R   R   R)   (    (    s(   lib/python2.7/site-packages/nltk/tree.pyt   right_siblings  s    c         C  sE   g  |  j  D]7 } t |  D]$ \ } } | |  k r | | f ^ q q
 S(   N(   R   R>   (   R   R   R)   R6   (    (    s(   lib/python2.7/site-packages/nltk/tree.pyR    s    c         C  s   t  |  j i   j    S(   u   
        The set of all roots of this tree.  This set is formed by
        tracing all possible parent paths until trees with no parents
        are found.

        :type: list(MultiParentedTree)
        (   R   t   _get_roots_helpert   values(   R   (    (    s(   lib/python2.7/site-packages/nltk/tree.pyt   roots  s    c         C  sA   |  j  r- x1 |  j  D] } | j |  q Wn |  | t |   <| S(   N(   R   R  t   id(   R   t   resultR   (    (    s(   lib/python2.7/site-packages/nltk/tree.pyR    s
    	c         C  sF   | |  j  k r g  Sg  t |  D] \ } } | |  k r  | ^ q  Sd S(   uY  
        Return a list of the indices where this tree occurs as a child
        of ``parent``.  If this child does not occur as a child of
        ``parent``, then the empty list is returned.  The following is
        always true::

          for parent_index in ptree.parent_indices(parent):
              parent[parent_index] is ptree
        N(   R   R>   (   R   R   R)   R6   (    (    s(   lib/python2.7/site-packages/nltk/tree.pyt   parent_indices  s    
c         C  ss   |  | k r d g Sg  |  j  D]N } | j |  D]8 } t |  D]% \ } } | |  k r@ | | f ^ q@ q0 q Sd S(   u  
        Return a list of all tree positions that can be used to reach
        this multi-parented tree starting from ``root``.  I.e., the
        following is always true::

          for treepos in ptree.treepositions(root):
              root[treepos] is ptree
        N(    (   R   R?   R>   (   R   R   R   RM   R)   R6   (    (    s(   lib/python2.7/site-packages/nltk/tree.pyR?     s    	c         C  s   t  | t  s t  |  | | k s+ t  t g  | j D] } | |  k r8 | ^ q8  d k se t  xF t |   D]( \ } } | | k rr | | k rr Pqr qr W| j j |   d  S(   Ni   (   R   R   R{   R(   R   R>   R   (   R   R6   R)   R<   R=   R   (    (    s(   lib/python2.7/site-packages/nltk/tree.pyR     s    :c         C  sc   t  | t  s" t d d   n  | s_ x4 | j D] } | |  k r2 Pq2 q2 W| j j |   n  d  S(   Nu'   Can not insert a non-MultiParentedTree u   into a MultiParentedTree(   R   R   R   R   R5   (   R   R6   R)   R   R   (    (    s(   lib/python2.7/site-packages/nltk/tree.pyR     s    N(   R   R   R   R   R   Rm   R  R  R  R  R  R  R
  R?   R   R   R   (    (    (    s(   lib/python2.7/site-packages/nltk/tree.pyR   U  s   						
				R   c           B  s   e  Z RS(    (   R   R   (    (    (    s(   lib/python2.7/site-packages/nltk/tree.pyR     s   R   c           B  s   e  Z RS(    (   R   R   (    (    (    s(   lib/python2.7/site-packages/nltk/tree.pyR     s   t   ProbabilisticTreec           B  s\   e  Z d d   Z d   Z d   Z d   Z e d  Z e	 d    Z
 d   Z d   Z RS(	   c         K  s'   t  j |  | |  t j |  |  d  S(   N(   R   R   R   (   R   R   R   t   prob_kwargs(    (    s(   lib/python2.7/site-packages/nltk/tree.pyR     s    c         C  s   t  S(   N(   t   ImmutableProbabilisticTree(   R   (    (    s(   lib/python2.7/site-packages/nltk/tree.pyRm     s    c         C  s   d t  j |   |  j   f S(   Nu	   %s (p=%r)(   R   R   t   prob(   R   (    (    s(   lib/python2.7/site-packages/nltk/tree.pyR     s    c         C  s    d |  j  d d  |  j   f S(   Nu   %s (p=%.6g)R   i<   (   R   R  (   R   (    (    s(   lib/python2.7/site-packages/nltk/tree.pyR   "  s    c         C  s?   | s( t  |   |  j |  d |  j   St  |   j |   Sd  S(   NR  (   R   R   R  Rc   (   R   Rh   (    (    s(   lib/python2.7/site-packages/nltk/tree.pyRf   %  s    "c         C  s}   t  | t  ru g  | D] } |  j |  ^ q } t  | t  r\ |  | j | d | j   S|  | j | d d Sn | Sd  S(   NR  g      ?(   R   R   Rc   R   R   R  (   Rd   t   valR6   R   (    (    s(   lib/python2.7/site-packages/nltk/tree.pyRc   +  s    "c         C  sL   |  j  | j  k oK |  j t |   |  j   f | j t |  | j   f k S(   N(   R   R   R   R  (   R   R   (    (    s(   lib/python2.7/site-packages/nltk/tree.pyR   6  s
    	c         C  s   t  | t  s" t d |  |  n  |  j | j k rn |  j t |   |  j   f | j t |  | j   f k  S|  j j | j j k  Sd  S(   Nu   <(   R   R   R   R   R   R   R  R   (   R   R   (    (    s(   lib/python2.7/site-packages/nltk/tree.pyR   =  s    	N(   R   R   R   R   Rm   R   R   R   Rf   R   Rc   R   R   (    (    (    s(   lib/python2.7/site-packages/nltk/tree.pyR    s   				R  c           B  sJ   e  Z d d   Z d   Z d   Z d   Z e d  Z e	 d    Z
 RS(   c         K  sN   t  j |  | |  t j |  |  t |  j t |   |  j   f  |  _ d  S(   N(   Rl   R   R   Rn   R   R'   R  R   (   R   R   R   R  (    (    s(   lib/python2.7/site-packages/nltk/tree.pyR   L  s    c         C  s   t  S(   N(   R  (   R   (    (    s(   lib/python2.7/site-packages/nltk/tree.pyRm   R  s    c         C  s   d t  j |   |  j   f S(   Nu   %s [%s](   R   R   R  (   R   (    (    s(   lib/python2.7/site-packages/nltk/tree.pyR   U  s    c         C  s    d |  j  d d  |  j   f S(   Nu   %s [%s]R   i<   (   R   R  (   R   (    (    s(   lib/python2.7/site-packages/nltk/tree.pyR   X  s    c         C  s?   | s( t  |   |  j |  d |  j   St  |   j |   Sd  S(   NR  (   R   R   R  Rc   (   R   Rh   (    (    s(   lib/python2.7/site-packages/nltk/tree.pyRf   [  s    "c         C  s}   t  | t  ru g  | D] } |  j |  ^ q } t  | t  r\ |  | j | d | j   S|  | j | d d Sn | Sd  S(   NR  g      ?(   R   R   Rc   R   R   R  (   Rd   R  R6   R   (    (    s(   lib/python2.7/site-packages/nltk/tree.pyRc   a  s    "N(   R   R   R   R   Rm   R   R   R   Rf   R   Rc   (    (    (    s(   lib/python2.7/site-packages/nltk/tree.pyR  J  s   			c         C  sP   g  } xC |  D]; } t  | t  r; | j t | j   q | j |  q W| S(   N(   R   R   R5   R   R   (   Re   t   namesR6   (    (    s(   lib/python2.7/site-packages/nltk/tree.pyRF   m  s    c         C  s   t  d   d S(   uE   
    Use Tree.read(s, remove_empty_top_bracketing=True) instead.
    u;   Use Tree.read(s, remove_empty_top_bracketing=True) instead.N(   t	   NameError(   R|   (    (    s(   lib/python2.7/site-packages/nltk/tree.pyt   bracket_parse|  s    c         C  s  t  j d |   } x t t |   D] } | | d k rc | | | | d | | d <| | <q% d | | k r | | j d  } t |  d k r | d | | <q d | d d | d d	 | | <q% | | d
 k r% d | | <q% q% Wd j |  } t j | d t S(   u  
    Parse a Sinica Treebank string and return a tree.  Trees are represented as nested brackettings,
    as shown in the following example (X represents a Chinese character):
    S(goal:NP(Head:Nep:XX)|theme:NP(Head:Nhaa:X)|quantity:Dab:X|Head:VL2:X)#0(PERIODCATEGORY)

    :return: A tree corresponding to the string representation.
    :rtype: Tree
    :param s: The string to be converted
    :type s: str
    u   ([()| ])u   (i   u   :i   iu    iu   )u   |u    R   (   Rs   R   RK   R(   R   R   R   Ri   (   R|   t   tokensR=   t   fieldst   treebank_string(    (    s(   lib/python2.7/site-packages/nltk/tree.pyt   sinica_parse  s    !%c          C  s0  d d l  m }  m } d } |  j |  } t d  t |  t | j    t d  t | j    t | d  t | d  t | j    t | j    t | d  t | d  t | d  | d } | j	 d |  j d   t d	  t |  |  j d
  | d <t |  t   t d  | j
   t |  t d  | j   t |  t   | d d d g d d } t d  t |  t   |  j | j    } t d  t |  t   t d  t | j    t   t d  t | j    t   | j d  t |  d S(   u   
    A demonstration showing how Trees and Trees can be
    used.  This demonstration creates a Tree, and loads a
    Tree from the Treebank corpus,
    and shows the results of calling several of their methods.
    i(   R   R  uA   (S (NP (DT the) (NN cat)) (VP (VBD ate) (NP (DT a) (NN cookie))))u#   Convert bracketed string into tree:u   Display tree properties:i    i   u   (JJ big)u   Tree modification:u	   (NN cake)u   Collapse unary:u   Chomsky normal form:u   xu   yu   zR  g      ?u   Probabilistic Tree:u0   Convert tree to bracketed string and back again:u   LaTeX output:u   Production output:u   testi   N(   i   i   (   i   i   i    (   i   i   i   (   u   testi   (   t   nltkR   R  R   R   R   R1   R9   R4   R   R_   RU   R   R   RG   R2   (   R   R  R|   t   tt   the_catt   pt(    (    s(   lib/python2.7/site-packages/nltk/tree.pyt   demo  sZ    


















u   ImmutableProbabilisticTreeu   ImmutableTreeu   ProbabilisticMixInu   ProbabilisticTreeu   Treeu   bracket_parseu   sinica_parseu   ParentedTreeu   MultiParentedTreeu   ImmutableParentedTreeu   ImmutableMultiParentedTree('   R   t
   __future__R    R   Rs   R   t   abcR   R   t   sixR   R   t   nltk.grammarR   R   t   nltk.probabilityR   t	   nltk.utilR	   t   nltk.compatR
   R   R   R   R   R   Rl   R   R   R   R   R   R  R  RF   R  R  R  t   __all__(    (    (    s(   lib/python2.7/site-packages/nltk/tree.pyt   <module>   sR   	   @z	4"			)	I