B
    P?ð[•  ã               @   s¸   d Z ddlmZmZmZ ddlmZ ddlmZ ddl	m
Z
mZmZmZmZ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eƒZdd„ Zdd„ Zedkr´eƒ  dS )z
Graphically display a Tree.
é    )ÚIntVarÚMenuÚTk)Úin_idle)ÚTree)ÚCanvasFrameÚCanvasWidgetÚ	BoxWidgetÚ
TextWidgetÚParenWidgetÚ
OvalWidgetc               @   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
| _	|  
|¡ x|D ]}|  
|¡ qdW d| _tj| ˆ f|Ž dS )zI
        :type node:
        :type subtrees: list(CanvasWidgetI)
        r   é
   é   Fc          	      s    g | ]}ˆ j d d d d dd‘qS )r   z#006060)Úfill)Úcreate_line)Ú.0Úc)Úcanvas© ú-lib/python3.7/site-packages/nltk/draw/tree.pyú
<listcomp>P   s    z.TreeSegmentWidget.__init__.<locals>.<listcomp>Ú Úhiddenz#006060)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r~|| _| jrLx| jD ]}|j|dd q$W |j| jdd n.x| jD ]}|j|dd qTW |j| jdd n^|dkr²|dkr–d| _n|dkr¦d	| _ntd
ƒ‚n*|dkrêx| jD ]}|j||d qÂW |j| j|d nòt|tƒr&|d	 dkr&| jt	|d ƒ }|j||d n¶|dkrB|j| j|d nš|dkr‚| | j||i¡ x|| jD ]}| |||i¡ qfW nZ|dk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Úcolor)r   )r   r   Úwidth)ÚxspaceÚyspacer4   r5   Ú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 )Nr-   r3   r2   r   r   r0   r   r4   r5   r.   r/   r1   r6   )r   r   Zitemcgetr$   r8   r9   r#   r:   r    r!   r   r"   r   Ú__getitem__)r(   r=   r?   r   r   r   r@   ‹   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 )N)r   )r(   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   ÚindexrA   r%   r;   )r(   ZoldchildZnewchildrC   r   r   r   Úreplace_child²   s
    


zTreeSegmentWidget.replace_childc             C   sB   | j  |¡}| j |= |  |¡ |  ¡  | j ¡ ¡ |  | j¡ d S )N)	r   rC   rA   r   Údeleter#   Úpopr;   r   )r(   ÚchildrC   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   z#006060)r   )	r   r   Úinsertr%   r#   Úappendr   r;   r   )r(   rC   rG   r   r   r   r   Úinsert_childÃ   s
    
zTreeSegmentWidget.insert_childc             C   s   | j r| jgS | jS d S )N)r   r$   r#   )r(   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   r0   é   g       @é   )r8   r   r)   Úbboxr   )r(   rG   rO   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 )NrN   r0   rM   g       @r   )r   rO   r   )r(   rO   r   r   r   Ú_node_bottomÜ   s    
zTreeSegmentWidget._node_bottomc             C   sj  t | jƒdkrd S | j ¡ d kr$d S || jkr6| j}n|g}| jrR| jsR|  |¡}|  ¡ \}}| jd  ¡ \}}}}xV| jdd … D ]D}	|	 ¡ }
t||
d ƒ}t||
d ƒ}t	||
d ƒ}t	||
d ƒ}q„W | j
rô|  ¡  | j||||||||¡	 n |  ¡  | j||||||||¡	 xP|D ]H}	|  ¡ \}}| j| j |	¡ }|  |	¡\}}|  ¡  |||||¡ qW d S )Nr   r0   rN   rM   )Úlenr   r   rO   r"   r&   Ú_maintain_orderrQ   ÚminÚmaxr   r   Zcoordsr$   r#   rC   rP   )r(   rG   Zneed_updateÚnodexÚnodeyZxminZyminZxmaxÚymaxr,   rO   ÚlineZsubtreexZsubtreeyr   r   r   Ú_updateã   s6    


zTreeSegmentWidget._updatec             C   s   | j r|  |¡S |  |¡S d S )N)r   Ú_maintain_order_horizontalÚ_maintain_order_vertical)r(   rG   r   r   r   rS     s    
z!TreeSegmentWidget._maintain_orderc             C   s¶  |  ¡ \}}}}|| jkrdxB| jD ]8}|  ¡ \}}}	}
|| j |kr"| d|| j | ¡ q"W | jS |g}| j |¡}|| j }xrt|d t| jƒƒD ]Z}| j|   ¡ \}}}	}
||kr–| j|  || d¡ ||	| | j 7 }| 	| j| ¡ q–W || j }xrt|d ddƒD ]^}| j|   ¡ \}}}	}
||	k r| j|  ||	 d¡ ||	| | j 8 }| 	| j| ¡ qW | j  ¡ \}}}	}
|
|| j kr²| j d|| j |
 ¡ | j}|S )Nr   r0   éÿÿÿÿ)
rO   r   r   r!   ÚmoverC   r    ÚrangerR   rJ   )r(   rG   ÚleftÚtopÚrightÚbotr,   Úx1Úy1Úx2Úy2ÚmovedrC   ÚxÚir   r   r   r\     s8    



z*TreeSegmentWidget._maintain_order_verticalc             C   s´  |  ¡ \}}}}|| jkrbx@| jD ]6}|  ¡ \}}}	}
|| j |kr"| || j | ¡ q"W | jS |g}| j |¡}|| j }xrt|d t| jƒƒD ]Z}| j|   ¡ \}}}	}
||kr”| j|  d|| ¡ ||
| | j 7 }| 	| j| ¡ q”W || j }xrt|d ddƒD ]^}| j|   ¡ \}}}	}
||
k r| j|  d||
 ¡ ||
| | j 8 }| 	| j| ¡ qW | j  ¡ \}}}	}
|	|| j kr°| j || j |	 d¡ | j}|S )Nr0   r   r]   )
rO   r   r   r    r^   rC   r!   r_   rR   rJ   )r(   rG   r`   ra   rb   rc   r,   rd   re   rf   rg   rh   rC   Úyrj   r   r   r   r[   <  s8    



z,TreeSegmentWidget._maintain_order_horizontalc       	      C   sÄ   |   ¡ \}}d}xZ| jD ]P}| ¡ }||d  | j }||d  }| ||¡ ||d |d  | j 7 }qW d}x | jD ]}||  |¡d 7 }qxW |t| jƒ }x| jD ]}| d|| ¡ q¨W d S )Né   r   r0   rM   g        )rQ   r   rO   r    r^   r!   rP   rR   )	r(   rV   rW   rk   r,   Úsubtree_bboxÚdxÚdyÚcenterr   r   r   Ú_manage_horizontalf  s    z$TreeSegmentWidget._manage_horizontalc       	      C   sÀ   |   ¡ \}}d}xZ| jD ]P}| ¡ }||d  | j }||d  }| ||¡ ||d |d  | j 7 }qW d}x*| jD ] }||  |¡d t| jƒ 7 }qxW x| jD ]}| || d¡ q¤W d S )Nr   r0   rN   g        )rQ   r   rO   r!   r^   r    rP   rR   )	r(   rV   rW   ri   r,   rm   ro   rn   rp   r   r   r   Ú_manage_vertical|  s     z"TreeSegmentWidget._manage_verticalc             C   s`   d| _ |  ¡ \}}t| jƒdkr$d S | jr4|  ¡  n|  ¡  x| jD ]}|  |¡ qDW d| _ d S )NTr   F)r&   rQ   rR   r   r   rq   rr   rZ   )r(   rV   rW   r,   r   r   r   Ú_manage‘  s    
zTreeSegmentWidget._managec             C   s   d| j | jf S )Nz[TreeSeg %s: %s])r   r   )r(   r   r   r   Ú__repr__¢  s    zTreeSegmentWidget.__repr__N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r'   r<   r@   r)   r*   rB   rD   rH   rK   rL   rP   rQ   rZ   rS   r\   r[   rq   rr   rs   rt   r   r   r   r   r      s*    ,	
	
)**r   c       
         s\   t |tƒrJˆˆ | ¡ fˆŽ}‡ ‡‡‡‡‡‡fdd„|D ƒ}	tˆ ||	fˆŽS ˆˆ |fˆŽS d S )Nc                s"   g | ]}t ˆ |ˆˆˆˆˆˆƒ‘qS r   )Ú_tree_to_treeseg)r   rG   )r   Úleaf_attribsÚloc_attribsÚ	make_leafÚ	make_nodeÚnode_attribsÚtree_attribsr   r   r   ³  s   
z$_tree_to_treeseg.<locals>.<listcomp>)r8   r   r)   r   )
r   Útr}   r|   r   r~   rz   r{   r)   r*   r   )r   rz   r{   r|   r}   r~   r   r   ry   ¦  s    


ry   c          	   K   sÔ   i }i }i }i }x¬t | ¡ ƒD ]œ\}	}
|	dd… dkrH|
||	dd… < q|	dd… dkrj|
||	dd… < q|	dd… dkrŒ|
||	dd… < q|	dd… dkr®|
||	dd… < qtd|	 ƒ‚qW 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Úitemsr7   ry   )r   r€   r}   r|   r+   r   r~   rz   r{   Úkeyr>   r   r   r   Útree_to_treesegmentÄ  s.    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|Ž d S )
Nr2   z#008000z#008080r0   z#c0c0c0Fr   r1   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(   Úpath_to_treer   r   r   Úexpanded_treeI  s    	zTreeWidget.expanded_treec             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(   rž   r   r   r   Úcollapsed_treeT  s    	zTreeWidget.collapsed_treer0   c             C   sL   x"t | j ¡ ƒD ]}| ||¡ qW x"t | j ¡ ƒD ]}| ||¡ q4W 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   sL   x"t | j ¡ ƒD ]}| ||¡ qW x"t | j ¡ ƒD ]}| ||¡ q4W dS )z5
        Add a binding to all tree segments.
        N)r†   r—   r¡   Ú	bind_dragr˜   )r(   r£   r¤   r¥   r   r   r   Úbind_drag_treesh  s    zTreeWidget.bind_drag_treesc             C   s<   x| j D ]}| ||¡ qW x| j D ]}| ||¡ q$W 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   s<   x| j D ]}| ||¡ qW x| j D ]}| ||¡ q$W dS )z.
        Add a binding to all leaves.
        N)rš   r§   )r(   r£   r¤   r©   r   r   r   Úbind_drag_leavesz  s    zTreeWidget.bind_drag_leavesc             C   s<   x| j D ]}| ||¡ qW x| j D ]}| ||¡ q$W 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   s<   x| j D ]}| ||¡ qW x| j D ]}| ||¡ q$W dS )z-
        Add a binding to all nodes.
        N)r™   r§   )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ˆjŽ}ˆj |¡ ‡ ‡‡fdd„| ¡ D ƒ}ˆ j	|7  _	t
ˆ ||dˆjˆjˆjd}|ˆj|< |ˆj|< | ¡  x0tt|ƒƒD ] }|| }	ˆ ˆ |	||f ¡ q¨W d S )Nc                s   g | ]}ˆˆ |fˆj Ž‘qS r   )rŽ   )r   r?   )r   r|   r(   r   r   r     s    z4TreeWidget._make_collapsed_trees.<locals>.<listcomp>r0   )r-   r2   r   r3   )r8   r   r‹   rŒ   r)   r   r™   rJ   Úleavesrš   r   r’   r“   r‘   r˜   r–   Úhider_   rR   r›   )
r(   r   r€   rˆ   r}   r¬   r¯   Útreesegrj   rG   r   )r   r|   r(   r   r›   •  s,    



z TreeWidget._make_collapsed_treesc       
         s¬   ˆj }ˆj}t|tƒrˆ|ˆ | ¡ fˆjŽ}ˆj |¡ |‰‡ ‡‡‡fdd„tt	ˆƒƒD ƒ}t
ˆ ||ˆjˆjd}|ˆjˆ< ˆˆj|< |S |ˆ |fˆjŽ}	ˆj |	¡ |	S d S )Nc                s$   g | ]}ˆ  ˆ ˆ| ˆ|f ¡‘qS r   )rœ   )r   rj   )r   Úchildrenrˆ   r(   r   r   r   ¼  s   z2TreeWidget._make_expanded_tree.<locals>.<listcomp>)r2   r3   )r‹   rŒ   r8   r   r)   r   r™   rJ   r_   rR   r   r   r‘   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   sN  |d d… dkr4x| j D ]}|||dd … < qW n|d d… dkrhx| jD ]}|||dd … < qLW nâ|dkrš|| _xt| j ¡ ƒD ]}||d< q†W n°|dkrì|| _xt| j ¡ ƒD ]}||d< q¸W xt| j ¡ ƒD ]}||d< qØW n^|dkr"|| _x t| j ¡ ƒD ]}||d< qW n(|d	krX|| _	x t| j ¡ ƒD ]}||d
< qBW nò|dkrÊ|| _
x t| j ¡ ƒD ]}||d< qxW x t| j ¡ ƒD ]}||d< qšW x| jD ]}||d< q´W n€|dkr*|| _x t| j ¡ ƒD ]}||d< qêW x t| j ¡ ƒD ]}||d< qW |  ¡  n |dkrˆ|| _x t| j ¡ ƒD ]}||d< qJW x t| j ¡ ƒD ]}||d< qlW |  ¡  nÂ|dkræ|| _x t| j ¡ ƒD ]}||d< q¨W x t| j ¡ ƒD ]}||d< qÊW |  ¡  nd|dkr<|| _x t| j ¡ ƒD ]}||d< qW x0t| j ¡ ƒD ]}||d< q(W nt | ||¡ d S )Nr   r‚   rƒ   Ú
line_colorr2   Ú
line_widthr3   Ú
roof_colorÚ	roof_fillr   Ú	shapeableÚ	draggabler4   r5   r.   r6   )r™   rš   r   r†   r—   r¡   r‘   r˜   r’   r“   r”   r    Úmanager!   r•   r"   r   r<   )r(   r=   r>   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·   r4   r5   r.   )r   ÚgetrŽ   r   r   r‘   r’   r“   r”   r    r!   r•   r   r@   )r(   r=   r   r   r   r@   	  s.    zTreeWidget.__getitem__c             C   s   g S )Nr   )r(   r   r   r   rL   #  s    zTreeWidget._tagsc             C   sN   t | j ¡ ƒt | j ¡ ƒ }x,|D ]$}| ¡ r"| ¡  | ¡  | ¡  q"W d S )N)r†   r—   r¡   r˜   r   Úshowr¹   r°   )r(   Zsegsr¥   r   r   r   rs   &  s    
zTreeWidget._managec             C   sÌ   |}|d r| j | j|  }n| j| j|  }| ¡ | krV|  |¡ |  |¡ || _n| ¡  ||¡ | ¡  | 	¡  
¡ dd… \}}| 	¡  
¡ dd… \}}| || || ¡ | ¡  | ¡  |¡ dS )z)
        Collapse/expand a tree.
        r-   NrN   )r—   r–   r˜   ÚparentrA   r%   r   rD   r»   r)   rO   r^   r°   r;   )r(   r±   Zold_treesegZnew_treesegZnewxZnewyÚoldxÚoldyr   r   r   Útoggle_collapsed0  s    

zTreeWidget.toggle_collapsedN)r0   )r0   )r0   )r0   )r0   )r0   )ru   rv   rw   rx   r
   r'   rŸ   r    r¦   r¨   rª   r«   r­   r®   r›   rœ   r<   r@   rL   rs   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   sF  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 | _x^tt|ƒƒD ]N}t| ¡ || |d
ddddd|d
}| |j¡ | j |¡ | |dd¡ qÐW |  ¡  | j
jddd |  ¡  d S )Nr   )ÚsqrtÚceilZNLTKz<Control-x>z<Control-q>z<Control-p>é   Ú	helveticaÚboldz#008040z#004080z#004040Úwhiter0   )Ú	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:   rR   Ú_widthÚ_widgetsr_   rŠ   r   r¦   r¿   rJ   Ú
add_widgetÚ_layoutZpackÚ_init_menubar)	r(   ÚtreesrÁ   rÂ   ÚcfrÅ   Úhelvrj   Úwidgetr   r   r   r'   V  s@    zTreeView.__init__c       	      C   sž   d } } }}| j }x‚tt| jƒƒD ]p}| j| }| ¡ d d… \}}|| dkr\|}d}| || || ¡ | ¡ d d }t|| ¡ d d ƒ}q&W d S )Nr   rN   r   rM   )rÓ   r_   rR   rÔ   rO   r^   rU   )	r(   rj   ri   rk   rX   r3   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Exitr0   zCtrl-xZFile)r)   rÜ   ÚmenuZTinyr   )r)   ZvariablerÜ   r>   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×   Ž  sV    





zTreeView._init_menubarc             G   sª   d| j  ¡  df}d| j  ¡  f}| j  ¡ }| j  ¡ }xf| jD ]\}||d< ||d< ||d< ||d< | j  ¡ dk rzd|d	< q>| j  ¡ d
k r’d|d	< q>d|d	< q>W |  ¡  d S )NrÄ   rÅ   rÇ   rÊ   r4   r5   rl   r0   r´   é   rN   rM   )rÑ   rº   rÔ   rÖ   )r(   ÚerÅ   rÚ   r4   r5   rÛ   r   r   r   râ   Å  s    



zTreeView.resizec             G   s"   | j d krd S | j  ¡  d | _ d S )N)rÌ   rÎ   )r(   rä   r   r   r   rÎ   ×  s    

zTreeView.destroyc             O   s   t ƒ r
dS | jj||Ž 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)	ru   rv   rw   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 )Nz#%06dr   i?B r   )Úrandint)Zcw)Úrandomr   r   r   ü  s    zdemo.<locals>.filli&  iÂ  rN   )r3   ZheightZcloseenoughzU
    (S (NP the very big cat)
       (VP (Adv sorta) (V saw) (NP (Det the) (N dog))))r0   )rÄ   iòÿÿÿrÅ   )rÄ   iôÿÿÿ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green)r   )r	   r
   )r   ÚtextZbigr   r   r   Úboxit  s    zdemo.<locals>.boxitc             S   s   t | t| |ƒddS )NZcyan)r   )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   i'  r2   )ré   )r¬   )rê   r   r   r2     s    zdemo.<locals>.colorc                s,   dˆ   dd¡ |  ¡ d< d|  ¡  ¡ d< d S )Nz#%06dr   i'  r   rÆ   r2   )ré   r)   rG   )r±   )rê   r   r   Úcolor2!  s    zdemo.<locals>.color2rM   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 )Nr.   r/   r0   r   r1   rM   )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Ä   iôÿÿÿrÅ   Zbrown4r/   )r¸   r³   rµ   rÇ   rÉ   r.   )rê   r   r   Z
fromstringrŠ   r   rÕ   r¦   r¿   r­   rŸ   r¢   r   rO   r‰   r
   Ústripr	   r¹   rå   )r   rÙ   r€   Ztcrì   rí   ZtreetokZtc2r2   rî   ZparenZtree3Ztc3rï   rë   ZtwidgetZtextboxZtree4Ztc4r   )rê   r   Údemoù  sn    
&
&
rñ   Ú__main__N)rx   Zsix.moves.tkinterr   r   r   Z	nltk.utilr   Z	nltk.treer   Znltk.draw.utilr   r   r	   r
   r   r   r   ry   r‰   rŠ   ÚobjectrÀ   rè   rñ   ru   r   r   r   r   Ú<module>
   s&       8  Z o