
3Ic           @   s  d  Z  d d k Z d d k Z d d k l Z l Z d d k l Z d d k l	 Z	 d e
 f d     YZ d e f d	     YZ d
 e f d     YZ d e f d     YZ d e f d     YZ d e e f d     YZ d e e f d     YZ d e e f d     YZ d e e f d     YZ d   Z d   Z d   Z d   Z e d j o e   n d d d d d d d d d d d g Z d S(    sh   
Class for representing hierarchical language structures, such as
syntax trees and morphological trees.
iN(   t
   Productiont   Nonterminal(   t   ProbabilisticMixIn(   t   slice_boundst   Treec           B   s  e  Z d  Z e e d  Z e 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 d   Z d d  Z e d  Z d   Z d   Z d   Z d   Z d e d d d d  Z e d d d d   Z e e d d!  Z  d"   Z! e" e!  Z! e d#  Z# d$   Z$ e d%  Z% e" d& e e e e e d'   Z& e" d(    Z' d)   Z( d*   Z) d+   Z* d, d d- d& e d.  Z+ d/   Z, d0   Z- RS(1   s  
    A hierarchical structure.

    Each C{Tree} represents a single hierarchical grouping of
    leaves and subtrees.  For example, each constituent in a syntax
    tree is represented by a single C{Tree}.

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

    Any other properties that a C{Tree} defines are known as
    X{node properties}, and are used to add information about
    individual hierarchical groupings.  For example, syntax trees use a
    NODE property to label syntactic constituents with phrase tags,
    such as "NP" and"VP".

    Several C{Tree} methods use X{tree positions} to specify
    children or descendants of a tree.  Tree positions are defined as
    follows:

      - The tree position M{i} specifies a C{Tree}'s M{i}th child.
      - The tree position C{()} specifies the C{Tree} itself.
      - If C{M{p}} is the tree position of descendant M{d}, then
        C{M{p}+(M{i})} specifies the C{M{i}}th child of M{d}.
    
    I.e., every tree position is either a single index C{M{i}},
    specifying C{self[M{i}]}; or a sequence C{(M{i1}, M{i2}, ...,
    M{iN})}, specifying
    C{self[M{i1}][M{i2}]...[M{iN}]}.
    c         C   s   | d  j o t i |   Sn | d  j o8 t | t  p t d |  i   n |  i |  SnL t | t  p t | d  o t d |  i   n t i |  | |  Sd  S(   Ns;   %s: Expected a node value and child list or a single stringt   __iter__s.   %s() argument 2 should be a list, not a string(	   t   Nonet   listt   __new__t
   isinstancet
   basestringt	   TypeErrort   __name__t   parset   hasattr(   t   clst   node_or_strt   children(    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyR   :   s    c         C   s2   | d j o d Sn t i |  |  | |  _ d S(   s  
        Construct a new tree.  This constructor can be called in one
        of two ways:

          - C{Tree(node, children)} constructs a new tree with the
            specified node value and list of children.
            
          - C{Tree(s)} constructs a new tree by parsing the string
            C{s}.  It is equivalent to calling the class method
            C{Tree.parse(s)}.
        N(   R   R   t   __init__t   node(   t   selfR   R   (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyR   I   s    c         C   s;   t  | t  p t Sn |  i | i j o t i |  |  S(   N(   R	   R   t   FalseR   R   t   __eq__(   R   t   other(    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyR   d   s    c         C   s   |  | j S(   N(    (   R   R   (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyt   __ne__g   s    c         C   s;   t  | t  p t Sn |  i | i j  p t i |  |  S(   N(   R	   R   R   R   R   t   __lt__(   R   R   (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyR   i   s    c         C   s;   t  | t  p t Sn |  i | i j p t i |  |  S(   N(   R	   R   R   R   R   t   __le__(   R   R   (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyR   l   s    c         C   s;   t  | t  p t Sn |  i | i j p t i |  |  S(   N(   R	   R   t   TrueR   R   t   __gt__(   R   R   (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyR   o   s    c         C   s;   t  | t  p t Sn |  i | i j p t i |  |  S(   N(   R	   R   R   R   R   t   __ge__(   R   R   (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyR   r   s    c         C   s   t  d   d  S(   Ns$   Tree does not support multiplication(   R   (   R   t   v(    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyt   __mul__z   s    c         C   s   t  d   d  S(   Ns$   Tree does not support multiplication(   R   (   R   R   (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyt   __rmul__|   s    c         C   s   t  d   d  S(   Ns   Tree does not support addition(   R   (   R   R   (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyt   __add__~   s    c         C   s   t  d   d  S(   Ns   Tree does not support addition(   R   (   R   R   (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyt   __radd__   s    c         C   s   t  | t t f  o t i |  |  Sn_ t |  d j o |  SnD t |  d j o |  t | d  Sn |  t | d  | d Sd  S(   Ni    i   (   R	   t   intt   sliceR   t   __getitem__t   len(   R   t   index(    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyR%      s    c         C   s   t  | t t f  o t i |  | |  Sn_ t |  d j o t d   n< t |  d j o | |  | d <n | |  | d | d <d  S(   Ni    s,   The tree position () may not be assigned to.i   (   R	   R#   R$   R   t   __setitem__R&   t
   IndexError(   R   R'   t   value(    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyR(      s    c         C   s   t  | t t f  o t i |  |  SnY t |  d j o t d   n6 t |  d j o |  | d =n |  | d | d =d  S(   Ni    s(   The tree position () may not be deleted.i   (   R	   R#   R$   R   t   __delitem__R&   R)   (   R   R'   (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyR+      s    c         C   sO   g  } xB |  D]: } t  | t  o | i | i    q | i |  q W| S(   s   
        @return: a list containing this tree's leaves.
            The order reflects the order of the
            leaves in the tree's hierarchical structure.
        @rtype: C{list}
        (   R	   R   t   extendt   leavest   append(   R   R-   t   child(    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyR-      s     c         C   s   t  |  i |  i    S(   s   
        @return: a tree consisting of this tree's root connected directly to
            its leaves, omitting all intervening non-terminal nodes.
        @rtype: C{Tree}
        (   R   R   R-   (   R   (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyt   flatten   s    c         C   sW   d } xF |  D]> } t  | t  o t | | i    } q t | d  } q Wd | S(   s3  
        @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: C{int}
        i    i   (   R	   R   t   maxt   height(   R   t   max_child_heightR/   (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyR2      s    	 t   preorderc            s   g  } | d j o | i  d  n xj t |   D]\ \   } t | t  o0 | i |  } | i   f d   | D  q1 | i    f  q1 W| d j o | i  d  n | S(	   sg   
        @param order: One of: C{preorder}, C{postorder}, C{bothorder},
            C{leaves}.
        R4   t	   bothorderc         3   s    x |  ] }   f | Vq Wd  S(   N(    (   t   .0t   p(   t   i(    s   /p/zhu/06/nlp/nltk/nltk/tree.pys	   <genexpr>   s    t	   postorder(   R4   R5   (    (   R9   R5   (    (   R.   t	   enumerateR	   R   t   treepositionsR,   (   R   t   ordert	   positionsR/   t   childpos(    (   R8   s   /p/zhu/06/nlp/nltk/nltk/tree.pyR;      s     !c         c   sf   | p | |   o	 |  Vn xA |  D]9 } t  | t  o# x  | i |  D] } | VqK Wq% q% Wd S(   s   
        Generate all the subtrees of this tree, optionally restricted
        to trees matching the filter function.
        @type filter: C{function}
        @param filter: the function to filter all local trees
        N(   R	   R   t   subtrees(   R   t   filterR/   t   subtree(    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyR?      s    	  c         C   sz   t  |  i t  p t d  n t t |  i  t |    g } x2 |  D]* } t  | t  o | | i   7} qH qH W| S(   s  
        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.

        @rtype: list of C{Production}s
        sP   Productions can only be generated from trees having node labels that are strings(	   R	   R   t   strR   R    R   t   _child_namesR   t   productions(   R   t   prodsR/   (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyRD      s    	! c         C   sX   g  } xK |  D]C } t  | t  o | i | i    q | i | |  i f  q W| S(   s   
        @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: C{list} of C{tuples}
        (   R	   R   R,   t   posR.   R   (   R   RF   R/   (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyRF     s     c         C   s   | d j  o t  d   n |  d f g } x | o | i   \ } } t | t  p# | d j o | Sq | d 8} q/ xB t t |  d d d  D]$ } | i | | | | f f  q Wq/ Wt  d   d S(   s/  
        @return: The tree position of the C{index}-th leaf in this
            tree.  I.e., if C{tp=self.leaf_treeposition(i)}, then
            C{self[tp]==self.leaves()[i]}.
        
        @raise IndexError: If this tree contains fewer than C{index+1}
            leaves, or if C{index<0}.
        i    s   index must be non-negativei   is-   index must be less than or equal to len(self)N(    (   R)   t   popR	   R   t   rangeR&   R.   (   R   R'   t   stackR*   t   treeposR8   (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyt   leaf_treeposition  s    	
 'c         C   s   | | j o t  d   n |  i |  } |  i | d  } xN t t |   D]: } | t |  j p | | | | j o | |  SqR qR W| S(   s   
        @return: The tree position of the lowest descendant of this
            tree that dominates C{self.leaves()[start:end]}.
        @raise ValueError: if C{end <= start}
        s   end must be greater than starti   (   t
   ValueErrorRK   RH   R&   (   R   t   startt   endt   start_treepost   end_treeposR8   (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyt   treeposition_spanning_leaves+  s     (t   righti    t   |t   ^c         C   s-   d d k  l } | |  | | | | |  d S(   s  
        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: C{string} = [left|right]
        @param horzMarkov: Markov order for sibling smoothing in artificial nodes (None (default) = include all siblings)
        @type  horzMarkov: C{int} | None
        @param vertMarkov: Markov order for parent smoothing (0 (default) = no vertical annotation)
        @type  vertMarkov: C{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: C{string}
        @param parentChar: A string used to separate the node representation from its vertical annotation
        @type  parentChar: C{string}
        i(   t   chomsky_normal_formN(   t   treetransformsRU   (   R   t   factort
   horzMarkovt
   vertMarkovt	   childChart
   parentCharRU   (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyRU   A  s    t   +c         C   s*   d d k  l } | |  | | | |  d S(   s  
        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: C{boolean}
        @param childChar: A string separating the head node from its children in an artificial node (default = "|")
        @type  childChar: C{string}
        @param parentChar: A sting separating the node label from its parent annotation (default = "^")
        @type  parentChar: C{string}
        @param unaryChar: A string joining two non-terminals in a unary production (default = "+")
        @type  unaryChar: C{string}  
        i(   t   un_chomsky_normal_formN(   RV   R]   (   R   t   expandUnaryRZ   R[   t	   unaryCharR]   (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyR]   \  s    c         C   s'   d d k  l } | |  | | |  d S(   s(  
        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: C{boolean}
        @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: C{boolean}
        @param joinChar: A string used to connect collapsed node values (default = "+")
        @type  joinChar: C{string}
        i(   t   collapse_unaryN(   RV   R`   (   R   t   collapsePOSt   collapseRoott   joinCharR`   (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyR`   r  s    c         C   sV   t  | t  o> g  } | D] } | |  i |  q ~ } |  | i |  Sn | Sd S(   s  
        Convert a tree between different subtypes of Tree.  C{cls} determines
        which class will be used to encode the new tree.

        @type val: L{Tree}
        @param val: The tree that should be converted.
        @return: The new C{Tree}.
        N(   R	   R   t   convertR   (   R   t   valt   _[1]R/   R   (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyRd     s    	*c         C   s2   | p |  i  |  i |   Sn |  i  i |   Sd  S(   N(   t	   __class__R   Rd   (   R   t   deep(    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyt   copy  s    c         C   s   t  S(   N(   t   ImmutableTree(   R   (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyt   _frozen_class  s    c         C   s   |  i    } | d  j o | i |   } nP |  i d t  } x+ | i d  D] } | | |  | | <qN W| i |  } t |  | S(   NRh   R-   (   Rk   R   Rd   Ri   R   R;   t   hash(   R   t   leaf_freezert   frozen_classt   newcopyRF   (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyt   freeze  s     
s   ()c         C   s  t  | t  p t |  d j o t d   n t i d |  o t d   n | \ } }	 t i |  t i |	  }
 } | t j o d |
 | f } n | t j o d |
 | f } n t i d |
 | | | f  } t g  f g } x| i	 |  D]} | i
   } | d | j o t |  d j o2 t | d d  d j o |  i | | d	  n | d i   } | t j	 o | |  } n | i | g  f  q| |	 j o t |  d j oI t | d d  d j o |  i | | |  q|  i | | d	  n | i   \ } } | d
 d i |  | |   qt |  d j o |  i | | |  n | t j	 o | |  } n | d
 d i |  qWt |  d j o |  i | d	 |	  nq t | d d  d j o |  i | d	 |  n? | d d t j p t  t | d d  d j p t  | d d d } | o1 | i d j o! t |  d j o | d } n | S(   st  
        Parse 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: C{str}
        @param s: The string to parse
        
        @type brackets: length-2 C{str}
        @param brackets: The bracket characters used to mark the
            beginning and end of trees and subtrees.
            
        @type parse_node: C{function}
        @type parse_leaf: C{function}
        @param parse_node, parse_leaf: If specified, these functions
            are applied to the substrings of C{s} corresponding to
            nodes and leaves (respectively) to obtain the values for
            those nodes and leaves.  They should have the following
            signature:

               >>> parse_node(str) -> value

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

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

        @type remove_empty_top_bracketing: C{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 C{s}.
            If this class method is called using a subclass of C{Tree},
            then it will return a tree of that type.
        @rtype: C{Tree}
        i   s"   brackets must be a length-2 strings   \ss   whitespace brackets not alloweds
   [^\s%s%s]+s   %s\s*(%s)?|%s|(%s)i    i   s   end-of-stringit    (   R	   R
   R&   R   t   ret   searcht   escapeR   t   compilet   finditert   groupt   _parse_errort   lstripR.   RG   t   AssertionErrorR   (   R   t   st   bracketst
   parse_nodet
   parse_leaft   node_patternt   leaf_patternt   remove_empty_top_bracketingt   open_bt   close_bt   open_patternt   close_patternt   token_reRI   t   matcht   tokenR   R   t   tree(    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyR     sV    5$	 .""*c      	   C   s  | d j o t  |  d } } n | i   | i   } } d |  i | | d | f } | i d d  i d d  } | } t  |  | d j o | | d  d } n | d j o d | | d } d	 } n | d
 d | d d | f 7} t |   d S(   s   
        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.
        s   end-of-strings1   %s.parse(): expected %r but got %r
%sat index %d.t    i   s   
s   	i
   s   ...i   s   
%s"%s"
%s^i   i   Ns               s                   (   R&   RM   Rw   R   t   replaceRL   (   R   R{   R   t	   expectingRF   R   t   msgt   offset(    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyRx      s    	
c         C   s   d d k  l } | |   d S(   sP   
        Open a new window containing a graphical diagram of this tree.
        i(   t
   draw_treesN(   t   nltk.draw.treeR   (   R   R   (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyt   draw=  s    c         C   s3   d i  d   |  D  } d |  i i |  i | f S(   Ns   , c         s   s   x |  ] } t  |  Vq Wd  S(   N(   t   repr(   R6   t   c(    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pys	   <genexpr>E  s    s   %s(%r, [%s])(   t   joinRg   R   R   (   R   t   childstr(    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyt   __repr__D  s    c         C   s
   |  i    S(   N(   t   pprint(   R   (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyt   __str__H  s    iF   Rq   c      	   C   sr  |  i  | | |  } t |  | | j  o | Sn t |  i t  o d | d |  i | f } n d | d |  i | f } x |  D] } t | t  o7 | d d | d | i | | d | | |  7} q t | t  o' | d d | d d i |  7} q t | t	  o* | o" | d d | d d | 7} q | d d | d d	 | 7} q W| | d
 S(   sS  
        @return: A pretty-printed string representation of this tree.
        @rtype: C{string}
        @param margin: The right margin at which to do line-wrapping.
        @type margin: C{int}
        @param indent: The indentation level at which printing
            begins.  This number is used to decide how far to indent
            subsequent lines.
        @type indent: C{int}
        @param nodesep: A string that is used to separate the node
            from the children.  E.g., the default value C{':'} gives
            trees like C{(S: (NP: I) (VP: (V: saw) (NP: it)))}.
        s   %s%s%si    s   %s%r%ss   
R   i   t   /s   %ss   %ri   (
   t   _pprint_flatR&   R	   R   R
   R   R   t   tupleR   RB   (   R   t   margint   indentt   nodesept   parenst   quotesR{   R/   (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyR   K  s"     "'""c      	   C   s    d |  i  d d d d d d	  S(
   s  
        Returns a representation of the tree compatible with the
        LaTeX qtree package. This consists of the string C{\Tree}
        followed by the parse tree represented in bracketed notation.

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

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

        See U{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: C{string}
        s   \Tree R   i   R   Rq   R   s   [.s    ](   s   [.s    ](   R   (   R   (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyt   pprint_latex_qtreep  s    c      	   C   s   g  } x |  D] } t  | t  o  | i | i | | |   q t  | t  o | i d i |   q t  | t  o | o | i d |  q | i d |  q Wt  |  i t  o/ d | d |  i | t	 i |  | d f Sn, d | d |  i | t	 i |  | d f Sd  S(   NR   s   %ss   %rs   %s%s%s %s%si    i   s   %s%r%s %s%s(
   R	   R   R.   R   R   R   RB   R   R
   t   string(   R   R   R   R   t	   childstrsR/   (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyR     s      (.   R   t
   __module__t   __doc__R   R   R   R   R   R   R   R   R   R   R    R!   R"   R%   R(   R+   R-   R0   R2   R;   R?   RD   RF   RK   RQ   RU   R   R]   R   R`   Rd   t   classmethodRi   Rk   Rp   R   Rx   R   R   R   R   R   R   (    (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyR      sX   																							k			%	Rj   c           B   s   e  Z e d   Z d   Z d   Z d   Z d   Z d   Z d   Z	 d   Z
 d   Z e d	  Z d
   Z d   Z d   Z d   Z d   Z d   Z e e e  Z RS(   c         C   s|   | d  j o d  Sn t t |   i | |  y" t |  i t |   f  |  _ Wn% t t	 f j
 o t	 d   n Xd  S(   Ns9   ImmutableTree's node value and children must be immutable(
   R   t   superRj   R   Rl   R   R   t   _hashR   RL   (   R   R   R   (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyR     s    "c         C   s   t  d  d  S(   Ns"   ImmutableTrees may not be modified(   RL   (   R   (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyR(     s    c         C   s   t  d  d  S(   Ns"   ImmutableTrees may not be modified(   RL   (   R   (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyt   __setslice__  s    c         C   s   t  d  d  S(   Ns"   ImmutableTrees may not be modified(   RL   (   R   (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyR+     s    c         C   s   t  d  d  S(   Ns"   ImmutableTrees may not be modified(   RL   (   R   (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyt   __delslice__  s    c         C   s   t  d  d  S(   Ns"   ImmutableTrees may not be modified(   RL   (   R   (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyt   __iadd__  s    c         C   s   t  d  d  S(   Ns"   ImmutableTrees may not be modified(   RL   (   R   (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyt   __imul__  s    c         C   s   t  d  d  S(   Ns"   ImmutableTrees may not be modified(   RL   (   R   R   (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyR.     s    c         C   s   t  d  d  S(   Ns"   ImmutableTrees may not be modified(   RL   (   R   R   (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyR,     s    c         C   s   t  d  d  S(   Ns"   ImmutableTrees may not be modified(   RL   (   R   R   (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyRG     s    c         C   s   t  d  d  S(   Ns"   ImmutableTrees may not be modified(   RL   (   R   R   (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyt   remove  s    c         C   s   t  d  d  S(   Ns"   ImmutableTrees may not be modified(   RL   (   R   (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyt   reverse  s    c         C   s   t  d  d  S(   Ns"   ImmutableTrees may not be modified(   RL   (   R   (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyt   sort  s    c         C   s   |  i  S(   N(   R   (   R   (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyt   __hash__  s    c         C   s*   t  |  d  o t d  n | |  _ d S(   s|   Set self._node.  This will only succeed the first time the
        node value is set, which should occur in Tree.__init__().R   s"   ImmutableTrees may not be modifiedN(   R   RL   t   _node(   R   R   (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyt	   _set_node  s    c         C   s   |  i  S(   N(   R   (   R   (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyt	   _get_node  s    (   R   R   R   R   R(   R   R+   R   R   R   R.   R,   RG   R   R   R   R   R   R   t   propertyR   (    (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyRj     s"   
														t   AbstractParentedTreec           B   s   e  Z d  Z e d  Z e d  Z d   Z d   Z d   Z	 d   Z
 d   Z d   Z d	 d
  Z d   Z e e d  o d   Z d   Z d   Z n RS(   s  
    An abstract base class for L{Tree}s that automatically maintain
    pointers to their parents.  These parent pointers are updated
    whenever any change is made to a tree's structure.  Two subclasses
    are currently defined:

      - L{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.
        
      - L{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 C{AbstractParentedTree} class redefines all operations that
    modify a tree's structure to call two methods, which are used by
    subclasses to update parent information:
    
      - L{_setparent()} is called whenever a new child is added.
      - L{_delparent()} is called whenever a child is removed.
    c         C   s   | d  j o d  Sn t t |   i | |  xD t |   D]6 \ } } t | t  o |  i | | d t q; q; Wx> t |   D]0 \ } } t | t  o |  i | |  q q Wd  S(   Nt   dry_run(	   R   R   R   R   R:   R	   R   t
   _setparentR   (   R   R   R   R8   R/   (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyR     s      c         C   s   t  d   d S(   s  
        Update C{child}'s parent pointer to point to self.  This
        method is only called if C{child}'s type is L{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 C{self}'s
        child list.

        @type child: L{Tree}
        @type index: C{int}
        @param index: The index of C{child} in C{self}.
        @raise TypeError: If C{child} is a tree with an impropriate
            type.  Typically, if C{child} is a tree, then its type needs
            to match C{self}'s type.  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.
        s   Abstract base classN(   Rz   (   R   R/   R'   R   (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyR     s    c         C   s   t  d   d S(   s  
        Update C{child}'s parent pointer to not point to self.  This
        method is only called if C{child}'s type is L{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
        C{self}'s child list.
        
        @type child: L{Tree}
        @type index: C{int}
        @param index: The index of C{child} in C{self}.
        s   Abstract base classN(   Rz   (   R   R/   R'   (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyt
   _delparent  s    c         C   su  t  | t  ou t |  |  \ } } xC t | |  D]2 } t  |  | t  o |  i |  | |  q5 q5 Wt t |   i |  n t  | t	  o | d j  o | t
 |   7} n | d j  o t d   n t  |  | t  o |  i |  | |  n t t |   i |  nY t
 |  d j o t d   n6 t
 |  d j o |  | d =n |  | d | d =d  S(   Ni    s   index out of ranges(   The tree position () may not be deleted.i   (   R	   R$   R   t   xrangeR   R   R   R   R+   R#   R&   R)   (   R   R'   RM   t   stopR8   (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyR+     s$     !c      	   C   st  t  | t  o.t |  |  \ } } t  | t t f  p t |  } n xH t |  D]: \ } } t  | t  o |  i | | | d t qX qX WxC t	 | |  D]2 } t  |  | t  o |  i
 |  | |  q q WxB t |  D]4 \ } } t  | t  o |  i | | |  q q Wt t |   i | |  n3t  | t  o | d j  o | t |   7} n | d j  o t d   n | |  | j o d  Sn t  | t  o |  i | |  n t  |  | t  o |  i
 |  | |  n t t |   i | |  n_ t |  d j o t d   n< t |  d j o | |  | d <n | |  | d | d <d  S(   NR   i    s   index out of ranges,   The tree position () may not be assigned to.i   (   R	   R$   R   R   R   R:   R   R   R   R   R   R   R   R(   R#   R&   R)   (   R   R'   R*   RM   R   R8   R/   (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyR(   =  s@     "  !c         C   sD   t  | t  o |  i | t |    n t t |   i |  d  S(   N(   R	   R   R   R&   R   R   R.   (   R   R/   (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyR.   r  s    c         C   sU   xN | D]F } t  | t  o |  i | t |    n t t |   i |  q Wd  S(   N(   R	   R   R   R&   R   R   R.   (   R   R   R/   (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyR,   w  s
     c         C   sy   | d j  o | t  |   7} n | d j  o
 d } n t | t  o |  i | |  n t t |   i | |  d  S(   Ni    (   R&   R	   R   R   R   R   t   insert(   R   R'   R/   (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyR   }  s
    !ic         C   s   | d j  o | t  |   7} n | d j  o t d   n t |  | t  o |  i |  | |  n t t |   i |  S(   Ni    s   index out of range(   R&   R)   R	   R   R   R   R   RG   (   R   R'   (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyRG     s
    !c         C   sU   |  i  |  } t |  | t  o |  i |  | |  n t t |   i |  d  S(   N(   R'   R	   R   R   R   R   R   (   R   R/   R'   (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyR     s    t   __getslice__c         C   s(   |  i  t t d |  t d |    S(   Ni    (   R%   R$   R1   (   R   RM   R   (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyR     s    c         C   s(   |  i  t t d |  t d |    S(   Ni    (   R+   R$   R1   (   R   RM   R   (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyR     s    c         C   s+   |  i  t t d |  t d |   |  S(   Ni    (   R(   R$   R1   (   R   RM   R   R*   (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyR     s    (   R   R   R   R   R   R   R   R   R+   R(   R.   R,   R   RG   R   R   R   R   R   R   (    (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyR     s   		!	5							t   ParentedTreec           B   s   e  Z d  Z e d  Z d   Z d   Z d   Z d   Z d   Z	 d   Z
 e d   d	 d
 Z e e d	 d Z e e d	 d Z e e d	 d Z e e
 d	 d Z e e	 d	 d Z e Z d   Z e d  Z RS(   s6  
    A L{Tree} that automatically maintains parent pointers for
    single-parented trees.  The following read-only property values
    are automatically updated whenever the structure of a parented
    tree is modified: L{parent}, L{parent_index}, L{left_sibling},
    L{right_sibling}, L{root}, L{treeposition}.

    Each C{ParentedTree} may have at most one parent.  In
    particular, subtrees may not be shared.  Any attempt to reuse a
    single C{ParentedTree} as a child of more than one parent (or
    as multiple children of the same parent) will cause a
    C{ValueError} exception to be raised.
    
    C{ParentedTrees} should never be used in the same tree as C{Trees}
    or C{MultiParentedTrees}.  Mixing tree implementations may result
    in incorrect parent pointers and in C{TypeError} exceptions.
    c         C   s;   | d  j o d  Sn d  |  _ t t |   i | |  d  S(   N(   R   t   _parentR   R   R   (   R   R   R   (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyR     s    	c         C   s   t  S(   N(   t   ImmutableParentedTree(   R   (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyRk     s    c         C   sb   |  i  d  j o d  Sn x2 t |  i   D]! \ } } | |  j o | Sq( q( Wt p
 t d  d  S(   Ns&   expected to find self in self._parent!(   R   R   R:   R   Rz   (   R   R8   R/   (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyt   _get_parent_index  s
     c         C   s:   |  i    } |  i o  | d j o |  i | d Sn d  S(   Ni    i   (   R   R   R   (   R   t   parent_index(    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyt   _get_left_sibling  s    c         C   sG   |  i    } |  i o- | t |  i  d j  o |  i | d Sn d  S(   Ni   (   R   R   R&   R   (   R   R   (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyt   _get_right_sibling  s    $c         C   s6   |  i  d  j o d Sn |  i  i   |  i   f Sd  S(   N(    (   R   R   t   _get_treepositionR   (   R   (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyR     s    c         C   s)   |  i  d  j o |  Sn |  i  i   Sd  S(   N(   R   R   t	   _get_root(   R   (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyR     s    c         C   s   |  i  S(    (   R   (   R   (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyt   <lambda>  s    t   docsA   
        The parent of this tree, or C{None} if it has no parent.s4  
        The index of this tree in its parent.  I.e.,
        C{ptree.parent[ptree.parent_index] is ptree}.  Note that
        C{ptree.parent_index} is not necessarily equal to
        C{ptree.parent.index(ptree)}, since the C{index()} method
        returns the first child that is I{equal} to its argument.sB   
        The left sibling of this tree, or C{None} if it has none.sC   
        The right sibling of this tree, or C{None} if it has none.s   
        The root of this tree.  I.e., the unique ancestor of this tree
        whose parent is C{None}.  If C{ptree.parent} is C{None}, then
        C{ptree} is its own root.s   
        The tree position of this tree, relative to the root of the
        tree.  I.e., C{ptree.root[ptree.treeposition] is ptree}.c         C   sS   t  | t  p t  |  | | j p t  | i |  j p t  d  | _ d  S(   N(   R	   R   Rz   R   R   (   R   R/   R'   (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyR     s    c         C   s\   t  | t  p t d d   n | i d  j	 o t d   n | p |  | _ n d  S(   Ns"   Can not insert a non-ParentedTree s   into a ParentedTrees3   Can not insert a subtree that already has a parent.(   R	   R   R   R   R   RL   (   R   R/   R'   R   (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyR     s    (   R   R   R   R   R   Rk   R   R   R   R   R   R   t   parentR   t   left_siblingt   right_siblingt   roott   treepositionRJ   R   R   R   (    (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyR     s.   																			t   MultiParentedTreec           B   s   e  Z d  Z e d  Z d   Z d   Z d   Z d   Z d   Z	 d   Z
 e d   d	 d
 Z e e d	 d Z e e d	 d Z e e	 d	 d Z d   Z d   Z d   Z e d  Z RS(   s.  
    A L{Tree} that automatically maintains parent pointers for
    multi-parented trees.  The following read-only property values are
    automatically updated whenever the structure of a multi-parented
    tree is modified: L{parents}, L{parent_indices}, L{left_siblings},
    L{right_siblings}, L{roots}, L{treepositions}.
    
    Each C{MultiParentedTree} may have zero or more parents.  In
    particular, subtrees may be shared.  If a single
    C{MultiParentedTree} is used as multiple children of the same
    parent, then that parent will appear multiple times in its
    C{parents} property.

    C{MultiParentedTrees} should never be used in the same tree as
    C{Trees} or C{ParentedTrees}.  Mixing tree implementations may
    result in incorrect parent pointers and in C{TypeError} exceptions.
    c         C   s;   | d  j o d  Sn g  |  _ t t |   i | |  d  S(   N(   R   t   _parentsR   R   R   (   R   R   R   (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyR   (  s    	c         C   s   t  S(   N(   t   ImmutableMultiParentedTree(   R   (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyRk   2  s    c         C   sR   g  } |  i  D]= } t |  D]* \ } } | |  j o | | | f q q q ~ S(   N(   R   R:   (   R   Rf   R   R'   R/   (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyt   _get_parent_indices8  s    c         C   sD   g  } |  i    D], \ } } | d j o | | | d q q ~ S(   Ni    i   (   R   (   R   Rf   R   R'   (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyt   _get_left_siblings>  s    c         C   sN   g  } |  i    D]6 \ } } | t |  d j  o | | | d q q ~ S(   Ni   (   R   R&   (   R   Rf   R   R'   (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyt   _get_right_siblingsC  s    c         C   s   |  i  h   i   S(   N(   t   _get_roots_helpert   values(   R   (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyt
   _get_rootsH  s    c         C   sC   |  i  o% x2 |  i  D] } | i |  q Wn |  | t |   <| S(   N(   R   R   t   id(   R   t   resultR   (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyR   K  s    

 c         C   s   t  |  i  S(    (   R   R   (   R   (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyR   S  s    R   s  
        The set of parents of this tree.  If this tree has no parents,
        then C{parents} is the empty set.  To check if a tree is used
        as multiple children of the same parent, use the
        L{parent_indices} property.

        @type: C{list} of L{MultiParentedTree}s|  
        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: C{list} of L{MultiParentedTree}s  
        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: C{list} of L{MultiParentedTree}s   
        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: C{list} of L{MultiParentedTree}c         C   sX   | |  i  j o g  Sn= g  } t |  D]$ \ } } | |  j o | | q) q) ~ Sd S(   sW  
        Return a list of the indices where this tree occurs as a child
        of C{parent}.  If this child does not occur as a child of
        C{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   Rf   R'   R/   (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyt   parent_indicest  s    
c         C   s   |  | j o d g Snj g  } |  i  D]T } | i |  D]> } t |  D]+ \ } } | |  j o | | | f qI qI q9 q& ~ Sd S(   s  
        Return a list of all tree positions that can be used to reach
        this multi-parented tree starting from C{root}.  I.e., the
        following is always true::

          for treepos in ptree.treepositions(root):
              root[treepos] is ptree
        N(    (   R   R;   R:   (   R   R   Rf   R   RJ   R'   R/   (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyR;     s    	c         C   s   t  | t  p t  |  | | j p t  t g  } | i D] } | |  j o | | q@ q@ ~  d j p t  xI t |   D]+ \ } } | | j o | | j o Pq q W| i i |   d  S(   Ni   (   R	   R   Rz   R&   R   R:   R   (   R   R/   R'   Rf   R7   R8   R   (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyR     s    I #c         C   si   t  | t  p t d d   n | p: x7 | i D] } | |  j o Pq5 q5 W| i i |   n d  S(   Ns'   Can not insert a non-MultiParentedTree s   into a MultiParentedTree(   R	   R   R   R   R.   (   R   R/   R'   R   R   (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyR     s    
 (   R   R   R   R   R   Rk   R   R   R   R   R   R   t   parentst   left_siblingst   right_siblingst   rootsR   R;   R   R   R   (    (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyR     s(   
																R   c           B   s   e  Z e d   Z RS(   c         C   s2   | d  j o d  Sn t t |   i | |  d  S(   N(   R   R   R   R   (   R   R   R   (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyR     s    (   R   R   R   R   (    (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyR     s   R   c           B   s   e  Z e d   Z RS(   c         C   s2   | d  j o d  Sn t t |   i | |  d  S(   N(   R   R   R   R   (   R   R   R   (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyR     s    (   R   R   R   R   (    (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyR     s   t   ProbabilisticTreec           B   sw   e  Z e d   Z e d  Z d   Z d   Z d   Z d   Z d   Z	 d   Z
 e d  Z d	   Z e e  Z RS(
   c         K   s   t  t |   i |  | |  S(   N(   R   R   R   (   R   R   R   t   prob_kwargs(    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyR     s    c         K   s<   | d  j o d  Sn t i |  | |  t i |  |  d  S(   N(   R   R   R   R   (   R   R   R   R   (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyR     s    c         C   s   t  S(   N(   t   ImmutableProbabilisticTree(   R   (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyRk     s    c         C   s   d t  i |   |  i   f S(   Ns	   %s (p=%s)(   R   R   t   prob(   R   (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyR     s    c         C   s    d |  i  d d  |  i   f S(   Ns	   %s (p=%s)R   i<   (   R   R   (   R   (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyR     s    c         C   s@   t  i |  |  } | d j o | Sn t |  i   | i    S(   Ni    (   R   t   __cmp__t   cmpR   (   R   R   R   (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyR     s    c         C   sA   t  | t  p t Sn t i |  |  o |  i   | i   j S(   N(   R	   R   R   R   R   (   R   R   (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyR     s    c         C   s   |  | j S(   N(    (   R   R   (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyR     s    c         C   s>   | p# |  i  |  i |  d |  i   Sn |  i  i |   Sd  S(   NR   (   Rg   R   R   Rd   (   R   Rh   (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyRi     s    *c         C   s   t  | t  ot g  } | D] } | |  i |  q ~ } t  | t  o  |  | i | d | i   Sq |  | i | d d Sn | Sd  S(   NR   g      ?(   R	   R   Rd   R   R   R   (   R   Re   Rf   R/   R   (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyRd     s    * (   R   R   R   R   R   Rk   R   R   R   R   R   R   Ri   Rd   R   (    (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyR     s   								R   c           B   sw   e  Z e d   Z e d  Z d   Z d   Z d   Z d   Z d   Z	 d   Z
 e d  Z d	   Z e e  Z RS(
   c         K   s   t  t |   i |  | |  S(   N(   R   R   R   (   R   R   R   R   (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyR     s    c         K   s<   | d  j o d  Sn t i |  | |  t i |  |  d  S(   N(   R   Rj   R   R   (   R   R   R   R   (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyR     s    c         C   s   t  S(   N(   R   (   R   (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyRk     s    c         C   s   d t  i |   |  i   f S(   Ns   %s [%s](   R   R   R   (   R   (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyR     s    c         C   s    d |  i  d d  |  i   f S(   Ns   %s [%s]R   i<   (   R   R   (   R   (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyR     s    c         C   s@   t  i |  |  } | d j o | Sn t |  i   | i    S(   Ni    (   R   R   R   R   (   R   R   R   (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyR     s    c         C   sA   t  | t  p t Sn t i |  |  o |  i   | i   j S(   N(   R	   R   R   R   R   (   R   R   (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyR     s    c         C   s   |  | j S(   N(    (   R   R   (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyR     s    c         C   s>   | p# |  i  |  i |  d |  i   Sn |  i  i |   Sd  S(   NR   (   Rg   R   R   Rd   (   R   Rh   (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyRi     s    *c         C   s   t  | t  ot g  } | D] } | |  i |  q ~ } t  | t  o  |  | i | d | i   Sq |  | i | d d Sn | Sd  S(   NR   i   (   R	   R   Rd   R   R   R   (   R   Re   Rf   R/   R   (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyRd      s    * (   R   R   R   R   R   Rk   R   R   R   R   R   R   Ri   Rd   R   (    (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyR     s   								c         C   sR   g  } xE |  D]= } t  | t  o | i t | i   q | i |  q W| S(   N(   R	   R   R.   R   R   (   R   t   namesR/   (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyRC     s     c         C   s   t  i |  d t S(   s$  
    Parse a treebank string and return a tree.  Trees are represented
    as nested brackettings, e.g. (S (NP (NNP John)) (VP (V runs))).

    @return: A tree corresponding to the string representation.
    @rtype: C{tree}
    @param s: The string to be converted
    @type s: C{string}
    R   (   R   R   R   (   R{   (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyt   bracket_parse  s    
c         C   s  t  i d |   } x t t |   D] } | | d j o) | | | | d | | d <| | <q% d | | j o^ | | i d  } t |  d j o | d | | <q d | d d | d d	 | | <q% | | d
 j o d | | <q% q% Wt i |  } t |  S(   s  
    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: C{tree}
    @param s: The string to be converted
    @type s: C{string}
    s   ([()| ])t   (i   t   :i   iR   it   )RS   Rq   (   Rr   t   splitRH   R&   R   R   R   (   R{   t   tokensR8   t   fieldst   treebank_string(    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyt   sinica_parse'  s     )&c          C   ss  d d k  l }  d } t |  } d GH| GH| i   GHd GH| i GH| d GH| d GH| i   GH| i   GH| d GH| d GH| d GH| d } | i d |  i d   d	 GH| GH|  i d
  | d <| GHHd GH| i	   | GHd GH| i
   | GHH|  i d d d g d d } d GH| GHH|  i | i    } d GH| GHHd GH| i   GHHd GH| i   GHHd | _ | GHd S(   s   
    A demonstration showing how C{Tree}s and C{Tree}s can be
    used.  This demonstration creates a C{Tree}, and loads a
    C{Tree} from the L{treebank<nltk.corpus.treebank>} corpus,
    and shows the results of calling several of their methods.
    i(   R   sA   (S (NP (DT the) (NN cat)) (VP (VBD ate) (NP (DT a) (NN cookie))))s#   Convert bracketed string into tree:s   Display tree properties:i    i   s   (JJ big)s   Tree modification:s	   (NN cake)s   Collapse unary:s   Chomsky normal form:t   xt   yt   zR   g      ?s   Probabilistic Tree:s0   Convert tree to bracketed string and back again:s   LaTeX output:s   Production output:t   testi   N(   i   i   (   i   i   i    (   i   i   i   (   s   testi   (   t   nltkR   R   R   R   R2   R-   R   R   R`   RU   R   R   R   RD   (   R   R{   t   tt   the_catt   pt(    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pyt   demoK  sZ    					


	t   __main__R   R   R   (   R   Rr   R   t   grammarR    R   t   probabilityR   t   utilR   R   R   Rj   R   R   R   R   R   R   R   RC   R   R   R   R   t   __all__(    (    (    s   /p/zhu/06/nlp/nltk/nltk/tree.pys   <module>   s4     ~4q&'			$	G			