
    g                          d Z ddlmZmZmZ ddlmZmZmZm	Z	m
Z
mZ ddlmZ ddlmZ  G d de      Zd Zeefd	Z G d
 de      Z G d d      Zd Zd Zedk(  r e        yy)z
Graphically display a Tree.
    )IntVarMenuTk)	BoxWidgetCanvasFrameCanvasWidget
OvalWidgetParenWidget
TextWidget)Tree)in_idlec                       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d Zy)TreeSegmentWidgeta  
    A canvas widget that displays a single segment of a hierarchical
    tree.  Each ``TreeSegmentWidget`` connects a single "node widget"
    to a sequence of zero or more "subtree widgets".  By default, the
    bottom of the node is connected to the top of each subtree by a
    single line.  However, if the ``roof`` attribute is set, then a
    single triangular "roof" will connect the node to all of its
    children.

    Attributes:
      - ``roof``: What sort of connection to draw between the node and
        its subtrees.  If ``roof`` is true, draw a single triangular
        "roof" over the subtrees.  If ``roof`` is false, draw a line
        between each subtree and the node.  Default value is false.
      - ``xspace``: The amount of horizontal space to leave between
        subtrees when managing this widget.  Default value is 10.
      - ``yspace``: The amount of space to place between the node and
        its children when managing this widget.  Default value is 15.
      - ``color``: The color of the lines connecting the node to its
        subtrees; and of the outline of the triangular roof.  Default
        value is ``'#006060'``.
      - ``fill``: The fill color for the triangular roof.  Default
        value is ``''`` (no fill).
      - ``width``: The width of the lines connecting the node to its
        subtrees; and of the outline of the triangular roof.  Default
        value is 1.
      - ``orientation``: Determines whether the tree branches downwards
        or rightwards.  Possible values are ``'horizontal'`` and
        ``'vertical'``.  The default value is ``'vertical'`` (i.e.,
        branch downwards).
      - ``draggable``: whether the widget can be dragged by the user.
    c           
         || _         || _        d| _        d| _        d| _        d| _        d| _        |D cg c]  }|j                  ddddd       c}| _        |j                  ddddd	      | _
        | j                  |       |D ]  }| j                  |        d| _        t        j                  | |fi | y
c c}w )zI
        :type node:
        :type subtrees: list(CanvasWidgetI)
        r   
      F#006060fill hidden)r   stateoutlineN)_label	_subtrees_horizontal_roof_xspace_yspace_orderedcreate_line_linescreate_polygon_polygon_add_child_widget	_managingr   __init__)selfcanvaslabelsubtreesattribscsubtrees          C/var/www/openai/venv/lib/python3.12/site-packages/nltk/draw/tree.pyr'   zTreeSegmentWidget.__init__@   s    
 ! 
 PXXx!v))!Q19)ExX--qr9 . 

 	u%G""7+   dF6g6 Ys   B>c                 ^   | j                         }|dk(  r|| _        | j                  rB| j                  D ]  }|j                  |d        |j                  | j                  d       y | j                  D ]  }|j                  |d        |j                  | j                  d       y |dk(  r%|dk(  rd| _        y |dk(  rd	| _        y t        d
      |dk(  rB| j                  D ]  }|j                  ||        |j                  | j                  |       y t        |t              r7|d	   dk(  r/| j                  t        |d            }|j                  ||       y |dk(  r|j                  | j                  |       y |dk(  rD|j                  | j                  ||i       | j                  D ]  }|j                  |||i        y |dv r5|dk(  r|| _
        n|dk(  r|| _        | j                  | j                         y |dk(  r|| _        y t        j                   | ||       y )Nroofr   )r   normalorientation
horizontal   verticalr   z*orientation must be horizontal or verticalcolorr   )r   r   width)xspaceyspacer9   r:   ordered)r)   r   r"   
itemconfigr$   r   
ValueError
isinstancetupleintr   r   updater   r    r   __setitem__)r(   attrvaluer)   ls        r/   rB   zTreeSegmentWidget.__setitem___   s   6>DJzzA%%ax%8 %!!$--x!@A%%ax%8 %!!$--x!@]"$#$ *$#$  !MNNW_[[!!!%!0 !dmmU;e$aG);CQL)Aae,V^dmm%8W_dmmdE];[[!!!dE]3 !))x$!$KK$Y!DM$$T47    c                 >   |dk(  r| j                   S |dk(  r*| j                         j                  | j                  |      S |dk(  r*| j                         j                  | j                  d      S t	        |t
              rC|d   dk(  r;| j                  t        |d            }| j                         j                  |d      S |dk(  r| j                  S |d	k(  r| j                  S |d
k(  r| j                  ryy|dk(  r| j                  S t        j                  | |      S )Nr1   r8   r7   r   r   r5   r   r9   r:   r3   r4   r6   r;   )r   r)   itemcgetr$   r>   r?   r"   r@   r   r   r   r    r   __getitem__)r(   rC   rE   s      r/   rI   zTreeSegmentWidget.__getitem__   s    6>::W_;;=))$-->>W_;;=))$--CCe$aG);CQL)A;;=))!V44X<<X<<]"#!Y== ++D$77rF   c                     | j                   S N)r   r(   s    r/   r*   zTreeSegmentWidget.label   s    {{rF   c                      | j                   d d  S rK   )r   rL   s    r/   r+   zTreeSegmentWidget.subtrees   s    ~~a  rF   c                     | j                  | j                         | j                  |       || _        | j                  | j                         y)z2
        Set the node label to ``label``.
        N)_remove_child_widgetr   r%   rA   )r(   r*   s     r/   	set_labelzTreeSegmentWidget.set_label   s;     	!!$++.u%DKK rF   c                     | j                   j                  |      }|| j                   |<   | j                  |       | j                  |       | j	                  |       y)zC
        Replace the child ``oldchild`` with ``newchild``.
        N)r   indexrO   r%   rA   )r(   oldchildnewchildrR   s       r/   replace_childzTreeSegmentWidget.replace_child   sN     $$X. (u!!(+x(HrF   c                    | j                   j                  |      }| j                   |= | j                  |       | j                         j	                  | j
                  j                                | j                  | j                         y rK   )	r   rR   rO   r)   deleter"   poprA   r   )r(   childrR   s      r/   remove_childzTreeSegmentWidget.remove_child   s_    $$U+NN5!!!%(T[[__./DKK rF   c           	         | j                         }| j                  j                  ||       | j                  |       | j                  j                  |j                  ddddd             | j                  | j                         y )Nr   r   r   )	r)   r   insertr%   r"   appendr!   rA   r   )r(   rR   rY   r)   s       r/   insert_childzTreeSegmentWidget.insert_child   sg    eU+u%6--aAqy-IJDKK rF   c                 L    | j                   r| j                  gS | j                  S rK   )r   r$   r"   rL   s    r/   _tagszTreeSegmentWidget._tags   s    ::MM?";;rF   c                     t        |t              r|j                         j                         }n|j                         }| j                  r|d   |d   |d   z   dz  fS |d   |d   z   dz  |d   fS )Nr   r5             @   )r>   r   r*   bboxr   )r(   rY   re   s      r/   _subtree_topzTreeSegmentWidget._subtree_top   sw    e./;;=%%'D::<DGd1gQ/3677!WtAw&#-tAw77rF   c                     | j                   j                         }| j                  r|d   |d   |d   z   dz  fS |d   |d   z   dz  |d   fS )Nrd   r5   rb   rc   r   )r   re   r   )r(   re   s     r/   _node_bottomzTreeSegmentWidget._node_bottom   s_    {{!Gd1gQ/3677!WtAw&#-tAw77rF   c                    t        | j                        dk(  ry | j                  j                         y || j                  u r| j                  }n|g}| j                  r| j
                  s| j                  |      }| j                         \  }}| j                  d   j                         \  }}}}| j                  dd  D ]N  }	|	j                         }
t        ||
d         }t        ||
d         }t        ||
d         }t        ||
d         }P | j                  r2| j                         j                  | j                  ||||||||	       n1| j                         j                  | j                  ||||||||	       |D ]t  }	| j                         \  }}| j                  | j                  j                  |	         }| j!                  |	      \  }}| j                         j                  |||||       v y )Nr   r5   rd   rb   )lenr   r   re   r    r&   _maintain_orderrh   minmaxr   r)   coordsr$   r"   rR   rf   )r(   rY   need_updatenodexnodeyxminyminxmaxymaxr.   re   linesubtreexsubtreeys                 r/   _updatezTreeSegmentWidget._update   s   t~~!#;;% DKK..K 'K==..u5K **,#'>>!#4#9#9#; tT4~~ab)G<<>DtT!W%DtT!W%DtT!W%DtT!W%D * KKM  ueT4tUE KKM  ueT4tUE
 #G!..0NUE;;t~~33G<=D#'#4#4W#= XxKKM  ueXxH	 #rF   c                 ^    | j                   r| j                  |      S | j                  |      S rK   )r   _maintain_order_horizontal_maintain_order_vertical)r(   rY   s     r/   rk   z!TreeSegmentWidget._maintain_order  s-    225990077rF   c                    |j                         \  }}}}|| j                  u rg| j                  D ]L  }|j                         \  }}}	}
|| j                  z   |kD  s+|j	                  d|| j                  z   |z
         N | j                  S |g}| j                  j                  |      }|| j                  z   }t        |dz   t        | j                              D ]  }| j                  |   j                         \  }}}	}
||kD  s+| j                  |   j	                  ||z
  d       ||	|z
  | j                  z   z  }|j                  | j                  |           || j                  z
  }t        |dz
  dd      D ]  }| j                  |   j                         \  }}}	}
||	k  s+| j                  |   j	                  ||	z
  d       ||	|z
  | j                  z   z  }|j                  | j                  |           | j                  j                         \  }}}	}
|
|| j                  z
  kD  r8| j                  j	                  d|| j                  z
  |
z
         | j                  }|S )Nr   r5   )
re   r   r   r   moverR   r   rangerj   r]   )r(   rY   lefttoprightbotr.   x1y1x2y2movedrR   xis                  r/   r|   z*TreeSegmentWidget._maintain_order_vertical  s-   "'**,sE3DKK>>#*<<> RR%*LLC$,,$6$;< *
 >>!GENN((/E $A519c$..&9:#'>>!#4#9#9#; RRr6NN1%**1r615b4<<//ALL!23 ; t||#A519b"-#'>>!#4#9#9#; RRr6NN1%**1r615b4<<//ALL!23 .  ${{//1RRC$,,&&  C$,,$6$;< rF   c                    |j                         \  }}}}|| j                  u rf| j                  D ]K  }|j                         \  }}}	}
|| j                  z   |kD  s+|j	                  || j                  z   |z
         M | j                  S |g}| j                  j                  |      }|| j                  z   }t        |dz   t        | j                              D ]  }| j                  |   j                         \  }}}	}
||kD  s+| j                  |   j	                  d||z
         ||
|z
  | j                  z   z  }|j                  | j                  |           || j                  z
  }t        |dz
  dd      D ]  }| j                  |   j                         \  }}}	}
||
k  s+| j                  |   j	                  d||
z
         ||
|z
  | j                  z   z  }|j                  | j                  |           | j                  j                         \  }}}	}
|	|| j                  z
  kD  r8| j                  j	                  || j                  z
  |	z
  d       | j                  }|S )Nr5   r   r~   )
re   r   r   r   r   rR   r   r   rj   r]   )r(   rY   r   r   r   r   r.   r   r   r   r   r   rR   yr   s                  r/   r{   z,TreeSegmentWidget._maintain_order_horizontal<  s+   "'**,sE3DKK>>#*<<> RR4<<'",LL!5!:; *
 >>!GENN((/E dll"A519c$..&9:#'>>!#4#9#9#; RRr6NN1%**1a"f5b4<<//ALL!23 ; dll"A519b"-#'>>!#4#9#9#; RRr6NN1%**1a"f5b4<<//ALL!23 .  ${{//1RRD4<<''  !4r!91= rF   c                    | j                         \  }}d}| j                  D ]\  }|j                         }||d   z
  | j                  z   }||d   z
  }|j	                  ||       ||d   |d   z
  | j
                  z   z  }^ d}| j                  D ]  }|| j                  |      d   z  } |t        | j                        z  }| j                  D ]  }|j	                  d||z
          y )N   r   r5   rb           )rh   r   re   r   r   r   rf   rj   )	r(   rp   rq   r   r.   subtree_bboxdxdycenters	            r/   _manage_horizontalz$TreeSegmentWidget._manage_horizontalf  s    **, ~~G"<<>La(4<<7B\!_$BLLR a<?2T\\AAA & ~~Gd''033F &#dnn%% ~~GLLEFN+ &rF   c                    | j                         \  }}d}| j                  D ]\  }|j                         }||d   z
  | j                  z   }||d   z
  }|j	                  ||       ||d   |d   z
  | j
                  z   z  }^ d}| j                  D ]/  }|| j                  |      d   t        | j                        z  z  }1 | j                  D ]  }|j	                  ||z
  d        y )Nr   r5   rd   r   )rh   r   re   r   r   r   rf   rj   )	r(   rp   rq   r   r.   r   r   r   r   s	            r/   _manage_verticalz"TreeSegmentWidget._manage_vertical|  s    **, ~~G"<<>La(4<<7B\!_$BLLR a<?2T\\AAA & ~~Gd''03c$..6IIIF & ~~GLL+ &rF   c                    d| _         | j                         \  }}t        | j                        dk(  ry | j                  r| j                          n| j                          | j                  D ]  }| j                  |        d| _         y )NTr   F)r&   rh   rj   r   r   r   r   ry   )r(   rp   rq   r.   s       r/   _managezTreeSegmentWidget._manage  su    **,t~~!###%!!# ~~GLL! & rF   c                 <    d| j                    d| j                   dS )Nz	[TreeSeg z: ])r   r   rL   s    r/   __repr__zTreeSegmentWidget.__repr__  s     4;;-r$..)9;;rF   N)__name__
__module____qualname____doc__r'   rB   rI   r*   r+   rP   rU   rZ   r^   r`   rf   rh   ry   rk   r|   r{   r   r   r   r    rF   r/   r   r      sq    B7>*8X80!!!!88'IR8(T(T,,,*"<rF   r   c                     t        |t              rE || |j                         fi |}|D 	cg c]  }	t        | |	||||||       }
}	t	        | ||
fi |S  || |fi |S c c}	w rK   )r>   r   r*   _tree_to_treesegr   )r)   t	make_node	make_leaftree_attribsnode_attribsleaf_attribsloc_attribsr*   rY   r+   s              r/   r   r     s     !T&!'')<|< 
  	  	 
 !ILII3l33
s   A c           
         i }i }i }i }t        |j                               D ]V  \  }	}
|	dd dk(  r	|
||	dd <   |	dd dk(  r	|
||	dd <   (|	dd dk(  r	|
||	dd <   9|	dd dk(  r	|
||	dd <   Jt        d|	z         t        | |||||||      S )	aS  
    Convert a Tree into a ``TreeSegmentWidget``.

    :param make_node: A ``CanvasWidget`` constructor or a function that
        creates ``CanvasWidgets``.  ``make_node`` is used to convert
        the Tree's nodes into ``CanvasWidgets``.  If no constructor
        is specified, then ``TextWidget`` will be used.
    :param make_leaf: A ``CanvasWidget`` constructor or a function that
        creates ``CanvasWidgets``.  ``make_leaf`` is used to convert
        the Tree's leafs into ``CanvasWidgets``.  If no constructor
        is specified, then ``TextWidget`` will be used.
    :param attribs: Attributes for the canvas widgets that make up the
        returned ``TreeSegmentWidget``.  Any attribute beginning with
        ``'tree_'`` will be passed to all ``TreeSegmentWidgets`` (with
        the ``'tree_'`` prefix removed.  Any attribute beginning with
        ``'node_'`` will be passed to all nodes.  Any attribute
        beginning with ``'leaf_'`` will be passed to all leaves.  And
        any attribute beginning with ``'loc_'`` will be passed to all
        text locations (for Trees).
    N   tree_node_leaf_   loc_zBad attribute: %s)listitemsr=   r   )r)   r   r   r   r,   r   r   r   r   keyrD   s              r/   tree_to_treesegmentr     s    0 LLLK7==?+
Ur7g$)LQR!!W$)LQR!!W$)LQR!!W#(KAB 03677 , 		 	rF   c                       e Zd ZdZeef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y)
TreeWidgeta  
    A canvas widget that displays a single Tree.
    ``TreeWidget`` manages a group of ``TreeSegmentWidgets`` that are
    used to display a Tree.

    Attributes:

      - ``node_attr``: Sets the attribute ``attr`` on all of the
        node widgets for this ``TreeWidget``.
      - ``node_attr``: Sets the attribute ``attr`` on all of the
        leaf widgets for this ``TreeWidget``.
      - ``loc_attr``: Sets the attribute ``attr`` on all of the
        location widgets for this ``TreeWidget`` (if it was built from
        a Tree).  Note that a location widget is a ``TextWidget``.

      - ``xspace``: The amount of horizontal space to leave between
        subtrees when managing this widget.  Default value is 10.
      - ``yspace``: The amount of space to place between the node and
        its children when managing this widget.  Default value is 15.

      - ``line_color``: The color of the lines connecting each expanded
        node to its subtrees.
      - ``roof_color``: The color of the outline of the triangular roof
        for collapsed trees.
      - ``roof_fill``: The fill color for the triangular roof for
        collapsed trees.
      - ``width``

      - ``orientation``: Determines whether the tree branches downwards
        or rightwards.  Possible values are ``'horizontal'`` and
        ``'vertical'``.  The default value is ``'vertical'`` (i.e.,
        branch downwards).

      - ``shapeable``: whether the subtrees can be independently
        dragged by the user.  THIS property simply sets the
        ``DRAGGABLE`` property on all of the ``TreeWidget``'s tree
        segments.
      - ``draggable``: whether the widget can be dragged by the user.
    c                    || _         || _        || _        i | _        i | _        ddi| _        d| _        d| _        d| _        d| _	        d| _
        d| _        d| _        d| _        d| _        i | _        i | _        i | _        g | _        g | _        | j)                  ||d	       | j+                  ||d	      | _        | j/                  | j,                         t1        j2                  | |fi | y )
Nr7   z#008000z#008080r5   z#c0c0c0Fr   r6   r   )
_make_node
_make_leaf_tree_nodeattribs_leafattribs_locattribs_line_color_line_width_roof_color
_roof_fill
_shapeabler   r   _orientationr    _keys_expanded_trees_collapsed_trees_nodes_leaves_make_collapsed_trees_make_expanded_tree_treesegr%   r   r'   )r(   r)   r   r   r   r,   s         r/   r'   zTreeWidget.__init__&  s     $#
 #Y/$$#& 
! """61b100B?t}}-dF6g6rF   c                      | j                   |   S aF  
        Return the ``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
            ``tree.children()[i1].children()[i2]....children()[in]``.
            For the root, the path is ``()``.
        )r   r(   path_to_trees     r/   expanded_treezTreeWidget.expanded_treeI  s     ##L11rF   c                      | j                   |   S r   )r   r   s     r/   collapsed_treezTreeWidget.collapsed_treeT  s     $$\22rF   c                     t        | j                  j                               D ]  }|j                  ||        t        | j                  j                               D ]  }|j                  ||        yz5
        Add a binding to all tree segments.
        N)r   r   values
bind_clickr   r(   callbackbuttontsegs       r/   bind_click_treeszTreeWidget.bind_click_trees_  s\     --4467DOOHf- 8..5578DOOHf- 9rF   c                     t        | j                  j                               D ]  }|j                  ||        t        | j                  j                               D ]  }|j                  ||        yr   )r   r   r   	bind_dragr   r   s       r/   bind_drag_treeszTreeWidget.bind_drag_treesh  s\     --4467DNN8V, 8..5578DNN8V, 9rF   c                     | j                   D ]  }|j                  ||        | j                   D ]  }|j                  ||        yz.
        Add a binding to all leaves.
        N)r   r   r(   r   r   leafs       r/   bind_click_leaveszTreeWidget.bind_click_leavesq  s<     LLDOOHf- !LLDOOHf- !rF   c                     | j                   D ]  }|j                  ||        | j                   D ]  }|j                  ||        yr   )r   r   r   s       r/   bind_drag_leaveszTreeWidget.bind_drag_leavesz  s<     LLDNN8V, !LLDNN8V, !rF   c                     | j                   D ]  }|j                  ||        | j                   D ]  }|j                  ||        yz-
        Add a binding to all nodes.
        N)r   r   r(   r   r   nodes       r/   bind_click_nodeszTreeWidget.bind_click_nodes  s<     KKDOOHf-  KKDOOHf-  rF   c                     | j                   D ]  }|j                  ||        | j                   D ]  }|j                  ||        yr   )r   r   r   s       r/   bind_drag_nodeszTreeWidget.bind_drag_nodes  s<     KKDNN8V,  KKDNN8V,  rF   c           	         t        |t              sy | j                  }| j                  } |||j	                         fi | j
                  }| j                  j                  |       |j                         D cg c]  } |||fi | j                   }}| xj                  |z  c_
        t        |||d| j                  | j                  | j                        }	|	| j                  |<   || j                   |	<   |	j#                          t%        t'        |            D ]  }
||
   }| j)                  ||||
fz            y c c}w )Nr5   )r1   r7   r   r8   )r>   r   r   r   r*   r   r   r]   leavesr   r   r   r   r   r   r   r   hider   rj   r   )r(   r)   r   r   r   r   r   rE   r   treesegr   rY   s               r/   r   z TreeWidget._make_collapsed_trees  s   !T"OO	OO	@d.?.?@4 EFXXZPZ)FA;):):;ZP#""""
 &-c"!

7 s1vAaDE&&vucQDjA % Qs   9D?c           	      6   | j                   }| j                  }t        |t              r |||j	                         fi | j
                  }| j                  j                  |       |}t        t        |            D cg c]  }| j                  |||   ||fz          }	}t        |||	| j                  | j                        }
|
| j                  |<   || j                  |
<   |
S  |||fi | j                   }| j"                  j                  |       |S c c}w )N)r7   r8   )r   r   r>   r   r*   r   r   r]   r   rj   r   r   r   r   r   r   r   r   )r(   r)   r   r   r   r   r   childrenr   r+   r   r   s               r/   r   zTreeWidget._make_expanded_tree  s   OO	OO	aVQWWYD$2C2CDDKKt$H s8}--A ((!cQDjI-   (hd.>.>dFVFVG )0D  %"%DJJwNVQ<$*;*;<DLL%Ks   >!Dc                 v   |d d dk(  r| j                   D ]
  }|||dd  <    y |d d dk(  r| j                  D ]
  }|||dd  <    y |dk(  r5|| _        t        | j                  j                               D ]  }||d<   	 y |dk(  rb|| _        t        | j                  j                               D ]  }||d<   	 t        | j                  j                               D ]  }||d<   	 y |dk(  r5|| _        t        | j                  j                               D ]  }||d<   	 y |d	k(  r5|| _	        t        | j                  j                               D ]  }||d
<   	 y |dk(  rx|| _
        t        | j                  j                               D ]  }||d<   	 t        | j                  j                               D ]  }||d<   	 | j                  D ]  }||d<   	 y |dk(  rr|| _        t        | j                  j                               D ]  }||d<   	 t        | j                  j                               D ]  }||d<   	 | j                          y |dk(  rr|| _        t        | j                  j                               D ]  }||d<   	 t        | j                  j                               D ]  }||d<   	 | j                          y |dk(  rr|| _        t        | j                  j                               D ]  }||d<   	 t        | j                  j                               D ]  }||d<   	 | j                          y |dk(  rb|| _        t        | j                  j                               D ]  }||d<   	 t        | j                  j                               D ]  }||d<   	 y t!        j"                  | ||       y )Nr   r   r   
line_colorr7   
line_widthr8   
roof_color	roof_fillr   	shapeable	draggabler9   r:   r3   r;   )r   r   r   r   r   r   r   r   r   r   r   r   manager   r   r    r   rB   )r(   rC   rD   r   r   r   s         r/   rB   zTreeWidget.__setitem__  sw   8w!&T!"X $"1X !&T!"X %\!$DT1188:; %W <\!$DT1188:; %W <T2299;< %W =\!$DT2299;< %W =[ #DOT2299;<$V =[ #DOT1188:;$)[! <T2299;<$)[! =$)[! %X DLT1188:;!&X <T2299;<!&X =KKMX DLT1188:;!&X <T2299;<!&X =KKM]" %DT1188:;&+]# <T2299;<&+]# =KKMY!DMT1188:;"'Y <T2299;<"'Y = $$T47rF   c                 (   |d d dk(  r| j                   j                  |dd  d       S |d d dk(  r| j                  j                  |dd  d       S |d d dk(  r| j                  j                  |dd  d       S |dk(  r| j                  S |dk(  r| j
                  S |dk(  r| j                  S |d	k(  r| j                  S |d
k(  r| j                  S |dk(  r| j                  S |dk(  r| j                  S |dk(  r| j                  S t        j                  | |      S )Nr   r   r   r   r   r   r   r   r   r   r9   r:   r3   )r   getr   r   r   r   r   r   r   r   r   r   r   rI   )r(   rC   s     r/   rI   zTreeWidget.__getitem__	  s4   8w$$((ab488"1X $$((ab488"1X##''QR$77\!###\!###\!###[ ??"[ ??"X<<X<<]"$$$++D$77rF   c                     g S rK   r   rL   s    r/   r`   zTreeWidget._tags#  s    	rF   c                 "   t        | j                  j                               t        | j                  j                               z   }|D ]C  }|j	                         s|j                          |j                          |j                          E y rK   )r   r   r   r   r   showr   r   )r(   segsr   s      r/   r   zTreeWidget._manage&  si    D((//12T!!((*6
 
 D{{}					 rF   c                 z   |}|d   r| j                   | j                  |      }n| j                  | j                  |      }|j                         | u r*| j	                  |       | j                  |       || _        n |j                         j                  ||       |j                          |j                         j                         dd \  }}|j                         j                         dd \  }}|j                  ||z
  ||z
         |j                          |j                         j                  |       y)z)
        Collapse/expand a tree.
        r1   Nrd   )r   r   r   parentrO   r%   r   rU   r   r*   re   r   r   rA   )r(   r   old_treesegnew_treesegnewxnewyoldxoldys           r/   toggle_collapsedzTreeWidget.toggle_collapsed0  s#    v..tzz+/FGK//

;0GHK 4'%%k2"";/'DM ..{KH 	"((*//1"15t"((*//1"15tdTk2 	 	##K0rF   N)r5   )r   r   r   r   r   r'   r   r   r   r   r   r   r   r   r   r   rB   rI   r`   r   r
  r   rF   r/   r   r     sb    &R $.!7F	2	3.-.-.-B<.=8~841rF   r   c                   0    e Zd Zd Zd Zd Zd Zd Zd Zy)TreeViewc                 h   ddl m}m} || _        t	               | _        | j
                  j                  d       | j
                  j                  d| j                         | j
                  j                  d| j                         t        | j
                        x}| _
        | j
                  j                  d| j                  j                         t        | j
                        | _        | j                  j                  d       d| j                  j                          d	f}d| j                  j                          f}t!         | |t#        |                        | _        g | _        t)        t#        |            D ]q  }t+        |j-                         ||   |d
ddddd|
      }|j/                  |j0                         | j&                  j3                  |       |j5                  |dd       s | j7                          | j                  j9                  dd       | j;                          y )Nr   )ceilsqrtNLTKz<Control-x>z<Control-q>z<Control-p>   	helveticaboldz#008040z#004080z#004040whiter5   )	node_font
leaf_color
node_colorr   r   r   r   	leaf_fontboth)expandr   )mathr  r  _treesr   _toptitlebinddestroyr   _cframeprint_to_filer   _sizesetr   r@   rj   _width_widgetsr   r   r)   r   r
  r]   
add_widget_layoutpack_init_menubar)	r(   treesr  r  cfr  helvr   widgets	            r/   r'   zTreeView.__init__V  s   #D					}dll3		}dll3'		22T\		}dll&@&@A DII&


rdjjnn..7djjnn../ $tCJ/01s5z"A		a$$$!$F ##F$;$;<MM  (MM&!Q' #" 	0rF   c                 p   dx}x}x}}| j                   }t        t        | j                              D ]  }| j                  |   }|j	                         d d \  }}||z  dk(  r|}d}|j                  ||z
  ||z
         |j	                         d   dz   }t        ||j	                         d   dz         } y )Nr   rd   r   rb   )r%  r   rj   r&  re   r   rm   )	r(   r   r   r   ru   r8   r.  r  r	  s	            r/   r(  zTreeView._layout  s    ADs4==)*A]]1%F!;;=!,LT45yA~KKD!d(+a 2%AtV[[]1-23D +rF   c                    t        | j                        }t        |d      }|j                  dd| j                  j                  d       |j                  dd| j
                  d       |j                  d	d|
       t        |d      }|j                  d| j                  dd| j                         |j                  d| j                  dd| j                         |j                  d| j                  dd| j                         |j                  d| j                  dd| j                         |j                  d| j                  dd| j                         |j                  dd|
       | j                  j                  |       y )Nr   )tearoffzPrint to PostscriptzCtrl-p)r*   	underlinecommandacceleratorExitr5   zCtrl-xFile)r*   r2  menuTinyr   )r*   variabler2  rD   r3  Smallr  Medium   Large   Huge2   Zoom)r7  )r   r  add_commandr!  r"  r   add_cascadeadd_radiobuttonr#  resizeconfig)r(   menubarfilemenuzoommenus       r/   r*  zTreeView._init_menubar  s   tyy/+'LL.. 	 	 	
 	At|| 	 	
 	&AHE+  ZZKK 	! 	
 	  ZZKK 	! 	
 	  ZZKK 	! 	
 	  ZZKK 	! 	
 	  ZZKK 	! 	
 	&AHE		g&rF   c                    d| j                   j                          df}d| j                   j                          f}| j                   j                         }| j                   j                         }| j                  D ]a  }||d<   ||d<   ||d<   ||d<   | j                   j                         dk  rd|d	<   :| j                   j                         d
k  rd|d	<   ]d|d	<   c | j                          y )Nr  r  r  r  r9   r:   r   r5   r      rd   rb   )r#  r   r&  r(  )r(   er  r-  r9   r:   r.  s          r/   rE  zTreeView.resize  s    djjnn..7djjnn../!!mmF"&F;"&F;%F8%F8zz~~"$'(|$!B&'(|$'(|$ $ 	rF   c                 `    | j                   y | j                   j                          d | _         y rK   )r  r   )r(   rL  s     r/   r   zTreeView.destroy  s&    99			rF   c                 R    t               ry | j                  j                  |i | y)z
        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)r   r  mainloop)r(   argskwargss      r/   rO  zTreeView.mainloop  s%     9		D+F+rF   N)	r   r   r   r'   r(  r*  rE  r   rO  r   rF   r/   r  r  U  s"    )V45'n$	,rF   r  c                  0    t        |  j                          y)zd
    Open a new window containing a graphical diagram of the given
    trees.

    :rtype: None
    N)r  rO  )r+  s    r/   
draw_treesrS    s     e
rF   c                  &   dd l fd} t        ddd      }t        j                  d      }t	        |j                         |dd	d
dddd	      }|j                  |dd       d }d }t        j                  d      }t	        |j                         |||d      }fd}fd}	|j                  |j                         |j                  |j                         |j                  |d       |j                  d      j                  |	d       |j                         j                  |	d       t        |j                         |      }
|j                  |
|j                         d   dz   d       t        j                  d      }t        |j                         |ddd      }d|d<   |j                  |d|j                         d   dz          d }d}t        |j                         |j!                               }t#        |j                         |dd      }|j                  ||j                         d   dz   |j                         d   dz          t        j                  d      }t	        |j                         |dddd d!d"#      }|j%                          |j                  ||j                         d   dz   |j                         d   dz          |j                  |       |j                  |j                  d       |j'                          y )$Nr   c                 6    dj                  dd      z  | d<   y )N#%06dr   i?B r   randint)cwrandoms    r/   r   zdemo.<locals>.fill  s    v~~a886
rF   i&  i  rd   )r8   heightcloseenoughzU
    (S (NP the very big cat)
       (VP (Adv sorta) (V saw) (NP (Det the) (N dog))))r5   )r  ir  )r  italicr  blackgreen4blue2)r   r  r  r   r   r  r  r   c                 :    d}t        | t        | ||      d      S )N)r  ir  )fontgreenr   )r   r   )r)   textbigs      r/   boxitzdemo.<locals>.boxit  s    (FDs!C'RRrF   c                 2    t        | t        | |      d      S )Ncyanr   )r	   r   )r)   re  s     r/   ovalitzdemo.<locals>.ovalit  s    &*VT":HHrF   z/(S (NP this tree) (VP (V is) (AdjP shapeable))))r   c                 6    dj                  dd      z  | d<   y )Nz#%04d00r   '  r7   rW  )r   rZ  s    r/   r7   zdemo.<locals>.color  s    !FNN1d$;;WrF   c                     dj                  dd      z  | j                         d<   d| j                         j                         d<   y )NrV  r   rl  r   r  r7   )rX  r*   rY   )r   rZ  s    r/   color2zdemo.<locals>.color2!  s<    ")FNN1d,C"C+2(rF   rb   zb
    (S (NP this tree) (AUX was)
       (VP (V built) (PP (P with) (NP (N tree_to_treesegment))))))
tree_colortree_xspace
tree_widthr   c                 >   | d   dk(  r| j                  dd      j                         d   j                  d       | j                  dd      j                         d   j                  d       | j                  d      j                         d   j                  d       | j                         j                         d   j                  d       d| d<   y | j                  dd      j                         d   j                  d       | j                  dd      j                         d   j                  d       | j                  d      j                         d   j                  d       | j                         j                         d   j                  d       d| d<   y )Nr3   r4   r5   r   r6   rb   )r   r+   set_textr   )
treewidgets    r/   orientswitchzdemo.<locals>.orientswitch9  s_   m$4$$Q*335a8AA*M%%a+446q9BB:N%%a(113A6??
K%%'00215>>zJ(2J}%$$Q*335a8AA,O%%a+446q9BB<P%%a(113A6??M%%'00215>>|L(4J}%rF   aM  
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   r   z/(S (NP this tree) (VP (V is) (Adj horizontal)))brown2)r  r]  r  brown4r4   )r   r   r   r  r  r3   )rZ  r   r   
fromstringr   r)   r'  r   r
  r   r   r   r
   re   r   r   stripr   r   rO  )r   r,  r   tcrg  rj  treetoktc2r7   rn  parentree3tc3ru  re  twidgettextboxtree4tc4rZ  s                      @r/   demor    s   9 
3s	:B	;	A 

			,.

B MM"b"SI ooOPG
RYY['5&A
FC<3 ++,--.q!a##FA.""61-		S)EMM%1*B/OO	EE
 
		UxQ1C CMM#r2779Q<",-5#D djjl3G		W7aHGMM'388:a=2-sxxz!}r/ABOOMNE

		, 	C JJLMM#sxxz!}r)7<<>!+<r+ABNN< --q1 KKMrF   __main__N)r   tkinterr   r   r   nltk.draw.utilr   r   r   r	   r
   r   	nltk.treer   	nltk.utilr   r   r   r   r   r  rS  r  r   r   rF   r/   <module>r     s    % $   E< E<P4> $z1rP1 P1p
Q, Q,h l^ zF rF   