a
    ±Kbk™  ã                   @   s¶   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„ deƒZdd	„ Zeefd
d„ZG dd„ deƒZG dd„ dƒZdd„ Zdd„ Zedkr²eƒ  dS )z
Graphically display a Tree.
é    )ÚIntVarÚMenuÚTk)Ú	BoxWidgetÚCanvasFrameÚCanvasWidgetÚ
OvalWidgetÚParenWidgetÚ
TextWidget)ÚTree)Úin_idlec                   @   s°   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zd d!„ Zd"d#„ Zd$d%„ Zd&d'„ Zd(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                    s   || _ || _d| _d| _d| _d| _d| _‡ fdd„|D ƒ| _ˆ jddddd	d
| _	|  
|¡ |D ]}|  
|¡ qbd| _tj| ˆ fi |¤Ž dS )zI
        :type node:
        :type subtrees: list(CanvasWidgetI)
        r   é
   é   Fc              	      s    g | ]}ˆ j d d d d dd‘qS )r   ú#006060©Úfill)Úcreate_line)Ú.0Úc©Úcanvas© ú-lib/python3.9/site-packages/nltk/draw/tree.pyÚ
<listcomp>P   ó    z.TreeSegmentWidget.__init__.<locals>.<listcomp>Ú Úhiddenr   )r   ÚstateÚoutlineN)Ú_labelÚ	_subtreesÚ_horizontalÚ_roofÚ_xspaceÚ_yspaceÚ_orderedÚ_linesZcreate_polygonÚ_polygonÚ_add_child_widgetÚ	_managingr   Ú__init__)Úselfr   ÚlabelÚsubtreesÚattribsÚsubtreer   r   r   r+   @   s     
ÿ
zTreeSegmentWidget.__init__c                 C   sÐ  |   ¡ }|dkrv|| _| jrH| jD ]}|j|dd q"|j| jdd n*| jD ]}|j|dd qN|j| jdd nV|dkrª|dkrŽd| _n|dkržd	| _ntd
ƒ‚n"|dkrÞ| jD ]}|j||d q¸|j| j|d nît|tƒr|d	 dkr| jt	|d ƒ }|j||d n²|dkr6|j| j|d n–|dkrr| | j||i¡ | jD ]}| |||i¡ qXnZ|dv r¬|dkrŽ|| _
n|dkrž|| _|  | j¡ n |dkr¾|| _nt | ||¡ d S )NÚroofr   )r   ZnormalÚorientationÚ
horizontalé   Úverticalr   z*orientation must be horizontal or verticalÚcolorr   )r   r   Úwidth)ÚxspaceÚyspacer8   r9   Úordered)r   r#   r'   Z
itemconfigr(   r"   Ú
ValueErrorÚ
isinstanceÚtupleÚintr$   r%   Úupdater    r&   r   Ú__setitem__)r,   ÚattrÚvaluer   Úlr   r   r   r@   _   sL    









zTreeSegmentWidget.__setitem__c                 C   sÌ   |dkr| j S |dkr(|  ¡  | j|¡S |dkrB|  ¡  | jd¡S t|tƒrz|d dkrz| jt|d ƒ }|  ¡  |d¡S |dkrˆ| jS |d	kr–| j	S |d
kr®| j
r¨dS dS n|dkr¼| jS t | |¡S d S )Nr1   r7   r6   r   r   r4   r   r8   r9   r2   r3   r5   r:   )r#   r   Zitemcgetr(   r<   r=   r'   r>   r$   r%   r"   r&   r   Ú__getitem__)r,   rA   rC   r   r   r   rD   ‹   s(    zTreeSegmentWidget.__getitem__c                 C   s   | j S ©N)r    ©r,   r   r   r   r-   £   s    zTreeSegmentWidget.labelc                 C   s   | j d d … S rE   )r!   rF   r   r   r   r.   ¦   s    zTreeSegmentWidget.subtreesc                 C   s,   |   | j¡ |  |¡ || _|  | j¡ dS )z2
        Set the node label to ``label``.
        N)Ú_remove_child_widgetr    r)   r?   )r,   r-   r   r   r   Ú	set_label©   s    
zTreeSegmentWidget.set_labelc                 C   s8   | j  |¡}|| j |< |  |¡ |  |¡ |  |¡ dS )zC
        Replace the child ``oldchild`` with ``newchild``.
        N)r!   ÚindexrG   r)   r?   )r,   ZoldchildZnewchildrI   r   r   r   Úreplace_child²   s
    


zTreeSegmentWidget.replace_childc                 C   sB   | j  |¡}| j |= |  |¡ |  ¡  | j ¡ ¡ |  | j¡ d S rE   )	r!   rI   rG   r   Údeleter'   Úpopr?   r    )r,   ÚchildrI   r   r   r   Úremove_child¼   s
    
zTreeSegmentWidget.remove_childc              	   C   sL   |   ¡ }| j ||¡ |  |¡ | j |jdddddd¡ |  | j¡ d S )Nr   r   r   )	r   r!   Úinsertr)   r'   Úappendr   r?   r    )r,   rI   rM   r   r   r   r   Úinsert_childÃ   s
    
zTreeSegmentWidget.insert_childc                 C   s   | j r| jgS | jS d S rE   )r#   r(   r'   rF   r   r   r   Ú_tagsÌ   s    zTreeSegmentWidget._tagsc                 C   sb   t |tƒr| ¡  ¡ }n| ¡ }| jrB|d |d |d  d fS |d |d  d |d fS d S )Nr   r4   é   ç       @é   )r<   r   r-   Úbboxr"   )r,   rM   rV   r   r   r   Ú_subtree_topÒ   s    
zTreeSegmentWidget._subtree_topc                 C   sL   | j  ¡ }| jr,|d |d |d  d fS |d |d  d |d fS d S )NrU   r4   rS   rT   r   )r    rV   r"   )r,   rV   r   r   r   Ú_node_bottomÜ   s    
zTreeSegmentWidget._node_bottomc                 C   sb  t | jƒdkrd S | j ¡ d u r$d S || ju r6| j}n|g}| jrR| jsR|  |¡}|  ¡ \}}| jd  ¡ \}}}}| jdd … D ]D}	|	 ¡ }
t||
d ƒ}t||
d ƒ}t	||
d ƒ}t	||
d ƒ}q‚| j
rð|  ¡  | j||||||||¡	 n |  ¡  | j||||||||¡	 |D ]H}	|  ¡ \}}| j| j |	¡ }|  |	¡\}}|  ¡  |||||¡ qd S )Nr   r4   rU   rS   )Úlenr!   r    rV   r&   r*   Ú_maintain_orderrX   ÚminÚmaxr"   r   Zcoordsr(   r'   rI   rW   )r,   rM   Zneed_updateÚnodexÚnodeyZxminZyminZxmaxÚymaxr0   rV   ÚlineZsubtreexZsubtreeyr   r   r   Ú_updateã   s:    

ÿÿzTreeSegmentWidget._updatec                 C   s   | j r|  |¡S |  |¡S d S rE   )r"   Ú_maintain_order_horizontalÚ_maintain_order_vertical)r,   rM   r   r   r   rZ     s    
z!TreeSegmentWidget._maintain_orderc                 C   sª  |  ¡ \}}}}|| ju r`| jD ]8}|  ¡ \}}}	}
|| j |kr | d|| j | ¡ q | jS |g}| j |¡}|| j }t|d t| jƒƒD ]Z}| j|   ¡ \}}}	}
||kr| j|  || d¡ ||	| | j 7 }| 	| j| ¡ q|| j }t|d ddƒD ]^}| j|   ¡ \}}}	}
||	k r| j|  ||	 d¡ ||	| | j 8 }| 	| j| ¡ q| j  ¡ \}}}	}
|
|| j kr¦| j d|| j |
 ¡ | j}|S )Nr   r4   éÿÿÿÿ)
rV   r    r!   r%   ÚmoverI   r$   ÚrangerY   rP   )r,   rM   ÚleftÚtopÚrightÚbotr0   Úx1Úy1Úx2Úy2ÚmovedrI   ÚxÚir   r   r   rc     s8    




z*TreeSegmentWidget._maintain_order_verticalc                 C   s¨  |  ¡ \}}}}|| ju r^| jD ]6}|  ¡ \}}}	}
|| j |kr | || j | ¡ q | jS |g}| j |¡}|| j }t|d t| jƒƒD ]Z}| j|   ¡ \}}}	}
||krŽ| j|  d|| ¡ ||
| | j 7 }| 	| j| ¡ qŽ|| j }t|d ddƒD ]^}| j|   ¡ \}}}	}
||
k r| j|  d||
 ¡ ||
| | j 8 }| 	| j| ¡ q| j  ¡ \}}}	}
|	|| j kr¤| j || j |	 d¡ | j}|S )Nr4   r   rd   )
rV   r    r!   r$   re   rI   r%   rf   rY   rP   )r,   rM   rg   rh   ri   rj   r0   rk   rl   rm   rn   ro   rI   Úyrq   r   r   r   rb   <  s8    




z,TreeSegmentWidget._maintain_order_horizontalc           	      C   s¸   |   ¡ \}}d}| jD ]P}| ¡ }||d  | j }||d  }| ||¡ ||d |d  | j 7 }qd}| jD ]}||  |¡d 7 }qr|t| jƒ }| jD ]}| d|| ¡ qžd S )Né   r   r4   rS   ç        )rX   r!   rV   r$   re   r%   rW   rY   )	r,   r]   r^   rr   r0   Úsubtree_bboxÚdxÚdyÚcenterr   r   r   Ú_manage_horizontalf  s    


z$TreeSegmentWidget._manage_horizontalc           	      C   s´   |   ¡ \}}d}| jD ]P}| ¡ }||d  | j }||d  }| ||¡ ||d |d  | j 7 }qd}| jD ] }||  |¡d t| jƒ 7 }qr| jD ]}| || d¡ qšd S )Nr   r4   rU   rt   )rX   r!   rV   r%   re   r$   rW   rY   )	r,   r]   r^   rp   r0   ru   rw   rv   rx   r   r   r   Ú_manage_vertical|  s    


z"TreeSegmentWidget._manage_verticalc                 C   s\   d| _ |  ¡ \}}t| jƒdkr$d S | jr4|  ¡  n|  ¡  | jD ]}|  |¡ qBd| _ d S )NTr   F)r*   rX   rY   r!   r"   ry   rz   ra   )r,   r]   r^   r0   r   r   r   Ú_manage‘  s    

zTreeSegmentWidget._managec                 C   s   d| j › d| j› dS )Nz	[TreeSeg z: ú])r    r!   rF   r   r   r   Ú__repr__¢  s    zTreeSegmentWidget.__repr__N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r+   r@   rD   r-   r.   rH   rJ   rN   rQ   rR   rW   rX   ra   rZ   rc   rb   ry   rz   r{   r}   r   r   r   r   r      s*   !,	
	
)**r   c           
         sh   t |tƒrRˆˆ | ¡ fi ˆ¤Ž}‡ ‡‡‡‡‡‡fdd„|D ƒ}	tˆ ||	fi ˆ¤ŽS ˆˆ |fi ˆ¤ŽS d S )Nc                    s"   g | ]}t ˆ |ˆˆˆˆˆˆƒ‘qS r   )Ú_tree_to_treeseg)r   rM   ©r   Úleaf_attribsÚloc_attribsÚ	make_leafÚ	make_nodeÚnode_attribsÚtree_attribsr   r   r   ²  s   öøz$_tree_to_treeseg.<locals>.<listcomp>)r<   r   r-   r   )
r   Útr‡   r†   r‰   rˆ   r„   r…   r-   r.   r   rƒ   r   r‚   ¦  s    

õr‚   c              	   K   sÐ   i }i }i }i }t | ¡ ƒD ]œ\}	}
|	dd… dkrF|
||	dd… < q|	dd… dkrh|
||	dd… < q|	dd… dkrŠ|
||	dd… < q|	dd… dkr¬|
||	dd… < qtd|	 ƒ‚q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é   Ztree_Únode_Úleaf_é   Úloc_zBad attribute: %s)ÚlistÚitemsr;   r‚   )r   rŠ   r‡   r†   r/   r‰   rˆ   r„   r…   ÚkeyrB   r   r   r   Útree_to_treesegmentÄ  s0    ør“   c                   @   s¢   e Zd ZdZeefdd„Zdd„ Zdd„ Zd$d	d
„Zd%dd„Z	d&dd„Z
d'dd„Zd(dd„Zd)dd„Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd „ Zd!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                 K   s¾   || _ || _|| _i | _i | _ddi| _d| _d| _d| _d| _	d| _
d| _d| _d| _d| _i | _i | _i | _g | _g | _|  ||d	¡ |  ||d	¡| _|  | j¡ tj| |fi |¤Ž d S )
Nr6   z#008000z#008080r4   z#c0c0c0Fr   r5   r   )Ú
_make_nodeÚ
_make_leafZ_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/   r   r   r   r+   &  s0    
zTreeWidget.__init__c                 G   s
   | 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,   Zpath_to_treer   r   r   Úexpanded_treeI  s    	zTreeWidget.expanded_treec                 G   s
   | j | S r¨   )r¢   r©   r   r   r   Úcollapsed_treeT  s    	zTreeWidget.collapsed_treer4   c                 C   sD   t | j ¡ ƒD ]}| ||¡ qt | j ¡ ƒD ]}| ||¡ q.dS ©z5
        Add a binding to all tree segments.
        N)r   r¡   ÚvaluesÚ
bind_clickr¢   ©r,   ÚcallbackÚbuttonÚtsegr   r   r   Úbind_click_trees_  s    zTreeWidget.bind_click_treesc                 C   sD   t | j ¡ ƒD ]}| ||¡ qt | j ¡ ƒD ]}| ||¡ q.dS r¬   )r   r¡   r­   Ú	bind_dragr¢   r¯   r   r   r   Úbind_drag_treesh  s    zTreeWidget.bind_drag_treesc                 C   s4   | j D ]}| ||¡ q| j D ]}| ||¡ qdS ©z.
        Add a binding to all leaves.
        N)r¤   r®   ©r,   r°   r±   Úleafr   r   r   Úbind_click_leavesq  s    

zTreeWidget.bind_click_leavesc                 C   s4   | j D ]}| ||¡ q| j D ]}| ||¡ qdS r¶   )r¤   r´   r·   r   r   r   Úbind_drag_leavesz  s    

zTreeWidget.bind_drag_leavesc                 C   s4   | j D ]}| ||¡ q| j D ]}| ||¡ qdS ©z-
        Add a binding to all nodes.
        N)r£   r®   ©r,   r°   r±   Únoder   r   r   Úbind_click_nodesƒ  s    

zTreeWidget.bind_click_nodesc                 C   s4   | j D ]}| ||¡ q| j D ]}| ||¡ qdS r»   )r£   r´   r¼   r   r   r   Úbind_drag_nodesŒ  s    

zTreeWidget.bind_drag_nodesc           
   	      sÐ   t |tƒsd S ˆj}ˆj‰|ˆ | ¡ fi ˆj¤Ž}ˆj |¡ ‡ ‡‡fdd„| ¡ D ƒ}ˆ j	|7  _	t
ˆ ||dˆjˆjˆjd}|ˆj|< |ˆj|< | ¡  tt|ƒƒD ] }|| }	ˆ ˆ |	||f ¡ qªd S )Nc                    s    g | ]}ˆˆ |fi ˆj ¤Ž‘qS r   )r˜   )r   rC   ©r   r†   r,   r   r   r     r   z4TreeWidget._make_collapsed_trees.<locals>.<listcomp>r4   )r1   r6   r   r7   )r<   r   r•   r–   r-   r—   r£   rP   Úleavesr¤   r   rœ   r   r›   r¢   r    Úhiderf   rY   r¥   )
r,   r   rŠ   r’   r‡   r½   rÁ   Útreesegrq   rM   r   rÀ   r   r¥   •  s.    
ù


z TreeWidget._make_collapsed_treesc           
         s´   ˆj }ˆj}t|tƒrŒ|ˆ | ¡ fi ˆj¤Ž}ˆj |¡ |‰‡ ‡‡‡fdd„tt	ˆƒƒD ƒ}t
ˆ ||ˆjˆjd}|ˆjˆ< ˆˆj|< |S |ˆ |fi ˆj¤Ž}	ˆj |	¡ |	S d S )Nc                    s$   g | ]}ˆ  ˆ ˆ| ˆ|f ¡‘qS r   )r¦   )r   rq   ©r   Zchildrenr’   r,   r   r   r   »  s   ÿz2TreeWidget._make_expanded_tree.<locals>.<listcomp>)r6   r7   )r•   r–   r<   r   r-   r—   r£   rP   rf   rY   r   rš   r›   r¡   r    r˜   r¤   )
r,   r   rŠ   r’   r‡   r†   r½   r.   rÃ   r¸   r   rÄ   r   r¦   ³  s$    

þÿ

zTreeWidget._make_expanded_treec                 C   s  |d d… dkr0| j D ]}|||dd … < qnÐ|d d… dkr`| jD ]}|||dd … < qFn |dkrŽ|| _t| j ¡ ƒD ]}||d< q|nr|dkrØ|| _t| j ¡ ƒD ]}||d< qªt| j ¡ ƒD ]}||d< qÆn(|dkr|| _t| j ¡ ƒD ]}||d< qönø|d	kr:|| _	t| j ¡ ƒD ]}||d
< q&nÆ|dkr || _
t| j ¡ ƒD ]}||d< qXt| j ¡ ƒD ]}||d< qv| jD ]}||d< qŒn`|dkrø|| _t| j ¡ ƒD ]}||d< q¾t| j ¡ ƒD ]}||d< qÜ|  ¡  n|dkrN|| _t| j ¡ ƒD ]}||d< qt| j ¡ ƒD ]}||d< q4|  ¡  n²|dkr¤|| _t| j ¡ ƒD ]}||d< qlt| j ¡ ƒD ]}||d< qŠ|  ¡  n\|dkrò|| _t| j ¡ ƒD ]}||d< qÂt| j ¡ ƒD ]}||d< qànt | ||¡ d S )Nr‹   rŒ   r   Ú
line_colorr6   Ú
line_widthr7   Ú
roof_colorÚ	roof_fillr   Ú	shapeableÚ	draggabler8   r9   r2   r:   )r£   r¤   rš   r   r¡   r­   r›   r¢   rœ   r   rž   r$   Úmanager%   rŸ   r&   r   r@   )r,   rA   rB   r½   r¸   r²   r   r   r   r@   Ê  sx    












zTreeWidget.__setitem__c                 C   sò   |d d… dkr&| j  |dd … d ¡S |d d… dkrL| j |dd … d ¡S |d d… dkrr| 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 | |¡S d S )Nr‹   rŒ   r   rŽ   r   rÅ   rÆ   rÇ   rÈ   rÉ   r8   r9   r2   )r—   Úgetr˜   r™   rš   r›   rœ   r   rž   r$   r%   rŸ   r   rD   )r,   rA   r   r   r   rD   	  s.    zTreeWidget.__getitem__c                 C   s   g S rE   r   rF   r   r   r   rR   #  s    zTreeWidget._tagsc                 C   sJ   t | j ¡ ƒt | j ¡ ƒ }|D ]$}| ¡ r | ¡  | ¡  | ¡  q d S rE   )r   r¡   r­   r¢   r   ÚshowrË   rÂ   )r,   Zsegsr²   r   r   r   r{   &  s    ÿzTreeWidget._managec                 C   sÌ   |}|d r| j | j|  }n| j| j|  }| ¡ | u rV|  |¡ |  |¡ || _n| ¡  ||¡ | ¡  | 	¡  
¡ dd… \}}| 	¡  
¡ dd… \}}| || || ¡ | ¡  | ¡  |¡ dS )z)
        Collapse/expand a tree.
        r1   NrU   )r¡   r    r¢   ÚparentrG   r)   r§   rJ   rÍ   r-   rV   re   rÂ   r?   )r,   rÃ   Zold_treesegZnew_treesegZnewxZnewyÚoldxÚoldyr   r   r   Útoggle_collapsed0  s    

zTreeWidget.toggle_collapsedN)r4   )r4   )r4   )r4   )r4   )r4   )r~   r   r€   r   r
   r+   rª   r«   r³   rµ   r¹   rº   r¾   r¿   r¥   r¦   r@   rD   rR   r{   rÑ   r   r   r   r   r”   ý  s$   )ÿ
#
	
	
	
	
	
	?
r”   c                   @   s<   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ ZdS )ÚTreeViewc           	      G   sB  ddl m}m} || _tƒ | _| j d¡ | j d| j¡ | j d| j¡ t	| jƒ }| _
| j d| j
j¡ t| jƒ| _| j d¡ d| j ¡  d	f}d| j ¡  f}t||t|ƒƒƒƒ| _g | _tt|ƒƒD ]N}t| ¡ || |d
ddddd|d
}| |j¡ | j |¡ | |dd¡ qÎ|  ¡  | j
jddd |  ¡  d S )Nr   )ÚceilÚsqrtZNLTKz<Control-x>z<Control-q>z<Control-p>é   Ú	helveticaÚboldz#008040z#004080z#004040Úwhiter4   )Ú	node_fontÚ
leaf_colorÚ
node_colorrÇ   rÈ   rÅ   rÊ   Ú	leaf_fontZboth)Úexpandr   )ZmathrÓ   rÔ   Z_treesr   Ú_topÚtitleZbindÚdestroyr   Ú_cframeÚprint_to_filer   Ú_sizeÚsetrÌ   r>   rY   Ú_widthÚ_widgetsrf   r”   r   r³   rÑ   rP   Ú
add_widgetÚ_layoutÚpackÚ_init_menubar)	r,   ÚtreesrÓ   rÔ   Úcfr×   Úhelvrq   Úwidgetr   r   r   r+   V  sB    özTreeView.__init__c           	      C   sš   d } } }}| j }tt| jƒƒD ]p}| j| }| ¡ d d… \}}|| dkrZ|}d}| || || ¡ | ¡ d d }t|| ¡ d d ƒ}q$d S )Nr   rU   r   rS   )rå   rf   rY   ræ   rV   re   r\   )	r,   rq   rp   rr   r_   r7   rî   rÏ   rÐ   r   r   r   rè     s    
zTreeView._layoutc                 C   sö   t | jƒ}t |dd}|jdd| jjdd |jdd| jdd |jd	d|d
 t |dd}|jd| jdd| j	d |jd| jdd| j	d |jd| jdd| j	d |jd| jdd| j	d |jd| jdd| j	d |jdd|d
 | jj
|d d S )Nr   )ZtearoffzPrint to PostscriptzCtrl-p)r-   Ú	underlineÚcommandZacceleratorZExitr4   zCtrl-xZFile)r-   rï   ÚmenuZTinyr   )r-   Úvariablerï   rB   rð   ZSmallrÕ   ZMediumé   ZLargeé   ZHugeé2   ZZoom)rñ   )r   rÞ   Zadd_commandrá   râ   rà   Zadd_cascadeZadd_radiobuttonrã   ÚresizeZconfig)r,   ZmenubarZfilemenuZzoommenur   r   r   rê   Ž  sd    
ü
ÿûûûûûzTreeView._init_menubarc                 G   s¦   d| j  ¡  df}d| j  ¡  f}| j  ¡ }| j  ¡ }| jD ]\}||d< ||d< ||d< ||d< | j  ¡ dk rxd|d	< q<| j  ¡ d
k rd|d	< q<d|d	< q<|  ¡  d S )NrÖ   r×   rÙ   rÜ   r8   r9   rs   r4   rÆ   é   rU   rS   )rã   rÌ   ræ   rè   )r,   Úer×   rí   r8   r9   rî   r   r   r   rö   Å  s    





zTreeView.resizec                 G   s"   | j d u rd S | j  ¡  d | _ d S rE   )rÞ   rà   )r,   rø   r   r   r   rà   ×  s    

zTreeView.destroyc                 O   s    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Úkwargsr   r   r   rù   Ý  s    zTreeView.mainloopN)	r~   r   r€   r+   rè   rê   rö   rà   rù   r   r   r   r   rÒ   U  s   +7rÒ   c                  G   s   t | Ž  ¡  dS )zd
    Open a new window containing a graphical diagram of the given
    trees.

    :rtype: None
    N)rÒ   rù   )rë   r   r   r   Ú
draw_treesé  s    rü   c                     s   dd l ‰ ‡ fdd„} tdddd}t d¡}t| ¡ |d	d
dddddd	}| |dd¡ dd„ }dd„ }t d¡}t| ¡ |||d	d}‡ fdd„}‡ fdd„}	| |j¡ | |j¡ | 	|d¡ | 
d	¡ |	d¡ | 
¡  |	d¡ t| ¡ |ƒ}
| |
| ¡ d d d¡ t d¡}t| ¡ |dddd}d	|d< | |d| ¡ d d ¡ d d!„ }d"}t| ¡ | ¡ ƒ}t| ¡ |dd	d#}| || ¡ d d | ¡ d d ¡ t d$¡}t| ¡ |d	d%d%d&d'd(d)}| ¡  | || ¡ d d | ¡ d d ¡ | |¡ | |jd¡ | ¡  d S )*Nr   c                    s   dˆ   dd¡ | d< d S )Nú#%06dr   i?B r   ©Úrandint)Zcw©Úrandomr   r   r   ü  s    zdemo.<locals>.filli&  iÂ  rU   )r7   ZheightZcloseenoughzU
    (S (NP the very big cat)
       (VP (Adv sorta) (V saw) (NP (Det the) (N dog))))r4   )rÖ   iòÿÿÿr×   )rÖ   éôÿÿÿZitalicrØ   ZblackZgreen4Zblue2)rÊ   rÙ   rÜ   rÈ   rÇ   rÚ   rÛ   r   c                 S   s   d}t | t| ||dddS )N)rÖ   iðÿÿÿr×   )ZfontZgreenr   )r   r
   )r   ÚtextZbigr   r   r   Úboxit  s    zdemo.<locals>.boxitc                 S   s   t | t| |ƒddS )NZcyanr   )r   r
   )r   r  r   r   r   Úovalit  s    zdemo.<locals>.ovalitz/(S (NP this tree) (VP (V is) (AdjP shapeable))))rÉ   c                    s   dˆ   dd¡ | d< d S )Nz#%04d00r   é'  r6   rþ   )r½   r   r   r   r6     s    zdemo.<locals>.colorc                    s,   dˆ   dd¡ |  ¡ d< d|  ¡  ¡ d< d S )Nrý   r   r  r   rØ   r6   )rÿ   r-   rM   )rÃ   r   r   r   Úcolor2!  s    zdemo.<locals>.color2rS   zb
    (S (NP this tree) (AUX was)
       (VP (V built) (PP (P with) (NP (N tree_to_treesegment))))))Z
tree_colorZtree_xspaceZ
tree_widthrÊ   c                 S   sæ   | d dkrx|   dd¡ ¡ d  d¡ |  dd¡ ¡ d  d¡ |  d¡ ¡ d  d¡ |  ¡  ¡ d  d¡ d| d< nj|   dd¡ ¡ d  d¡ |  dd¡ ¡ d  d¡ |  d¡ ¡ d  d¡ |  ¡  ¡ d  d¡ d| d< d S )Nr2   r3   r4   r   r5   rS   )rª   r.   Zset_textr«   )Z
treewidgetr   r   r   Úorientswitch9  s    
zdemo.<locals>.orientswitchaM  
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)))Zbrown2)rÖ   r  r×   Zbrown4r3   )rÊ   rÅ   rÇ   rÙ   rÛ   r2   )r  r   r   Z
fromstringr”   r   rç   r³   rÑ   r¾   rª   r®   r	   rV   r“   r
   Ústripr   rË   rù   )r   rì   rŠ   Ztcr  r  ZtreetokZtc2r6   r  ZparenZtree3Ztc3r  r  ZtwidgetZtextboxZtree4Ztc4r   r   r   Údemoù  sx    ÿ÷
ÿÿ	&
ø
&
r
  Ú__main__N)r   Ztkinterr   r   r   Znltk.draw.utilr   r   r   r   r	   r
   Z	nltk.treer   Z	nltk.utilr   r   r‚   r“   r”   rÒ   rü   r
  r~   r   r   r   r   Ú<module>   s(       ÿ
9  Z o