o
    rZhzN                    @   s  d Z ddlZddlZddlmZmZmZmZm	Z	m
Z
mZmZmZmZ ddlmZmZ ddlmZ ddlmZm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#m$Z$ dd
l%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/ ddl0m1Z1 ddl2m3Z3 G dd deZ4G dd dZ5G dd dZ6G dd dZ7G dd dZ8G dd dZ9G dd de9e.Z:G dd de9e'Z;G dd de9e&Z<G dd  d e9e+Z=G d!d" d"Z>d#d$ Z?e@d%kre?  d$gZAdS )&a  
A graphical tool for exploring chart parsing.

Chart parsing is a flexible parsing algorithm that uses a data
structure called a "chart" to record hypotheses about syntactic
constituents.  Each hypothesis is represented by a single "edge" on
the chart.  A set of "chart rules" determine when new edges can be
added to the chart.  This set of rules controls the overall behavior
of the parser (e.g. whether it parses top-down or bottom-up).

The chart parsing tool demonstrates the process of parsing a single
sentence, with a given grammar and lexicon.  Its display is divided
into three sections: the bottom section displays the chart; the middle
section displays the sentence; and the top section displays the
partial syntax tree corresponding to the selected edge.  Buttons along
the bottom of the window are used to control the execution of the
algorithm.

The chart parsing tool allows for flexible control of the parsing
algorithm.  At each step of the algorithm, you can select which rule
or strategy you wish to apply.  This allows you to experiment with
mixing different strategies (e.g. top-down and bottom-up).  You can
exercise fine-grained control over the algorithm by selecting which
edge you wish to apply a rule to.
    N)
ButtonCanvasCheckbuttonFrameIntVarLabelMenu	ScrollbarTkToplevel)askopenfilenameasksaveasfilename)Font)	showerrorshowinfo)	CFGEditorTreeSegmentWidgettree_to_treesegment)CanvasFrameColorizedListEntryDialogMutableOptionMenuShowTextSymbolWidget)CFGNonterminal)
BottomUpPredictCombineRuleBottomUpPredictRuleChartLeafEdgeLeafInitRuleSingleEdgeFundamentalRuleSteppingChartParserTopDownInitRuleTopDownPredictRuleTreeEdge)Tree)in_idlec                   @   s&   e Zd Zejd Zdd Zdd ZdS )EdgeListZ
rightarrowc                 C   s@   |j ddd |j dddd |j dd	d |j d
ddd d S )Nterminalz#006000)
foregroundarrowsymbol0)font	underlinedotz#000000nonterminalblue)	helveticabold)r*   r.   )Z
tag_config)selfZ
textwidgetoptions r8   G/var/www/auris/lib/python3.10/site-packages/nltk/app/chartparser_app.py_init_colortags^   s   
zEdgeList._init_colortagsc                 C   s   g }| d|  df | | jdf t| D ])\}}|| kr*| d t|tr;| d|  df q| d| df q|	 rN| d |S )Nz%s	r1   r+   )z *r0   z %sz %rr)   )
appendlhsARROW	enumeraterhsr0   
isinstancer   r,   is_complete)r6   itemcontentsieltr8   r8   r9   
_item_reprf   s   


zEdgeList._item_reprN)__name__
__module____qualname__r   SYMBOLSr=   r:   rF   r8   r8   r8   r9   r(   [   s    
r(   c                   @   s   e Zd ZdZ	d2d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d3ddZdd Zd d! Zd"d# Zd$d% Zd&d' Zd(d) Zd3d*d+Zd,d- Zd.d/ Zd0d1 ZdS )4ChartMatrixViewzS
    A view of a chart that displays the contents of the corresponding matrix.
    TChart MatrixFc                 C   s   || _ g | _g | _d | _|r(t|| _| j| | jd| j | 	| j nt
|| _| | j | | j |rB| | j nd | _i | _d| _|   d S )N<Control-q>r   )_chart_cells_marks_selected_cellr   _roottitlebinddestroy
_init_quitr   _init_matrix
_init_list_init_numedges_numedges_label
_callbacks
_num_edgesdraw)r6   parentcharttoplevelrS   show_numedgesr8   r8   r9   __init__   s$   

zChartMatrixView.__init__c                 C   s$   t |d| jd}|jdddd d S )NQuittextcommandbottomr   nonesideexpandfill)r   rU   pack)r6   rootquitr8   r8   r9   rV      s   zChartMatrixView._init_quitc                 C   sH   t |ddd}|jdddddd	 t|d
d
dd| _| jjddd d S )N   sunkenborderreliefr   rh         top)rk   rl   padxpadyrj      white)widthheight
background)rk   rl   )r   rm   r   _canvas)r6   rn   cframer8   r8   r9   rW      s   zChartMatrixView._init_matrixc                 C   s$   t |dd| _| jjdddd d S )Nz0 edgesre   r   rh   rw   )rk   rl   rj   )r   rZ   rm   r6   rn   r8   r8   r9   rY      s   zChartMatrixView._init_numedgesc                 C   sN   t |g ddd| _| jjddddd | fd	d
}| jd| | j  d S )N      )r|   r}   rw   ru   bothrv   )rj   rk   rl   ry   c                 S   s   | d|  d S Nselect_fire_callbacks)edger6   r8   r8   r9   cb      z&ChartMatrixView._init_list.<locals>.cbr   )r(   _listrm   add_callbackZfocus)r6   rn   r   r8   r8   r9   rX      s
   zChartMatrixView._init_listc                 G   0   | j d u rd S z| j   W n   Y d | _ d S NrR   rU   r6   er8   r8   r9   rU         

zChartMatrixView.destroyc                 C   s&   || j ur|| _ d| _|   d S d S Nr   )rN   r\   r]   r6   r_   r8   r8   r9   	set_chart   s
   
zChartMatrixView.set_chartc                    sv  | j d u rd S t| j  fddt D }| jD ]}||  |   d7  < qt D ]d}t| D ]\}|| | dkrCd}n dtddd	|| |  d
  t	dd	d	|| |  d
  }| j| | }| j
j||d ||f| jkr| j
j|ddd | j
| q6| j
j|ddd q6q/t| jj| jd}| j| | j | _| jd urd| j | jd< d S d S )Nc                       g | ]}d d t  D qS )c                 S      g | ]}d qS r   r8   .0rD   r8   r8   r9   
<listcomp>       z5ChartMatrixView.update.<locals>.<listcomp>.<listcomp>ranger   jNr8   r9   r          z*ChartMatrixView.update.<locals>.<listcomp>ru   r   gray20z#00{:02x}{:02x}   2      
   rl   z#00ffffrv   )outliner|   black)spanz%d edgesre   )rR   lenrO   r   rN   startendformatminmaxr   
itemconfigrQ   Z	tag_raiselistr   r   set	num_edgesr\   rZ   )r6   Z
cell_edgesr   rD   r   colorZcell_tagedgesr8   r   r9   update   s6   



zChartMatrixView.updatec                 C      | j jddd |   d S )Ninactiveboxhiddenstater   r   r   r6   r8   r8   r9   activate      zChartMatrixView.activatec                 C   r   )Nr   normalr   r   r   r8   r8   r9   
inactivate   r   zChartMatrixView.inactivatec                 C      d| j |i |< d S Nru   r[   
setdefaultr6   eventfuncr8   r8   r9   r         zChartMatrixView.add_callbackNc                 C   4   |d u r
| j |= d S z	| j | |= W d S    Y d S r   r[   r   r8   r8   r9   remove_callback      zChartMatrixView.remove_callbackc                 G   2   || j vrd S t| j |  D ]}||  qd S r   r[   r   keysr6   r   argsZcb_funcr8   r8   r9   r      
   

zChartMatrixView._fire_callbacksc                 C   sT   | j d u rd S ||f| jkr| j | jkrd S ||f| _|   | d|| d S )Nselect_cell)rR   rQ   rN   r   r\   r   r   r6   rD   r   r8   r8   r9   r     s   

zChartMatrixView.select_cellc                 C   s,   | j d u rd S d | _| jg  |   d S r   )rR   rQ   r   r   r   r   r8   r8   r9   deselect_cell  s
   
zChartMatrixView.deselect_cellc                 C   s*   | j ||fkr|   d S | || d S r   )rQ   r   r   r   r8   r8   r9   _click_cell  s   zChartMatrixView._click_cellc                 C   s   | j |   | j| d S r   )r   r   r   viewr6   r   r8   r8   r9   	view_edge   s   zChartMatrixView.view_edgec                 C   ,   | j d u rd S | j|   | j| d S r   )rR   r   r   r   markr   r8   r8   r9   	mark_edge$     
zChartMatrixView.mark_edgec                 C   s   | j d u rd S | j| d S r   )rR   r   Zunmarkr   r8   r8   r9   unmark_edge*  s   
zChartMatrixView.unmark_edgec                 C   r   r   )rR   r   r   r   Zmarkonlyr   r8   r8   r9   markonly_edge/  r   zChartMatrixView.markonly_edgec              	      sJ  | j d u rd S d }}d}| j}|d | j d  t|d |   }t|d | |   }|d t D ]`}|j|d || |d  | t|dd	 |j|| |d  |  | | d t|d
d	 |j	|||d  | |  | ||d  | dd |j	|| | ||| | |  | dd q;|j
||||   |  | dd  fddt D | _t D ]>}t| D ]6}|j
|| | || | |d | | |d | | dd}	|	| j| |< | ||fdd}
||	d|
 qqt|d t|d }}|j
dd|d |d dddd}	||	 |   d S )N   r   allru   r|   r}   rp   r   )re   anchorn.)dashr|   c                    r   )c                 S   r   r   r8   r   r8   r8   r9   r   b  r   z3ChartMatrixView.draw.<locals>.<listcomp>.<listcomp>r   r   r   r8   r9   r   b  r   z(ChartMatrixView.draw.<locals>.<listcomp>r   r   c                 S   s   | || d S r   )r   )r   r6   rD   r   r8   r8   r9   r   n  r   z ChartMatrixView.draw.<locals>.cb
<Button-1>id   gray50r   r   )rl   r   tag)rR   r   deleterN   
num_leavesintr   create_textreprcreate_linecreate_rectanglerO   tag_bind	tag_lowerr   )r6   ZLEFT_MARGINZ
BOT_MARGINZ
TOP_MARGINcZdxdyrD   r   tr   ZxmaxZymaxr8   r   r9   r]   5  s~   


 



	


	zChartMatrixView.drawc                 O      | j j|i | d S r   rR   rm   r6   r   kwargsr8   r8   r9   rm     r   zChartMatrixView.pack)TrL   Fr   )rG   rH   rI   __doc__rb   rV   rW   rY   rX   rU   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r]   rm   r8   r8   r8   r9   rK   {   s2    

	%
	
NrK   c                   @   sp   e Z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d Zdd Zdd ZdS )ChartResultsViewTc                 C   s   || _ || _g | _d| _g | _d | _d | _|r+t|| _| j	d | j
d| j nt|| _|rct| j}|jdddd t|d| jd	jd
d t|d| jd	jdd t|d| jd	jdd t| jdd| _| jjdddd |   d S )Nr   z!Chart Parser Application: ResultsrM   rg   r   xri   rc   rd   rightrj   z	Print AllleftzPrint Selectionr   )closeenoughrw   ru   r   )rN   _grammar_trees_y_treewidgets
_selection
_selectboxr   rR   rS   rT   rU   r   rm   r   	print_allprint_selectionr   _cframer   )r6   r^   r_   grammarr`   buttonsr8   r8   r9   rb     s.   


zChartResultsView.__init__Nc                 C   sv   | j d u rd S |d ur#| | j krd S | d| j fkr#d S | j| j D ]}|| jvr8| 	| q,d S r   )
rR   r<   r  r   r   rN   r   Zparsesr	  _add)r6   r   parser8   r8   r9   r     s   


zChartResultsView.updatec                 C   s`   | j | | j }t||}| j| | j|d| j || j	 |
 d d | _d S )Nr   rv   )r	  r;   r  canvasr   r  Z
add_widgetr
  Z
bind_click_clickbbox)r6   r  r   
treewidgetr8   r8   r9   r    s   

zChartResultsView._addc                 C   sR   | j  }| jd ur|| j || _| \}}}}|j||||ddd| _d S )Nrp   z#088)r|   r   )r  r  r  r   r  r  r   )r6   widgetr   x1y1x2y2r8   r8   r9   r    s   

zChartResultsView._clickc                 C   s>   ||  d< | D ]}t|tr| || q
||d< q
d S )Nr   )labelZsubtreesr@   r   _color)r6   r  r   childr8   r8   r9   r    s   

zChartResultsView._colorc                 G   s   | j d u rd S | j  d S r   )rR   r  print_to_filer   r8   r8   r9   r    s   
zChartResultsView.print_allc                 G   s   | j d u rd S | jd u rtdd d S | j }| jD ]}|| jur(| j| q|| j | j	 \}}}}| j
d| d|  d|| d  d|| d  |d< | j  | jg| _|   |   d S )NzPrint ErrorzNo tree selectedr   z0 0 r    scrollregion)rR   r  r   r  r  r  destroy_widgetr   r  r  mover!  clearr   )r6   r   r   r  r  r  r  r  r8   r8   r9   r    s"   




$

z ChartResultsView.print_selectionc                 C   s^   | j d u rd S | jD ]}| j| q
g | _g | _| jd ur'| j | j d | _d| _	d S )Nr   )
rR   r  r  r$  r	  r  r  r   r  r
  )r6   r  r8   r8   r9   r&    s   



zChartResultsView.clearc                 C      |    || _|   d S r   )r&  rN   r   r   r8   r8   r9   r        zChartResultsView.set_chartc                 C   r'  r   )r&  r  r   r6   r  r8   r8   r9   set_grammar	  r(  zChartResultsView.set_grammarc                 G   r   r   r   r   r8   r8   r9   rU     r   zChartResultsView.destroyc                 O   r   r   r   r   r8   r8   r9   rm     r   zChartResultsView.pack)Tr   )rG   rH   rI   rb   r   r  r  r  r  r  r&  r   r*  rU   rm   r8   r8   r8   r9   r    s    

!	r  c                   @   s   e Zd ZdZdejd ej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g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d0d1 Zd2d3 Zd4d5 Zd6d7 Zd8d9 Z d:S );ChartComparera  

    :ivar _root: The root window

    :ivar _charts: A dictionary mapping names to charts.  When
        charts are loaded, they are added to this dictionary.

    :ivar _left_chart: The left ``Chart``.
    :ivar _left_name: The name ``_left_chart`` (derived from filename)
    :ivar _left_matrix: The ``ChartMatrixView`` for ``_left_chart``
    :ivar _left_selector: The drop-down ``MutableOptionsMenu`` used
          to select ``_left_chart``.

    :ivar _right_chart: The right ``Chart``.
    :ivar _right_name: The name ``_right_chart`` (derived from filename)
    :ivar _right_matrix: The ``ChartMatrixView`` for ``_right_chart``
    :ivar _right_selector: The drop-down ``MutableOptionsMenu`` used
          to select ``_right_chart``.

    :ivar _out_chart: The out ``Chart``.
    :ivar _out_name: The name ``_out_chart`` (derived from filename)
    :ivar _out_matrix: The ``ChartMatrixView`` for ``_out_chart``
    :ivar _out_label: The label for ``_out_chart``.

    :ivar _op_label: A Label containing the most recent operation.
    -intersectionunion)r,  andorc                 G   s   dgd }t || _d| _d| _| j| _| j| _d| ji| _| j| _d | _t	 | _
| j
d | j
d| j | j
d| j | | j
 | | j
 | | j
 | | j
 | | j
 |D ]}| | q_d S )N    NonezChart ComparisonrM   <Control-x>)r   _emptychart
_left_name_right_name_left_chart_right_chart_charts
_out_chart	_operatorr
   rR   rS   rT   rU   _init_menubar_init_chartviews_init_divider_init_buttons_init_bindings
load_chart)r6   Zchart_filenamesZfaketokfilenamer8   r8   r9   rb   B  s*   

zChartComparer.__init__c                 G   r   r   r   r   r8   r8   r9   rU   h  r   zChartComparer.destroyc                 O   s   d S r   )rR   mainloopr   r8   r8   r9   rD  q  s   zChartComparer.mainloopc                 C   s   t |}t |dd}|jddd| jd |jddd| jd |  |jdd	| jd
d |jdd|d t |dd}|jd| jdd |jd| jdd |jd| j	dd |  |jd| j
d |jdd|d | jj|d d S )Nr   Ztearoff
Load ChartCtrl-o)r  acceleratorr/   rf   zSave OutputCtrl-sExitru   Ctrl-xr  r/   rf   rH  Filer  r/   menuIntersection+)r  rf   rH  Union*
Differencer,  Swap Chartsr  rf   ComparerO  )r   add_commandload_chart_dialogsave_chart_dialogadd_separatorrU   add_cascade_intersection_union_difference_swapchartsrR   config)r6   rn   menubarfilemenuZopmenur8   r8   r9   r=  y  s>   
zChartComparer._init_menubarc                 C   s"   t |ddd}|jdddd d S )Nrp   rq   rr   rw   r  )rj   rl   Zipady)r   rm   )r6   rn   dividerr8   r8   r9   r?    s   zChartComparer._init_dividerc                 C   sH  d}d}t |dd}|jdddd t |d	d
d}|jdddddd t|t| j | jd| _| jjdddd t|| j	ddd| _
| j
jdddddd | j
d| j | j
d| j | j
  t|dd	d|d| _| jjdddd t |d	d
d}|jdddddd t|t| j | jd| _| jjdddd t|| j	ddd| _| jjdddddd | jd| j | jd| j | j  t|dd	d|djdddd t |d	d
d}|jdddddd t|dd| _| jjdd d! t|| j	ddd| _| jjdddddd | jd| j | jd| j | j  d S )"N)r,   )r3   rf  z#c0c0c0r~   rw   ru   r   ri   rv   grooverr   r  r2     )rj   rx   ry   rk   rl   )rf   r   r  )rj   ry   rl   FT)r`   ra   rg   r   r   r"  )re   r|   r~   r.   )rj   rx   ry   =Outputr   	   )rj   ry   )r   rm   r   r   r:  r   _select_left_left_selectorrK   r5  _left_matrixr   select_edger   r   r   	_op_label_select_right_right_selector_right_matrix
_out_label_out_matrix)r6   rn   ZopfontZeqfontframeZ	cv1_frameZ	cv2_frameZ	out_framer8   r8   r9   r>    s`   





zChartComparer._init_chartviewsc                 C   s   t |}|jddddd t|d| jdjdd	 t|d
| jdjdd	 t|d| jdjdd	 t |ddjdd	 t|d| jdjdd	 t|d| jdjdd	 d S )Nrg   r   r  r   )rj   ry   rl   rk   rP  rd   r  r  rR  rT  r   r   rU  zDetach Outputr  )r   rm   r   r^  r_  r`  ra  _detach_out)r6   rn   r  r8   r8   r9   r@    s   
zChartComparer._init_buttonsc                 C   s   | d| j d S )N<Control-o>)rT   rZ  r   r8   r8   r9   rA    s   zChartComparer._init_bindingsc                 C   >   || _ | j| | _| j| j |dkr| j  |   d S Nr3  )r6  r:  r8  ro  r   r   	_apply_opr6   namer8   r8   r9   rm       
zChartComparer._select_leftc                 C   rz  r{  )r7  r:  r9  rt  r   r   r|  r}  r8   r8   r9   rr    r  zChartComparer._select_rightc                 C   sF   | j dkr|   d S | j dkr|   d S | j dkr!|   d S d S )Nr,  r0  r/  )r<  r`  r_  r^  r   r8   r8   r9   r|  	  s   


zChartComparer._apply_opzPickle file.picklez	All filesrS  c              
   G   s   t | jdd}|sd S z!t|d}t| j| W d    W d S 1 s%w   Y  W d S  tyJ } ztdd|d|  W Y d }~d S d }~ww )Nr  	filetypesZdefaultextensionwbzError Saving ChartUnable to open file: 
)r   CHART_FILE_TYPESopenpickledumpr;  	Exceptionr   r6   r   rC  outfiler   r8   r8   r9   r[    s   &$zChartComparer.save_chart_dialogc              
   G   sd   t | jdd}|sd S z| | W d S  ty1 } ztdd|d|  W Y d }~d S d }~ww )Nr  r  zError Loading Chartr  r  )r   r  rB  r  r   )r6   r   rC  r   r8   r8   r9   rZ  "  s   $zChartComparer.load_chart_dialogc                 C   s   t |d}t|}W d    n1 sw   Y  tj|}|dr+|d d }|dr6|d d }|| j|< | j	| | j
	| | j| ju rU| j| d S | j| ju rc| j
| d S d S )Nrbr  iz.charti)r  r  loadospathbasenameendswithr:  rn  addrs  r8  r5  r   r9  )r6   rC  infiler_   r~  r8   r8   r9   rB  -  s    


zChartComparer.load_chartc                 C   s"   | j   | j  | j  d S r   )ro  r   rt  rv  r   r8   r8   r9   _update_chartviews@  s   

z ChartComparer._update_chartviewsc                 C   sl   || j v r| j| n| j  || jv r| j| n| j  || jv r/| j| d S | j  d S r   )r8  ro  r   r   r9  rt  r;  rv  r   r8   r8   r9   rp  I  s   




zChartComparer.select_edgec                 C   s.   | j || | j|| | j|| d S r   )ro  r   rt  rv  r   r8   r8   r9   r   W  s   zChartComparer.select_cellc                 C   sL   |   sd S t| j }| jD ]}|| jvr||g  q| d| d S )Nr,  _checkcompatr   r8  tokensr9  insert_updater6   	out_chartr   r8   r8   r9   r`  `     

zChartComparer._differencec                 C   sL   |   sd S t| j }| jD ]}|| jv r||g  q| d| d S )Nr/  r  r  r8   r8   r9   r^  k  r  zChartComparer._intersectionc                 C   sZ   |   sd S t| j }| jD ]}||g  q| jD ]}||g  q| d| d S )Nr0  )r  r   r8  r  r  r9  r  r  r8   r8   r9   r_  v  s   

zChartComparer._unionc                 C   s*   | j | j}}| j| | j| d S r   )r6  r7  rn  r   rs  )r6   r  r  r8   r8   r9   ra    s   zChartComparer._swapchartsc                 C   sr   | j  | j ks | j  | j ks | j | jks | j| jkr7| j| _| j| j | j  d| j	d< dS dS )Nrk  re   FT)
r8  r  r9  Zproperty_namesr5  r;  rv  r   r   ru  r   r8   r8   r9   r    s   

zChartComparer._checkcompatc                 C   sF   || _ | j| | jd< || _| j| d| j| j | j| j	d< d S )Nre   z{} {} {})
r<  	_OPSYMBOLrq  r;  rv  r   r   r6  r7  ru  )r6   operatorr  r8   r8   r9   r    s   zChartComparer._updatec                 C   s.   | j | _| j| j d| jd< | j  d S )Nr"  re   )r5  r;  rv  r   rq  r   r   r8   r8   r9   _clear_out_chart  s   
zChartComparer._clear_out_chartc                 C   s   t | j| j| jd d d S )Nre   )rS   )rK   rR   r;  ru  r   r8   r8   r9   rx    s   zChartComparer._detach_outN)!rG   rH   rI   r  r   rJ   r  rb   rU   rD  r=  r?  r>  r@  rA  rm  rr  r|  r  r[  rZ  rB  r  rp  r   r`  r^  r_  ra  r  r  r  rx  r8   r8   r8   r9   r+     s@    &	'=			r+  c                   @   sF  e Zd ZdZdZdZdZdZdOddZdd	 Z	dPd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dOdd Zd!d" Zd#d$ ZdQd&d'Zd(d) Zd*d+ ZdRd,d-ZdSd/d0ZdOd1d2ZdSd3d4Zd5d6 Zd7d8 Zd9d: Zd;d< Z d=d> Z!dOd?d@Z"dAdB Z#dCdD Z$dQdEdFZ%dGdH Z&dIdJ Z'dOdKdLZ(dMdN Z)dS )T	ChartViewa  
    A component for viewing charts.  This is used by ``ChartParserApp`` to
    allow students to interactively experiment with various chart
    parsing techniques.  It is also used by ``Chart.draw()``.

    :ivar _chart: The chart that we are giving a view of.  This chart
       may be modified; after it is modified, you should call
       ``update``.
    :ivar _sentence: The list of tokens that the chart spans.

    :ivar _root: The root window.
    :ivar _chart_canvas: The canvas we're using to display the chart
        itself.
    :ivar _tree_canvas: The canvas we're using to display the tree
        that each edge spans.  May be None, if we're not displaying
        trees.
    :ivar _sentence_canvas: The canvas we're using to display the sentence
        text.  May be None, if we're not displaying the sentence text.
    :ivar _edgetags: A dictionary mapping from edges to the tags of
        the canvas elements (lines, etc) used to display that edge.
        The values of this dictionary have the form
        ``(linetag, rhstag1, dottag, rhstag2, lhstag)``.
    :ivar _treetags: A list of all the tags that make up the tree;
        used to erase the tree (without erasing the loclines).
    :ivar _chart_height: The height of the chart canvas.
    :ivar _sentence_height: The height of the sentence canvas.
    :ivar _tree_height: The height of the tree

    :ivar _text_height: The height of a text string (in the normal
        font).

    :ivar _edgelevels: A list of edges at each level of the chart (the
        top level is the 0th element).  This list is used to remember
        where edges should be drawn; and to make sure that no edges
        are overlapping on the chart view.

    :ivar _unitsize: Pixel size of one unit (from the location).  This
       is determined by the span of the chart's location, and the
       width of the chart display canvas.

    :ivar _fontsize: The current font size

    :ivar _marks: A dictionary from edges to marks.  Marks are
        strings, specifying colors (e.g. 'green').
    r      (   Nc                 K   s  | dd}| dd}| dd| _|| _i | _g | _i | _i | _g | _d| _d| _	g | _
d| _|du r`t }|d |fd	d
}|fdd}|d| t|d|d}	|	jdd || _n|| _| | | | j\| _| _d| jd< d| jd< |rt| jddd}
|
jddd t|
dd| _d| jd< | jjdd nd| _|r| | jd d!\}}||| _| _d"| jd< nd| _|   |   |   |   | jd#| j dS )$z4
        Construct a new ``Chart`` display.
        	draw_treer   draw_sentenceru   Zfontsizer4   Nz
Chart Viewc                 S   s   |   d S r   rU   )r   rw   r8   r8   r9   destroy1     z$ChartView.__init__.<locals>.destroy1c                 S   s   |    d S r   r  )rw   r8   r8   r9   destroy2  r  z$ChartView.__init__.<locals>.destroy2qDonerd   rg   r  i,  r}   r   r  sunkrp   rt   rs   r   )rl   rj   r   r}   #e0e0e0r~   r   r   r  rz   z<Configure>) get	_fontsizerN   r[   _edgelevels	_edgetagsrP   	_treetoks_treetoks_edge_treetoks_index
_tree_tags_compactr
   rS   rT   r   rm   rR   _init_fonts
_sb_canvasZ	_chart_sb_chart_canvasr   r   _sentence_canvasZ_tree_sb_tree_canvas_analyzer]   _resize_grow
_configure)r6   r_   rn   kwr  r  rw   r  r  br   sbr  r8   r8   r9   rb     sX   




zChartView.__init__c                 C   sF   t dd| jd| _t d| jd| _t t d d| _|d| j d S )Nr3   r5   familyweightsizer  r  r.   r.   *Font)r   r  	_boldfont_fontr   _sysfont
option_addr   r8   r8   r9   r  ?  s   zChartView._init_fontsyr   rg   c                 C   sp   t |ddd}|j|||d t|dd}t|dd}|jd	d
d |jd|dd |j|d< |j|d< ||fS )zK
        Helper for __init__: construct a canvas with a scrollbar.
        r  rp   r  )rl   rk   rj   r  rg  vertical)Zorientr  r  rj   rl   r  yesrj   rl   rk   rf   Zyscrollcommand)r   rm   r   r	   yviewr   )r6   rn   rk   rl   rj   r   r  r  r8   r8   r9   r  F  s   

zChartView._sb_canvasc                 G      | j ddd d S )Nscrollunitsr  r  r   r8   r8   r9   	scroll_upY     zChartView.scroll_upc                 G   r  )Nr  ru   r  r  r   r8   r8   r9   scroll_down\  r  zChartView.scroll_downc                 G   r  )Nr  r  pagesr  r   r8   r8   r9   page_up_  r  zChartView.page_upc                 G   r  )Nr  ru   r  r  r   r8   r8   r9   	page_downb  r  zChartView.page_downc                 C   s   | j  }tt| jd || j tjd  }| jj|d | jj| jd d |dtj  | | _| j	dur?| j
| j	d< dS dS )z/
        Grow the window, if necessary
        r|   rp   r   r}   r  N)rN   r   r   r   r  	_unitsizer  _MARGIN	configurer  _sentence_height)r6   r   r|   r8   r8   r9   r  e  s   

zChartView._growc                 C   sX   | j jt| d | jjt| d | jjt| d |   |   |   d S N)r  )r  r  absr  r  r  r  r]   r6   r  r8   r8   r9   set_font_sizez  s   zChartView.set_font_sizec                 C   s
   t | jS r   )r  r  r   r8   r8   r9   get_font_size     
zChartView.get_font_sizec                 C   s,   | j  }|jdtj  | | _|   dS )z
        The configure callback.  This is called whenever the window is
        resized.  It is also called when the window is first mapped.
        It figures out the unit size, and redraws the contents of each
        canvas.
        rp   N)rN   r   r|   r  r  r  r]   )r6   r   r   r8   r8   r9   r    s   
zChartView._configurec                 C   sr   |dur#|| _ g | _i | _|   |   |   |   |   dS | j D ]}|| jvr2| 	| q&|   dS )au  
        Draw any edges that have not been drawn.  This is typically
        called when a after modifies the canvas that a CanvasView is
        displaying.  ``update`` will cause any edges that have been
        added to the chart to be drawn.

        If update is given a ``chart`` argument, then it will replace
        the current chart with the given chart.
        N)
rN   r  rP   r  r  r]   
erase_treer  r  	_add_edge)r6   r_   r   r8   r8   r9   r     s   



zChartView.updatec                 C   s   |  \}}| j| D ]4}|  \}}||  kr|k s<n ||  kr(|k s<n ||  kr:|  kr:|kr? dS  q dS qdS )z
        Return True if the given edge overlaps with any edge on the given
        level.  This is used by _add_edge to figure out what level a
        new edge should be added to.
        TF)r   r  )r6   r   lvls1e1Z	otheredges2e2r8   r8   r9   _edge_conflict  s   HzChartView._edge_conflictc              	   C   s   | j }t|tr1| }g }| D ]}t|tr#|t|  q|t	| qd
|}n| }d}||fD ]9}|jdd|| jddd}||}	|| |	d }
t| d}t| j|
| | _t| j|	d	 |	d  | _q;d
S )z
        Given a new edge, recalculate:

            - _text_height
            - _unitsize (if the edge text is too big for the current
              _unitsize, then increase _unitsize)
        r"  r1  r   nwr  re   r.   r   justifyrp   ru   rv   N)r  r@   r%   r<   r?   r   r;   strr,   r   joinr   r  r  r   r   lengthr  _text_height)r6   r   r   r<   ZrhseltsrE   r?   sr   r  r|   Zedgelenr8   r8   r9   _analyze_edge  s,   



zChartView._analyze_edger   c                 C   s   t |trdS || jv rdS | | |   | js4| j|g t| jd }| 	|| | 
  dS d}	 |t| jkrO| jg  | 
  |t| jks>||krb| ||sb| j| | n|d7 }q7| 	|| dS )z
        Add a single edge to the ChartView:

            - Call analyze_edge to recalculate display parameters
            - Find an available level
            - Call _draw_edge
        Nru   r   )r@   r   r  r  r  r  r  r;   r   
_draw_edger  r  )r6   r   Zminlvlr  r8   r8   r9   r    s0   
	

zChartView._add_edgec                 C   s   d }t t| jD ]}|| j| v r|} nq	|d u rd S |d | j }| jd }| jdd | jdkrC| jd|| | j  d S d S )Nru   r   Zmovetog      ?r   )r   r   r  _chart_level_sizer  r  r  _chart_height)r6   r   levelrD   r  r   r8   r8   r9   r     s   

zChartView.view_edgec                 C   s  | j }| | j tj }| | j tj }||kr%|td| jd 7 }|d | j }|j||||ddd}t	|t
r`g }| D ]}	t	|	trS|t|	  qB|t|	 qB| }
ng }d}
d|d	|
 }d||
d	 }|j|d ||| jd
d}||d d }||d ||d  d }||d |d |d |d }|j|d ||| jd
d}|j|| d |t| d| jd}|||||f| j|< | |fdd}||d| ||d| ||d| ||d| ||d| | | d	S )z6
        Draw a single edge on the ChartView.
           r   ru   lastrv   )r+   r|   r   r"  Nr  )re   r.   r   rp      r  re   r   r.   c                 S   s   | d| d S r   r   )r   r6   r   r8   r8   r9   r   <  r   z ChartView._draw_edge.<locals>.cbr   )r  r   r  r  r  r   r   r  r   r@   r%   r?   r   r;   r  r,   r   r0   r  r   r  r  Zcreate_ovalr<   r  r  r   _color_edge)r6   r   r  r   r  r  r  Zlinetagr?   rE   posZrhs1Zrhs2Zrhstag1ZdotxZdotyZdottagZrhstag2Zlhstagr   r8   r8   r9   r    sD   


  zChartView._draw_edgec                 C   s  || j vrdS | j}|durQ|durQ|| jv r| j| }| j | }|j|d |d |j|d |d |j|d ||d |j|d |d |j|d |d dS | j }|| jv rc| | j|  | rx| d|fkrx| |d	d
 dS t	|t
r| |dd dS | |dd dS )z
        Color in an edge with the given colors.
        If no colors are specified, use intelligent defaults
        (dependent on selection, etc.)
        Nr   r   ru   rp   rl   r   rv   r  #084#042z#48cz#246z#00fz#008)r  r  rP   r   rN   r   r  rA   r   r@   r   )r6   r   Z	linecolorZ	textcolorr   tagsr   r8   r8   r9   r  G  s*   






zChartView._color_edge#0dfc                 C   s   || j |< | | dS )z
        Mark an edge
        N)rP   r  r6   r   r   r8   r8   r9   r   f  s   
zChartView.mark_edgec                 C   sJ   |du rt | j }i | _|D ]}| | qdS | j|= | | dS )z/
        Unmark an edge (or all edges)
        N)r   rP   r   r  )r6   r   Zold_marked_edgesr8   r8   r9   r   m  s   zChartView.unmark_edgec                 C   s   |    | || d S r   )r   r   r  r8   r8   r9   r   z  s   zChartView.markonly_edgec           	   	   C   s   d}d}| j }| j D ]1}|jddt|| jddd}||}|| |d tj	 }t
||}t
||d |d  }q|| _|| _| jdtj  | _| j D ]}| | qR| jd | _dtj| j  | _|   d	S )
z
        Analyze the sentence string, to figure out how big a unit needs
        to be, How big the tree should be, etc.
        F   r   r  r  r  rp   rv   ru   N)r  rN   leavesr   r   r  r  r   r  _LEAF_SPACINGr   r  r  r  r  r   r  r  _TREE_LEVEL_SIZE_tree_heightr  )	r6   ZunitsizeZtext_heightr   leafr   r  r|   r   r8   r8   r9   r  ~  s(   


zChartView._analyzec                 C   sn   | j }| j | j tjd  }t| j}|d | j | _	dd|| j	f|d< | j
r5dd|| jf| j
d< dS dS )z
        Update the scroll-regions for each canvas.  This ensures that
        everything is within a scroll-region, so the user can use the
        scrollbars to view the entire display.  This does *not*
        resize the window.
        rp   r   r#  N)r  rN   r   r  r  r  r   r  r  r   r  r  )r6   r   r|   Zlevelsr8   r8   r9   r    s   
zChartView._resizec                 C   s@  d}| j }| j}| j}tj}g | _td| j d D ]}|| j	 | }|r3|
|d||}|| |rC|
|d|| j}	||	 |
|d||}
||
 |j|d dt|d| jd}|| |d dkr|rs|j|dd |r||j|	dd |j|
dd q|r|j|d	d |r|j|	d	d |j|
d	d qd
S )zv
        Draw location lines.  These are vertical gridlines used to
        show where each location unit is.
        iP  r   ru   rp   r  r  Zgray60r   Zgray80N)r  r  r  r  r  Z	_loclinesr   rN   r   r  r   r   r  r   r   r  r   )r6   ZBOTTOMc1c2c3marginrD   r  t1t2t3Zt4r8   r8   r9   _draw_loclines  s<   



zChartView._draw_loclinesc              	   C   s   | j  dkr	dS | j}tj}tj}t| j  D ]K\}}|| j | }|| j }|| d }|j||t	|| j
ddd}	||	}
|j|d |
d tjd  |d |
d tjd  d	d	d
}|| qdS )zDraw the sentence string.r   Nrp   r   r  r  ru   rv   z#f0f0f0r  )rN   r   r  r  r  r>   r  r  r   r   r  r  r   r  r   )r6   r   r  r  rD   r  r  r  r  r   r  rtr8   r8   r9   _draw_sentence  s.   

zChartView._draw_sentencec                 C   s.   | j D ]}| j| qg | _d | _d| _d S r   )r  r  r   r  r  r  )r6   r   r8   r8   r9   r    s
   

zChartView.erase_treec                 C   s   |d u r| j d u rd S |d u r| j }| j |kr)dd | j|D | _|| _ d| _t| jdkr2d S | jD ]}| j| q5| j| j }| 	||
  |   | j | j dtj  }| tj| j  }dd||f| jd< d S )Nc                 S   s   g | ]	}t |tr|qS r8   )r@   r&   )r   r   r8   r8   r9   r   
  s    z'ChartView.draw_tree.<locals>.<listcomp>r   rp   r#  )r  rN   Ztreesr  r  r   r  r  r   _draw_treetokr   _draw_treecycler   r  r  r  r}   r  r  )r6   r   r   treewhr8   r8   r9   r    s$   

zChartView.draw_treec                 C   s&   | j d t| j | _ | | j d S r   )r  r   r  r  r  r   r8   r8   r9   
cycle_tree"  s   zChartView.cycle_treec                 C   s  t | jdkr	d S dt | j }| j}tj}| j | j | d }|j|dd|| j	d}| j
| ||\}}}}tt | jD ]B}|dt | j| d   }	|| jkrZd}
nd}
|j|	|d	 |	d
 ||	d	 |d	 |
dd}| j
| | |fdd}||d| qCd S )Nru   z%d Treesrp   ne)r   re   r.   r   r	  z#fffr   r   r   r  c                 S   s   ||_ |  d S r   )r  r  )r   r6   rD   r8   r8   r9   r   A  s   z%ChartView._draw_treecycle.<locals>.cbr   )r   r  r  r  r  rN   r   r  r   r  r  r;   r  r   r  Zcreate_polygonr   )r6   r  r   r  r  r   _r  rD   r  rl   r   r8   r8   r9   r  &  s*   
 zChartView._draw_treecyclec              
   C   s  | j }tj}g }|D ],}t|tr"| |||d \}}|| q
|d| d | j d |  |d7 }q
|rBt|t	| }	nd| d | j d | }	|d7 }|tj
| j  }
|j|	|
ddt| d| jd}| j| |
tj
 | j }t||D ]T\}}t|tr|r|j|	|
| j ||ddd}| j| t|tr|s|j|	|
| j ||dd	d
d}| j| t|ts|j|	|
| j |dddd}| j| q|	|fS )z
        :param index: The index of the first leaf in the tree.
        :return: The index of the first leaf after the tree.
        ru   rp   r   centerr
  )r   r  re   rl   r.   r	  )r|   rl   z#048z2 3)r|   rl   r   i'  )r  r  r  r@   r&   r  r;   r  sumr   r  r  r   r  r  r  r  zipr   )r6   Ztreetokindexdepthr   r  Zchild_xsr   Zchild_xZnodexZnodeyr   ZchildyZchildxr8   r8   r9   r  G  sv   
 

		
zChartView._draw_treetokc                 C   s   | j r| j d |   | jr| jd |   | jd i | _tt| j	D ]}| j	| D ]}| 
|| q1q*| jD ]}| | q>|   dS )z1
        Draw everything (from scratch).
        r   N)r  r   r  r  r  r  r  r   r   r  r  rN   r  r  )r6   r  r   r8   r8   r9   r]     s   
zChartView.drawc                 C   r   r   r   r   r8   r8   r9   r     r   zChartView.add_callbackc                 C   r   r   r   r   r8   r8   r9   r     r   zChartView.remove_callbackc                 G   r   r   r   r   r8   r8   r9   r     r   zChartView._fire_callbacksr   )r  r   rg   r   )NN)r  )*rG   rH   rI   r  r  r  r  Z_CHART_LEVEL_SIZErb   r  r  r  r  r  r  r  r  r  r  r   r  r  r  r   r  r  r   r   r   r  r  r  r  r  r  r#  r  r  r]   r   r   r   r8   r8   r8   r9   r    sP    .
Y


"+
5


&)
 
!P
	r  c                       s8   e Zd ZdZ fddZ fddZ fddZ  ZS )EdgeRulez
    To create an edge rule, make an empty base class that uses
    EdgeRule as the first base class, and the basic rule as the
    second base class.  (Order matters!)
    c                    s"   | j jd }|| _|jd | _d S r   )	__class__	__bases___edgeZ	NUM_EDGES)r6   r   superr,  r8   r9   rb     s   zEdgeRule.__init__c                 '   s:    | j jd }|| jf7 }|j| ||g|R  E d H  d S r   )r,  r-  r.  apply)r6   r_   r  r   r/  r0  r8   r9   r1    s    zEdgeRule.applyc                    s   | j jd }|| S r   )r,  r-  __str__)r6   r/  r0  r8   r9   r2    s   
zEdgeRule.__str__)rG   rH   rI   r  rb   r1  r2  __classcell__r8   r8   r0  r9   r+    s
    r+  c                   @      e Zd ZdS )TopDownPredictEdgeRuleNrG   rH   rI   r8   r8   r8   r9   r5        r5  c                   @   r4  )BottomUpEdgeRuleNr6  r8   r8   r8   r9   r8    r7  r8  c                   @   r4  )BottomUpLeftCornerEdgeRuleNr6  r8   r8   r8   r9   r9    r7  r9  c                   @   r4  )FundamentalEdgeRuleNr6  r8   r8   r8   r9   r:    r7  r:  c                   @   s  e Zd Zdcd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)gZg d*Zd+d, Zd-d. Zd/d0 Zd1d2 Zd3d4 Zd5d6 Zd7d8 Zd9d: Zd;d< Z d=d> Z!d?d@ Z"dAdB Z#dCdD Z$dEdF Z%dddHdIZ&dJdK Z'dedMdNZ(dOdP Z)dQdR Z*e+ gZ,e- gZ.e/ gZ0e1 gZ2e3 gZ4e,e. e4 Z5e0e4 Z6e2e4 Z7dSdT Z8dUdV Z9dWdX Z:dYdZ Z;d[d\ Z<d]d^ Z=d_d` Z>dadb Z?dGS )fChartParserAppChart Parser Applicationc                 C   s   |  || d | _zat | _| j| | jd| j t| j}t| j}t| j}|jddd |jddd |jdddd | | j | 	  | 
| | | | | |   d | _d | _|   W d S    td	 |    )
NrM   rg   rh   r  r  r   ru   r  zError creating Tree View)_init_parserrR   r
   rS   rT   rU   r   rm   r  _init_animation_init_chartview_init_rulelabelr@  r=  _matrix_resultsrA  print)r6   r  r  rS   Zframe3frame2frame1r8   r8   r9   rb     s2   





zChartParserApp.__init__c                 G   s"   | j d u rd S | j   d | _ d S r   r   r6   r   r8   r8   r9   rU     s   


zChartParserApp.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'   rR   rD  r   r8   r8   r9   rD  !  s   zChartParserApp.mainloopc                 C   s   || _ || _|   d S r   )r  _tokens_reset_parser)r6   r  r  r8   r8   r9   r=  0  s   zChartParserApp._init_parserc                 C   sT   t | j| _| j| j | j | _t | j| jD ]}q| j	 | _
d | _d S r   )r"   r  _cpZ
initializerG  r_   rN   r    r1  step_cpstepr  )r6   Z	_new_edger8   r8   r9   rH  5  s   
zChartParserApp._reset_parserc                 C   sl   t t d d| _|d| j t|| _| j| jd t dd| j d| _	t d| j d| _
d S )	Nr.   r  r  r  r3   r5   r  r  )r   r   r  r  r   _sizer   Zcgetr  r  r  r   r8   r8   r9   r  D  s   
zChartParserApp._init_fontsc                 C   s:   t | j| _| jd t | j| _| jd d| _d S )Nru   rv   r   )r   rR   _stepr   _animate
_animatingr   r8   r8   r9   r>  P  s
   
zChartParserApp._init_animationc                 C   s(   t | j|ddd| _| jd| j d S )Nru   )r  r  r   )r  rN   _cvr   _click_cv_edge)r6   r^   r8   r8   r9   r?  \  s   zChartParserApp._init_chartviewc                 C   sh   d}t ||| jd| _t |ddd| jd| _| jjdd | jjdd t|| jd	d
}|jdd d S )NzLast edge generated by:)re   r.   r  rh  r!  )r|   rt   r   r.   r  r  Step)variablere   r  )r   r  Z_rulelabel1_rulelabel2rm   r   rM  )r6   r^   ZruletxtrJ  r8   r8   r9   r@  `  s   zChartParserApp._init_rulelabelc                 C   sh  t |}t |}|jddd |jddd t|ddd| jd	jd
d t|ddd| jd	jdd t|ddd| jd	jdd t|ddd| jd	jdd t|ddd| jd	jdd t|ddd| jd	jdd t |ddjdd t|ddd| j	d	jdd t |ddjdd t|ddd| j
d	jdd t |ddjdd t|ddd| jd	jdd d S )Nrg   r  r  rw   rh   zReset
Parserz#90c0d0r   )re   r~   r*   rf   r  r  zTop Down
Strategyr  zBottom Up
StrategyzBottom Up
Left-Corner StrategyzTop Down Init
Rulez#90f090zTop Down Predict
Ruler   r   zBottom Up Predict
Rulez"Bottom Up Left-Corner
Predict RulezFundamental
Rule)r   rm   r   resettop_down_strategybottom_up_strategybottom_up_leftcorner_strategytop_down_inittop_down_predict	bottom_upbottom_up_leftcornerfundamental)r6   r^   rE  rD  r8   r8   r9   r@  l  s   
zChartParserApp._init_buttonsc                 C   sl  | j d| jj | j d| jj | j d| jj | j d| jj | j d| j | j d| j | j d| j | j d| j	 | j d	| j
 | j d
| j | j d| j | j d| j | j d| j | j d| j | j d| j | j d| j | j d| jfdd | j d| jfdd | j d| jfdd | j d| jfdd d S )Nz<Up>z<Down>z<Prior>z<Next>rM   r4  z<F1>z<Control-s>ry  z<Control-r>r   r  r   z<space>z<Control-g>z<Control-t>r,  c                 S   
   | dS r   r   r   ar8   r8   r9   <lambda>     
 z/ChartParserApp._init_bindings.<locals>.<lambda>rj  c                 S   r^  )Nrp   r_  r`  r8   r8   r9   rb    rc  rQ  c                 S   r^  )Nrv   r_  r`  r8   r8   r9   rb    rc  r  c                 S   s   | |  S r   )r   r  )r   r  r8   r8   r9   rb    r   )rR   rT   rP  r  r  r  r  rU   help
save_chartrB  rU  rV  rW  rX  _stop_animationedit_grammaredit_sentencerN  rM  r   r8   r8   r9   rA    s(   zChartParserApp._init_bindingsc           	      C   sP  t | j}t |dd}|jdd| jdd |jdd| jdd |jdd| jd	d |  |jd
| jd |jd| jd |  |jdd| j	dd |j
dd|d t |dd}|jdd| jdd |jdd| jdd |j
dd|d t |dd}|jdd| jd |jdd| jd |j
dd|d t |dd}|jdd| jdd |jdd| jd d |jd!d| jd"d |  |jd#| jd |jd$| jd |jd%| jd |jd&| jd |jd'| jd |j
d(d|d t |dd}|jd)d| jd*d+ |  |jd,d| jdd- |jd.d| jdd/d0 |jd1d| jd2d3d0 |jd4d| jd5d6d0 |j
d7d|d t |dd}|jd8| jdd9| jd: |jd;| jdd<| jd: |jd=| jdd>| jd: |jd?| jdd@| jd: |jdA| jddB| jd: |j
dCd|d t |dd}|jdDd| jd |jdEd| jdFd |j
dGd|d | jj|dH d S )INr   rE  z
Save ChartrI  rL  rF  rG  zReset ChartzCtrl-rzSave GrammarrV  zLoad GrammarrJ  ru   rK  rM  rN  zEdit Grammarr   zCtrl-g	Edit TextzCtrl-tZEditrL   r  )r  r/   rf   ZResultsZViewzTop Down Strategyr   zBottom Up Strategyr  zBottom Up Left-Corner Strategyr   zBottom Up RulezBottom Up Left-Corner RulezTop Down Init RulezTop Down Predict RulezFundamental RuleZApplyrR  r  )r  r/   rS  rH  zNo Animation)r  r/   rS  valuezSlow Animationr,  )r  r/   rS  rj  rH  zNormal Animationrp   rj  zFast Animationrv   rQ  ZAnimateZTinyr   )r  rS  r/   rj  rf   ZSmallr  ZMedium   ZLarge   ZHuge   ZZoomZAboutZInstructionsZF1ZHelprX  ) r   rR   rY  re  rB  rU  r\  save_grammarload_grammarrU   r]  rg  rh  view_matrixview_resultsrV  rW  rX  r[  r\  rY  rZ  r]  Zadd_checkbuttonrM  Zadd_radiobuttonrN  rL  resizeaboutrd  rb  )	r6   rc  rd  ZeditmenuZviewmenuZrulemenuZanimatemenuZzoommenuZhelpmenur8   r8   r9   r=    s*  





zChartParserApp._init_menubarc                 C   s&   || j kr| | d S | j  d S r   )r  _select_edgerP  r#  r   r8   r8   r9   rQ  }  s   
zChartParserApp._click_cv_edgec                 C   s   |  | | j| d S r   )rt  rP  r   r   r8   r8   r9   _select_matrix_edge  s   
z"ChartParserApp._select_matrix_edgec                 C   sL   || _ | j|d | j| | jr| j| | jr$| j| d S d S )Nz#f00)r  rP  r   r  rA  r   r   r8   r8   r9   rt    s   zChartParserApp._select_edgec                 C   s2   d | _ | j  | j  | jr| j  d S d S r   )r  rP  r   r  rA  r   r8   r8   r9   _deselect_edge  s   

zChartParserApp._deselect_edgec                 C   s   |  | j  | j  | j| | j|d | j| | jr(| j  | jr1| j| | jr:| j| | j	rE| j	| d S d S )Nr  )
_display_rulerI  Zcurrent_chartrulerP  r   r  r   r   rA  rB  r   r8   r8   r9   _show_new_edge  s   

zChartParserApp._show_new_edgec              	   G   sP   d| _ zt| jdtpd ddd W d S    t| jdtpd dd Y d S )Nr   zHelp: Chart Parser Applicationr1  K   fixed)r|   r.   r   )rO  r   rR   r  stripr   r8   r8   r9   rd    s    

zChartParserApp.helpc                 G   s   d}t d| d S )Nz5NLTK Chart Parser Application
Written by Edward LoperzAbout: Chart Parser Application)r   )r6   r   ZABOUTr8   r8   r9   rs    s   zChartParserApp.aboutr  r  ))zPlaintext grammar file.cfgr  r  c              
   G   s   t | jdd}|sdS zFt|d}t|}W d   n1 s!w   Y  || _| j| | jr8| j	| | jr@| j
  | jrI| j	| | j	| W dS  ty^ } z d}~ww )zLoad a chart from a pickle filer  r  Nr  )r   r  r  r  r  rN   rP  r   rA  r   r   rB  rI  r  r   )r6   r   rC  r  r_   r   r8   r8   r9   rB    s,   
zChartParserApp.load_chartc              
   G   st   t | jdd}|sdS z!t|d}t| j| W d   W dS 1 s%w   Y  W dS  ty9 } z d}~ww )zSave a chart to a pickle filer  r  Nr  )r   r  r  r  r  rN   r  r   r  r8   r8   r9   re    s   &zChartParserApp.save_chartc              
   G   s   t | jdd}|sdS zC|dr,t|d}t|}W d   n1 s&w   Y  nt|}t| }W d   n1 sBw   Y  | 	| W dS  t
yh } ztdd|  W Y d}~dS d}~ww )z!Load a grammar from a pickle filer|  r  Nr  r  zError Loading GrammarUnable to open file: %r)r   GRAMMAR_FILE_TYPESr  r  r  r  r   
fromstringreadr*  r  r   )r6   r   rC  r  r  r   r8   r8   r9   ro    s&   

zChartParserApp.load_grammarc           	   
      s0  t  jdd}|sd S zr|dr5t|d}t j jf| W d    W d S 1 s-w   Y  W d S t|d9} j	 } fdd|D } fdd|D }|D ]	}|
d	|  qT|D ]	}|
d	|  q`W d    W d S 1 svw   Y  W d S  ty } ztd
d|  W Y d }~d S d }~ww )Nr|  r  r  r  r!  c                    s"   g | ]}|   j kr|qS r8   r<   r  r   r   pr   r8   r9   r   	     " z/ChartParserApp.save_grammar.<locals>.<listcomp>c                    s"   g | ]}|   j kr|qS r8   r  r  r   r8   r9   r   	  r  z%s
zError Saving Grammarr}  )r   r~  r  r  r  r  rN   rG  r  productionswriter  r   )	r6   r   rC  r  Zprodsr   restprodr   r8   r   r9   rn  	  s0   
&
&zChartParserApp.save_grammarc                 G   s\   d| _ |   | j| j | jr| j| j | jr | j  | jr,| j| j d S d S r   )	rO  rH  rP  r   rN   rA  r   r   rB  rF  r8   r8   r9   rU  $	  s   
zChartParserApp.resetc                 G   s   t | j| j| j d S r   )r   rR   r  r*  r   r8   r8   r9   rg  3	  r   zChartParserApp.edit_grammarc                 C   s,   || _ | j| | jr| j| d S d S r   )r  rI  r*  rB  r)  r8   r8   r9   r*  6	  s
   zChartParserApp.set_grammarc                 G   s,   d | j}d}d}t| j||| j| d S )Nr"  ri  zEnter a new sentence to parse.)r  rG  r   rR   set_sentence)r6   r   sentencerS   instrr8   r8   r9   rh  <	  s   zChartParserApp.edit_sentencec                 C   s   t | | _|   d S r   )r   splitrG  rU  )r6   r  r8   r8   r9   r  B	  s   zChartParserApp.set_sentencec                 G   s8   | j d ur
| j   t| j| j| _ | j d| j d S r   )rA  rU   rK   rR   rN   r   ru  r   r8   r8   r9   rp  J	  s   

zChartParserApp.view_matrixc                 G   s,   | j d ur
| j   t| j| j| j| _ d S r   )rB  rU   r  rR   rN   r  r   r8   r8   r9   rq  P	  s   

zChartParserApp.view_resultsc                 C   s   d| _ | | j  d S r   )rO  r  rL  r  r   r8   r8   r9   rr  Y	  s   zChartParserApp.resizec                 C   sL   | j | | jjt| d | jjt| d | jjt| d d S r  )rP  r  r  r  r  r  r  r  r8   r8   r9   r  ]	  s   zChartParserApp.set_font_sizec                 C   s   t | j S r   )r  rL  r  r   r8   r8   r9   r  c	  s   zChartParserApp.get_font_sizeNc                 C   s   | j rd| _ d S | d  | j rF| j}|d ur:|d ur:| j||g |  }|d u r8| j	  d | _d S d S | j| |   d S | j| | j
 rZd| _ |   d S | jD ]}|d u re nq]| j  | jrs| j  | jr}| j  d S d S )Nr   ru   )rO  rw  rM  r  r  rI  Zset_strategy_apply_strategyrP  r   rN  _animate_strategyrK  r   rA  rB  )r6   ZstrategyZedge_strategyZ	selectionZnewedger   r8   r8   r9   apply_strategyj	  s:   







zChartParserApp.apply_strategyc                 G   s
   d| _ d S r   )rO  r   r8   r8   r9   rf  	  r  zChartParserApp._stop_animationru   c                 C   s   | j dkrd S |  d urI| j dks| j dkrd S | j dkr.| jd| j d S | j dkr?| jd| j d S | jd| j d S d S )Nr   ru   i  rp   i  r   )rO  r  rN  r  rM  rR   afterr  )r6   speedr8   r8   r9   r  	  s   
z ChartParserApp._animate_strategyc                 C   s    t | j}|d ur| | |S r   )nextrK  rx  )r6   Znew_edger8   r8   r9   r  	  s   

zChartParserApp._apply_strategyc                 C   s6   |d u rd| j d< d S t|}|| j d< | j }d S )Nr1  re   )rT  r  rP  r  )r6   ruler~  r  r8   r8   r9   rw  	  s
   
zChartParserApp._display_rulec                 G   s   |  | jd  d S r   )r  _TD_INITr   r8   r8   r9   rY  	     zChartParserApp.top_down_initc                 G      |  | jt d S r   )r  _TD_PREDICTr5  r   r8   r8   r9   rZ  	  r  zChartParserApp.top_down_predictc                 G   r  r   )r  _BU_RULEr8  r   r8   r8   r9   r[  	  r  zChartParserApp.bottom_upc                 G   r  r   )r  _BU_LC_RULEr9  r   r8   r8   r9   r\  	  r  z#ChartParserApp.bottom_up_leftcornerc                 G   r  r   )r  _FUNDAMENTALr:  r   r8   r8   r9   r]  	  r  zChartParserApp.fundamentalc                 G   r  r   )r  _BU_STRATEGYr8  r   r8   r8   r9   rW  	  r  z!ChartParserApp.bottom_up_strategyc                 G   r  r   )r  _BU_LC_STRATEGYr9  r   r8   r8   r9   rX  	  r  z,ChartParserApp.bottom_up_leftcorner_strategyc                 G   r  r   )r  _TD_STRATEGYr5  r   r8   r8   r9   rV  	  r  z ChartParserApp.top_down_strategy)r<  r   )ru   )@rG   rH   rI   rb   rU   rD  r=  rH  r  r>  r?  r@  r@  rA  r=  rQ  ru  rt  rv  rx  rd  rs  r  r~  rB  re  ro  rn  rU  rg  r*  rh  r  rp  rq  rr  r  r  r  rf  r  r  rw  r#   r  r$   r  r   r  r   r  r!   r  r  r  r  rY  rZ  r[  r\  r]  rW  rX  rV  r8   r8   r8   r9   r;    st    
%Q $
		
(
r;  c                  C   sv   t d} d}d}t| }td |  D ]}tdt|d f qtd td|  td	 t| |  d S )
NaW  
    # Grammatical productions.
        S -> NP VP
        VP -> VP PP | V NP | V
        NP -> Det N | NP PP
        PP -> P NP
    # Lexical productions.
        NP -> 'John' | 'I'
        Det -> 'the' | 'my' | 'a'
        N -> 'dog' | 'cookie' | 'table' | 'cake' | 'fork'
        V -> 'ate' | 'saw'
        P -> 'on' | 'under' | 'with'
    z*John ate the cake on the table with a forkzJohn ate the cake on the tablez
grammar= (z    ,)ztokens = %rz,Calling "ChartParserApp(grammar, tokens)"...)	r   r  r   r  rC  r  r   r;  rD  )r  sentr  r  r8   r8   r9   app	  s   r  __main__)Br  Zos.pathr  r  tkinterr   r   r   r   r   r   r   r	   r
   r   Ztkinter.filedialogr   r   Ztkinter.fontr   Ztkinter.messageboxr   r   Z	nltk.drawr   r   r   Znltk.draw.utilr   r   r   r   r   r   Znltk.grammarr   r   Znltk.parse.chartr   r   r   r   r    r!   r"   r#   r$   r%   Z	nltk.treer&   Z	nltk.utilr'   r(   rK   r  r+  r  r+  r5  r8  r9  r:  r;  r  rG   __all__r8   r8   r8   r9   <module>   sZ   	0 0
             	     k
