
4Ic           @   s   d  Z  d d k Z d d k l Z l Z d d k Td e f d     YZ d   Z e	 e	 d  Z
 d	 e f d
     YZ d e f d     YZ d   Z d   Z e d j o e   n d S(   s    
Graphically display a C{Tree}.
iN(   t   Treet   bracket_parse(   t   *t   TreeSegmentWidgetc           B   s   e  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   Z d   Z d   Z d   Z d   Z RS(   s?  
    A canvas widget that displays a single segment of a hierarchical
    tree.  Each C{TreeSegmentWidget} connects a single X{node widget}
    to a sequence of zero or more X{subtree widgets}.  By default, the
    bottom of the node is connected to the top of each subtree by a
    single line.  However, if the C{roof} attribute is set, then a
    single triangular "roof" will connect the node to all of its
    children.  

    Attributes:
      - C{roof}: What sort of connection to draw between the node and
        its subtrees.  If C{roof} is true, draw a single triangular
        "roof" over the subtrees.  If C{roof} is false, draw a line
        between each subtree and the node.  Default value is false.
      - C{xspace}: The amount of horizontal space to leave between
        subtrees when managing this widget.  Default value is 10.
      - C{yspace}: The amount of space to place between the node and
        its children when managing this widget.  Default value is 15.
      - C{color}: The color of the lines connecting the node to its
        subtrees; and of the outline of the triangular roof.  Default
        value is C{'#006060'}.
      - C{fill}: The fill color for the triangular roof.  Default
        value is C{''} (no fill).
      - C{width}: The width of the lines connecting the node to its
        subtrees; and of the outline of the triangular roof.  Default
        value is 1.
      - C{orientation}: Determines whether the tree branches downwards
        or rightwards.  Possible values are C{'horizontal'} and
        C{'vertical'}.  The default value is C{'vertical'} (i.e.,
        branch downwards).
      - C{draggable}: whether the widget can be dragged by the user.

    The following attributes may also be added in the near future:
      - C{lineM{n}_color}: The color of the line connecting the node
        to its C{M{n}}th subtree.
      - C{lineM{n}_color}: The width of the line connecting the node
        to its C{M{n}}th subtree.
      - C{lineM{n}_color}: The dash pattern of the line connecting the
        node to its C{M{n}}th subtree.
        
    c         K   s   | |  _  | |  _ d |  _ d |  _ d |  _ d |  _ t |  _ g  } | D]% } | | i d d d d d d qJ ~ |  _	 | i
 d d d d d d d	 d |  _ |  i |  x | D] } |  i |  q Wt |  _ t i |  | |  d
 S(   sR   
        @type node: 
        @type subtrees: C{list} of C{CanvasWidgetI}
        i    i
   i   t   fills   #006060t    t   statet   hiddent   outlineN(   t   _nodet	   _subtreest   _horizontalt   _rooft   _xspacet   _yspacet   Falset   _orderedt   create_linet   _linest   create_polygont   _polygont   _add_child_widgett	   _managingt   CanvasWidgett   __init__(   t   selft   canvast   nodet   subtreest   attribst   _[1]t   ct   subtree(    (    s$   /p/zhu/06/nlp/nltk/nltk/draw/tree.pyR   B   s"    							5 	c         C   s  |  i    } | d j o | |  _ |  i oA x$ |  i D] } | i | d d q6 W| i |  i d d qx$ |  i D] } | i | d d qw W| i |  i d d n| d j oD | d j o d |  _ q| d j o d	 |  _ qt d
   n| d j oA x$ |  i D] } | i | d | qW| i |  i d | nSt | t  o? | d	 d j o. |  i t	 | d  } | i | d | n| d j o | i |  i d | n | d j oM | i |  i h  | | < x |  i D] } | i | h  | | < qWn | d j oH | d j o | |  _
 n | d j o | |  _ n |  i |  i  n. | d j o | |  _ n t i |  | |  d  S(   Nt   roofR   R   t   normalt   orientationt
   horizontali   t   verticali    s*   orientation must be horizontal or verticalt   colorR   R   t   widtht   xspacet   yspacet   ordered(   s   xspaces   yspace(   R   R   R   t
   itemconfigR   R   t
   ValueErrort
   isinstancet   tuplet   intR   R   t   updateR	   R   R   t   __setitem__(   R   t   attrt   valueR   t   l(    (    s$   /p/zhu/06/nlp/nltk/nltk/draw/tree.pyR1   a   sD    	

 
 
 !
 'c         C   sA  | d j o |  i  Sn&| d j o |  i   i |  i |  Sn | d j o |  i   i |  i d  Sn t | t  oB | d d j o1 |  i t | d  } |  i   i | d  Sn | d j o |  i Snh | d	 j o |  i	 SnP | d
 j o |  i
 o d Sq=d Sn) | d j o |  i Sn t i |  |  Sd  S(   NR!   R'   R&   R   i    i   R   R(   R)   R#   R$   R%   R*   (   R   R   t   itemcgetR   R-   R.   R   R/   R   R   R   R   R   t   __getitem__(   R   R2   R4   (    (    s$   /p/zhu/06/nlp/nltk/nltk/draw/tree.pyR6      s     !c         C   s   |  i  S(   N(   R	   (   R   (    (    s$   /p/zhu/06/nlp/nltk/nltk/draw/tree.pyR      s    c         C   s   |  i  S(   N(   R
   (   R   (    (    s$   /p/zhu/06/nlp/nltk/nltk/draw/tree.pyR      s    c         C   s:   |  i  |  i  |  i |  | |  _ |  i |  i  d S(   s*   
        Set the node to C{node}.
        N(   t   _remove_child_widgetR	   R   R0   (   R   R   (    (    s$   /p/zhu/06/nlp/nltk/nltk/draw/tree.pyt   set_node   s    	c         C   sJ   |  i  i |  } | |  i  | <|  i |  |  i |  |  i |  d S(   sA   
        Replace the child C{oldchild} with C{newchild}.
        N(   R
   t   indexR7   R   R0   (   R   t   oldchildt   newchildR9   (    (    s$   /p/zhu/06/nlp/nltk/nltk/draw/tree.pyt   replace_child   s
    c         C   sY   |  i  i |  } |  i  | =|  i |  |  i   i |  i i    |  i |  i  d  S(   N(	   R
   R9   R7   R   t   deleteR   t   popR0   R	   (   R   t   childR9   (    (    s$   /p/zhu/06/nlp/nltk/nltk/draw/tree.pyt   remove_child   s
    
c         C   s\   |  i  i | |  |  i |  |  i i t i d d d d d d  |  i |  i  d  S(   Ni    R   s   #006060(	   R
   t   insertR   R   t   appendR   R   R0   R	   (   R   R9   R?   (    (    s$   /p/zhu/06/nlp/nltk/nltk/draw/tree.pyt   insert_child   s    (c         C   s#   |  i  o |  i g Sn |  i Sd  S(   N(   R   R   R   (   R   (    (    s$   /p/zhu/06/nlp/nltk/nltk/draw/tree.pyt   _tags   s    
c         C   s   t  | t  o | i   i   } n | i   } |  i o" | d | d | d d f Sn | d | d d | d f Sd  S(   Ni    i   i   g       @i   (   R-   R   R   t   bboxR   (   R   R?   RE   (    (    s$   /p/zhu/06/nlp/nltk/nltk/draw/tree.pyt   _subtree_top   s    
"c         C   s]   |  i  i   } |  i o" | d | d | d d f Sn | d | d d | d f Sd  S(   Ni   i   i   g       @i    (   R	   RE   R   (   R   RE   (    (    s$   /p/zhu/06/nlp/nltk/nltk/draw/tree.pyt   _node_bottom   s    
"c         C   s	  t  |  i  d j o d  Sn |  i i   d  j o d  Sn | |  i j o |  i } n
 | g } |  i o |  i o |  i |  } n |  i   \ } } |  i d i   \ } } } } xm |  i d D]^ }	 |	 i   }
 t	 | |
 d  } t	 | |
 d  } t
 | |
 d  } t
 | |
 d  } q W|  i o2 |  i   i |  i | | | | | | | | 	 n/ |  i   i |  i | | | | | | | | 	 xm | D]e }	 |  i   \ } } |  i |  i i |	  } |  i |	  \ } } |  i   i | | | | |  qWd  S(   Ni    i   i   i   (   t   lenR
   R	   RE   t   NoneR   R   t   _maintain_orderRG   t   mint   maxR   R   t   coordsR   R   R9   RF   (   R   R?   t   need_updatet   nodext   nodeyt   xmint   ymint   xmaxt   ymaxR    RE   t   linet   subtreext   subtreey(    (    s$   /p/zhu/06/nlp/nltk/nltk/draw/tree.pyt   _update   s4    	 
 c         C   s,   |  i  o |  i |  Sn |  i |  Sd  S(   N(   R   t   _maintain_order_horizontalt   _maintain_order_vertical(   R   R?   (    (    s$   /p/zhu/06/nlp/nltk/nltk/draw/tree.pyRJ      s    
c         C   sU  | i    \ } } } } | |  i j oj x\ |  i D]Q } | i    \ } } }	 }
 | |  i | j o | i d | |  i |  q2 q2 W|  i Sn| g } |  i i |  } | |  i } x t | d t |  i   D]z } |  i | i    \ } } }	 }
 | | j oH |  i | i | | d  | |	 | |  i 7} | i	 |  i |  q q W| |  i } x t | d d d  D]z } |  i | i    \ } } }	 }
 | |	 j  oH |  i | i | |	 d  | |	 | |  i 8} | i	 |  i |  qyqyW|  i i    \ } } }	 }
 |
 | |  i j o+ |  i i d | |  i |
  |  i } n | S(   Ni    i   i(
   RE   R	   R
   R   t   moveR9   R   t   rangeRH   RB   (   R   R?   t   leftt   topt   rightt   botR    t   x1t   y1t   x2t   y2t   movedR9   t   xt   i(    (    s$   /p/zhu/06/nlp/nltk/nltk/draw/tree.pyRZ      s>    
 #	  c         C   sR  | i    \ } } } } | |  i j og xY |  i D]N } | i    \ } } }	 }
 | |  i | j o | i | |  i |  q2 q2 W|  i Sn| g } |  i i |  } | |  i } x t | d t |  i   D]z } |  i | i    \ } } }	 }
 | | j oH |  i | i d | |  | |
 | |  i 7} | i	 |  i |  q q W| |  i } x t | d d d  D]z } |  i | i    \ } } }	 }
 | |
 j  oH |  i | i d | |
  | |
 | |  i 8} | i	 |  i |  qvqvW|  i i    \ } } }	 }
 |	 | |  i j o+ |  i i | |  i |	 d  |  i } n | S(   Ni   i    i(
   RE   R	   R
   R   R[   R9   R   R\   RH   RB   (   R   R?   R]   R^   R_   R`   R    Ra   Rb   Rc   Rd   Re   R9   t   yRg   (    (    s$   /p/zhu/06/nlp/nltk/nltk/draw/tree.pyRY   *  s>    
  	  c   	   	   C   s   |  i    \ } } d } xm |  i D]b } | i   } | | d |  i } | | d } | i | |  | | d | d |  i 7} q" Wd } x( |  i D] } | |  i |  d 7} q W| t |  i  :} x% |  i D] } | i d | |  q Wd  S(   Ni   i    i   i   g        (   RG   R
   RE   R   R[   R   RF   RH   (	   R   RO   RP   Rh   R    t   subtree_bboxt   dxt   dyt   center(    (    s$   /p/zhu/06/nlp/nltk/nltk/draw/tree.pyt   _manage_horizontalT  s"    
 !
 
 c   	      C   s   |  i    \ } } d } xm |  i D]b } | i   } | | d |  i } | | d } | i | |  | | d | d |  i 7} q" Wd } x5 |  i D]* } | |  i |  d t |  i  7} q Wx% |  i D] } | i | | d  q Wd  S(   Ni    i   i   g        (   RG   R
   RE   R   R[   R   RF   RH   (	   R   RO   RP   Rf   R    Ri   Rk   Rj   Rl   (    (    s$   /p/zhu/06/nlp/nltk/nltk/draw/tree.pyt   _manage_verticalj  s     
 !
 (
 c         C   s   t  |  _ |  i   \ } } t |  i  d j o d  Sn |  i o |  i   n |  i   x |  i D] } |  i |  qe Wt	 |  _ d  S(   Ni    (
   t   TrueR   RG   RH   R
   R   Rm   Rn   RX   R   (   R   RO   RP   R    (    (    s$   /p/zhu/06/nlp/nltk/nltk/draw/tree.pyt   _manage  s    	

 c         C   s   d |  i  |  i f S(   Ns   [TreeSeg %s: %s](   R	   R
   (   R   (    (    s$   /p/zhu/06/nlp/nltk/nltk/draw/tree.pyt   __repr__  s    (   t   __name__t
   __module__t   __doc__R   R1   R6   R   R   R8   R<   R@   RC   RD   RF   RG   RX   RJ   RZ   RY   Rm   Rn   Rp   Rq   (    (    (    s$   /p/zhu/06/nlp/nltk/nltk/draw/tree.pyR      s*   )		$						
				
		#		*	*			c         C   s   t  | t  oh | |  | i |  } g  }	 | D]( }
 |	 t |  |
 | | | | | |  q0 ~	 } t |  | | |  Sn | |  | |  Sd  S(   N(   R-   R    R   t   _tree_to_treesegR   (   R   t   tt	   make_nodet	   make_leaft   tree_attribst   node_attribst   leaf_attribst   loc_attribsR   R   R?   R   (    (    s$   /p/zhu/06/nlp/nltk/nltk/draw/tree.pyRu     s    5c         K   s   h  } h  } h  } h  } x | i    D] \ }	 }
 |	 d  d j o |
 | |	 d <q% |	 d  d j o |
 | |	 d <q% |	 d  d j o |
 | |	 d <q% |	 d  d j o |
 | |	 d <q% t d |	   q% Wt |  | | | | | | |  S(   sM  
    Convert a C{Tree} into a C{TreeSegmentWidget}.

    @param make_node: A C{CanvasWidget} constructor or a function that
        creates C{CanvasWidgets}.  C{make_node} is used to convert
        the C{Tree}'s nodes into C{CanvasWidgets}.  If no constructor
        is specified, then C{TextWidget} will be used.
    @param make_leaf: A C{CanvasWidget} constructor or a function that
        creates C{CanvasWidgets}.  C{make_leaf} is used to convert
        the C{Tree}'s leafs into C{CanvasWidgets}.  If no constructor
        is specified, then C{TextWidget} will be used.
    @param attribs: Attributes for the canvas widgets that make up the
        returned C{TreeSegmentWidget}.  Any attribute beginning with
        C{'tree_'} will be passed to all C{TreeSegmentWidget}s (with
        the C{'tree_'} prefix removed.  Any attribute beginning with
        C{'node_'} will be passed to all nodes.  Any attribute
        beginning with C{'leaf_'} will be passed to all leaves.  And
        any attribute beginning with C{'loc_'} will be passed to all
        text locations (for C{Tree}s).
    i   t   tree_t   node_t   leaf_i   t   loc_s   Bad attribute: %s(   t   itemsR,   Ru   (   R   Rv   Rw   Rx   R   Ry   Rz   R{   R|   t   keyR3   (    (    s$   /p/zhu/06/nlp/nltk/nltk/draw/tree.pyt   tree_to_treesegment  s     ####t
   TreeWidgetc           B   s   e  Z d  Z e e d  Z d   Z d   Z d d  Z d d  Z d d  Z	 d d  Z
 d d	  Z d d
  Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z RS(   s  
    A canvas widget that displays a single C{Tree}.
    C{TreeWidget} manages a group of C{TreeSegmentWidget}s that are
    used to display a C{Tree}.

    Attributes:
    
      - C{node_M{attr}}: Sets the attribute C{M{attr}} on all of the
        node widgets for this C{TreeWidget}.
      - C{node_M{attr}}: Sets the attribute C{M{attr}} on all of the
        leaf widgets for this C{TreeWidget}.
      - C{loc_M{attr}}: Sets the attribute C{M{attr}} on all of the
        location widgets for this C{TreeWidget} (if it was built from
        a C{Tree}).  Note that location widgets are
        C{TextWidget}s. 
      
      - C{xspace}: The amount of horizontal space to leave between
        subtrees when managing this widget.  Default value is 10.
      - C{yspace}: The amount of space to place between the node and
        its children when managing this widget.  Default value is 15.
        
      - C{line_color}: The color of the lines connecting each expanded
        node to its subtrees.
      - C{roof_color}: The color of the outline of the triangular roof
        for collapsed trees.
      - C{roof_fill}: The fill color for the triangular roof for
        collapsed trees.
      - C{width}
      
      - C{orientation}: Determines whether the tree branches downwards
        or rightwards.  Possible values are C{'horizontal'} and
        C{'vertical'}.  The default value is C{'vertical'} (i.e.,
        branch downwards).
        
      - C{shapeable}: whether the subtrees can be independantly
        dragged by the user.  THIS property simply sets the
        C{DRAGGABLE} property on all of the C{TreeWidget}'s tree
        segments. 
      - C{draggable}: whether the widget can be dragged by the user.
    c         K   s  | |  _  | |  _ | |  _ h  |  _ h  |  _ h  d d <|  _ d |  _ d |  _ d |  _ d |  _	 t
 |  _ d |  _ d |  _ d |  _ t
 |  _ h  |  _ h  |  _ h  |  _ g  |  _ g  |  _ |  i | | d  |  i | | d	  |  _ |  i |  i  t i |  | |  d  S(
   Ns   #008000R&   s   #008080i   s   #c0c0c0i
   R%   (    (    (   t
   _make_nodet
   _make_leaft   _treet   _nodeattribst   _leafattribst   _locattribst   _line_colort   _line_widtht   _roof_colort
   _roof_fillR   t
   _shapeableR   R   t   _orientationR   t   _keyst   _expanded_treest   _collapsed_treest   _nodest   _leavest   _make_collapsed_treest   _make_expanded_treet   _treesegR   R   R   (   R   R   Rv   Rw   Rx   R   (    (    s$   /p/zhu/06/nlp/nltk/nltk/draw/tree.pyR     s0    																			c         G   s   |  i  | S(   sC  
        Return the C{TreeSegmentWidget} for the specified subtree.

        @param path_to_tree: A list of indices i1, i2, ..., in, where
            the desired widget is the widget corresponding to
            C{tree.children()[i1].children()[i2]....children()[in]}.
            For the root, the path is C{()}.
        (   R   (   R   t   path_to_tree(    (    s$   /p/zhu/06/nlp/nltk/nltk/draw/tree.pyt   expanded_tree  s    	c         G   s   |  i  | S(   sC  
        Return the C{TreeSegmentWidget} for the specified subtree.

        @param path_to_tree: A list of indices i1, i2, ..., in, where
            the desired widget is the widget corresponding to
            C{tree.children()[i1].children()[i2]....children()[in]}.
            For the root, the path is C{()}.
        (   R   (   R   R   (    (    s$   /p/zhu/06/nlp/nltk/nltk/draw/tree.pyt   collapsed_tree  s    	i   c         C   sX   x' |  i  i   D] } | i | |  q Wx' |  i i   D] } | i | |  q: Wd S(   s5   
        Add a binding to all tree segments.
        N(   R   t   valuest
   bind_clickR   (   R   t   callbackt   buttont   tseg(    (    s$   /p/zhu/06/nlp/nltk/nltk/draw/tree.pyt   bind_click_trees(  s      c         C   sX   x' |  i  i   D] } | i | |  q Wx' |  i i   D] } | i | |  q: Wd S(   s5   
        Add a binding to all tree segments.
        N(   R   R   t	   bind_dragR   (   R   R   R   R   (    (    s$   /p/zhu/06/nlp/nltk/nltk/draw/tree.pyt   bind_drag_trees1  s      c         C   sL   x! |  i  D] } | i | |  q
 Wx! |  i  D] } | i | |  q. Wd S(   s.   
        Add a binding to all leaves.
        N(   R   R   (   R   R   R   t   leaf(    (    s$   /p/zhu/06/nlp/nltk/nltk/draw/tree.pyt   bind_click_leaves:  s    
 
 c         C   sL   x! |  i  D] } | i | |  q
 Wx! |  i  D] } | i | |  q. Wd S(   s.   
        Add a binding to all leaves.
        N(   R   R   (   R   R   R   R   (    (    s$   /p/zhu/06/nlp/nltk/nltk/draw/tree.pyt   bind_drag_leavesA  s    
 
 c         C   sL   x! |  i  D] } | i | |  q
 Wx! |  i  D] } | i | |  q. Wd S(   s-   
        Add a binding to all nodes.
        N(   R   R   (   R   R   R   R   (    (    s$   /p/zhu/06/nlp/nltk/nltk/draw/tree.pyt   bind_click_nodesH  s    
 
 c         C   sL   x! |  i  D] } | i | |  q
 Wx! |  i  D] } | i | |  q. Wd S(   s-   
        Add a binding to all nodes.
        N(   R   R   (   R   R   R   R   (    (    s$   /p/zhu/06/nlp/nltk/nltk/draw/tree.pyt   bind_drag_nodesO  s    
 
 c         C   s3  t  | t  p d  Sn |  i } |  i } | | | i |  i  } |  i i |  g  } | i   D] } | | | | |  i	  qc ~ }	 |  i
 |	 7_
 t | | |	 d d d |  i d |  i d |  i }
 |
 |  i | <| |  i |
 <|
 i   x> t t |   D]* } | | } |  i | | | | f  qWd  S(   NR!   i   R&   R   R'   (   R-   R    R   R   R   R   R   RB   t   leavesR   R   R   R   R   R   R   R   t   hideR\   RH   R   (   R   R   Rv   R   Rw   Rx   R   R   R4   R   t   treesegRg   R?   (    (    s$   /p/zhu/06/nlp/nltk/nltk/draw/tree.pyR   V  s&    		/		
 
c      
   C   s
  |  i  } |  i } t | t  o | | | i |  i  } |  i i |  | } g  } t t	 |   D]' }	 | |  i
 | | |	 | |	 f  qg ~ }
 t | | |
 d |  i d |  i } | |  i | <| |  i | <| Sn* | | | |  i  } |  i i |  | Sd  S(   NR&   R'   (   R   R   R-   R    R   R   R   RB   R\   RH   R   R   R   R   R   R   R   R   (   R   R   Rv   R   Rw   Rx   R   t   childrenR   Rg   R   R   R   (    (    s$   /p/zhu/06/nlp/nltk/nltk/draw/tree.pyR   o  s"    		@	c      
   C   s  | d  d j o& x|  i  D] } | | | d <q Wn| d  d j o& xx|  i D] } | | | d <qR WnV| d j o1 | |  _ x<|  i i   D] } | | d <q Wn| d j oU | |  _ x! |  i i   D] } | | d <q Wx|  i i   D] } | | d <q Wn| d j o1 | |  _ x|  i i   D] } | | d <q4Wnx| d	 j o1 | |  _ x^|  i i   D] } | | d
 <qrWn:| d j os | |  _	 x! |  i i   D] } | | d <qWx! |  i i   D] } | | d <qWx|  i D] } | | d <qWn| d j o_ | |  _
 x! |  i i   D] } | | d <q0Wx! |  i i   D] } | | d <qTW|  i   nN| d j o_ | |  _ x! |  i i   D] } | | d <qWx! |  i i   D] } | | d <qW|  i   n | d j o_ | |  _ x! |  i i   D] } | | d <qWx! |  i i   D] } | | d <q,W|  i   nv | d j oU | |  _ x! |  i i   D] } | | d <qtWx8 |  i i   D] } | | d <qWn t i |  | |  d  S(   Ni   R~   R   t
   line_colorR&   t
   line_widthR'   t
   roof_colort	   roof_fillR   t	   shapeablet	   draggableR(   R)   R#   R*   (   R   R   R   R   R   R   R   R   R   R   R   t   manageR   R   R   R   R1   (   R   R2   R3   R   R   R   (    (    s$   /p/zhu/06/nlp/nltk/nltk/draw/tree.pyR1     s    
 
 	 	  	 	 	  
 	  	  	  	  c         C   sX  | d  d j o |  i  i | d d   Sn)| d  d j o |  i i | d d   Sn | d  d j o |  i i | d d   Sn | d j o |  i Sn | d j o |  i Sn | d j o |  i Sn | d	 j o |  i Snq | d
 j o |  i	 SnY | d j o |  i
 SnA | d j o |  i Sn) | d j o |  i Sn t i |  |  Sd  S(   Ni   R~   R   i   R   R   R   R   R   R   R(   R)   R#   (   R   t   getRI   R   R   R   R   R   R   R   R   R   R   R   R6   (   R   R2   (    (    s$   /p/zhu/06/nlp/nltk/nltk/draw/tree.pyR6     s    c         C   s   g  S(   N(    (   R   (    (    s$   /p/zhu/06/nlp/nltk/nltk/draw/tree.pyRD     s    c         C   s`   |  i  i   |  i i   } x= | D]5 } | i   o" | i   | i   | i   q# q# Wd  S(   N(   R   R   R   R   t   showR   R   (   R   t   segsR   (    (    s$   /p/zhu/06/nlp/nltk/nltk/draw/tree.pyRp     s     

c         C   s  | } | d o |  i  |  i | } n |  i |  i | } | i   |  j o' |  i |  |  i |  | |  _ n | i   i | |  | i   | i	   i
   d  \ } } | i	   i
   d  \ } } | i | | | |  | i   | i   i |  d S(   s)   
        Collapse/expand a tree.
        R!   i   N(   R   R   R   t   parentR7   R   R   R<   R   R   RE   R[   R   R0   (   R   R   t   old_treesegt   new_treesegt   newxt   newyt   oldxt   oldy(    (    s$   /p/zhu/06/nlp/nltk/nltk/draw/tree.pyt   toggle_collapsed  s    

(   Rr   Rs   Rt   t
   TextWidgetR   R   R   R   R   R   R   R   R   R   R   R1   R6   RD   Rp   R   (    (    (    s$   /p/zhu/06/nlp/nltk/nltk/draw/tree.pyR     s$   (!							6			t   TreeViewc           B   s>   e  Z d    Z d   Z d   Z d   Z d   Z d   Z RS(   c   
      G   s  d d k  l } d d k l } l } | |  _ t   |  _ |  i i d  |  i i	 d |  i
  |  i i	 d |  i
  | |  i  } |  _ |  i i	 d |  i i  t |  i  |  _ |  i i d  d	 |  i i   d
 f } d	 |  i i   f } t | | t |     |  _ g  |  _ x t t |   D] } t | i   | | d | d d d d d d d d d d d d d | }	 |	 i |	 i  |  i i |	  | i |	 d d  q4W|  i   |  i i d d d d  |  i   d  S(   Ni(   t   CanvasFrame(   t   sqrtt   ceilt   NLTKs   <Control-x>s   <Control-q>s   <Control-p>i   t	   helveticat   boldt	   node_fontt
   leaf_colors   #008040t
   node_colors   #004080R   s   #004040R   t   whiteR   R   i   t	   leaf_fonti    t   expandR   t   both(   t	   nltk.drawR   t   mathR   R   t   _treest   Tkt   _topt   titlet   bindt   destroyt   _cframet   print_to_filet   IntVart   _sizet   setR   R/   RH   t   _widtht   _widgetsR\   R   R   R   R   RB   t
   add_widgett   _layoutt   packt   _init_menubar(
   R   t   treesR   R   R   t   cfR   t   helvRg   t   widget(    (    s$   /p/zhu/06/nlp/nltk/nltk/draw/tree.pyR     s8    	!	 	
c   	      C   s   d } } } } |  i  } x t t |  i   D] } |  i | } | i   d  \ } } | | d j o | } d } n | i | | | |  | i   d d } t | | i   d d  } q1 Wd  S(   Ni    i   i
   i   (   R   R\   RH   R   RE   R[   RL   (	   R   Rg   Rf   Rh   RT   R'   R   R   R   (    (    s$   /p/zhu/06/nlp/nltk/nltk/draw/tree.pyR     s    	 
c         C   s  t  |  i  } t  | d d } | i d d d d d |  i i d d  | i d d	 d d
 d |  i d d  | i d d d d d |  t  | d d } | i d d d |  i d d d d d |  i	  | i d d d |  i d d d d d |  i	  | i d d d |  i d d d d d |  i	  | i d d d |  i d d d d d |  i	  | i d d d |  i d d d d d |  i	  | i d d d d d |  |  i i
 d |  d  S(   Nt   tearoffi    t   labels   Print to Postscriptt	   underlinet   commandt   accelerators   Ctrl-pt   Exiti   s   Ctrl-xt   Filet   menut   Tinyt   variableR3   i
   t   Smalli   t   Mediumi   t   Largei   t   Hugei2   t   Zoom(   t   MenuR   t   add_commandR   R   R   t   add_cascadet   add_radiobuttonR   t   resizet   config(   R   t   menubart   filemenut   zoommenu(    (    s$   /p/zhu/06/nlp/nltk/nltk/draw/tree.pyR   +  s*    c         G   s   d |  i  i   d f } d |  i  i   f } |  i  i   } |  i  i   } x |  i D] } | | d <| | d <| | d <| | d <|  i  i   d j  o d | d	 <qW |  i  i   d
 j  o d | d	 <qW d | d	 <qW W|  i   d  S(   NR   R   R   R   R(   R)   i   i   R   i   i   i   (   R   R   R   R   (   R   t   eR   R   R(   R)   R   (    (    s$   /p/zhu/06/nlp/nltk/nltk/draw/tree.pyR   E  s    
 



$$c         G   s2   |  i  d  j o d  Sn |  i  i   d  |  _  d  S(   N(   R   RI   R   (   R   R   (    (    s$   /p/zhu/06/nlp/nltk/nltk/draw/tree.pyR   T  s    c         O   s)   t    o d Sn |  i i | |   d S(   s   
        Enter the Tkinter mainloop.  This function must be called if
        this demo is created from a non-interactive program (e.g.
        from a secript); otherwise, the demo will close as soon as
        the script completes.
        N(   t   in_idleR   t   mainloop(   R   t   argst   kwargs(    (    s$   /p/zhu/06/nlp/nltk/nltk/draw/tree.pyR   Y  s    (   Rr   Rs   R   R   R   R   R   R   (    (    (    s$   /p/zhu/06/nlp/nltk/nltk/draw/tree.pyR     s   	%				c          G   s   t  |    i   d S(   sl   
    Open a new window containing a graphical diagram of the given
    trees.
        
    @rtype: None
    N(   R   R   (   R   (    (    s$   /p/zhu/06/nlp/nltk/nltk/draw/tree.pyt
   draw_treesc  s    c             s
  d d  k      f d   }  t d d d d d d  } t d	  } t | i   | d
 d d d0 d d1 d d d d d d d d } | i | d d  d   } d   } t d  } t | i   | | | d d }   f d    }   f d!   }	 | i | i  | i | i  | i | d"  | i	 d  i
 |	 d"  | i	   i
 |	 d"  t | i   |  }
 | i |
 | i   d d d  t d#  } t | i   | d$ d d% d d& d } d | d
 <| i | d | i   d" d  d'   } d( } t | i   | i    } t | i   | d) d d
 d } | i | | i   d d | i   d" d  t d*  } t | i   | d
 d d+ d, d d, d d2 d d- d. d/ } | i   | i | | i   d d | i   d" d  | i
 |  | i | i d"  | i   d  S(3   Nic            s   d   i  d d  |  d <d  S(   Ns   #%06di    i?B R   (   t   randint(   t   cw(   t   random(    s$   /p/zhu/06/nlp/nltk/nltk/draw/tree.pyR   s  s    R'   i&  t   heighti  t   closeenoughi   sU   
    (S (NP the very big cat)
       (VP (Adv sorta) (V saw) (NP (Det the) (N dog))))R   i   R   R   iR   R   it   italicR   R   R   t   blackR   t   green4R   t   blue2i
   c         S   s(   d } t  |  t |  | d | d d S(   NR   iR   t   fontR   t   green(   s	   helveticais   bold(   t	   BoxWidgetR   (   R   t   textt   big(    (    s$   /p/zhu/06/nlp/nltk/nltk/draw/tree.pyt   boxit  s    c         S   s   t  |  t |  |  d d S(   NR   t   cyan(   t
   OvalWidgetR   (   R   R  (    (    s$   /p/zhu/06/nlp/nltk/nltk/draw/tree.pyt   ovalit  s    s/   (S (NP this tree) (VP (V is) (AdjP shapeable)))R   c            s   d   i  d d  |  d <d  S(   Ns   #%04d00i    i'  R&   (   R  (   R   (   R  (    s$   /p/zhu/06/nlp/nltk/nltk/draw/tree.pyR&     s    c            s:   d   i  d d  |  i   d <d |  i   i   d <d  S(   Ns   #%06di    i'  R   R   R&   (   R  R   R?   (   R   (   R  (    s$   /p/zhu/06/nlp/nltk/nltk/draw/tree.pyt   color2  s     i   sb   
    (S (NP this tree) (AUX was)
       (VP (V built) (PP (P with) (NP (N tree_to_treesegment)))))t
   tree_colort   tree_xspacet
   tree_widthc         S   s3  |  d d j o |  i  d d  i   d i d  |  i d d  i   d i d  |  i d  i   d i d  |  i   i   d i d  d |  d <n |  i  d d  i   d i d  |  i d d  i   d i d  |  i d  i   d i d  |  i   i   d i d  d |  d <d  S(   NR#   R$   i   i    R%   i   (   R   R   t   set_textR   (   t
   treewidget(    (    s$   /p/zhu/06/nlp/nltk/nltk/draw/tree.pyt   orientswitch  s    ## ## sM  
Try clicking, right clicking, and dragging
different elements of each of the trees.
The top-left tree is a TreeWidget built from
a Tree.  The top-right is a TreeWidget built
from a Tree, using non-default widget
constructors for the nodes & leaves (BoxWidget
and OvalWidget).  The bottom-left tree is
built from tree_to_treesegment.R   s/   (S (NP this tree) (VP (V is) (Adj horizontal)))R   t   brown2t   brown4R#   R$   (   s	   helveticais   bold(   s	   helveticais   italic(   s	   helveticais   bold(   R  R   R   R   R   R   R   R   R   R   R   t   ParenWidgetRE   R   R   t   stripR  R   R   (   R   R   Rv   t   tcR  R  t   treetokt   tc2R&   R  t   parent   tree3t   tc3R  R  t   twidgett   textboxt   tree4t   tc4(    (   R  s$   /p/zhu/06/nlp/nltk/nltk/draw/tree.pyt   demoq  sZ    			!!	
!	!/
/t   __main__(   Rt   t   syst   nltkR    R   t   utilR   R   Ru   R   R   R   t   objectR   R  R*  Rr   (    (    (    s$   /p/zhu/06/nlp/nltk/nltk/draw/tree.pys   <module>   s   
 y	) 2k		\