o
    rZŽh •  ã                   @   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 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 ]}|  
|¡ q1d| _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© ú=/var/www/auris/lib/python3.10/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r<|| _| jr%| jD ]	}|j|dd q|j| jdd d S | jD ]	}|j|dd q(|j| jdd d S |dkrV|dkrId| _d S |dkrRd	| _d S td
ƒ‚|dkrq| jD ]	}|j||d q]|j| j|d d S t|tƒrŽ|d	 dkrŽ| jt	|d ƒ }|j||d d S |dkrœ|j| j|d d S |dkr¹| | j||i¡ | jD ]
}| |||i¡ q¬d S |dv rÔ|dkrÅ|| _
n|dkrÌ|| _|  | j¡ d S |dkrÝ|| _d S t | ||¡ d S )NÚroofr   )r   ÚnormalÚorientationÚ
horizontalé   Úverticalr   z*orientation must be horizontal or verticalÚcolorr   )r   r   Úwidth)ÚxspaceÚyspacer9   r:   Úordered)r   r#   r'   Z
itemconfigr(   r"   Ú
ValueErrorÚ
isinstanceÚtupleÚintr$   r%   Úupdater    r&   r   Ú__setitem__)r,   ÚattrÚvaluer   Úlr   r   r   rA   _   sN   





ÿ
zTreeSegmentWidget.__setitem__c                 C   sÆ   |dkr| j S |dkr|  ¡  | j|¡S |dkr!|  ¡  | jd¡S t|tƒr=|d dkr=| jt|d ƒ }|  ¡  |d¡S |dkrD| jS |d	krK| j	S |d
krV| j
rTdS dS |dkr]| jS t | |¡S )Nr1   r8   r7   r   r   r5   r   r9   r:   r3   r4   r6   r;   )r#   r   Zitemcgetr(   r=   r>   r'   r?   r$   r%   r"   r&   r   Ú__getitem__)r,   rB   rD   r   r   r   rE   ‹   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 rF   )r!   rG   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!   ÚindexrH   r)   r@   )r,   ZoldchildZnewchildrJ   r   r   r   Úreplace_child²   s
   


zTreeSegmentWidget.replace_childc                 C   sB   | j  |¡}| j |= |  |¡ |  ¡  | j ¡ ¡ |  | j¡ d S rF   )	r!   rJ   rH   r   Údeleter'   Úpopr@   r    )r,   ÚchildrJ   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,   rJ   rN   r   r   r   r   Úinsert_childÃ   s
   
zTreeSegmentWidget.insert_childc                 C   s   | j r| jgS | jS rF   )r#   r(   r'   rG   r   r   r   Ú_tagsÌ   s   zTreeSegmentWidget._tagsc                 C   s^   t |tƒr| ¡  ¡ }n| ¡ }| jr!|d |d |d  d fS |d |d  d |d fS )Nr   r5   é   ç       @é   )r=   r   r-   Úbboxr"   )r,   rN   rW   r   r   r   Ú_subtree_topÒ   s   
zTreeSegmentWidget._subtree_topc                 C   sH   | j  ¡ }| jr|d |d |d  d fS |d |d  d |d fS )NrV   r5   rT   rU   r   )r    rW   r"   )r,   rW   r   r   r   Ú_node_bottomÜ   s   
zTreeSegmentWidget._node_bottomc                 C   s`  t | jƒdkr	d S | j ¡ d u rd S || ju r| j}n|g}| jr)| js)|  |¡}|  ¡ \}}| jd  ¡ \}}}}| jdd … D ]"}	|	 ¡ }
t||
d ƒ}t||
d ƒ}t	||
d ƒ}t	||
d ƒ}qA| j
rx|  ¡  | j||||||||¡	 n|  ¡  | j||||||||¡	 |D ]#}	|  ¡ \}}| j| j |	¡ }|  |	¡\}}|  ¡  |||||¡ qŠd S )Nr   r5   rV   rT   )Úlenr!   r    rW   r&   r*   Ú_maintain_orderrY   ÚminÚmaxr"   r   Zcoordsr(   r'   rJ   rX   )r,   rN   Zneed_updateÚnodexÚnodeyZxminZyminZxmaxÚymaxr0   rW   ÚlineZsubtreexZsubtreeyr   r   r   Ú_updateã   s<   

ÿÿüzTreeSegmentWidget._updatec                 C   s   | j r|  |¡S |  |¡S rF   )r"   Ú_maintain_order_horizontalÚ_maintain_order_vertical)r,   rN   r   r   r   r[     s   

z!TreeSegmentWidget._maintain_orderc                 C   s¤  |  ¡ \}}}}|| ju r0| jD ]}|  ¡ \}}}	}
|| j |kr,| d|| j | ¡ q| jS |g}| j |¡}|| j }t|d t| jƒƒD ]-}| j|   ¡ \}}}	}
||kru| j|  || d¡ ||	| | j 7 }| 	| j| ¡ qH|| 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   r5   éÿÿÿÿ)
rW   r    r!   r%   ÚmoverJ   r$   ÚrangerZ   rQ   )r,   rN   ÚleftÚtopÚrightÚbotr0   Úx1Úy1Úx2Úy2ÚmovedrJ   ÚxÚir   r   r   rd     s>   

€
€
€z*TreeSegmentWidget._maintain_order_verticalc                 C   s¢  |  ¡ \}}}}|| ju r/| jD ]}|  ¡ \}}}	}
|| j |kr+| || j | ¡ q| jS |g}| j |¡}|| j }t|d t| jƒƒD ]-}| j|   ¡ \}}}	}
||krt| j|  d|| ¡ ||
| | j 7 }| 	| j| ¡ qG|| j }t|d ddƒD ]-}| j|   ¡ \}}}	}
||
k r¯| j|  d||
 ¡ ||
| | j 8 }| 	| j| ¡ q‚| j  ¡ \}}}	}
|	|| j krÏ| j || j |	 d¡ | j}|S )Nr5   r   re   )
rW   r    r!   r$   rf   rJ   r%   rg   rZ   rQ   )r,   rN   rh   ri   rj   rk   r0   rl   rm   rn   ro   rp   rJ   Úyrr   r   r   r   rc   <  s>   

€
€
€z,TreeSegmentWidget._maintain_order_horizontalc           	      C   s¸   |   ¡ \}}d}| jD ](}| ¡ }||d  | j }||d  }| ||¡ ||d |d  | j 7 }qd}| jD ]}||  |¡d 7 }q9|t| jƒ }| jD ]
}| d|| ¡ qOd S )Né   r   r5   rT   ç        )rY   r!   rW   r$   rf   r%   rX   rZ   )	r,   r^   r_   rs   r0   Úsubtree_bboxÚdxÚdyÚcenterr   r   r   Ú_manage_horizontalf  s   


ÿz$TreeSegmentWidget._manage_horizontalc           	      C   s´   |   ¡ \}}d}| jD ](}| ¡ }||d  | j }||d  }| ||¡ ||d |d  | j 7 }qd}| jD ]}||  |¡d t| jƒ 7 }q9| jD ]
}| || d¡ qMd S )Nr   r5   rV   ru   )rY   r!   rW   r%   rf   r$   rX   rZ   )	r,   r^   r_   rq   r0   rv   rx   rw   ry   r   r   r   Ú_manage_vertical|  s   


ÿz"TreeSegmentWidget._manage_verticalc                 C   s\   d| _ |  ¡ \}}t| jƒdkrd S | jr|  ¡  n|  ¡  | jD ]}|  |¡ q!d| _ d S )NTr   F)r*   rY   rZ   r!   r"   rz   r{   rb   )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!   rG   r   r   r   Ú__repr__¢  s   zTreeSegmentWidget.__repr__N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r+   rA   rE   r-   r.   rI   rK   rO   rR   rS   rX   rY   rb   r[   rd   rc   rz   r{   r|   r~   r   r   r   r   r      s,    !,	
	
)**r   c           
         sd   t |tƒr)ˆˆ | ¡ fi ˆ¤Ž}‡ ‡‡‡‡‡‡fdd„|D ƒ}	tˆ ||	fi ˆ¤ŽS ˆˆ |fi ˆ¤ŽS )Nc                    s"   g | ]}t ˆ |ˆˆˆˆˆˆƒ‘qS r   )Ú_tree_to_treeseg)r   rN   ©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 ]M\}	}
|	dd… dkr#|
||	dd… < q|	dd… dkr4|
||	dd… < q|	dd… dkrE|
||	dd… < q|	dd… dkrV|
||	dd… < qtd|	 ƒ‚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†   ÚkeyrC   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 )
Nr7   z#008000z#008080r5   z#c0c0c0Fr   r6   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   ó
   | 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  ó   
	zTreeWidget.expanded_treec                 G   r©   rª   )r£   r«   r   r   r   Úcollapsed_treeT  r­   zTreeWidget.collapsed_treer5   c                 C   óD   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_  ó
   ÿzTreeWidget.bind_click_treesc                 C   r¯   r°   )r‘   r¢   r±   Ú	bind_dragr£   r³   r   r   r   Úbind_drag_treesh  r¸   zTreeWidget.bind_drag_treesc                 C   ó4   | 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  ó
   

ÿzTreeWidget.bind_click_leavesc                 C   r»   r¼   )r¥   r¹   r½   r   r   r   Úbind_drag_leavesz  rÀ   zTreeWidget.bind_drag_leavesc                 C   r»   ©z-
        Add a binding to all nodes.
        N)r¤   r²   ©r,   r´   rµ   Únoder   r   r   Úbind_click_nodesƒ  rÀ   zTreeWidget.bind_click_nodesc                 C   r»   rÂ   )r¤   r¹   rÃ   r   r   r   Úbind_drag_nodesŒ  rÀ   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 ¡ qUd S )Nc                    s    g | ]}ˆˆ |fi ˆj ¤Ž‘qS r   )r™   )r   rD   ©r   r‡   r,   r   r   r     r   z4TreeWidget._make_collapsed_trees.<locals>.<listcomp>r5   )r1   r7   r   r8   )r=   r   r–   r—   r-   r˜   r¤   rQ   Úleavesr¥   r   r   rž   rœ   r£   r¡   Úhiderg   rZ   r¦   )
r,   r   r‹   r“   rˆ   rÄ   rÈ   Útreesegrr   rN   r   rÇ   r   r¦   •  s0   
ù


þz TreeWidget._make_collapsed_treesc           
         s°   ˆj }ˆj}t|tƒrF|ˆ | ¡ fi ˆj¤Ž}ˆj |¡ |‰‡ ‡‡‡fdd„tt	ˆƒƒD ƒ}t
ˆ ||ˆjˆjd}|ˆjˆ< ˆˆj|< |S |ˆ |fi ˆj¤Ž}	ˆj |	¡ |	S )Nc                    s$   g | ]}ˆ  ˆ ˆ| ˆ|f ¡‘qS r   )r§   )r   rr   ©r   Úchildrenr“   r,   r   r   r   »  s    ÿÿz2TreeWidget._make_expanded_tree.<locals>.<listcomp>)r7   r8   )r–   r—   r=   r   r-   r˜   r¤   rQ   rg   rZ   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r| j D ]
}|||dd … < qd S |d d… dkr0| jD ]
}|||dd … < q#d S |dkrG|| _t| j ¡ ƒD ]}||d< q>d S |dkrl|| _t| j ¡ ƒD ]}||d< qUt| j ¡ ƒD ]}||d< qcd S |dkrƒ|| _t| j ¡ ƒD ]}||d< qzd S |d	krš|| _	t| j ¡ ƒD ]}||d
< q‘d S |dkrÉ|| _
t| j ¡ ƒD ]}||d< q¨t| j ¡ ƒD ]}||d< q¶| jD ]}||d< qÀd S |dkrò|| _t| j ¡ ƒD ]}||d< q×t| j ¡ ƒD ]}||d< qå|  ¡  d S |dkr|| _t| j ¡ ƒD ]}||d< qt| j ¡ ƒD ]}||d< q|  ¡  d S |dkrJ|| _t| j ¡ ƒD ]}||d< q-t| j ¡ ƒD ]}||d< q<|  ¡  d S |dkrr|| _t| j ¡ ƒD ]}||d< qYt| j ¡ ƒD ]}||d< qhd S t | ||¡ d S )NrŒ   r   rŽ   Ú
line_colorr7   Ú
line_widthr8   Ú
roof_colorÚ	roof_fillr   Ú	shapeableÚ	draggabler9   r:   r3   r;   )r¤   r¥   r›   r‘   r¢   r±   rœ   r£   r   rž   rŸ   r$   Úmanager%   r    r&   r   rA   )r,   rB   rC   rÄ   r¾   r¶   r   r   r   rA   Ê  sˆ   
ÿ
ÿ
ÿ

ÿ
ÿ
ÿ



ÿ




ÿzTreeWidget.__setitem__c                 C   sî   |d d… dkr| j  |dd … d ¡S |d d… dkr&| j |dd … d ¡S |d d… dkr9| j |dd … d ¡S |dkr@| jS |dkrG| jS |dkrN| jS |d	krU| jS |d
kr\| jS |dkrc| j	S |dkrj| j
S |dkrq| jS t | |¡S )NrŒ   r   rŽ   r   r   rÍ   rÎ   rÏ   rÐ   rÑ   r9   r:   r3   )r˜   Úgetr™   rš   r›   rœ   r   rž   rŸ   r$   r%   r    r   rE   )r,   rB   r   r   r   rE   	  s.   zTreeWidget.__getitem__c                 C   s   g S rF   r   rG   r   r   r   rS   #  s   zTreeWidget._tagsc                 C   sJ   t | j ¡ ƒt | j ¡ ƒ }|D ]}| ¡ r"| ¡  | ¡  | ¡  qd S rF   )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 r+|  |¡ |  |¡ || _n| ¡  ||¡ | ¡  | 	¡  
¡ dd… \}}| 	¡  
¡ dd… \}}| || || ¡ | ¡  | ¡  |¡ dS )z)
        Collapse/expand a tree.
        r1   NrV   )r¢   r¡   r£   ÚparentrH   r)   r¨   rK   rÕ   r-   rW   rf   rÉ   r@   )r,   rÊ   Zold_treesegZnew_treesegZnewxZnewyÚoldxÚoldyr   r   r   Útoggle_collapsed0  s   

zTreeWidget.toggle_collapsedN)r5   )r   r€   r   r‚   r
   r+   r¬   r®   r·   rº   r¿   rÁ   rÅ   rÆ   r¦   r§   rA   rE   rS   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 ]'}t| ¡ || |d
ddddd|d
}| |j¡ | j |¡ | |dd¡ qg|  ¡  | j
jddd |  ¡  d S )Nr   )ÚceilÚsqrtZNLTKz<Control-x>z<Control-q>z<Control-p>é   Ú	helveticaÚboldz#008040z#004080z#004040Úwhiter5   )Ú	node_fontÚ
leaf_colorÚ
node_colorrÏ   rÐ   rÍ   rÒ   Ú	leaf_fontZboth)Úexpandr   )ÚmathrÛ   rÜ   Z_treesr   Ú_topÚtitleÚbindÚdestroyr   Ú_cframeÚprint_to_filer   Ú_sizeÚsetrÔ   r?   rZ   Ú_widthÚ_widgetsrg   r•   r   r·   rÙ   rQ   Ú
add_widgetÚ_layoutÚpackÚ_init_menubar)	r,   ÚtreesrÛ   rÜ   Úcfrß   Úhelvrr   Úwidgetr   r   r   r+   V  sB   özTreeView.__init__c           	      C   sš   d } } }}| j }tt| jƒƒD ]8}| j| }| ¡ d d… \}}|| dkr-|}d}| || || ¡ | ¡ d d }t|| ¡ d d ƒ}qd S )Nr   rV   r   rT   )rï   rg   rZ   rð   rW   rf   r]   )	r,   rr   rq   rs   r`   r8   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Exitr5   zCtrl-xÚFile)r-   rù   ÚmenuZTinyr   )r-   Úvariablerù   rC   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í   ÚresizeÚ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 r<d|d	< q| j  ¡ d
k rHd|d	< qd|d	< q|  ¡  d S )NrÞ   rß   rá   rä   r9   r:   rt   r5   rÎ   é   rV   rT   )rí   rÔ   rð   rò   )r,   Úerß   r÷   r9   r:   rø   r   r   r   r  Å  s   





zTreeView.resizec                 G   s"   | j d u rd S | j  ¡  d | _ d S rF   )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                    ó   dˆ   dd¡ | d< d S )Nú#%06dr   i?B r   ©Úrandint)Zcw©Úrandomr   r   r   ü  ó   zdemo.<locals>.filli&  iÂ  rV   )r8   ÚheightZcloseenoughzU
    (S (NP the very big cat)
       (VP (Adv sorta) (V saw) (NP (Det the) (N dog))))r5   )rÞ   iòÿÿÿrß   )rÞ   éôÿÿÿÚitalicrà   ÚblackÚgreen4Úblue2)rÒ   rá   rä   rÐ   rÏ   râ   rã   r   c                 S   s   d}t | t| ||dddS )N)rÞ   iðÿÿÿrß   )ZfontÚgreenr   )r   r
   )r   ÚtextÚbigr   r   r   Úboxit  s   zdemo.<locals>.boxitc                 S   s   t | t| |ƒddS )NÚ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                    r	  )Nz#%04d00r   é'  r7   r  )rÄ   r  r   r   r7     r  zdemo.<locals>.colorc                    s,   dˆ   dd¡ |  ¡ d< d|  ¡  ¡ d< d S )Nr
  r   r  r   rà   r7   )r  r-   rN   )rÊ   r  r   r   Úcolor2!  s   zdemo.<locals>.color2rT   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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 )Nr3   r4   r5   r   r6   rT   )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brown4r4   )rÒ   rÍ   rÏ   rá   rã   r3   )r  r   r   Z
fromstringr•   r   rñ   r·   rÙ   rÅ   r¬   r²   r	   rW   r”   r
   Ústripr   rÓ   r  )r   rö   r‹   Ztcr  r  ZtreetokZtc2r7   r  ZparenZtree3Ztc3r  r  ZtwidgetZtextboxZtree4Ztc4r   r  r   Údemoù  sx   ÿ÷
ÿÿ	&
ø
&
r   Ú__main__N)r‚   Ú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
ÿ