
    Ng                          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             dS dS )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dS )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| _        fd|D             | _                            ddddd	          | _	        | 
                    |           |D ]}| 
                    |           d| _        t          j        | fi | d
S )zI
        :type node:
        :type subtrees: list(CanvasWidgetI)
        r   
      Fc           	      D    g | ]}                     d d d d d          S )r   #006060fill)create_line).0ccanvass     J/var/www/html/ai-engine/env/lib/python3.11/site-packages/nltk/draw/tree.py
<listcomp>z.TreeSegmentWidget.__init__.<locals>.<listcomp>P   s2    XXX!v))!Q19)EEXXX     hiddenr   )r   stateoutlineN)_label	_subtrees_horizontal_roof_xspace_yspace_ordered_linescreate_polygon_polygon_add_child_widget	_managingr   __init__)selfr   labelsubtreesattribssubtrees    `    r   r.   zTreeSegmentWidget.__init__@   s    
 ! 
 YXXXxXXX--qr9 . 
 

 	u%%% 	, 	,G""7++++ dF66g66666r   c                    |                                  }|dk    r|| _        | j        r?| j        D ]}|                    |d           |                    | j        d           d S | j        D ]}|                    |d           |                    | j        d           d S |dk    r-|dk    r	d| _        d S |dk    r	d	| _        d S t          d
          |dk    r?| j        D ]}|                    ||           |                    | j        |           d S t          |t                    rE|d	         dk    r9| j        t          |d                            }|                    ||           d S |dk    r|                    | j        |           d S |dk    rA|                    | j        ||i           | j        D ]}|                    |||i           d S |dv r7|dk    r|| _
        n|dk    r|| _        |                     | j                   d S |dk    r	|| _        d S t          j        | ||           d S )Nroofr   )r    normalorientation
horizontal   verticalr   z*orientation must be horizontal or verticalcolorr   )r!   r   width)xspaceyspacer=   r>   ordered)r   r%   r)   
itemconfigr+   r$   
ValueError
isinstancetupleintr&   r'   updater"   r(   r   __setitem__)r/   attrvaluer   ls        r   rF   zTreeSegmentWidget.__setitem___   s   6>>DJz A 9 9A%%ax%8888!!$-x!@@@@@ 9 9A%%ax%8888!!$-x!@@@@@]""$$#$   *$$#$    !MNNNW__[ 1 1!!!%!0000dmU;;;;;e$$ 	8aG););CQLL)Aae,,,,,V^^dm%88888W__dmdE];;;[ 4 4!!!dE]33334 4)))x$!!$KK$$$$$Y!DMMM$T477777r   c                 T   |dk    r| j         S |dk    r-|                                                     | j        |          S |dk    r-|                                                     | j        d          S t	          |t
                    rT|d         dk    rH| j        t          |d                            }|                                                     |d          S |dk    r| j        S |d	k    r| j	        S |d
k    r| j
        rdS dS |dk    r| j        S t          j        | |          S )Nr5   r<   r;   r!   r   r9   r   r=   r>   r7   r8   r:   r?   )r%   r   itemcgetr+   rB   rC   r)   rD   r&   r'   r$   r(   r   __getitem__)r/   rG   rI   s      r   rL   zTreeSegmentWidget.__getitem__   s%   6>>:W__;;==))$->>>W__;;==))$-CCCe$$ 	8aG););CQLL)A;;==))!V444X<X<]"" "#|!zY= +D$777r   c                     | j         S N)r"   r/   s    r   r0   zTreeSegmentWidget.label   s
    {r   c                      | j         d d          S rN   )r#   rO   s    r   r1   zTreeSegmentWidget.subtrees   s    ~aaa  r   c                     |                      | j                   |                     |           || _        |                     | j                   dS )z2
        Set the node label to ``label``.
        N)_remove_child_widgetr"   r,   rE   )r/   r0   s     r   	set_labelzTreeSegmentWidget.set_label   sO     	!!$+...u%%%DK     r   c                     | j                             |          }|| j         |<   |                     |           |                     |           |                     |           dS )zC
        Replace the child ``oldchild`` with ``newchild``.
        N)r#   indexrR   r,   rE   )r/   oldchildnewchildrU   s       r   replace_childzTreeSegmentWidget.replace_child   se     $$X.. (u!!(+++x(((Hr   c                 $   | j                             |          }| j         |= |                     |           |                                                     | j                                                   |                     | j                   d S rN   )	r#   rU   rR   r   deleter)   poprE   r"   )r/   childrU   s      r   remove_childzTreeSegmentWidget.remove_child   sw    $$U++N5!!!%(((T[__..///DK     r   c           	      &   |                                  }| j                            ||           |                     |           | j                            |                    ddddd                     |                     | j                   d S )Nr   r   r   )	r   r#   insertr,   r)   appendr   rE   r"   )r/   rU   r\   r   s       r   insert_childzTreeSegmentWidget.insert_child   s    eU+++u%%%6--aAqy-IIJJJDK     r   c                 .    | j         r| j        gS | j        S rN   )r%   r+   r)   rO   s    r   _tagszTreeSegmentWidget._tags   s    : 	M?";r   c                     t          |t                    r'|                                                                }n|                                }| j        r|d         |d         |d         z   dz  fS |d         |d         z   dz  |d         fS )Nr   r9             @   )rB   r   r0   bboxr$   )r/   r\   rh   s      r   _subtree_topzTreeSegmentWidget._subtree_top   s    e.// 	 ;;==%%''DD::<<D 	8Gd1gQ/3677!WtAw&#-tAw77r   c                     | j                                         }| j        r|d         |d         |d         z   dz  fS |d         |d         z   dz  |d         fS )Nrg   r9   re   rf   r   )r"   rh   r$   )r/   rh   s     r   _node_bottomzTreeSegmentWidget._node_bottom   sb    {!! 	8Gd1gQ/3677!WtAw&#-tAw77r   c                 8   t          | j                  dk    rd S | j                                        d S || j        u r| j        }n|g}| j        r| j        s|                     |          }|                                 \  }}| j        d                                         \  }}}}| j        dd          D ]n}	|	                                }
t          ||
d                   }t          ||
d                   }t          ||
d                   }t          ||
d                   }o| j
        r5|                                                     | j        ||||||||	  	         n4|                                                     | j        ||||||||	  	         |D ]}	|                                 \  }}| j        | j                            |	                   }|                     |	          \  }}|                                                     |||||           d S )Nr   r9   rg   re   )lenr#   r"   rh   r(   r-   _maintain_orderrk   minmaxr$   r   coordsr+   r)   rU   ri   )r/   r\   need_updatenodexnodeyxminyminxmaxymaxr3   rh   linesubtreexsubtreeys                 r   _updatezTreeSegmentWidget._update   s2   t~!##F;%F DK.KK 'K= 	6 	6..u55K **,,#'>!#4#9#9#;#; tT4~abb) 	& 	&G<<>>DtT!W%%DtT!W%%DtT!W%%DtT!W%%DD 	KKMM  ueT4tUE    KKMM  ueT4tUE  
 # 	I 	IG!..00NUE;t~33G<<=D#'#4#4W#=#= XxKKMM  ueXxHHHH		I 	Ir   c                 d    | j         r|                     |          S |                     |          S rN   )r$   _maintain_order_horizontal_maintain_order_vertical)r/   r\   s     r   rn   z!TreeSegmentWidget._maintain_order  s5     	822599900777r   c                    |                                 \  }}}}|| j        u rY| j        D ]J}|                                 \  }}}	}
|| j        z   |k    r!|                    d|| j        z   |z
             K| j        S |g}| j                            |          }|| j        z   }t          |dz   t          | j                            D ]}| j        |                                          \  }}}	}
||k    rT| j        |                             ||z
  d           ||	|z
  | j        z   z  }|	                    | j        |                    || j        z
  }t          |dz
  dd          D ]}| j        |                                          \  }}}	}
||	k     rT| j        |                             ||	z
  d           ||	|z
  | j        z   z  }|	                    | j        |                    | j                                         \  }}}	}
|
|| j        z
  k    r-| j                            d|| j        z
  |
z
             | j        }|S )Nr   r9   )
rh   r"   r#   r'   moverU   r&   rangerm   r`   )r/   r\   lefttoprightbotr3   x1y1x2y2movedrU   xis                  r   r   z*TreeSegmentWidget._maintain_order_vertical  s[   "'**,,sE3DK> = =#*<<>> RR%**LLC$,$6$;<<<>!GEN((//E $A519c$.&9&9:: 4 4#'>!#4#9#9#;#; RRr66N1%**1r61555b4<//ALL!2333 t|#A519b"-- 4 4#'>!#4#9#9#;#; RRr66N1%**1r61555b4<//ALL!2333  ${//11RRC$,&&&  C$,$6$;<<< r   c                    |                                 \  }}}}|| j        u rX| j        D ]I}|                                 \  }}}	}
|| j        z   |k    r |                    || j        z   |z
             J| j        S |g}| j                            |          }|| j        z   }t          |dz   t          | j                            D ]}| j        |                                          \  }}}	}
||k    rT| j        |                             d||z
             ||
|z
  | j        z   z  }|	                    | j        |                    || j        z
  }t          |dz
  dd          D ]}| j        |                                          \  }}}	}
||
k     rT| j        |                             d||
z
             ||
|z
  | j        z   z  }|	                    | j        |                    | j                                         \  }}}	}
|	|| j        z
  k    r-| j                            || j        z
  |	z
  d           | j        }|S )Nr9   r   r   )
rh   r"   r#   r&   r   rU   r'   r   rm   r`   )r/   r\   r   r   r   r   r3   r   r   r   r   r   rU   yr   s                  r   r~   z,TreeSegmentWidget._maintain_order_horizontal<  sY   "'**,,sE3DK> < <#*<<>> RR4<'",,LL!5!:;;;>!GEN((//E dl"A519c$.&9&9:: 4 4#'>!#4#9#9#;#; RRr66N1%**1a"f555b4<//ALL!2333 dl"A519b"-- 4 4#'>!#4#9#9#;#; RRr66N1%**1a"f555b4<//ALL!2333  ${//11RRD4<'''  !4r!91=== r   c                    |                                  \  }}d}| j        D ]f}|                                }||d         z
  | j        z   }||d         z
  }|                    ||           ||d         |d         z
  | j        z   z  }gd}| j        D ] }||                     |          d         z  }!|t          | j                  z  }| j        D ]}|                    d||z
             d S )N   r   r9   re           )rk   r#   rh   r&   r   r'   ri   rm   )	r/   rs   rt   r   r3   subtree_bboxdxdycenters	            r   _manage_horizontalz$TreeSegmentWidget._manage_horizontalf  s   **,, ~ 	B 	BG"<<>>La(4<7B\!_$BLLR   a<?2T\AAAA ~ 	4 	4Gd''0033FF#dn%%% ~ 	, 	,GLLEFN++++	, 	,r   c                    |                                  \  }}d}| j        D ]f}|                                }||d         z
  | j        z   }||d         z
  }|                    ||           ||d         |d         z
  | j        z   z  }gd}| j        D ]5}||                     |          d         t          | j                  z  z  }6| j        D ]}|                    ||z
  d           d S )Nr   r9   rg   r   )rk   r#   rh   r'   r   r&   ri   rm   )	r/   rs   rt   r   r3   r   r   r   r   s	            r   _manage_verticalz"TreeSegmentWidget._manage_vertical|  s   **,, ~ 	B 	BG"<<>>La(4<7B\!_$BLLR   a<?2T\AAAA ~ 	J 	JGd''003c$.6I6IIIFF ~ 	, 	,GLL++++	, 	,r   c                 "   d| _         |                                 \  }}t          | j                  dk    rd S | j        r|                                  n|                                  | j        D ]}|                     |           d| _         d S )NTr   F)r-   rk   rm   r#   r$   r   r   r|   )r/   rs   rt   r3   s       r   _managezTreeSegmentWidget._manage  s    **,,t~!##F 	$##%%%%!!### ~ 	" 	"GLL!!!!r   c                 (    d| j          d| j         dS )Nz	[TreeSeg z: ])r"   r#   rO   s    r   __repr__zTreeSegmentWidget.__repr__  s    ;4;;;$.;;;;r   N)__name__
__module____qualname____doc__r.   rF   rL   r0   r1   rS   rX   r]   ra   rc   ri   rk   r|   rn   r   r~   r   r   r   r    r   r   r   r      sR        B7 7 7>*8 *8 *8X8 8 80  ! ! !! ! !  ! ! !! ! !  8 8 88 8 8'I 'I 'IR8 8 8( ( (T( ( (T, , ,,, , ,*  "< < < < <r   r   c                      t          |t                    r?  |                                fi } fd|D             }	t           ||	fi S   |fi S )Nc                 <    g | ]}t          |          S r   )_tree_to_treeseg)	r   r\   r   leaf_attribsloc_attribs	make_leaf	make_nodenode_attribstree_attribss	     r   r   z$_tree_to_treeseg.<locals>.<listcomp>  sM     
 
 
  	 	
 
 
r   )rB   r   r0   r   )
r   tr   r   r   r   r   r   r0   r1   s
   ` ``````  r   r   r     s     !T 4	&!''))<<|<<
 
 
 
 
 
 
 
 
 
 
 
 
 !IILIIIy33l333r   c           
         i }i }i }i }t          |                                          D ]\  }	}
|	dd         dk    r|
||	dd         <   !|	dd         dk    r|
||	dd         <   =|	dd         dk    r|
||	dd         <   Y|	dd         dk    r|
||	dd         <   ut          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itemsrA   r   )r   r   r   r   r2   r   r   r   r   keyrH   s              r   tree_to_treesegmentr     s   0 LLLK7==??++ 
8 
8
Urr7g$)LQRR!!!W$)LQRR!!!W$)LQRR!!!W#(KABB  036777		 	 	r   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dS )
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 | _        |                     ||d	           |                     ||d	          | _        |                     | j                   t1          j        | |fi | d S )
Nr;   z#008000z#008080r9   z#c0c0c0Fr   r:   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   r2   s         r   r.   zTreeWidget.__init__&  s
    $#
 #Y/$$#& 
! """61b11100B??t}---dF66g66666r   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1r   c                     | j         |         S r   )r   r   s     r   collapsed_treezTreeWidget.collapsed_treeT  s     $\22r   r9   c                    t          | j                                                  D ]}|                    ||           t          | j                                                  D ]}|                    ||           dS 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677 	. 	.DOOHf----.557788 	. 	.DOOHf----	. 	.r   c                    t          | j                                                  D ]}|                    ||           t          | j                                                  D ]}|                    ||           dS r   )r   r   r   	bind_dragr   r   s       r   bind_drag_treeszTreeWidget.bind_drag_treesh  s     -446677 	- 	-DNN8V,,,,.557788 	- 	-DNN8V,,,,	- 	-r   c                     | j         D ]}|                    ||           | j         D ]}|                    ||           dS z.
        Add a binding to all leaves.
        N)r   r   r/   r   r   leafs       r   bind_click_leaveszTreeWidget.bind_click_leavesq  s^     L 	. 	.DOOHf----L 	. 	.DOOHf----	. 	.r   c                     | j         D ]}|                    ||           | j         D ]}|                    ||           dS r   )r   r   r   s       r   bind_drag_leaveszTreeWidget.bind_drag_leavesz  s^     L 	- 	-DNN8V,,,,L 	- 	-DNN8V,,,,	- 	-r   c                     | j         D ]}|                    ||           | j         D ]}|                    ||           dS z-
        Add a binding to all nodes.
        N)r   r   r/   r   r   nodes       r   bind_click_nodeszTreeWidget.bind_click_nodes  s^     K 	. 	.DOOHf----K 	. 	.DOOHf----	. 	.r   c                     | j         D ]}|                    ||           | j         D ]}|                    ||           dS r   )r   r   r   s       r   bind_drag_nodeszTreeWidget.bind_drag_nodes  s^     K 	- 	-DNN8V,,,,K 	- 	-DNN8V,,,,	- 	-r   c           	      N   
 t          |t                    sd S  j        } j        
 ||                                fi  j        } j                            |           
 fd|                                D             } xj	        |z  c_	        t          ||d j         j         j                  }| j        |<   | j        |<   |                                 t#          t%          |                    D ]%}||         }	                     |	||fz              &d S )Nc                 .    g | ]} |fi j         S r   )r   )r   rI   r   r   r/   s     r   r   z4TreeWidget._make_collapsed_trees.<locals>.<listcomp>  s0    PPP))FA;;):;;PPPr   r9   )r5   r;   r   r<   )rB   r   r   r   r0   r   r   r`   leavesr   r   r   r   r   r   r   hider   rm   r   )r/   r   r   r   r   r   r   treesegr   r\   r   s   ``        @r   r   z TreeWidget._make_collapsed_trees  sI   !T"" 	FO	O	y@@d.?@@4   PPPPPPQXXZZPPP#""
 
 
 &-c"!
7 s1vv 	B 	BAaDE&&vucQDjAAAA	B 	Br   c                    
  j         } j        }t          |t                    r ||                                fi  j        } j                            |           |

 fdt          t          
                    D             }t          || j         j                  }| j        <    j        |<   |S  ||fi  j        }	 j                            |	           |	S )Nc                 R    g | ]#}                     |         |fz             $S r   )r   )r   r   r   childrenr   r/   s     r   r   z2TreeWidget._make_expanded_tree.<locals>.<listcomp>  sE        ((!cQDjII  r   )r;   r<   )r   r   rB   r   r0   r   r   r`   r   rm   r   r   r   r   r   r   r   )r/   r   r   r   r   r   r   r1   r   r   r   s   `` `      @r   r   zTreeWidget._make_expanded_tree  s(   O	O	a 	9VQWWYYDD$2CDDDKt$$$H      s8}}--  H (hd.>dFV  G )0D %"%DJwN9VQ<<$*;<<DL%%%Kr   c                    |d d         dk    r| j         D ]}|||dd          <   d S |d d         dk    r| j        D ]}|||dd          <   d S |dk    r7|| _        t          | j                                                  D ]}||d<   d S |dk    re|| _        t          | j                                                  D ]}||d<   t          | j                                                  D ]}||d<   d S |dk    r7|| _        t          | j                                                  D ]}||d<   d S |d	k    r7|| _	        t          | j                                                  D ]}||d
<   d S |dk    rt|| _
        t          | j                                                  D ]}||d<   t          | j                                                  D ]}||d<   | j        D ]}||d<   d S |dk    ry|| _        t          | j                                                  D ]}||d<   t          | j                                                  D ]}||d<   |                                  d S |dk    ry|| _        t          | j                                                  D ]}||d<   t          | j                                                  D ]}||d<   |                                  d S |dk    ry|| _        t          | j                                                  D ]}||d<   t          | j                                                  D ]}||d<   |                                  d S |dk    re|| _        t          | j                                                  D ]}||d<   t          | j                                                  D ]}||d<   d S t!          j        | ||           d S )Nr   r   r   
line_colorr;   
line_widthr<   
roof_color	roof_fillr   	shapeable	draggabler=   r>   r7   r?   )r   r   r   r   r   r   r   r   r   r   r   r&   manager'   r   r(   r   rF   )r/   rG   rH   r   r   r   s         r   rF   zTreeWidget.__setitem__  s   8w ' '!&T!""X' '"1"X   ' '!&T!""X' '\!!$DT188::;; & & %W& &\!!$DT188::;; & & %WT299;;<< & & %W& &\!!$DT299;;<< & & %W& &[  #DOT299;;<< % %$V% %[  #DOT188::;; * *$)[!!T299;;<< * *$)[!! * *$)[!!* *X DLT188::;; ' '!&XT299;;<< ' '!&XKKMMMMMX DLT188::;; ' '!&XT299;;<< ' '!&XKKMMMMM]"" %DT188::;; , ,&+]##T299;;<< , ,&+]##KKMMMMMY!DMT188::;; ( ("'YT299;;<< ( ("'Y( ( $T477777r   c                 "   |d d         dk    r#| j                             |dd          d           S |d d         dk    r#| j                            |dd          d           S |d d         dk    r#| 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   r=   r>   r7   )r   getr   r   r   r   r   r   r   r&   r'   r   r   rL   )r/   rG   s     r   rL   zTreeWidget.__getitem__	  sM   8w$((abb4888"1"X  $((abb4888"1"X#''QRR$777\!!##\!!##\!!##[  ?"[  ?"X<X<]""$$+D$777r   c                     g S rN   r   rO   s    r   rc   zTreeWidget._tags#  s    	r   c                 J   t          | j                                                  t          | j                                                  z   }|D ]R}|                                r<|                                 |                                 |                                 Sd S rN   )r   r   r   r   r   showr  r   )r/   segsr   s      r   r   zTreeWidget._manage&  s    D(//1122T!((**6
 6
 
  	 	D{{}} 						 	r   c                    |}|d         r| j         | j        |                  }n| j        | j        |                  }|                                | u r2|                     |           |                     |           || _        n(|                                                    ||           |                                 |	                                
                                dd         \  }}|	                                
                                dd         \  }}|                    ||z
  ||z
             |                                 |                                                    |           dS )z)
        Collapse/expand a tree.
        r5   Nrg   )r   r   r   parentrR   r,   r   rX   r  r0   rh   r   r   rE   )r/   r   old_treesegnew_treesegnewxnewyoldxoldys           r   toggle_collapsedzTreeWidget.toggle_collapsed0  so    v 	I.tz+/FGKK/
;0GHK 4''%%k222"";///'DMM  ..{KHHH 	"((**//11"1"5t"((**//11"1"5tdTk222 	 	##K00000r   N)r9   )r   r   r   r   r   r.   r   r   r   r   r   r   r   r   r   r   rF   rL   rc   r   r  r   r   r   r   r     s>       & &R $.!7 !7 !7 !7F	2 	2 	2	3 	3 	3. . . .- - - -. . . .- - - -. . . .- - - -B B B<  .=8 =8 =8~8 8 84    1 1 1 1 1r   r   c                   2    e Zd Zd Zd Zd Zd Zd Zd ZdS )TreeViewc                    ddl m}m} || _        t	                      | _        | j                            d           | j                            d| j                   | j                            d| j                   t          | j                  x}| _
        | j                            d| j
        j                   t          | j                  | _        | j                            d           d| j                                         d	f}d| j                                         f}t!           | |t#          |                                        | _        g | _        t)          t#          |                    D ]~}t+          |                                ||         |d
ddddd|
  
        }|                    |j                   | j                            |           |                    |dd           |                                  | j
                            dd           |                                  d S )Nr   )ceilsqrtNLTKz<Control-x>z<Control-q>z<Control-p>   	helveticaboldz#008040z#004080z#004040whiter9   )	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  rD   rm   _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   ########DD			}dl333	}dl333'	222T\	}dl&@AAA DI&&

rdjnn...7djnn.../ $$ttCJJ//0011s5zz"" 	( 	(A		a$$$!$  F ##F$;<<<M  (((MM&!Q''''000r   c                    dx}x}x}}| j         }t          t          | j                            D ]}| j        |         }|                                d d         \  }}||z  dk    r|}d}|                    ||z
  ||z
             |                                d         dz   }t          ||                                d         dz             }d S )Nr   rg   r   re   )r+  r   rm   r,  rh   r   rp   )	r/   r   r   r   rx   r<   r4  r  r  s	            r   r.  zTreeView._layout  s    ADs4=))** 	4 	4A]1%F!;;==!,LT45yA~~KKD!d(+++a 2%AtV[[]]1-233DD	4 	4r   c                    t          | j                  }t          |d          }|                    dd| j        j        d           |                    dd| j        d           |                    d	d|
           t          |d          }|                    d| j        dd| j	                   |                    d| j        dd| j	                   |                    d| j        dd| j	                   |                    d| j        dd| j	                   |                    d| j        dd| j	                   |                    dd|
           | j        
                    |           d S )Nr   )tearoffzPrint to PostscriptzCtrl-p)r0   	underlinecommandacceleratorExitr9   zCtrl-xFile)r0   r8  menuTinyr   )r0   variabler8  rH   r9  Smallr  Medium   Large   Huge2   Zoom)r=  )r   r#  add_commandr'  r(  r&  add_cascadeadd_radiobuttonr)  resizeconfig)r/   menubarfilemenuzoommenus       r   r0  zTreeView._init_menubar  s   ty//+++'L. 	 	 	
 	
 	
 	At| 	 	
 	
 	
 	&AHEEE+++  ZK 	! 	
 	
 	
 	  ZK 	! 	
 	
 	
 	  ZK 	! 	
 	
 	
 	  ZK 	! 	
 	
 	
 	  ZK 	! 	
 	
 	
 	&AHEEE	g&&&&&r   c                    d| j                                          df}d| j                                          f}| j                                         }| j                                         }| j        D ]a}||d<   ||d<   ||d<   ||d<   | j                                         dk     rd|d	<   9| j                                         d
k     rd|d	<   \d|d	<   b|                                  d S )Nr  r  r  r  r=   r>   r   r9   r      rg   re   )r)  r  r,  r.  )r/   er  r3  r=   r>   r4  s          r   rK  zTreeView.resize  s    djnn...7djnn.../!!!!m 
	) 
	)F"&F;"&F;%F8%F8z~~"$$'(|$$!!B&&'(|$$'(|$$r   c                 X    | j         d S | j                                          d | _         d S rN   )r#  r&  )r/   rR  s     r   r&  zTreeView.destroy  s.    9F				r   c                 J    t                      rdS  | j        j        |i | dS )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   rU  zTreeView.mainloop  s5     99 	F	D+F+++++r   N)	r   r   r   r.   r.  r0  rK  r&  rU  r   r   r   r  r  U  sp        ) ) )V4 4 45' 5' 5'n  $  	, 	, 	, 	, 	,r   r  c                  <    t          |                                   dS )zd
    Open a new window containing a graphical diagram of the given
    trees.

    :rtype: None
    N)r  rU  )r1  s    r   
draw_treesrY    s      e
Fr   c                  V   dd l fd} t          ddd          }t          j        d          }t	          |                                |dd	d
dddd	  	        }|                    |dd           d }d }t          j        d          }t	          |                                |||d          }fd}fd}	|                    |j                   |                    |j                   |	                    |d           |
                    d                              |	d           |
                                                    |	d           t          |                                |          }
|                    |
|                                d         dz   d           t          j        d          }t          |                                |ddd          }d|d<   |                    |d|                                d         dz              d }d}t          |                                |                                          }t#          |                                |dd          }|                    ||                                d         dz   |                                d         dz              t          j        d          }t	          |                                |dddd d!d"#          }|                                 |                    ||                                d         dz   |                                d         dz              |                    |           |                    |j        d           |                                 d S )$Nr   c                 @    d                     dd          z  | d<   d S )N#%06dr   i?B r   randint)cwrandoms    r   r   zdemo.<locals>.fill  s#    v~~a8886


r   i&  i  rg   )r<   heightcloseenoughzU
    (S (NP the very big cat)
       (VP (Adv sorta) (V saw) (NP (Det the) (N dog))))r9   )r  ir  )r  italicr  blackgreen4blue2)r   r  r  r   r   r  r  r   c                 J    d}t          | t          | ||          d          S )N)r  ir  )fontgreenr   )r   r   )r   textbigs      r   boxitzdemo.<locals>.boxit  s+    (FDs!C!C!C'RRRRr   c                 B    t          | t          | |          d          S )Ncyanr   )r	   r   )r   rk  s     r   ovalitzdemo.<locals>.ovalit  s!    &*VT":":HHHHr   z/(S (NP this tree) (VP (V is) (AdjP shapeable))))r   c                 @    d                     dd          z  | d<   d S )Nz#%04d00r   '  r;   r]  )r   r`  s    r   r;   zdemo.<locals>.color  s#    !FNN1d$;$;;Wr   c                     d                     dd          z  |                                 d<   d|                                                                 d<   d S )Nr\  r   rr  r   r  r;   )r^  r0   r\   )r   r`  s    r   color2zdemo.<locals>.color2!  sL    ")FNN1d,C,C"C+2(((r   re   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|                      dd                                          d                             d           |                     dd                                          d                             d           |                     d                                          d                             d           |                                                                 d                             d           d| d<   d S |                      dd                                          d                             d           |                     dd                                          d                             d           |                     d                                          d                             d           |                                                                 d                             d           d| d<   d S )Nr7   r8   r9   r   r:   re   )r   r1   set_textr   )
treewidgets    r   orientswitchzdemo.<locals>.orientswitch9  s   m$44$$Q**3355a8AA*MMM%%a++4466q9BB:NNN%%a((1133A6??
KKK%%''002215>>zJJJ(2J}%%%$$Q**3355a8AA,OOO%%a++4466q9BB<PPP%%a((1133A6??MMM%%''002215>>|LLL(4J}%%%r   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  rc  r  brown4r8   )r   r   r   r  r  r7   )r`  r   r   
fromstringr   r   r-  r   r  r   r   r   r
   rh   r   r   stripr   r  rU  )r   r2  r   tcrm  rp  treetoktc2r;   rt  parentree3tc3r{  rk  twidgettextboxtree4tc4r`  s                      @r   demor    s   MMM9 9 9 9 9 
3s	:	:	:B	;	 	A 

			,.

 

 

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