
    \jN                       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 ddl4m5Z5  G d de      Z6 G d d      Z7 G d d      Z8 G d d      Z9 G d d      Z: G d d      Z; G d de;e.      Z< G d de;e'      Z= G d de;e&      Z> G d  d!e;e+      Z? G d" d#      Z@d$ ZAeBd%k(  r eA        d&gZCy)'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)pickle_load)Tree)in_idlec                   6    e Zd Zej                  d   Zd Zd Zy)EdgeList
rightarrowc                     |j                  dd       |j                  ddd       |j                  dd	       |j                  d
dd       y )Nterminalz#006000)
foregroundarrowsymbol0)font	underlinedotz#000000nonterminalblue)	helveticabold)r/   r3   )
tag_config)self
textwidgetoptionss      B/root/env/lib/python3.12/site-packages/nltk/app/chartparser_app.py_init_colortagszEdgeList._init_colortags_   sX    jY?gHDe	:f3M 	 	
    c                    g }|j                  d|j                         z  df       |j                  | j                  df       t        |j	                               D ]t  \  }}||j                         k(  r|j                  d       t        |t              r%|j                  d|j                         z  df       _|j                  d|z  df       v |j                         r|j                  d       |S )Nz%s	r6   r0   )z *r5   z %sz %rr.   )
appendlhsARROW	enumeraterhsr5   
isinstancer   r1   is_complete)r<   itemcontentsielts        r?   
_item_reprzEdgeList._item_reprg   s    $((*,m<=W-.
+ 	;FAsDHHJ.#{+!5} EFj 9:	; OOM*rA   N)__name__
__module____qualname__r   SYMBOLSrE   r@   rN    rA   r?   r+   r+   \   s      .E
rA   r+   c                       e Zd ZdZ	 ddZd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd ZddZd Zd Zd Zd Zd Zd ZddZd Zd Zd Zy)ChartMatrixViewzS
    A view of a chart that displays the contents of the corresponding matrix.
    c                 ,   || _         g | _        g | _        d | _        |rmt	        |      | _        | j
                  j                  |       | j
                  j                  d| j                         | j                  | j
                         nt        |      | _        | j                  | j
                         | j                  | j
                         |r| j                  | j
                         nd | _        i | _        d| _        | j#                          y )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)r<   parentcharttoplevelr]   show_numedgess         r?   __init__zChartMatrixView.__init__   s     "!&)DJJJU#JJOOM4<<8OODJJ'vDJ$**%

#

+#'D 		rA   c                 \    t        |d| j                        }|j                  ddd       y )NQuittextcommandbottomr   nonesideexpandfill)r   r_   pack)r<   rootquits      r?   r`   zChartMatrixView._init_quit   s&    d>		x	7rA   c                     t        |dd      }|j                  ddddd	       t        |d
d
d      | _        | j                  j                  dd       y )N   sunkenborderreliefr   rs         top)rv   rw   padxpadyru      white)widthheight
background)rv   rw   )r   rx   r   _canvas)r<   ry   cframes      r?   ra   zChartMatrixView._init_matrix   sO    tAh716FfCP0rA   c                 d    t        |d      | _        | j                  j                  ddd       y )Nz0 edgesrp   r   rs   r   )rv   rw   ru   )r   rd   rx   r<   ry   s     r?   rc   zChartMatrixView._init_numedges   s,    $T	:!!e!DrA   c                     t        |g dd      | _        | j                  j                  dddd       | fd	}| j                  j                  d
|       | j                  j	                          y )N      )r   r   r   r   bothr   )ru   rv   rw   r   c                 (    |j                  d|        y Nselect_fire_callbacks)edger<   s     r?   cbz&ChartMatrixView._init_list.<locals>.cb         40rA   r   )r+   _listrx   add_callbackfocus)r<   ry   r   s      r?   rb   zChartMatrixView._init_list   sZ    dBb;


U16B 	1 	

"-

rA   c                     | j                   y 	 | j                   j                          d | _         y # t        $ r
 Y d | _         y w xY wNr\   r_   	Exceptionr<   es     r?   r_   zChartMatrixView.destroy   H    ::	JJ  
  	
	   1 	AAc                 ^    || j                   ur|| _         d| _        | j                          y y Nr   )rX   rf   rg   r<   ri   s     r?   	set_chartzChartMatrixView.set_chart   s*    #DKDOIIK $rA   c                 t   | j                   y t        | j                        }t        |      D cg c]  }t        |      D cg c]  }d c} }}}| j                  D ].  }||j                            |j                         xx   dz  cc<   0 t        |      D ]  }t        ||      D ]  }||   |   dk(  rd}nDdj                  t        ddd||   |   z  dz  z         t        ddd||   |   z  dz  z
              }| j                  |   |   }| j                  j                  ||	       ||f| j                  k(  r:| j                  j                  |d
d       | j                  j                  |       | j                  j                  |dd         t        | j                  j                  | j                              }| j                   j#                  |       | j                  j%                         | _        | j(                  d| j&                  z  | j(                  d<   y y c c}w c c}}w )Nr   r   gray20z#00{:02x}{:02x}   2      
   rw   z#00ffffr   )outliner   black)spanz%d edgesrp   )r\   lenrY   rangerX   startendformatminmaxr   
itemconfigr[   	tag_raiselistr   r   set	num_edgesrf   rd   )	r<   NjrL   
cell_edgesr   colorcell_tagedgess	            r?   updatezChartMatrixView.update   s   :: 5:1X>%(+Qq+>
>KK 	6Dtzz|$TXXZ0A50	6 q 	PA1a[ Pa=#q($E-44CcJqM!,<&<r&A!ABAsS:a=+;%;b%@@AE  ;;q>!,''u'=q6T000LL++Hiq+QLL**84LL++HgQ+OP	P$ T[[''T-@-@'AB

u ++//1++5+GD  ( ,9 ,>s   H4	H/H4/H4c                 ^    | j                   j                  dd       | j                          y )Ninactiveboxhiddenstater   r   r   r<   s    r?   activatezChartMatrixView.activate   "    X>rA   c                 ^    | j                   j                  dd       | j                          y )Nr   normalr   r   r   s    r?   
inactivatezChartMatrixView.inactivate   r   rA   c                 B    d| j                   j                  |i       |<   y Nr   re   
setdefaultr<   eventfuncs      r?   r   zChartMatrixView.add_callback       67""5"-d3rA   Nc                 d    || j                   |= y 	 | j                   |   |= y # t        $ r Y y w xY wr   re   KeyErrorr   s      r?   remove_callbackzChartMatrixView.remove_callback   ;    <&OOE*40    # 	//c                     || j                   vry t        | j                   |   j                               D ]  } ||  	 y r   re   r   keysr<   r   argscb_funcs       r?   r   zChartMatrixView._fire_callbacks   >    'DOOE2779: 	GTN	rA   c                     | j                   y ||f| j                  k(  r(| j                  j                         | j                  k(  ry ||f| _        | j                          | j                  d||       y )Nselect_cell)r\   r[   rX   r   rf   r   r   r<   rL   r   s      r?   r   zChartMatrixView.select_cell  sg    :: q6T(((T[[-B-B-D-W !f 	]Aq1rA   c                     | j                   y d | _        | j                  j                  g        | j	                          y r   )r\   r[   r   r   r   r   s    r?   deselect_cellzChartMatrixView.deselect_cell  s0    ::"

rrA   c                 l    | j                   ||fk(  r| j                          y | j                  ||       y r   )r[   r   r   r   s      r?   _click_cellzChartMatrixView._click_cell  s0    1a&( Q"rA   c                 t     | j                   |j                           | j                  j                  |       y r   )r   r   r   viewr<   r   s     r?   	view_edgezChartMatrixView.view_edge!  s(    $))+&

rA   c                     | j                   y  | j                  |j                           | j                  j	                  |       y r   )r\   r   r   r   markr   s     r?   	mark_edgezChartMatrixView.mark_edge%  s5    ::$))+&

rA   c                 T    | j                   y | j                  j                  |       y r   )r\   r   unmarkr   s     r?   unmark_edgezChartMatrixView.unmark_edge+  s!    ::

$rA   c                     | j                   y  | j                  |j                           | j                  j	                  |       y r   )r\   r   r   r   markonlyr   s     r?   markonly_edgezChartMatrixView.markonly_edge0  s7    ::$))+&

D!rA   c           	      "   | j                   y dx}}d}| j                  }|j                  d       | j                  j	                         dz   }t        |d         |z
  |z  }t        |d         |z
  |z
  |z  }|j                  d       t        |      D ]  }|j                  |dz
  ||z  |dz  z   |z   t        |      d	       |j                  ||z  |dz  z   |z   ||z  |z   dz   t        |      d
	       |j                  |||dz   z  |z   ||z  |z   ||dz   z  |z   d       |j                  ||z  |z   |||z  |z   ||z  |z   d        |j                  |||||z  z   ||z  |z   d       t        |      D 	cg c]  }	t        |      D cg c]  }d  c} c}}	| _        t        |      D ]s  }t        ||      D ]b  }	|j                  |	|z  |z   ||z  |z   |	dz   |z  |z   |dz   |z  |z   d      }
|
| j                  |   |	<   | ||	fd}|j                  |
d|       d u t        |d         t        |d         }}|j                  dd|dz   |dz   ddd      }
|j                  |
       | j                          y c c}w c c}}	w )N   r   allr   r   r   r|   r   )rp   anchorn.)dashr   r   r   c                 (    |j                  ||       y r   )r   )r   r<   rL   r   s       r?   r   z ChartMatrixView.draw.<locals>.cbo  s    $$Q*rA   
<Button-1>id   gray50r   r   )rw   r   tag)r\   r   deleterX   
num_leavesintr   create_textreprcreate_linecreate_rectanglerY   tag_bind	tag_lowerr   )r<   LEFT_MARGIN
BOT_MARGIN
TOP_MARGINcr   dxdyrL   r   tr   xmaxymaxs                 r?   rg   zChartMatrixView.draw6  s   ::#%%j
LL	KK""$q(!G*o+q0!H++j8A=	 q 	AMMaR"q&:!=DGTW   MMBa+-B#a'!W	   MMa!ez)Q$a!ez)   MMQ$Q$Q#  #	4 	
[26%926J;NVW 	 	

 :?qBAeAh//Bq 	0A1a[ 0&&F[(FZ'UbL;.UbL:-! '  %&Aq!#'1 + 

1lB/0	0" 7_c!H+&6d3J3J  
 	
A 	? 0Bs    J	JJJc                 <     | j                   j                  |i | y r   r\   rx   r<   r   kwargss      r?   rx   zChartMatrixView.pack      

((rA   )TChart MatrixFr   )rO   rP   rQ   __doc__rl   r`   ra   rc   rb   r_   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rg   rx   rS   rA   r?   rU   rU   |   s    
 QV<81E#HJ82# 
"L\)rA   rU   c                   X    e Zd ZddZddZd Zd Zd Zd Zd Z	d	 Z
d
 Zd Zd Zd Zy)ChartResultsViewc                    || _         || _        g | _        d| _        g | _        d | _        d | _        |rRt        |      | _        | j                  j                  d       | j                  j                  d| j                         nt        |      | _        |rt        | j                        }|j                  ddd       t        |d| j                  	      j                  d
       t        |d| j                  	      j                  d       t        |d| j                   	      j                  d       t#        | j                  d      | _        | j$                  j                  ddd       | j'                          y )Nr   z!Chart Parser Application: ResultsrW   rr   r   xrt   rn   ro   rightru   z	Print AllleftzPrint Selectionr   )closeenoughr   r   r   )rX   _grammar_trees_y_treewidgets
_selection
_selectboxr   r\   r]   r^   r_   r   rx   r   	print_allprint_selectionr   _cframer   )r<   rh   ri   grammarrj   buttonss         r?   rl   zChartResultsView.__init__  s8   !&)DJJJ@AJJOOM4<<8vDJ DJJ'GLLhqsL;7>CCCQ7dnnEJJPVJW7!2D<P<PQVV W 
 #4::2>uQV< 	rA   Nc                    | j                   y |Z|j                         | j                  j                         k7  ry |j	                         d| j
                  j                         fk7  ry | j
                  j                  | j                  j                               D ]"  }|| j                  vs| j                  |       $ y r   )
r\   rD   r+  r   r   rX   r  parsesr,  _add)r<   r   parses      r?   r   zChartResultsView.update  s    ::xxzT]]0022yy{q$++"8"8":;;[[''(;(;(=> 	!EDKK'		% 	!rA   c                 v   | j                   j                  |       | j                  j                         }t	        ||      }| j
                  j                  |       | j                  j                  |d| j                         |j                  | j                         |j                         d   dz   | _        y )Nr   r   )r,  rC   r3  canvasr   r.  
add_widgetr-  
bind_click_clickbbox)r<   r9  r  
treewidgets       r?   r8  zChartResultsView._add  s    5! LL!(E2
 	  ,
B8 	dkk* //#A&+rA   c                     | j                   j                         }| j                  |j                  | j                         || _        |j                         \  }}}}|j                  ||||dd      | _        y )Nr|   z#088)r   r   )r3  r;  r/  r
  r0  r?  r  )r<   widgetr  x1y1x2y2s          r?   r>  zChartResultsView._click  sh    LL!??&HHT__% !;;=RR,,RR1f,UrA   c                     ||j                         d<   |j                         D ]*  }t        |t              r| j	                  ||       &||d<   , y )Nr   )labelsubtreesrH   r   _color)r<   r@  r   childs       r?   rJ  zChartResultsView._color  sO    &+
7#((* 	'E%!23E5)!&g		'rA   c                 R    | j                   y | j                  j                          y r   )r\   r3  print_to_filer   s     r?   r1  zChartResultsView.print_all  s    ::""$rA   c                 x   | j                   y | j                  t        dd       y | j                  j	                         }| j
                  D ],  }|| j                  us| j                  j                  |       . |j                  | j                         | j                  j                         \  }}}}| j                  j                  d|z
  d|z
         d||z
  dz    d||z
  dz    |d<   | j                  j                          | j                  g| _        | j                          | j                          y )NzPrint ErrorzNo tree selectedr   z0 0 r    scrollregion)r\   r/  r   r3  r;  r.  destroy_widgetr
  r0  r?  moverM  clearr   )r<   r   r  rB  rC  rD  rE  rF  s           r?   r2  z ChartResultsView.print_selection  s   ::??"m%78##%A++ 80LL//78 HHT__%#335RROO  b"r'2"&rBw|nAb2gl^ DAnLL&&( "& 1DJJLKKMrA   c                 ,   | j                   y | j                  D ]  }| j                  j                  |        g | _        g | _        | j
                  3| j                  j                         j                  | j                         d | _        d| _	        y )Nr   )
r\   r.  r3  rQ  r,  r/  r;  r
  r0  r-  )r<   r@  s     r?   rS  zChartResultsView.clear  s|    ::++ 	4JLL''
3	4??&LL!((9rA   c                 R    | j                          || _        | j                          y r   )rS  rX   r   r   s     r?   r   zChartResultsView.set_chart  s    

rA   c                 R    | j                          || _        | j                          y r   )rS  r+  r   r<   r4  s     r?   set_grammarzChartResultsView.set_grammar
  s    

rA   c                     | j                   y 	 | j                   j                          d | _         y # t        $ r
 Y d | _         y w xY wr   r   r   s     r?   r_   zChartResultsView.destroy  r   r   c                 <     | j                   j                  |i | y r   r  r  s      r?   rx   zChartResultsView.pack  r   rA   )Tr   )rO   rP   rQ   rl   r   r8  r>  rJ  r1  r2  rS  r   rX  r_   rx   rS   rA   r?   r$  r$    s@    B!,$V'%
*


)rA   r$  c                       e Zd ZdZdej
                  d   ej
                  d   dZd Zd Zd Z	d	 Z
d
 Zd Zd Zd Zd Zd Zd ZddgZd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd  Z y!)"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                    dgdz  }t        |      | _        d| _        d| _        | j                  | _        | j                  | _        d| j                  i| _        | j                  | _        d | _        t               | _
        | j                  j                  d       | j                  j                  d| j                         | j                  j                  d| j                         | j                  | j                         | j                  | j                         | j!                  | j                         | j#                  | j                         | j%                  | j                         |D ]  }| j'                  |        y )N    NonezChart ComparisonrW   <Control-x>)r   _emptychart
_left_name_right_name_left_chart_right_chart_charts
_out_chart	_operatorr   r\   r]   r^   r_   _init_menubar_init_chartviews_init_divider_init_buttons_init_bindings
load_chart)r<   chart_filenamesfaketokfilenames       r?   rl   zChartComparer.__init__C  s7    $( > !!++ ,,  0 01 **  T


+,

t||4

t||4 	4::&djj)4::&4::&DJJ' ( 	&HOOH%	&rA   c                     | j                   y 	 | j                   j                          d | _         y # t        $ r
 Y d | _         y w xY wr   r   r   s     r?   r_   zChartComparer.destroyi  r   r   c                      y r   )r\   mainloopr  s      r?   rz  zChartComparer.mainloopr  s    rA   c                    t        |      }t        |d      }|j                  ddd| j                         |j                  ddd| j                         |j	                          |j                  dd	| j
                  d
       |j                  dd|       t        |d      }|j                  d| j                  d       |j                  d| j                  d       |j                  d| j                  d       |j	                          |j                  d| j                         |j                  dd|       | j                  j                  |       y )Nr   tearoff
Load ChartCtrl-o)rH  acceleratorr4   rq   zSave OutputCtrl-sExitr   Ctrl-xrH  r4   rq   r  FilerH  r4   menuIntersection+)rH  rq   r  Union*
Differencer]  Swap ChartsrH  rq   Comparer  )r	   add_commandload_chart_dialogsave_chart_dialogadd_separatorr_   add_cascade_intersection_union_difference_swapchartsr\   config)r<   ry   menubarfilemenuopmenus        r?   ro  zChartComparer._init_menubarz  sf   t* + **	 	 	
 	 **	 	 	
 	 At|| 	 	
 	&AHE gq) $*<*<# 	 	
 	$++3O(8(8c 	 	
 	8H8HI)qvF 	

w'rA   c                 H    t        |dd      }|j                  ddd       y )Nr|   r}   r~   r   r&  )ru   rw   ipady)r   rx   )r<   ry   dividers      r?   rq  zChartComparer._init_divider  s"    Qx8%c3rA   c                 v   d}d}t        |d      }|j                  ddd       t        |d	d
      }|j                  ddddd       t        |t        | j                  j                               | j                        | _        | j                  j                  ddd       t        || j                  dd      | _
        | j                  j                  ddddd       | j                  j                  d| j                         | j                  j                  d| j                         | j                  j                          t        |dd	d|      | _        | j                   j                  ddd       t        |d	d
      }|j                  ddddd       t        |t        | j                  j                               | j"                        | _        | j$                  j                  ddd       t        || j                  dd      | _        | j&                  j                  ddddd       | j&                  j                  d| j                         | j&                  j                  d| j                         | j&                  j                          t        |dd	d|      j                  ddd       t        |d	d
      }|j                  ddddd       t        |d      | _        | j(                  j                  dd !       t        || j                  dd      | _        | j*                  j                  ddddd       | j*                  j                  d| j                         | j*                  j                  d| j                         | j*                  j                          y )"N)r1   )r8   r  z#c0c0c0r   r   r   r   rt   r   groover~   r)  rd     )ru   r   r   rv   rw   )rq   r   r&  )ru   r   rw   FT)rj   rk   rr   r   r   rO  )rp   r   r   r3   )ru   r   r   =Outputr   	   )ru   r   )r   rx   r   r   rl  r   _select_left_left_selectorrU   rg  _left_matrixr   select_edger   r   r   	_op_label_select_right_right_selector_right_matrix
_out_label_out_matrix)r<   ry   opfonteqfontframe	cv1_frame	cv2_frame	out_frames           r?   rp  zChartComparer._init_chartviews  sF    #dy1

af
5 %(;	F16J/tDLL--/0$:K:K
 	  e!# >+t''%t
 	H11QVT&&x1A1AB&&}d6F6FG$$& 1
 	aa8 %(;	F16J0tDLL--/0$:L:L 
 	!!u13!?,t''%t
 	XAAafU''$2B2BC''t7G7GH%%' 	e#Q96JOOaa 	P 	

 %(;	F16J	9%a0*t''%t
 	8!!AFS%%h0@0@A%%mT5E5EF##%rA   c                    t        |      }|j                  dddd       t        |d| j                        j                  d	       t        |d
| j                        j                  d	       t        |d| j
                        j                  d	       t        |d      j                  d	       t        |d| j                        j                  d	       t        |d| j                        j                  d	       y )Nrr   r   r&  r   )ru   r   rw   rv   r  ro   r)  r(  r  r  r   r  r  zDetach Outputr'  )r   rx   r   r  r  r  r  _detach_out)r<   ry   r5  s      r?   rr  zChartComparer._init_buttons  s    +(Q?w^T5G5GHMM 	N 	
 	wWdkk:??V?Lw\43C3CDIIvIVgR %%6%2w]D4D4DEJJPVJWw_d6F6FGLL 	M 	
rA   c                 <    |j                  d| j                         y )N<Control-o>)r^   r  r   s     r?   rs  zChartComparer._init_bindings  s    		-!7!78rA   c                     || _         | j                  |   | _        | j                  j	                  | j                         |dk(  r| j                  j                          | j                          y Nre  )rh  rl  rj  r  r   r   	_apply_opr<   names     r?   r  zChartComparer._select_left  sW    <<-##D$4$456>((*rA   c                     || _         | j                  |   | _        | j                  j	                  | j                         |dk(  r| j                  j                          | j                          y r  )ri  rl  rk  r  r   r   r  r  s     r?   r  zChartComparer._select_right  sX     LL.$$T%6%676>))+rA   c                     | j                   dk(  r| j                          y | j                   dk(  r| j                          y | j                   dk(  r| j                          y y )Nr]  ra  r`  )rn  r  r  r  r   s    r?   r  zChartComparer._apply_op
  sL    >>S ^^t#KKM^^u$  %rA   zPickle file.picklez	All filesr  c                    t        | j                  d      }|sy 	 t        |d      5 }t        j                  | j
                  |       d d d        y # 1 sw Y   y xY w# t        $ r}t        dd|d|        Y d }~y d }~ww xY w)Nr  	filetypesdefaultextensionwbzError Saving ChartUnable to open file: 
)r   CHART_FILE_TYPESopenpickledumprm  r   r   r<   r   rw  outfiler   s        r?   r  zChartComparer.save_chart_dialog  s    $++i
 	Wh% 6DOOW56 6 6 	W*.CH<rRSQT,UVV	Ws4   A !A	A AA A 	B'A>>Bc                     t        | j                  d      }|sy 	 | j                  |       y # t        $ r}t	        dd|d|        Y d }~y d }~ww xY w)Nr  r  zError Loading Chartr  r  )r   r  rt  r   r   )r<   r   rw  r   s       r?   r  zChartComparer.load_chart_dialog#  sb    "++i
 	XOOH% 	X+/DXLPRSTRU-VWW	Xs   . 	AAAc                 N   t        |d      5 }t        |      }d d d        t        j                  j	                  |      }|j                  d      r|d d }|j                  d      r|d d }| j                  |<   | j                  j                  |       | j                  j                  |       | j                  | j                  u r| j                  j                  |       y | j                  | j                  u r| j                  j                  |       y y # 1 sw Y   xY w)Nrbr  iz.charti)r  r'   ospathbasenameendswithrl  r  addr  rj  rg  r   rk  )r<   rw  infileri   r  s        r?   rt  zChartComparer.load_chart.  s    (D! 	(V'E	(ww)==#9D=="9D"T%  & t///##D)$"2"22  $$T* 3	( 	(s   DD$c                     | j                   j                          | j                  j                          | j                  j                          y r   )r  r   r  r  r   s    r?   _update_chartviewsz ChartComparer._update_chartviewsA  s8      "!!#!rA   c                    || j                   v r| j                  j                  |       n| j                  j                          || j                  v r| j
                  j                  |       n| j
                  j                          || j                  v r| j                  j                  |       y | j                  j                          y r   )rj  r  r   r   rk  r  rm  r  r   s     r?   r  zChartComparer.select_edgeJ  s    4###++D1))+4$$$,,T2**,4??"**40((*rA   c                     | j                   j                  ||       | j                  j                  ||       | j                  j                  ||       y r   )r  r   r  r  r   s      r?   r   zChartComparer.select_cellX  sD    %%a+&&q!,$$Q*rA   c                     | j                         sy t        | j                  j                               }| j                  D ]#  }|| j                  vs|j                  |g        % | j                  d|       y )Nr]  _checkcompatr   rj  tokensrk  insert_updater<   	out_chartr   s      r?   r  zChartComparer._differencea  sm      "$**1134	$$ 	+D4,,,  r*	+ 	S)$rA   c                     | j                         sy t        | j                  j                               }| j                  D ]#  }|| j                  v s|j                  |g        % | j                  d|       y )Nr`  r  r  s      r?   r  zChartComparer._intersectionl  sm      "$**1134	$$ 	+Dt(((  r*	+ 	UI&rA   c                    | j                         sy t        | j                  j                               }| j                  D ]  }|j	                  |g         | j
                  D ]  }|j	                  |g         | j                  d|       y )Nra  )r  r   rj  r  r  rk  r  r  s      r?   r  zChartComparer._unionw  s      "$**1134	$$ 	'DT2&	'%% 	'DT2&	' 	T9%rA   c                     | j                   | j                  }}| j                  j                  |       | j                  j                  |       y r   )rh  ri  r  r   r  )r<   r)  r'  s      r?   r  zChartComparer._swapcharts  s=    oot'7'7e&  &rA   c                    | j                   j                         | j                  j                         k7  sg| j                   j                         | j                  j                         k7  s2| j                   | j                  k(  s| j                  | j                  k(  r`| j                  | _        | j                  j                  | j
                         | j                  j                          d| j                  d<   yy)Nr  rp   FT)
rj  r  rk  property_namesrg  rm  r  r   r   r  r   s    r?   r  zChartComparer._checkcompat  s    ##%):):)A)A)CC..0D4E4E4T4T4VV4#3#33  D$4$44 #..DO&&t7'')&.DOOF#rA   c                 
   || _         | j                  |   | j                  d<   || _        | j                  j                  |       dj                  | j                  | j                   | j                        | j                  d<   y )Nrp   z{} {} {})
rn  	_OPSYMBOLr  rm  r  r   r   rh  ri  r  )r<   operatorr  s      r?   r  zChartComparer._update  sk    !!%!9v#""9-","3"3OONN#
rA   c                     | j                   | _        | j                  j                  | j                         d| j                  d<   | j                  j                          y )NrO  rp   )rg  rm  r  r   r  r   r   s    r?   _clear_out_chartzChartComparer._clear_out_chart  sG    **""4??3!$v##%rA   c                 b    t        | j                  | j                  | j                  d          y )Nrp   )r]   )rU   r\   rm  r  r   s    r?   r  zChartComparer._detach_out  s    

DOO4??6;RSrA   N)!rO   rP   rQ   r"  r   rR   r  rl   r_   rz  ro  rq  rp  rr  rs  r  r  r  r  r  r  rt  r  r  r   r  r  r  r  r  r  r  r  rS   rA   r?   r\  r\  !  s    8 ##N3""7+I$&L-%(N4;&z
9! 34FG
W	X+&"++	%	'
&'
"	
&TrA   r\  c                      e Zd ZdZdZdZdZdZd(dZd Z	d)dZ
d	 Zd
 Zd Zd Zd Zd Zd Zd Zd(dZd Zd Zd*dZd Zd Zd+dZd,dZd(dZd,dZd Zd Zd Zd Z d Z!d(d Z"d! Z#d" Z$d*d#Z%d$ Z&d% Z'd(d&Z(d' Z)y)-	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                    |j                  dd      }|j                  dd      }|j                  dd      | _        || _        i | _        g | _        i | _        i | _        g | _        d| _        d| _	        g | _
        d| _        |_t               }|j                  d       |fd	}|fd
}|j                  d|       t        |d|      }	|	j!                  d       || _        n|| _        | j%                  |       | j'                  | j"                        \  | _        | _        d| j*                  d<   d| j*                  d<   |rit-        | j"                  dd      }
|
j!                  dd       t/        |
d      | _        d| j0                  d<   | j0                  j!                  d       nd| _        |r?| j'                  | j"                  dd      \  }}||c| _        | _        d | j4                  d<   nd| _        | j7                          | j9                          | j;                          | j=                          | j*                  j                  d!| j>                         y)"z4
        Construct a new ``Chart`` display.
        	draw_treer   draw_sentencer   fontsizer9   Nz
Chart Viewc                 $    |j                          y r   r_   )r   r   s     r?   destroy1z$ChartView.__init__.<locals>.destroy1      rA   c                 $    | j                          y r   r  )r   s    r?   destroy2z$ChartView.__init__.<locals>.destroy2  r  rA   qDonero   rr   r(  i,  r   r   r*  sunkr|   r   r   r   )rw   ru   r   r   #e0e0e0r   r   r  r&  r   z<Configure>) get	_fontsizerX   re   _edgelevels	_edgetagsrZ   	_treetoks_treetoks_edge_treetoks_index
_tree_tags_compactr   r]   r^   r   rx   r\   _init_fonts
_sb_canvas	_chart_sb_chart_canvasr   r   _sentence_canvas_tree_sb_tree_canvas_analyzerg   _resize_grow
_configure)r<   ri   ry   kwr  r  r   r  r  br   sbr;  s                r?   rl   zChartView.__init__  s2   
 FF;*	2
C0     "    <$CIIl# #  !  HHS(#s:AFFF!DJDJ 	 04tzz/J,+'*8$,.=) 4::fQ?FKKV(K3$*6"$=D!2;D!!,/!!&&F&3 %)D! ??4::sC@LR24f.T]D-*-Dh' $D 			

 	t?rA   c                     t        dd| j                        | _        t        d| j                        | _        t        t	               d         | _        |j                  d| j
                         y )Nr8   r:   familyweightsizer!  r#  r3   r3   *Font)r   r	  	_boldfont_fontr   _sysfont
option_addr   s     r?   r  zChartView._init_fonts@  sM    [dnnU4>>B
&(6"23/rA   c                    t        |dd      }|j                  |||       t        |d      }t        |d      }|j                  d	d
       |j                  d|d       |j                  |d<   |j
                  |d<   ||fS )zK
        Helper for __init__: construct a canvas with a scrollbar.
        r  r|   r  )rw   rv   ru   r  r  vertical)orientr'  yru   rw   r)  yesru   rw   rv   rq   yscrollcommand)r   rx   r   r
   yviewr   )r<   ry   rv   rw   ru   r   r;  r  s           r?   r  zChartView._sb_canvasG  s     tF15f4895 vj1
W3'd59 9#%66 F|rA   c                 >    | j                   j                  ddd       y )Nscrollunitsr  r3  r   s     r?   	scroll_upzChartView.scroll_upZ        2w7rA   c                 >    | j                   j                  ddd       y )Nr5  r   r7  r8  r   s     r?   scroll_downzChartView.scroll_down]        1g6rA   c                 >    | j                   j                  ddd       y )Nr5  r6  pagesr8  r   s     r?   page_upzChartView.page_up`  r:  rA   c                 >    | j                   j                  ddd       y )Nr5  r   r?  r8  r   s     r?   	page_downzChartView.page_downc  r=  rA   c                    | j                   j                         }t        t        | j                  d         || j
                  z  t        j                  dz  z         }| j                  j                  |       | j                  j                  | j                  d          |dt        j                  z  z
  |z  | _        | j                  | j                  | j                  d<   yy)z/
        Grow the window, if necessary
        r   r|   r  r   r  N)rX   r  r   r  r  	_unitsizer  _MARGIN	configurer  _sentence_height)r<   r   r   s      r?   r  zChartView._growf  s    
 KK""$""7+,a$...@9CTCTWXCX.X
 	$$5$1$$D,>,>x,H$I!i&7&7"771<   ,.2.C.CD!!(+ -rA   c                 H   | j                   j                  t        |              | j                  j                  t        |              | j                  j                  t        |              | j                          | j                          | j                          y N)r#  )r(  rF  absr'  r)  r  r  rg   r<   r#  s     r?   set_font_sizezChartView.set_font_size{  sn    

3t9*-  s4yj 1c$iZ0

		rA   c                 ,    t        | j                        S r   )rJ  r	  r   s    r?   get_font_sizezChartView.get_font_size  s    4>>""rA   c                     | j                   j                         }|j                  dt        j                  z  z
  |z  | _        | j                          y)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.
        r|   N)rX   r  r   r  rE  rD  rg   )r<   r   r   s      r?   r  zChartView._configure  s?     KK""$''A	(9(9$99Q>		rA   c                 V   |f|| _         g | _        i | _        | j                          | j	                          | j                          | j                          | j                          y| j                   D ]"  }|| j                  vs| j                  |       $ | j                          y)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)
rX   r
  rZ   r  r  rg   
erase_treer  r  	_add_edge)r<   ri   r   s      r?   r   zChartView.update  s     DK!DDKMMOJJLIIKOOLLN )t~~-NN4() LLNrA   c                     |j                         \  }}| j                  |   D ]F  }|j                         \  }}||cxk  r|k  s%n ||cxk  r|k  sn ||cxk(  r|cxk(  r|k(  sB y F y y)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
  )r<   r   lvls1e1	otheredges2e2s           r?   _edge_conflictzChartView._edge_conflict  st     99;R))#. 	I ~~'HRb22=b=bB6J"6J6J 7K	 rA   c           	         | j                   }t        |t              r|j                         }g }|j	                         D ]U  }t        |t
              r)|j                  t        |j                                      <|j                  t        |             W dj                  |      }n|j                         }d}||fD ]  }|j                  dd|| j                  dd      }|j                  |      }	|j                  |       |	d   }
t        |j!                         d      }t        | j"                  |
|z        | _        t        | j$                  |	d	   |	d   z
        | _         y
)z
        Given a new edge, recalculate:

            - _text_height
            - _unitsize (if the edge text is too big for the current
              _unitsize, then increase _unitsize)
        rO  rc  r   nwr)  rp   r3   r   justifyr|   r   r   N)r  rH   r&   rD   rG   r   rC   strr1   r  joinr  r'  r?  r
  r   lengthrD  _text_height)r<   r   r  rD   rhseltsrM   rG   sr	  r?  r   edgelens               r?   _analyze_edgezChartView._analyze_edge  s5    dH%((*CGxxz .c;/NN3szz|#45NN49-	.
 ((7#C((*CCs 		JA--114>>$   C 66#;DHHSMGE$++-+G ADN #D$5$5tAwa7H ID		JrA   c                    t        |t              ry|| j                  v ry| j                  |       | j	                          | j
                  sW| j                  j                  |g       t        | j                        dz
  }| j                  ||       | j                          yd}	 |t        | j                        k\  rD| j                  j                  g        | j                          |t        | j                        k\  rD||k\  r1| j                  ||      s| j                  |   j                  |       n|dz  }| j                  ||       y)z
        Add a single edge to the ChartView:

            - Call analyze_edge to recalculate display parameters
            - Find an available level
            - Call _draw_edge
        Nr   r   )rH   r    r  rf  r  r  r
  rC   r   
_draw_edger  rZ  )r<   r   minlvlrT  s       r?   rR  zChartView._add_edge  s&    dH%4>>!4 

}}##TF+d&&'!+COOD#&LLN T--..  ''+ T--..
 f}T%8%8s%C  %,,T2 1HC  	c"rA   c                 p   d }t        t        | j                              D ]  }|| j                  |   v s|} n |y |dz   | j                  z  }| j                  dz   }| j
                  j                  dd       | j                  dk7  r-| j
                  j                  d||z
  | j                  z         y y )Nr   r   movetog      ?r   )r   r   r
  _chart_level_sizerb  r  r3  _chart_height)r<   r   levelrL   r.  r  s         r?   r   zChartView.view_edge  s    s4++,- 	At''**	 =QY$000#  3/"$$XB$:L:L/LM #rA   c                    | j                   }|j                         | j                  z  t        j                  z   }|j                         | j                  z  t        j                  z   }||k(  r|t        d| j                  dz        z  }|dz   | j                  z  }|j                  ||||dd      }t        |t              r{g }|j                         D ]U  }	t        |	t              r)|j                  t        |	j                                      <|j                  t!        |	             W |j#                         }
ng }d}
dj%                  |d	|
       }dj%                  ||
d	       }|j'                  |dz   ||| j(                  d
      }|j+                  |      d   dz   }|j+                  |      d   |j+                  |      d   z   dz  }|j-                  |dz
  |dz
  |dz   |dz         }|j'                  |dz   ||| j(                  d
      }|j'                  ||z   dz  |t        |j/                               d| j0                        }|||||f| j2                  |<   | |fd}|j5                  |d|       |j5                  |d|       |j5                  |d|       |j5                  |d|       |j5                  |d|       | j7                  |       y	)z6
        Draw a single edge on the ChartView.
           r   r   lastr   )r0   r   r   rO  Nr\  )rp   r3   r   r|      rd  rp   r   r3   c                 (    |j                  d|       y r   r   )r   r<   r   s      r?   r   z ChartView._draw_edge.<locals>.cb=  r   rA   r  )r  r   rD  r  rE  r   r   rl  r  rH   r&   rG   r   rC   r_  r1   r  r5   r`  r  r(  r?  create_ovalrD   r'  r  r  _color_edge)r<   r   rT  r  rC  rE  r.  linetagrG   rM   posrhs1rhs2rhstag1dotxdotydottagrhstag2lhstagr   s                       r?   rh  zChartView._draw_edge  s     ZZ\DNN*Y->->>XXZ$..(9+<+<<8#a!+,,B1W...--Ar1F!-D dH%Cxxz *c;/JJs3::<01JJtCy)	*
 ((*CCCxxDS	"xxCD	"--Q4::d-Svvgq!A%w"QVVG_Q%771<tax4!8TAXF--q!$TZZPT-U"WM13txxz?3T^^  

 !(&'6Jt  d 	1 	


7L"-	

7L"-	

7L"-	

6<,	

6<,rA   c                    || j                   vry| j                  }|||| j                  v r| j                  |   }| j                   |   }|j                  |d   |       |j                  |d   |       |j                  |d   ||       |j                  |d   |       |j                  |d   |       y| j                  j                         }|| j                  v r| j                  | j                  |          |j                         r)|j                         d|fk(  r| j                  |d	d
       yt        |t              r| j                  |dd       y| j                  |dd       y)z
        Color in an edge with the given colors.
        If no colors are specified, use intelligent defaults
        (dependent on selection, etc.)
        Nr   r   r   r|   rw   r   r   rp  #084#042z#48cz#246z#00fz#008)r  r  rZ   r   rX   r  rv  rI   r   rH   r    )r<   r   	linecolor	textcolorr  tagsr   s          r?   rv  zChartView._color_edgeH  sL    t~~% Y%:t{{" KK-	>>$'DLLayL1LLayL1LLay)LDLLayL1LLayL1&&(At{{"  T!23!diikaV&;  vv6D(+  vv6  vv6rA   c                 D    || j                   |<   | j                  |       y)z
        Mark an edge
        N)rZ   rv  r<   r   r   s      r?   r   zChartView.mark_edgeg  s      !DrA   c                     |Ct        | j                  j                               }i | _        |D ]  }| j                  |        y| j                  |= | j                  |       y)z/
        Unmark an edge (or all edges)
        N)r   rZ   r   rv  )r<   r   old_marked_edgess      r?   r   zChartView.unmark_edgen  sa     <#DKK$4$4$67DK( '  &' D!T"rA   c                 H    | j                          | j                  ||       y r   )r   r   r  s      r?   r   zChartView.markonly_edge{  s    tT"rA   c           	         d}d}| j                   }| j                  j                         D ]  }|j                  ddt	        |      | j
                  dd      }|j                  |      }|j                  |       |d   t        j                  z   }t        ||      }t        ||d   |d   z
        } || _        || _        | j                  dt        j                  z  z   | _        | j                  j                         D ]  }| j!                  |        | j                  dz  | _        dt        j$                  | j                  z   z  | _        | j)                          y	)
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]  r|   r   r   N)r  rX   leavesr  r  r(  r?  r
  r  _LEAF_SPACINGr   rD  rb  rE  rG  r   rf  rl  _TREE_LEVEL_SIZE_tree_heightr  )	r<   unitsizetext_heightr  leafr	  r?  r   r   s	            r?   r  zChartView._analyze  sM     KK&&( 	>D--14:DJJtV   C 66#;DHHSMGi555E5(+Hk47T!W+<=K	> "' $ 1 1A	8I8I4I I KK%%' 	%Dt$	% "&!2!2Q!6 !;!;d>O>O!OP 	rA   c                 d   | j                   }| j                  j                         | j                  z  t        j
                  dz  z   }t        | j                        }|dz   | j                  z  | _	        dd|| j                  f|d<   | j                  rdd|| j                  f| j                  d<   yy)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.
        r|   r   rP  N)r  rX   r  rD  r  rE  r   r
  rl  rm  r  r  )r<   r  r   levelss       r?   r  zChartView._resize  s      &&(4>>9I<M<MPQ<QQT%%&$qjD,B,BB5$*<*<=. 12Aud>O>O0PDn- rA   c                 z   d}| j                   }| j                  }| j                  }t        j                  }g | _        t        d| j                  j                         dz         D ]S  }|| j                  z  |z   }|r%|j                  |d||      }|j                  |       |r/|j                  |d|| j                        }	|j                  |	       |j                  |d||      }
|j                  |
       |j                  |dz   dt        |      d| j                        }|j                  |       |dz  dk(  r?|r|j!                  d       |r|j!                  	d       |j!                  |
d       |r|j!                  d	       |r|j!                  	d	       |j!                  |
d	       V y
)zv
        Draw location lines.  These are vertical gridlines used to
        show where each location unit is.
        iP  r   r   r|   r\  rs  gray60r   gray80N)r  r  r  r  rE  	_loclinesr   rX   r  rD  r  r  rG  r  r  r(  r   )r<   BOTTOMc1c2c3marginrL   r&  t1t2t3t4s               r?   _draw_loclineszChartView._draw_loclines  s   
 """"q$++002Q67 	1ADNN"V+A^^Aq!V4R ^^Aq!T-B-BCR 1a0BLLAqtAwt$**UBLL
 1uzMM"8M4MM"8M4bx0MM"8M4MM"8M4bx09	1rA   c           	      n   | j                   j                         dk(  ry| j                  }t        j                  }t        j                  }t        | j                   j                               D ]  \  }}|| j                  z  |z   }|| j                  z   }||z   dz  }|j                  ||t        |      | j                  dd      }	|j                  |	      }
|j                  |dz   |
d   t        j                  dz  z
  |dz
  |
d   t        j                  dz  z   d	d	
      }|j                  |        y)zDraw the sentence string.r   Nr|   r  r)  r]  r   r   z#f0f0f0r  )rX   r  r  r  rE  rF   r  rD  r  r  r(  r?  r  r  r  )r<   r  r  r.  rL   r  rC  rE  r&  r	  r?  rts               r?   _draw_sentencezChartView._draw_sentence  s)   ;;!!#q(!!"" !3!3!56 	GAtT^^#f,Bdnn$BbAA--14:DJJsF   C 66#;D##QQ922Q67QQ922Q67! $ B KKO!	rA   c                     | j                   D ]  }| j                  j                  |        g | _        d | _        d| _        y r   )r  r  r
  r  r  r  )r<   r	  s     r?   rQ  zChartView.erase_tree  s@    ?? 	*C$$S)	*" rA   c                    || j                   y || j                   }| j                   |k7  rM| j                  j                  |      D cg c]  }t        |t              s| c}| _        || _         d| _        t        | j
                        dk(  ry | j                  D ]  }| j                  j                  |        | j
                  | j                     }| j                  ||j                                | j                          | j                  j                         | j                  z  dt         j"                  z  z   }|j%                         t         j&                  | j(                  z   z  }dd||f| j                  d<   y c c}w )Nr   r|   rP  )r  rX   treesrH   r(   r  r  r   r  r  r
  _draw_treetokr   _draw_treecycler  rD  r  rE  r   r  rb  )r<   r   r  r	  treewhs          r?   r  zChartView.draw_tree  sU   <D//7<&&D $&)-):):4)@XAJqRVDWaXDN"&D#$D  t~~!# ?? 	*C$$S)	* ~~d2234. 	 KK""$t~~5I<M<M8MMKKMY77$:K:KKL-.1aL.)- Ys   
F Fc                     | j                   dz   t        | j                        z  | _         | j                  | j                         y r   )r  r   r  r  r  r   s    r?   
cycle_treezChartView.cycle_tree#  s5     $ 4 4q 8C<OOt**+rA   c                    t        | j                        dk  ry dt        | j                        z  }| j                  }t        j                  }| j
                  j                         | j                  z  |z   dz
  }|j                  |dd|| j                        }| j                  j                  |       |j                  |      \  }}}}t        t        | j                              D ]  }|dt        | j                        |z
  dz
  z  z
  }	|| j                  k(  rd}
nd}
|j                  |	|d	z   |	d
z
  ||	d	z
  |d	z   |
d      }| j                  j                  |       | |fd}|j!                  |d|        y )Nr   z%d Treesr|   ne)r   rp   r3   r   r  z#fffr   r   r   r  c                 2    ||_         |j                          y r   )r  r  )r   r<   rL   s      r?   r   z%ChartView._draw_treecycle.<locals>.cbB  s    '($ rA   r  )r   r  r  r  rE  rX   r  rD  r  r'  r  rC   r?  r   r  create_polygonr  )r<   rH  r  r  r'  r	  _r.  rL   r&  rw   r   s               r?   r  zChartView._draw_treecycle'  sj   t~~!# S00""&&(4>>9FBQFmmE1TDNNmSs#VVC[
1a s4>>*+ 	.Ac$..1A59::AD(((""1r61q5!QVQV$ # C OO""3'  $q ! JJsL"-#	.rA   c           
         | j                   }t        j                  }g }|D ]l  }t        |t              r+| j                  |||dz         \  }}|j                  |       >|j                  d|z  dz   | j                  z  dz  |z          |dz  }n |rt        |      t        |      z  }	n d|z  dz   | j                  z  dz  |z   }	|dz  }|t        j                  | j                  z   z  }
|j                  |	|
ddt        |j                               d| j                        }| j                   j                  |       |
t        j                  z   | j                  z   }t#        ||      D ]  \  }}t        |t              rA|r?|j%                  |	|
| j                  z   ||dd      }| j                   j                  |       t        |t              rB|s@|j%                  |	|
| j                  z   ||dd	d
      }| j                   j                  |       t        |t              r|j%                  |	|
| j                  z   |ddd      }| j                   j                  |        |	|fS )z
        :param index: The index of the first leaf in the tree.
        :return: The index of the first leaf after the tree.
        r   r|   r  centerr  )r   r^  rp   rw   r3   r  )r   rw   z#048z2 3)r   rw   r  i'  )r  r  rE  rH   r(   r  rC   rD  sumr   r  rb  r  r_  rH  r'  r  zipr  )r<   treetokindexdepthr  r  child_xsrK  child_xnodexnodeyr	  childychildxs                 r?   r  zChartView._draw_treetokH  sr   
 ""  	E%&!%!3!3E5%!)!L(UQ$.. @1 Dv MN
	 MCM1E Y]dnn4q86AEQJE 33d6G6GGHmmW]]_%  
 	s# 333d6G6GG 73 "	,MFE%&5mmD--- $  &&s+%&ummD--- $  &&s+eT*mmD--- $  &&s+E"	,H e|rA   c                    | j                   r+| j                   j                  d       | j                          | j                  r+| j                  j                  d       | j	                          | j
                  j                  d       i | _        t        t        | j                              D ](  }| j                  |   D ]  }| j                  ||        * | j                  D ]  }| j                  |        | j                          y)z1
        Draw everything (from scratch).
        r   N)r  r
  r  r  r  r  r  r   r   r
  rh  rX   rR  r  )r<   rT  r   s      r?   rg   zChartView.draw  s     $$U+NN  !!((/!!!%( T--./ 	+C((- +c*+	+ KK 	!DNN4 	! 	rA   c                 B    d| j                   j                  |i       |<   y r   r   r   s      r?   r   zChartView.add_callback  r   rA   c                 d    || j                   |= y 	 | j                   |   |= y # t        $ r Y y w xY wr   r   r   s      r?   r   zChartView.remove_callback  r   r   c                     || j                   vry t        | j                   |   j                               D ]  } ||  	 y r   r   r   s       r?   r   zChartView._fire_callbacks  r   rA   r   )r.  r   rr   )r   )NN)#0df)*rO   rP   rQ   r"  r  rE  r  _CHART_LEVEL_SIZErl   r  r  r9  r<  r@  rB  r  rL  rN  r  r   rZ  rf  rR  r   rh  rv  r   r   r   r  r  r  r  rQ  r  r  r  r  rg   r   r   r   rS   rA   r?   r  r    s    ,\ MGW@r0&8787D*#	2 JD)#VN3j7>##$LQ('1R4!9@,.BN`28rA   r  c                   6     e Zd ZdZ fdZ fdZ fdZ x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                 n    | j                   j                  d   }|| _        |j                  dz
  | _        y r   )	__class__	__bases___edge	NUM_EDGES)r<   r   superr  s      r?   rl   zEdgeRule.__init__  s.    ((+
1,rA   c              '      K   | j                   j                  d   }|| j                  fz  } |j                  | ||g| E d {    y 7 wr   )r  r  r  apply)r<   ri   r4  r   r  r  s        r?   r  zEdgeRule.apply  sH     ((+$**5;;tUG<e<<<s   AAAAc                 X    | j                   j                  d   }|j                  |       S r   )r  r  __str__)r<   r  r  s     r?   r  zEdgeRule.__str__  s&    ((+}}T""rA   )rO   rP   rQ   r"  rl   r  r  __classcell__)r  s   @r?   r  r    s    -
=
# #rA   r  c                       e Zd Zy)TopDownPredictEdgeRuleNrO   rP   rQ   rS   rA   r?   r  r        rA   r  c                       e Zd Zy)BottomUpEdgeRuleNr  rS   rA   r?   r  r    r  rA   r  c                       e Zd Zy)BottomUpLeftCornerEdgeRuleNr  rS   rA   r?   r  r    r  rA   r  c                       e Zd Zy)FundamentalEdgeRuleNr  rS   rA   r?   r  r    r  rA   r  c                      e Zd Zd3dZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zd ZddgZg dZd Zd Zd Zd Zd Zd Zd Zd Zd Z d  Z!d! Z"d" Z#d# Z$d$ Z%d4d&Z&d' Z'd5d(Z(d) Z)d* Z* e+       gZ, e-       gZ. e/       gZ0 e1       gZ2 e3       gZ4e,e.z   e4z   Z5e0e4z   Z6e2e4z   Z7d+ Z8d, Z9d- Z:d. Z;d/ Z<d0 Z=d1 Z>d2 Z?y%)6ChartParserAppc                 4   | j                  ||       d | _        	 t               | _        | j                  j                  |       | j                  j	                  d| j
                         t        | j                        }t        | j                        }t        | j                        }|j                  dd       |j                  dd       |j                  ddd       | j                  | j                         | j                          | j                  |       | j                  |       | j                  |       | j                          d | _        d | _        | j!                          y # t"        $ r t%        d	       | j                           w xY w)
NrW   rr   rs   r/  r&  r   r   r1  zError creating Tree View)_init_parserr\   r   r]   r^   r_   r   rx   r  _init_animation_init_chartview_init_rulelabelrr  ro  _matrix_resultsrs  r   print)r<   r4  r  r]   frame3frame2frame1s          r?   rl   zChartParserApp.__init__  sA   '6*
	DJJJU#JJOOM4<<8 4::&F4::&F4::&FKKXFK3KKXCK0KKXF1K=TZZ(  "  (  (v& DL DM ! 	,-LLN	s   EE1 1&Fc                 `    | j                   y | j                   j                          d | _         y r   )r\   r_   r<   r   s     r?   r_   zChartParserApp.destroy  s&    ::


rA   c                 R    t               ry | j                  j                  |i | y)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\   rz  r  s      r?   rz  zChartParserApp.mainloop"  s%     9

T,V,rA   c                 @    || _         || _        | j                          y r   )r+  _tokens_reset_parser)r<   r4  r  s      r?   r  zChartParserApp._init_parser1  s    rA   c                 r   t        | j                        | _        | j                  j                  | j                         | j                  j                         | _        t               j                  | j                  | j                        D ]  } | j                  j                         | _
        d | _        y r   )r#   r+  _cp
initializer  ri   rX   r!   r  step_cpstepr/  )r<   	_new_edges     r?   r  zChartParserApp._reset_parser6  s    &t}}5DLL)hhnn& &--dkk4==I 	I	 xx}} rA   c                    t        t               d         | _        |j                  d| j                         t	        |      | _        | j
                  j                  | j                  j                  d             t        dd| j
                  j                               | _	        t        d| j
                  j                               | _
        y )	Nr3   r%  r&  r#  r8   r:   r   r$  )r   r   r)  r*  r   _sizer   cgetr  r'  r(  r   s     r?   r  zChartParserApp._init_fontsE  s    &(6"23/ D\


t}}))&12[djjnnFVW4::>>3CD
rA   c                     t        | j                        | _        | j                  j                  d       t        | j                        | _        | j                  j                  d       d| _        y )Nr   r   r   )r   r\   _stepr   _animate
_animatingr   s    r?   r  zChartParserApp._init_animationQ  sO    DJJ'


q tzz*! rA   c                     t        | j                  |dd      | _        | j                  j                  d| j                         y )Nr   )r  r  r   )r  rX   _cvr   _click_cv_edge)r<   rh   s     r?   r  zChartParserApp._init_chartview]  s2    T[[&AQOh(;(;<rA   c                 D   d}t        ||| j                        | _        t        |ddd| j                        | _        | j                  j	                  d       | j                  j	                  d       t        || j                  d	
      }|j	                  d       y )NzLast edge generated by:)rp   r3   r  r  r  )r   r   r   r3   r)  r(  Step)variablerp   r'  )r   r'  _rulelabel1_rulelabel2rx   r   r  )r<   rh   ruletxtr  s       r?   r  zChartParserApp._init_rulelabela  s    + gDNNK "Xc
 	6*6*6DJJVD		w	rA   c                    t        |      }t        |      }|j                  dd       |j                  dd       t        |ddd| j                  	      j                  d
       t        |ddd| j                  	      j                  d       t        |ddd| j
                  	      j                  d       t        |ddd| j                  	      j                  d       t        |ddd| j                  	      j                  d       t        |ddd| j                  	      j                  d       t        |d      j                  d       t        |ddd| j                  	      j                  d       t        |d      j                  d       t        |ddd| j                  	      j                  d       t        |d      j                  d       t        |ddd| j                  	      j                  d       y )Nrr   r&  r/  r   rs   zReset
Parserz#90c0d0r   )rp   r   r/   rq   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   rx   r   resettop_down_strategybottom_up_strategybottom_up_leftcorner_strategytop_down_inittop_down_predict	bottom_upbottom_up_leftcornerfundamental)r<   rh   r  r  s       r?   rr  zChartParserApp._init_buttonsm  s   vv,V,  JJ	
 $G$

 	% **	
 $F$
& ++	
 $F$
2 66	
 $F$
& &&	
 $F$
) ))	
 $F$
fB$$&$1* NN	
 $F$
fB$$&$16 --	
 $F$
fB$$&$1$ $$	
 $F$
rA   c                 \   | j                   j                  d| j                  j                         | j                   j                  d| j                  j                         | j                   j                  d| j                  j
                         | j                   j                  d| j                  j                         | j                   j                  d| j                         | j                   j                  d| j                         | j                   j                  d| j                         | j                   j                  d| j                         | j                   j                  d	| j                         | j                   j                  d
| j                         | j                   j                  d| j                         | j                   j                  d| j                         | j                   j                  d| j                         | j                   j                  d| j                         | j                   j                  d| j                          | j                   j                  d| j"                         | j                   j                  d| j$                  fd       | j                   j                  d| j$                  fd       | j                   j                  d| j$                  fd       | j                   j                  d| j&                  fd       y )Nz<Up>z<Down>z<Prior>z<Next>rW   rf  z<F1>z<Control-s>r  z<Control-r>r  r  r  z<space>z<Control-g>z<Control-t>r]  c                 $    |j                  d      S r   r   r   as     r?   <lambda>z/ChartParserApp._init_bindings.<locals>.<lambda>      a rA   r  c                 $    |j                  d      S )Nr|   r  r  s     r?   r  z/ChartParserApp._init_bindings.<locals>.<lambda>  r  rA   r  c                 $    |j                  d      S )Nr   r  r  s     r?   r  z/ChartParserApp._init_bindings.<locals>.<lambda>  r  rA   rd  c                 B    |j                  |j                                S r   )r   r  )r   rd  s     r?   r  z/ChartParserApp._init_bindings.<locals>.<lambda>  s    QUUquuw;5G rA   )r\   r^   r  r9  r<  r@  rB  r_   help
save_chartrt  r  r  r  r  _stop_animationedit_grammaredit_sentencer   r  r   s    r?   rs  zChartParserApp._init_bindings  s   

 2 23

$(("6"67

	488#3#34

$(("4"45

t||4

t||4

		*

t7

t7

tzz2

T334

T445

T??@

	4#7#78

t'8'89

t'9'9: 	

@A

@A

@A 	

GHrA   c                 J
   t        | j                        }t        |d      }|j                  dd| j                  d       |j                  dd| j                  d       |j                  dd| j
                  d	       |j                          |j                  d
| j                         |j                  d| j                         |j                          |j                  dd| j                  d       |j                  dd|       t        |d      }|j                  dd| j                  d       |j                  dd| j                  d       |j                  dd|       t        |d      }|j                  dd| j                         |j                  dd| j                         |j                  dd|       t        |d      }|j                  dd| j                  d       |j                  dd| j                   d        |j                  d!d| j"                  d"       |j                          |j                  d#| j$                         |j                  d$| j&                         |j                  d%| j(                         |j                  d&| j*                         |j                  d'| j,                         |j                  d(d|       t        |d      }|j/                  d)d| j0                  d*+       |j                          |j3                  d,d| j4                  d-       |j3                  d.d| j4                  dd/0       |j3                  d1d| j4                  d2d30       |j3                  d4d| j4                  d5d60       |j                  d7d|       t        |d      }|j3                  d8| j6                  dd9| j8                  :       |j3                  d;| j6                  dd<| j8                  :       |j3                  d=| j6                  dd>| j8                  :       |j3                  d?| j6                  dd@| j8                  :       |j3                  dA| j6                  ddB| j8                  :       |j                  dCd|       t        |d      }|j                  dDd| j:                         |j                  dEd| j<                  dF       |j                  dGd|       | j                  j?                  |H       y )INr   r|  z
Save Chartr  r  r~  r  zReset ChartzCtrl-rzSave Grammarr  zLoad Grammarr  r   r  r  r  zEdit Grammarr   zCtrl-g	Edit TextzCtrl-tEditr!  rr  )rH  r4   rq   Results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 RuleApplyr  rd  )rH  r4   r  r  zNo Animation)rH  r4   r  valuezSlow Animationr]  )rH  r4   r  r*  r  zNormal Animationr|   r  zFast Animationr   r  AnimateTinyr   )rH  r  r4   r*  rq   Smallr  Medium   Large   Huge   ZoomAboutInstructionsF1Helpr  ) r	   r\   r  r   rt  r  r  save_grammarload_grammarr_   r  r"  r#  view_matrixview_resultsr  r  r  r  r  r  r  r  add_checkbuttonr  add_radiobuttonr   r  resizeaboutr  r  )	r<   r  r  editmenuviewmenurulemenuanimatemenuzoommenuhelpmenus	            r?   ro  zChartParserApp._init_menubar  s[   tzz"+OO 	 	 	
 	OO 	 	 	
 	1djjh 	 	
 	 >4;L;LM>4;L;LM At|| 	 	
 	&AHE+ %% 	 	 	
 	&& 	 	 	
 	&AHE+ At7G7G 	 	
 	94CTCTU&AHE+%**	 	 	
 	&++	 	 	
 	266	 	 	
 	 #3T^^L.8Q8Q 	 	
 	#7ASAST)43H3H 	 	
 	#5t?O?OP'QXF7A.##A

 	$ 	
 	!!### AQ 	$ 	
 	##"]] 	$ 	
 	##$]] 	$ 	
 	##"]] 	$ 	
 	)q{K+  ZZKK 	! 	
 	  ZZKK 	! 	
 	  ZZKK 	! 	
 	  ZZKK 	! 	
 	  ZZKK 	! 	
 	&AHE+7aL Atyyd 	 	
 	&AHE

w'rA   c                 z    || j                   k7  r| j                  |       y | j                  j                          y r   )r/  _select_edger  r  r   s     r?   r  zChartParserApp._click_cv_edge~  s.    4??"d# HH!rA   c                 \    | j                  |       | j                  j                  |       y r   )rH  r  r   r   s     r?   _select_matrix_edgez"ChartParserApp._select_matrix_edge  s"    $4 rA   c                    || _         | j                  j                  |d       | j                  j                  |       | j                  r| j                  j                  |       | j                  r| j                  j                  |       y y )Nz#f00)r/  r  r   r  r  r   r   s     r?   rH  zChartParserApp._select_edge  sf    tV,4 <<LL&&t,<<LL""4( rA   c                     d | _         | j                  j                          | j                  j                          | j                  r| j                  j                          y y r   )r/  r  r   rQ  r  r   s    r?   _deselect_edgezChartParserApp._deselect_edge  sE    <<LL$$& rA   c                 f   | j                  | j                  j                                | j                  j	                          | j                  j                  |       | j                  j                  |d       | j                  j                  |       | j                  r| j                  j	                          | j                  r| j                  j                  |       | j                  r| j                  j                  |       | j                  r| j                  j	                  |       y y )Nr  )
_display_ruler  current_chartruler  r   r  r   r   r  r  r   s     r?   _show_new_edgezChartParserApp._show_new_edge  s    48855784 tV,4 <<LL!<<LL&&t,<<LL""4(==MM  & rA   c                     d| _         	 t        | j                  dt        xs dj	                         dd       y # t
        $ r2 t        | j                  dt        xs dj	                         d       Y y w xY w)Nr   zHelp: Chart Parser Applicationrc  K   fixed)r   r3   r  )r  r   r\   r"  stripr   r   s     r?   r  zChartParserApp.help  sl    	

0B%%'  	

0B%%'		s   0: 8A54A5c                      d}t        d|       y )Nz5NLTK Chart Parser Application
Written by Edward LoperzAbout: Chart Parser Application)r   )r<   r   ABOUTs      r?   r@  zChartParserApp.about  s    M2E:rA   r  r  ))zPlaintext grammar file.cfgr  r  c                    t        | j                  d      }|sy	 t        |d      5 }t        |      }ddd       | _        | j
                  j                  |       | j                  r| j                  j                  |       | j                  r| j                  j                          | j                  r| j                  j                  |       | j                  j                  |       y# 1 sw Y   xY w# t        $ r} d}~ww xY w)zLoad a chart from a pickle filer  r  Nr  )r   r  r  r'   rX   r  r   r  r   r   r  r  r   r   )r<   r   rw  r  ri   r   s         r?   rt  zChartParserApp.load_chart  s    "++i
 	Sh% ,#F+,DKHHOOE"||&&u-||**,}}''.HHu%, ,  	S	Ss.   C: C.B9C: .C73C: :	D	DD	c                     t        | j                  d      }|sy	 t        |d      5 }t        j                  | j
                  |       ddd       y# 1 sw Y   yxY w# t        $ r} d}~ww xY w)zSave a chart to a pickle filer  r  Nr  )r   r  r  r  r  rX   r   r   r  s        r?   r   zChartParserApp.save_chart  sl    $++i
 	Rh% 2DKK12 2 2 	R	Rs4   A !A	A AA A 	A-'A((A-c                    t        | j                  d      }|sy	 |j                  d      r!t        |d      5 }t	        |      }ddd       n7t        |      5 }t        j                  |j                               }ddd       | j                         y# 1 sw Y   xY w# 1 sw Y   'xY w# t        $ r}t        dd|z         Y d}~yd}~ww xY w)z!Load a grammar from a pickle filerX  r  Nr  r  zError Loading GrammarUnable to open file: %r)r   GRAMMAR_FILE_TYPESr  r  r'   r   
fromstringreadrX  r   r   )r<   r   rw  r  r4  r   s         r?   r:  zChartParserApp.load_grammar  s    "--
 		U  +(D) 2V)&1G2 2 (^ <v!nnV[[];G<W%2 2< <  	U-/H8/STT	UsF   B/ BB/ $B#=B/ B B/ #B,(B/ /	C8CCc                 &   t        | j                  d      }|sy 	 |j                  d      rBt        |d      5 }t	        j
                  | j                  | j                  f|       d d d        y t        |d      5 }| j                  j                         }|D cg c]0  }|j                         | j                  j                         k(  s/|2 }}|D cg c]0  }|j                         | j                  j                         k7  s/|2 }}|D ]  }|j                  d|z          |D ]  }|j                  d|z          	 d d d        y # 1 sw Y   y xY wc c}w c c}w # 1 sw Y   y xY w# t        $ r}	t        dd|z         Y d }	~	y d }	~	ww xY w)	NrX  r  r  r  r  z%s
zError Saving Grammarr\  )r   r]  r  r  r  r  rX   r  r+  productionsrD   r   writer   r   )
r<   r   rw  r  prodspr   restprodr   s
             r?   r9  zChartParserApp.save_grammar	  sz   $--
 	T  +(D) FWKKdll ;WEF F (C( 5G MM557E(-R1DMM<O<O<Q1QQRER',Q!4==;N;N;P0PAQDQ % 5ftm45 $ 5ftm455 5F F
 SQ5 5  	T,.G(.RSS	Ts   E. -E&E. /E. ;E"0EEE"0EE
8E"E. EE. E. 
E""E+'E. +E. .	F7FFc                    d| _         | j                          | j                  j                  | j                         | j
                  r%| j
                  j                  | j                         | j
                  r| j
                  j                          | j                  r&| j                  j                  | j                         y y r   )	r  r  r  r   rX   r  r   r   r  r  s     r?   r  zChartParserApp.reset%	  s    $<<LL""4;;/<<LL&&(==MM##DKK0 rA   c                 Z    t        | j                  | j                  | j                         y r   )r   r\   r+  rX  r   s     r?   r"  zChartParserApp.edit_grammar4	  s    $**dmmT-=-=>rA   c                     || _         | j                  j                  |       | j                  r| j                  j                  |       y y r   )r+  r  rX  r  rW  s     r?   rX  zChartParserApp.set_grammar7	  s9    W%==MM%%g. rA   c                     dj                  | j                        }d}d}t        | j                  ||| j                  |       y )NrO  r%  zEnter a new sentence to parse.)r`  r  r   r\   set_sentence)r<   r   sentencer]   instrs        r?   r#  zChartParserApp.edit_sentence=	  s8    88DLL)0DJJ%1B1BEJrA   c                 `    t        |j                               | _        | j                          y r   )r   splitr  r  )r<   rl  s     r?   rk  zChartParserApp.set_sentenceC	  s    HNN,-

rA   c                     | j                   | j                   j                          t        | j                  | j                        | _         | j                   j                  d| j                         y r   )r  r_   rU   r\   rX   r   rJ  r   s     r?   r;  zChartParserApp.view_matrixK	  sL    <<#LL  "&tzz4;;?!!(D,D,DErA   c                     | j                   | j                   j                          t        | j                  | j                  | j
                        | _         y r   )r  r_   r$  r\   rX   r+  r   s     r?   r<  zChartParserApp.view_resultsQ	  s8    ==$MM!!#(T[[$--PrA   c                 d    d| _         | j                  | j                  j                                y r   )r  rL  r  r  r   s    r?   r?  zChartParserApp.resizeZ	  s"    4::>>+,rA   c                    | j                   j                  |       | j                  j                  t	        |              | j
                  j                  t	        |              | j                  j                  t	        |              y rI  )r  rL  r(  rF  rJ  r'  r)  rK  s     r?   rL  zChartParserApp.set_font_size^	  se    t$

3t9*-  s4yj 1c$iZ0rA   c                 H    t        | j                  j                               S r   )rJ  r  r  r   s    r?   rN  zChartParserApp.get_font_sized	  s    4::>>#$$rA   Nc                 :   | j                   rd| _         y | j                  d        | j                  j                         r| j                  }|X|V| j
                  j                   ||      g       | j                         }|N| j                  j                          d | _        y | j
                  j                  |       | j                          y y | j
                  j                  |       | j                  j                         rd| _         | j                          y | j                  D ]  }| n | j                  j                          | j                  r| j                  j                          | j                  r| j                  j                          y y )Nr   r   )r  rO  r  r  r/  r  set_strategy_apply_strategyr  r   r   _animate_strategyr  r   r  r  )r<   strategyedge_strategy	selectionnewedger   s         r?   apply_strategyzChartParserApp.apply_strategyk	  sE   ??DO 	4  ::>>I%M,E%%}Y'?&@A..0 ?HH((*&*DO%%h/$$& # HH!!(+}}  ""#&&( LL D| !<<LL'')==MM((* !rA   c                     d| _         y r   )r  r   s     r?   r!  zChartParserApp._stop_animation	  s	    rA   c                    | j                   dk(  ry | j                         | j                  j                         dk(  s| j                  j                         dk(  ry | j                  j                         dk(  r'| j
                  j                  d| j                         y | j                  j                         dk(  r'| j
                  j                  d| j                         y | j
                  j                  d| j                         y y )Nr   r   i  r|   i  r   )r  rw  r   r  r  r\   afterrx  )r<   speeds     r?   rx  z ChartParserApp._animate_strategy	  s    ??a!-}}  "a'4::>>+;q+@}}  "a'

  t'='=>""$)

  t'='=>

  T%;%;< .rA   c                 V    t        | j                        }|| j                  |       |S r   )nextr  rQ  )r<   new_edges     r?   rw  zChartParserApp._apply_strategy	  s)    %)rA   c                     |d| j                   d<   y t        |      }|| j                   d<   | j                  j                         }y )Nrc  rp   )r	  r_  r  rN  )r<   ruler  r#  s       r?   rO  zChartParserApp._display_rule	  sB    <')DV$t9D'+DV$88))+DrA   c                 <    | j                  | j                  d        y r   )r}  _TD_INITr   s     r?   r  zChartParserApp.top_down_init	  s    DMM40rA   c                 D    | j                  | j                  t               y r   )r}  _TD_PREDICTr  r   s     r?   r  zChartParserApp.top_down_predict	  s    D,,.DErA   c                 D    | j                  | j                  t               y r   )r}  _BU_RULEr  r   s     r?   r  zChartParserApp.bottom_up	  s    DMM+;<rA   c                 D    | j                  | j                  t               y r   )r}  _BU_LC_RULEr  r   s     r?   r  z#ChartParserApp.bottom_up_leftcorner	  s    D,,.HIrA   c                 D    | j                  | j                  t               y r   )r}  _FUNDAMENTALr  r   s     r?   r  zChartParserApp.fundamental	  s    D--/BCrA   c                 D    | j                  | j                  t               y r   )r}  _BU_STRATEGYr  r   s     r?   r  z!ChartParserApp.bottom_up_strategy	  s    D--/?@rA   c                 D    | j                  | j                  t               y r   )r}  _BU_LC_STRATEGYr  r   s     r?   r  z,ChartParserApp.bottom_up_leftcorner_strategy	  s    D002LMrA   c                 D    | j                  | j                  t               y r   )r}  _TD_STRATEGYr  r   s     r?   r  z ChartParserApp.top_down_strategy	  s    D--/EFrA   )zChart Parser Applicationr   )r   )@rO   rP   rQ   rl   r_   rz  r  r  r  r  r  r  rr  rs  ro  r  rJ  rH  rM  rQ  r  r@  r  r]  rt  r   r:  r9  r  r"  rX  r#  rk  r;  r<  r?  rL  rN  r}  r!  rx  rw  rO  r$   r  r%   r  r   r  r   r  r"   r  r  r  r  r  r  r  r  r  r  r  r  rS   rA   r?   r  r    sq   #J	-

E
=
 ObI:](F"!	)'',&; 34FGS.RU$T,	1?/KFQ-1%&+P=,  !"H%'(K#%&H-/0K-/0L k)L8Ll*L!L0O1F=JDANGrA   r  c                  V   t        j                  d      } d}d}t        |j                               }t	        d       | j                         D ]  }t	        dt        |      dz   f        t	        d       t	        d|z         t	        d	       t        | |      j                          y )
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   ro  r  ra  r  r  rz  )r4  sentr  r  s       r?   appr  	  s    nn	G  8D+D$**,F	,##% *vtDzC'()*	#J	-&
 !	
897F#,,.rA   __main__r  )Dr"  os.pathr  r  tkinterr   r   r   r   r   r   r	   r
   r   r   tkinter.filedialogr   r   tkinter.fontr   tkinter.messageboxr   r   	nltk.drawr   r   r   nltk.draw.utilr   r   r   r   r   r   nltk.grammarr   r   nltk.parse.chartr   r   r   r    r!   r"   r#   r$   r%   r&   nltk.picklesecr'   	nltk.treer(   	nltk.utilr)   r+   rU   r$  r\  r  r  r  r  r  r  r  r  rO   __all__rS   rA   r?   <module>r     s  <     B  2 G G  *   '  } @I) I)bL) L)hJT JTdN Np# #,	X'9 		x!4 		+E 		($= 	cG cGL/< zE 'rA   