
    gzN                       d Z ddlZddlZddlmZmZmZmZm	Z	m
Z
mZmZmZmZ ddlmZmZ ddlmZ ddlmZmZ ddlmZmZmZ ddlmZmZmZmZm Z m!Z! dd	l"m#Z#m$Z$ dd
l%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/ ddl0m1Z1 ddl2m3Z3  G d de      Z4 G d d      Z5 G d d      Z6 G d d      Z7 G d d      Z8 G d d      Z9 G d de9e.      Z: G d de9e'      Z; G d de9e&      Z< G d d e9e+      Z= G d! d"      Z>d# Z?e@d$k(  r e?        d%gZAy)&a  
A graphical tool for exploring chart parsing.

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

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

The chart parsing tool allows for flexible control of the parsing
algorithm.  At each step of the algorithm, you can select which rule
or strategy you wish to apply.  This allows you to experiment with
mixing different strategies (e.g. top-down and bottom-up).  You can
exercise fine-grained control over the algorithm by selecting which
edge you wish to apply a rule to.
    N)
ButtonCanvasCheckbuttonFrameIntVarLabelMenu	ScrollbarTkToplevel)askopenfilenameasksaveasfilename)Font)	showerrorshowinfo)	CFGEditorTreeSegmentWidgettree_to_treesegment)CanvasFrameColorizedListEntryDialogMutableOptionMenuShowTextSymbolWidget)CFGNonterminal)
BottomUpPredictCombineRuleBottomUpPredictRuleChartLeafEdgeLeafInitRuleSingleEdgeFundamentalRuleSteppingChartParserTopDownInitRuleTopDownPredictRuleTreeEdge)Tree)in_idlec                   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.   r2   )
tag_config)self
textwidgetoptionss      M/var/www/openai/venv/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	r5   r/   )z *r4   z %sz %rr-   )
appendlhsARROW	enumeraterhsr4   
isinstancer   r0   is_complete)r;   itemcontentsielts        r>   
_item_reprzEdgeList._item_reprf   s    $((*,m<=W-.
+FAsDHHJ.#{+!5} EFj 9: , OOM*r@   N)__name__
__module____qualname__r   SYMBOLSrD   r?   rM    r@   r>   r*   r*   [   s      .E
r@   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 		r@   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	7r@   c                     t        |dd      }|j                  ddddd	       t        |d
d
d      | _        | j                  j                  dd       y )N   sunkenborderreliefr   rr         top)ru   rv   padxpadyrt      white)widthheight
background)ru   rv   )r   rw   r   _canvas)r;   rx   cframes      r>   r`   zChartMatrixView._init_matrix   sO    tAh716FfCP0r@   c                 d    t        |d      | _        | j                  j                  ddd       y )Nz0 edgesro   r   rr   r   )ru   rv   rt   )r   rc   rw   r;   rx   s     r>   rb   zChartMatrixView._init_numedges   s,    $T	:!!e!Dr@   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   )rt   ru   rv   r   c                 (    |j                  d|        y Nselect_fire_callbacks)edger;   s     r>   cbz&ChartMatrixView._init_list.<locals>.cb         40r@   r   )r*   _listrw   add_callbackfocus)r;   rx   r   s      r>   ra   zChartMatrixView._init_list   sZ    dBb;


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

"-

r@   c                 ~    | j                   y 	 | j                   j                          d | _         y #  Y d | _         y xY wNr[   r^   r;   es     r>   r^   zChartMatrixView.destroy   >    ::	JJ  
	
   1 <c                 ^    || j                   ur|| _         d| _        | j                          y y Nr   )rW   re   rf   r;   rh   s     r>   	set_chartzChartMatrixView.set_chart   s*    #DKDOIIK $r@   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      
   rv   z#00ffffr   )outliner   black)spanz%d edgesro   )r[   lenrX   rangerW   startendformatminmaxr   
itemconfigrZ   	tag_raiselistr   r   set	num_edgesre   rc   )	r;   NjrK   
cell_edgesr   colorcell_tagedgess	            r>   updatezChartMatrixView.update   s    :: 5:1X>X%(+(Qq(+X
>KKDtzz|$TXXZ0A50   qA1a[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 ! $ 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>r@   c                 ^    | j                   j                  dd       | j                          y )Nr   normalr   r   r   s    r>   
inactivatezChartMatrixView.inactivate   r   r@   c                 B    d| j                   j                  |i       |<   y Nr   rd   
setdefaultr;   eventfuncs      r>   r   zChartMatrixView.add_callback       67""5"-d3r@   Nc                 T    || j                   |= y 	 | j                   |   |= y #  Y y xY wr   rd   r   s      r>   remove_callbackzChartMatrixView.remove_callback   3    <&OOE*40   # 'c                     || j                   vry t        | j                   |   j                               D ]  } ||  	 y r   rd   r   keysr;   r   argscb_funcs       r>   r   zChartMatrixView._fire_callbacks   9    'DOOE2779:GTN ;r@   c                     | j                   y ||f| j                  k(  r(| j                  j                         | j                  k(  ry ||f| _        | j                          | j                  d||       y )Nselect_cell)r[   rZ   rW   r   re   r   r   r;   rK   r   s      r>   r   zChartMatrixView.select_cell  sg    :: q6T(((T[[-B-B-D-W !f 	]Aq1r@   c                     | j                   y d | _        | j                  j                  g        | j	                          y r   )r[   rZ   r   r   r   r   s    r>   deselect_cellzChartMatrixView.deselect_cell  s0    ::"

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

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

r@   c                 T    | j                   y | j                  j                  |       y r   )r[   r   unmarkr   s     r>   unmark_edgezChartMatrixView.unmark_edge*  s!    ::

$r@   c                     | j                   y  | j                  |j                           | j                  j	                  |       y r   )r[   r   r   r   markonlyr   s     r>   markonly_edgezChartMatrixView.markonly_edge/  s7    ::$))+&

D!r@   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   )ro   anchorn.)dashr   r   r   c                 (    |j                  ||       y r   )r   )r   r;   rK   r   s       r>   r   z ChartMatrixView.draw.<locals>.cbn  s    $$Q*r@   
<Button-1>id   gray50r   r   )rv   r   tag)r[   r   deleterW   
num_leavesintr   create_textreprcreate_linecreate_rectanglerX   tag_bind	tag_lowerr   )r;   LEFT_MARGIN
BOT_MARGIN
TOP_MARGINcr   dxdyrK   r   tr   xmaxymaxs                 r>   rf   zChartMatrixView.draw5  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BAeAh/hh/BqA1a[&&F[(FZ'UbL;.UbL:-! '  %&Aq!#'1 + 

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

((r@   )TChart MatrixFr   )rN   rO   rP   __doc__rk   r_   r`   rb   ra   r^   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rf   rw   rR   r@   r>   rT   rT   {   s    
 QV<81E#HJ82# 
"L\)r@   rT   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: ResultsrV   rq   r   xrs   rm   rn   rightrt   z	Print AllleftzPrint Selectionr   )closeenoughr   r   r   )rW   _grammar_trees_y_treewidgets
_selection
_selectboxr   r[   r\   r]   r^   r   rw   r   	print_allprint_selectionr   _cframer   )r;   rg   rh   grammarri   buttonss         r>   rk   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< 	r@   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[   rC   r(  r   r   rW   r  parsesr)  _add)r;   r   parses      r>   r   zChartResultsView.update  s    ::xxzT]]0022yy{q$++"8"8":;;[[''(;(;(=>EDKK'		%  ?r@   c                 v   | j                   j                  |       | j                  j                         }t	        ||      }| j
                  j                  |       | j                  j                  |d| j                         |j                  | j                         |j                         d   dz   | _        y )Nr   r   )r)  rB   r0  canvasr   r+  
add_widgetr*  
bind_click_clickbbox)r;   r6  r  
treewidgets       r>   r5  zChartResultsView._add  s    5! LL!(E2
 	  ,
B8 	dkk* //#A&+r@   c                     | j                   j                         }| j                  |j                  | j                         || _        |j                         \  }}}}|j                  ||||dd      | _        y )Nr{   z#088)r   r   )r0  r8  r,  r  r-  r<  r  )r;   widgetr  x1y1x2y2s          r>   r;  zChartResultsView._click  sh    LL!??&HHT__% !;;=RR,,RR1f,Ur@   c                     ||j                         d<   |j                         D ]*  }t        |t              r| j	                  ||       &||d<   , y )Nr   )labelsubtreesrG   r   _color)r;   r=  r   childs       r>   rG  zChartResultsView._color  sJ    &+
7#((*E%!23E5)!&g	 +r@   c                 R    | j                   y | j                  j                          y r   )r[   r0  print_to_filer   s     r>   r.  zChartResultsView.print_all  s    ::""$r@   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   r0  r8  r+  destroy_widgetr  r-  r<  moverJ  clearr   )r;   r   r  r?  r@  rA  rB  rC  s           r>   r/  z ChartResultsView.print_selection  s   ::??"m%78##%A++0LL//7 , HHT__%#335RROO  b"r'2"&rBw|nAb2gl^ DAnLL&&( "& 1DJJLKKMr@   c                 ,   | j                   y | j                  D ]  }| j                  j                  |        g | _        g | _        | j
                  3| j                  j                         j                  | j                         d | _        d| _	        y )Nr   )
r[   r+  r0  rN  r)  r,  r8  r  r-  r*  )r;   r=  s     r>   rP  zChartResultsView.clear  sw    ::++JLL''
3 ,??&LL!((9r@   c                 R    | j                          || _        | j                          y r   )rP  rW   r   r   s     r>   r   zChartResultsView.set_chart  s    

r@   c                 R    | j                          || _        | j                          y r   )rP  r(  r   r;   r1  s     r>   set_grammarzChartResultsView.set_grammar	  s    

r@   c                 ~    | j                   y 	 | j                   j                          d | _         y #  Y d | _         y xY wr   r   r   s     r>   r^   zChartResultsView.destroy  r   r   c                 <     | j                   j                  |i | y r   r  r  s      r>   rw   zChartResultsView.pack  r  r@   )Tr   )rN   rO   rP   rk   r   r5  r;  rG  r.  r/  rP  r   rU  r^   rw   rR   r@   r>   r!  r!    s@    B!,$V'%
*


)r@   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)rZ  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 ComparisonrV   <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>   rk   zChartComparer.__init__B  s2    $( > !!++ ,,  0 01 **  T


+,

t||4

t||4 	4::&djj)4::&4::&DJJ' (HOOH% (r@   c                 ~    | j                   y 	 | j                   j                          d | _         y #  Y d | _         y xY wr   r   r   s     r>   r^   zChartComparer.destroyh  r   r   c                      y r   )r[   mainloopr  s      r>   rw  zChartComparer.mainloopq  s    r@   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)rE  acceleratorr3   rp   zSave OutputCtrl-sExitr   Ctrl-xrE  r3   rp   r}  FilerE  r3   menuIntersection+)rE  rp   r}  Union*
DifferencerZ  Swap ChartsrE  rp   Comparer  )r	   add_commandload_chart_dialogsave_chart_dialogadd_separatorr^   add_cascade_intersection_union_difference_swapchartsr[   config)r;   rx   menubarfilemenuopmenus        r>   rl  zChartComparer._init_menubary  sf   t* + **	 	 	
 	 **	 	 	
 	 At|| 	 	
 	&AHE gq) $*<*<# 	 	
 	$++3O(8(8c 	 	
 	8H8HI)qvF 	

w'r@   c                 H    t        |dd      }|j                  ddd       y )Nr{   r|   r}   r   r#  )rt   rv   ipady)r   rw   )r;   rx   dividers      r>   rn  zChartComparer._init_divider  s"    Qx8%c3r@   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)r0   )r7   r  z#c0c0c0r   r   r   r   rs   r   groover}   r&  ra     )rt   r   r   ru   rv   )rp   r   r#  )rt   r   rv   FT)ri   rj   rq   r   r   rL  )ro   r   r   r2   )rt   r   r   =Outputr   	   )rt   r   )r   rw   r   r   ri  r   _select_left_left_selectorrT   rd  _left_matrixr   select_edger   r   r   	_op_label_select_right_right_selector_right_matrix
_out_label_out_matrix)r;   rx   opfonteqfontframe	cv1_frame	cv2_frame	out_frames           r>   rm  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##%r@   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 )Nrq   r   r#  r   )rt   r   rv   ru   r  rn   r&  r%  r  r  r   r  r  zDetach Outputr$  )r   rw   r   r  r  r  r  _detach_out)r;   rx   r2  s      r>   ro  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 	
r@   c                 <    |j                  d| j                         y )N<Control-o>)r]   r  r   s     r>   rp  zChartComparer._init_bindings  s    		-!7!78r@   c                     || _         | j                  |   | _        | j                  j	                  | j                         |dk(  r| j                  j                          | j                          y Nrb  )re  ri  rg  r  r   r   	_apply_opr;   names     r>   r  zChartComparer._select_left  sW    <<-##D$4$456>((*r@   c                     || _         | j                  |   | _        | j                  j	                  | j                         |dk(  r| j                  j                          | j                          y r  )rf  ri  rh  r  r   r   r  r  s     r>   r  zChartComparer._select_right  sX     LL.$$T%6%676>))+r@   c                     | j                   dk(  r| j                          y | j                   dk(  r| j                          y | j                   dk(  r| j                          y y )NrZ  r^  r]  )rk  r  r  r  r   s    r>   r  zChartComparer._apply_op	  sL    >>S ^^t#KKM^^u$  %r@   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dumprj  	Exceptionr   r;   r   rt  outfiler   s        r>   r  zChartComparer.save_chart_dialog  s~    $++i
 	Wh%DOOW5 &%% 	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  rq  r  r   )r;   r   rt  r   s       r>   r  zChartComparer.load_chart_dialog"  sb    "++i
 	XOOH% 	X+/DXLPRSTRU-VWW	Xs   . 	AAAc                 b   t        |d      5 }t        j                  |      }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  loadospathbasenameendswithri  r  addr  rg  rd  r   rh  )r;   rt  infilerh   r  s        r>   rq  zChartComparer.load_chart-  s    (D!VKK'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_chartviews@  s8      "!!#!r@   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   )rg  r  r   r   rh  r  rj  r  r   s     r>   r  zChartComparer.select_edgeI  s    4###++D1))+4$$$,,T2**,4??"**40((*r@   c                     | j                   j                  ||       | j                  j                  ||       | j                  j                  ||       y r   )r  r   r  r  r   s      r>   r   zChartComparer.select_cellW  sD    %%a+&&q!,$$Q*r@   c                     | j                         sy t        | j                  j                               }| j                  D ]#  }|| j                  vs|j                  |g        % | j                  d|       y )NrZ  _checkcompatr   rg  tokensrh  insert_updater;   	out_chartr   s      r>   r  zChartComparer._difference`  sh      "$**1134	$$D4,,,  r* % 	S)$r@   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._intersectionk  sh      "$**1134	$$Dt(((  r* % 	UI&r@   c                    | j                         sy t        | j                  j                               }| j                  D ]  }|j	                  |g         | j
                  D ]  }|j	                  |g         | j                  d|       y )Nr^  )r  r   rg  r  r  rh  r  r  s      r>   r  zChartComparer._unionv  sz      "$**1134	$$DT2& %%%DT2& & 	T9%r@   c                     | j                   | j                  }}| j                  j                  |       | j                  j                  |       y r   )re  rf  r  r   r  )r;   r&  r$  s      r>   r  zChartComparer._swapcharts  s=    oot'7'7e&  &r@   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  ro   FT)
rg  r  rh  property_namesrd  rj  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#r@   c                 
   || _         | j                  |   | j                  d<   || _        | j                  j                  |       dj                  | j                  | j                   | j                        | j                  d<   y )Nro   z{} {} {})
rk  	_OPSYMBOLr  rj  r  r   r   re  rf  r  )r;   operatorr  s      r>   r  zChartComparer._update  sk    !!%!9v#""9-","3"3OONN#
r@   c                     | j                   | _        | j                  j                  | j                         d| j                  d<   | j                  j                          y )NrL  ro   )rd  rj  r  r   r  r   r   s    r>   _clear_out_chartzChartComparer._clear_out_chart  sG    **""4??3!$v##%r@   c                 b    t        | j                  | j                  | j                  d          y )Nro   )r\   )rT   r[   rj  r  r   s    r>   r  zChartComparer._detach_out  s    

DOO4??6;RSr@   N)!rN   rO   rP   r  r   rQ   r  rk   r^   rw  rl  rn  rm  ro  rp  r  r  r  r  r  r  rq  r  r  r   r  r  r  r  r  r  r  r  rR   r@   r>   rY  rY     s    8 ##N3""7+I$&L-%(N4;&z
9! 34FG
W	X+&"++	%	'
&'
"	
&Tr@   rY  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   fontsizer8   Nz
Chart Viewc                 $    |j                          y r   r^   )r   r   s     r>   destroy1z$ChartView.__init__.<locals>.destroy1      r@   c                 $    | j                          y r   r  )r   s    r>   destroy2z$ChartView.__init__.<locals>.destroy2  r  r@   qDonern   rq   r%  i,  r   r   r'  sunkr{   r   r~   r   )rv   rt   r   r   #e0e0e0r   r   r   r#  r   z<Configure>) get	_fontsizerW   rd   _edgelevels	_edgetagsrY   	_treetoks_treetoks_edge_treetoks_index
_tree_tags_compactr   r\   r]   r   rw   r[   _init_fonts
_sb_canvas	_chart_sb_chart_canvasr   r   _sentence_canvas_tree_sb_tree_canvas_analyzerf   _resize_grow
_configure)r;   rh   rx   kwr  r  r   r  r   br   sbr8  s                r>   rk   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?r@   c                     t        dd| j                        | _        t        d| j                        | _        t        t	               d         | _        |j                  d| j
                         y )Nr7   r9   familyweightsizer   r"  r2   r2   *Font)r   r  	_boldfont_fontr   _sysfont
option_addr   s     r>   r  zChartView._init_fonts?  sM    [dnnU4>>B
&(6"23/r@   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  )rv   ru   rt   r  r  vertical)orientr$  yrt   rv   r&  yesrt   rv   ru   rp   yscrollcommand)r   rw   r   r
   yviewr   )r;   rx   ru   rv   rt   r   r8  r  s           r>   r  zChartView._sb_canvasF  s     tF15f4895 vj1
W3'd59 9#%66 F|r@   c                 >    | j                   j                  ddd       y )Nscrollunitsr  r2  r   s     r>   	scroll_upzChartView.scroll_upY        2w7r@   c                 >    | j                   j                  ddd       y )Nr4  r   r6  r7  r   s     r>   scroll_downzChartView.scroll_down\        1g6r@   c                 >    | j                   j                  ddd       y )Nr4  r5  pagesr7  r   s     r>   page_upzChartView.page_up_  r9  r@   c                 >    | j                   j                  ddd       y )Nr4  r   r>  r7  r   s     r>   	page_downzChartView.page_downb  r<  r@   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)rW   r  r   r	  r  	_unitsizer  _MARGIN	configurer  _sentence_height)r;   r   r   s      r>   r  zChartView._growe  s    
 KK""$""7+,a$...@9CTCTWXCX.X
 	$$5$1$$D,>,>x,H$I!i&7&7"771<   ,.2.C.CD!!(+ -r@   c                 H   | j                   j                  t        |              | j                  j                  t        |              | j                  j                  t        |              | j                          | j                          | j                          y N)r"  )r'  rE  absr&  r(  r  r  rf   r;   r"  s     r>   set_font_sizezChartView.set_font_sizez  sn    

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

		r@   c                 ,    t        | j                        S r   )rI  r  r   s    r>   get_font_sizezChartView.get_font_size  s    4>>""r@   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)rW   r  r   r  rD  rC  rf   )r;   r   r   s      r>   r  zChartView._configure  s?     KK""$''A	(9(9$99Q>		r@   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)
rW   r	  rY   r  r  rf   
erase_treer  r
  	_add_edge)r;   rh   r   s      r>   r   zChartView.update  s     DK!DDKMMOJJLIIKOOLLNt~~-NN4( $ LLNr@   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  so     99;R))#.I ~~'HRb22=b=bB6J"6J6J 7K / r@   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)
        rL  r`  r   nwr&  ro   r2   r   justifyr{   r   r   N)r  rG   r&   rC   rF   r   rB   strr0   r  joinr
  r&  r<  r  r   lengthrC  _text_height)r;   r   r  rC   rhseltsrL   rF   sr  r<  r   edgelens               r>   _analyze_edgezChartView._analyze_edge  s)    dH%((*CGxxzc;/NN3szz|#45NN49-	 "
 ((7#C((*CCsA--114>>$   C 66#;DHHSMGE$++-+G ADN #D$5$5tAwa7H ID r@   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   )rG   r    r
  re  r  r  r	  rB   r   
_draw_edger  rY  )r;   r   minlvlrS  s       r>   rQ  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"r@   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_sizera  r  r2  _chart_height)r;   r   levelrK   r-  r  s         r>   r   zChartView.view_edge  s    s4++,-At''** . =QY$000#  3/"$$XB$:L:L/LM #r@   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   )r/   r   r   rL  Nr[  )ro   r2   r   r{      rc  ro   r   r2   c                 (    |j                  d|       y r   r   )r   r;   r   s      r>   r   z ChartView._draw_edge.<locals>.cb<  r   r@   r  )r  r   rC  r  rD  r   r   rk  r  rG   r&   rF   r   rB   r^  r0   r  r4   r_  r
  r'  r<  create_ovalrC   r&  r
  r  _color_edge)r;   r   rS  r  r@  rB  r-  linetagrF   rL   posrhs1rhs2rhstag1dotxdotydottagrhstag2lhstagr   s                       r>   rg  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<,r@   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{   rv   r   r   ro  #084#042z#48cz#246z#00fz#008)r
  r  rY   r   rW   r  ru  rH   r   rG   r    )r;   r   	linecolor	textcolorr  tagsr   s          r>   ru  zChartView._color_edgeG  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6r@   c                 D    || j                   |<   | j                  |       y)z
        Mark an edge
        N)rY   ru  r;   r   r   s      r>   r   zChartView.mark_edgef  s      !Dr@   c                     |Ct        | j                  j                               }i | _        |D ]  }| j                  |        y| j                  |= | j                  |       y)z/
        Unmark an edge (or all edges)
        N)r   rY   r   ru  )r;   r   old_marked_edgess      r>   r   zChartView.unmark_edgem  s\     <#DKK$4$4$67DK(  & ) D!T"r@   c                 H    | j                          | j                  ||       y r   )r   r   r  s      r>   r   zChartView.markonly_edgez  s    tT"r@   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  rW   leavesr
  r  r'  r<  r  r  _LEAF_SPACINGr   rC  ra  rD  rF  r   re  rk  _TREE_LEVEL_SIZE_tree_heightr  )	r;   unitsizetext_heightr  leafr  r<  r   r   s	            r>   r  zChartView._analyze~  sC     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 	r@   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   rM  N)r  rW   r  rC  r  rD  r   r	  rk  rl  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- r@   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[  rr  gray60r   gray80N)r  r  r  r  rD  	_loclinesr   rW   r  rC  r  r  rF  r
  r  r'  r   )r;   BOTTOMc1c2c3marginrK   r#  t1t2t3t4s               r>   _draw_loclineszChartView._draw_loclines  s   
 """"q$++002Q67A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 8r@   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  )rW   r  r  r  rD  rE   r  rC  r
  r  r'  r<  r  r  r  )r;   r  r  r-  rK   r  r@  rB  r#  r  r<  rts               r>   _draw_sentencezChartView._draw_sentence  s$   ;;!!#q(!!"" !3!3!56GAt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! 7r@   c                     | j                   D ]  }| j                  j                  |        g | _        d | _        d| _        y r   )r  r  r  r  r  r  )r;   r  s     r>   rP  zChartView.erase_tree  s;    ??C$$S) #" r@   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{   rM  )r  rW   treesrG   r'   r  r  r   r  r  r  _draw_treetokr   _draw_treecycler  rC  r  rD  r   r  ra  )r;   r   r  r  treewhs          r>   r  zChartView.draw_tree  sV   <D//7<&&D $&)-):):4)@X)@AJqRVDWa)@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**+r@   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   ro   r2   r   r  z#fffr   r   r   r  c                 2    ||_         |j                          y r   )r  r  )r   r;   rK   s      r>   r   z%ChartView._draw_treecycle.<locals>.cbA  s    '($ r@   r  )r   r  r  r  rD  rW   r  rC  r
  r&  r  rB   r<  r   r  create_polygonr  )r;   rE  r  r  r$  r  _r-  rK   r#  rv   r   s               r>   r  zChartView._draw_treecycle&  se   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"-# ,r@   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]  ro   rv   r2   r  )r   rv   z#048z2 3)r   rv   r   i'  )r  r  rD  rG   r'   r  rB   rC  sumr   r  ra  r
  r^  rE  r&  r  zipr  )r;   treetokindexdepthr  r  child_xsrH  child_xnodexnodeyr  childychildxs                 r>   r  zChartView._draw_treetokG  sh   
 "" 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3MFE%&5mmD--- $  &&s+%&ummD--- $  &&s+eT*mmD--- $  &&s+E 4H e|r@   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	  rg  rW   rQ  r  )r;   rS  r   s      r>   rf   zChartView.draw  s     $$U+NN  !!((/!!!%( T--./C((-c* . 0 KKDNN4    	r@   c                 B    d| j                   j                  |i       |<   y r   r   r   s      r>   r   zChartView.add_callback  r   r@   c                 T    || j                   |= y 	 | j                   |   |= y #  Y y 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   r@   r   )r-  r   rq   )r   )NN)#0df)*rN   rO   rP   r  r  rD  r  _CHART_LEVEL_SIZErk   r  r  r8  r;  r?  rA  r  rK  rM  r  r   rY  re  rQ  r   rg  ru  r   r   r   r  r  r  r  rP  r  r  r  r  rf   r   r   r   rR   r@   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r@   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>   rk   zEdgeRule.__init__  s.    ((+
1,r@   c              '      K   | j                   j                  d   }|| j                  fz  } |j                  | ||g| E d {    y 7 wr   )r  r  r  apply)r;   rh   r1  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""r@   )rN   rO   rP   r  rk   r  r  __classcell__)r  s   @r>   r  r    s    -
=
# #r@   r  c                       e Zd Zy)TopDownPredictEdgeRuleNrN   rO   rP   rR   r@   r>   r  r        r@   r  c                       e Zd Zy)BottomUpEdgeRuleNr  rR   r@   r>   r  r    r  r@   r  c                       e Zd Zy)BottomUpLeftCornerEdgeRuleNr  rR   r@   r>   r  r    r  r@   r  c                       e Zd Zy)FundamentalEdgeRuleNr  rR   r@   r>   r  r    r  r@   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                 $   | 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#        d	       | j                           xY w)
NrV   rq   rr   r.  r#  r   r   r0  zError creating Tree View)_init_parserr[   r   r\   r]   r^   r   rw   r  _init_animation_init_chartview_init_rulelabelro  rl  _matrix_resultsrp  print)r;   r1  r  r\   frame3frame2frame1s          r>   rk   zChartParserApp.__init__  s9   '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   s     r>   r^   zChartParserApp.destroy  s&    ::


r@   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[   rw  r  s      r>   rw  zChartParserApp.mainloop!  s%     9

T,V,r@   c                 @    || _         || _        | j                          y r   )r(  _tokens_reset_parser)r;   r1  r  s      r>   r  zChartParserApp._init_parser0  s    r@   c                 r   t        | j                        | _        | j                  j                  | j                         | j                  j                         | _        t               j                  | j                  | j                        D ]  } | j                  j                         | _
        d | _        y r   )r#   r(  _cp
initializer  rh   rW   r!   r  step_cpstepr,  )r;   	_new_edges     r>   r  zChartParserApp._reset_parser5  s}    &t}}5DLL)hhnn& &--dkk4==II J xx}} r@   c                    t        t               d         | _        |j                  d| j                         t	        |      | _        | j
                  j                  | j                  j                  d             t        dd| j
                  j                               | _	        t        d| j
                  j                               | _
        y )	Nr2   r$  r%  r"  r7   r9   r  r#  )r   r   r(  r)  r   _sizer   cgetr  r&  r'  r   s     r>   r  zChartParserApp._init_fontsD  s    &(6"23/ D\


t}}))&12[djjnnFVW4::>>3CD
r@   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_animationP  sO    DJJ'


q tzz*! r@   c                     t        | j                  |dd      | _        | j                  j                  d| j                         y )Nr   )r  r  r   )r  rW   _cvr   _click_cv_edge)r;   rg   s     r>   r  zChartParserApp._init_chartview\  s2    T[[&AQOh(;(;<r@   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:)ro   r2   r  r  r  )r   r   r   r2   r&  r%  Step)variablero   r$  )r   r&  _rulelabel1_rulelabel2rw   r   r  )r;   rg   ruletxtr  s       r>   r  zChartParserApp._init_rulelabel`  s    + gDNNK "Xc
 	6*6*6DJJVD		w	r@   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 )Nrq   r#  r.  r   rr   zReset
Parserz#90c0d0r   )ro   r   r.   rp   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   rw   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;   rg   r  r  s       r>   ro  zChartParserApp._init_buttonsl  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$
r@   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>rV   rc  z<F1>z<Control-s>r  z<Control-r>r  r  r  z<space>z<Control-g>z<Control-t>rZ  c                 $    |j                  d      S r   r   r   as     r>   <lambda>z/ChartParserApp._init_bindings.<locals>.<lambda>      ar@   r  c                 $    |j                  d      S )Nr{   r  r  s     r>   r  z/ChartParserApp._init_bindings.<locals>.<lambda>  r  r@   r  c                 $    |j                  d      S )Nr   r  r  s     r>   r  z/ChartParserApp._init_bindings.<locals>.<lambda>  r  r@   rc  c                 B    |j                  |j                                S r   )r   r  )r   rc  s     r>   r  z/ChartParserApp._init_bindings.<locals>.<lambda>  s    QUUquuw;5Gr@   )r[   r]   r  r8  r;  r?  rA  r^   help
save_chartrq  r  r  r  r  _stop_animationedit_grammaredit_sentencer  r  r   s    r>   rp  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Hr@   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   ry  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  rq  )rE  r3   rp   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  rc  )rE  r3   r  r}  zNo Animation)rE  r3   r  valuezSlow AnimationrZ  )rE  r3   r  r)  r}  zNormal Animationr{   r  zFast Animationr   r  AnimateTinyr   )rE  r  r3   r)  rp   Smallr  Medium   Large   Huge   ZoomAboutInstructionsF1Helpr  ) r	   r[   r  r  rq  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>   rl  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'r@   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!r@   c                 \    | j                  |       | j                  j                  |       y r   )rG  r  r   r   s     r>   _select_matrix_edgez"ChartParserApp._select_matrix_edge  s"    $4 r@   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>   rG  zChartParserApp._select_edge  sf    tV,4 <<LL&&t,<<LL""4( r@   c                     d | _         | j                  j                          | j                  j                          | j                  r| j                  j                          y y r   )r,  r  r   rP  r  r   s    r>   _deselect_edgezChartParserApp._deselect_edge  sE    <<LL$$& r@   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  & r@   c                     d| _         	 t        | j                  dt        xs dj	                         dd       y #  t        | j                  dt        xs dj	                         d       Y y xY w)Nr   zHelp: Chart Parser Applicationr`  K   fixed)r   r2   r  )r   r   r[   r  stripr   s     r>   r  zChartParserApp.help  sb    	

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

0B%%'	s	   0: 1A-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:r@   r  r  ))zPlaintext grammar file.cfgr  r  c                 ,   t        | j                  d      }|sy	 t        |d      5 }t        j                  |      }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  r  rW   r  r   r  r   r   r  r  r  r   )r;   r   rt  r  rh   r   s         r>   rq  zChartParserApp.load_chart  s    "++i
 	Sh%F+ &DKHHOOE"||&&u-||**,}}''.HHu% &%  	S	Ss.   D C8B9D 8D=D 	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  rW   r  r   r  s        r>   r  zChartParserApp.save_chart  sc    $++i
 	Rh%DKK1 &%% 	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	        j
                  |      }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 filerW  r  Nr  r  zError Loading GrammarUnable to open file: %r)r   GRAMMAR_FILE_TYPESr  r  r  r  r   
fromstringreadrU  r  r   )r;   r   rt  r  r1  r   s         r>   r9  zChartParserApp.load_grammar  s    "--
 		U  +(D)V$kk&1G *) (^v!nnV[[];G $W% *) $^  	U-/H8/STT	UsF   B9 B!B9 #$B-B9 !B*&B9 -B62B9 9	C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)	NrW  r  r  r  r  z%s
zError Saving Grammarr[  )r   r\  r  r  r  r  rW   r  r(  productionsrC   r   writer  r   )
r;   r   rt  r  prodspr   restprodr   s
             r>   r8  zChartParserApp.save_grammar	  s[   $--
 	T  +(D)WKKdll ;WE *) (C(G MM557E(-R1DMM<O<O<Q1QQER',Qu!4==;N;N;P0PAuDQ %ftm4 !& $ftm4 !% )( *)
 SQ )(  	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   rW   r  r   r   r  r  s     r>   r  zChartParserApp.reset$	  s    $<<LL""4;;/<<LL&&(==MM##DKK0 r@   c                 Z    t        | j                  | j                  | j                         y r   )r   r[   r(  rU  r   s     r>   r!  zChartParserApp.edit_grammar3	  s    $**dmmT-=-=>r@   c                     || _         | j                  j                  |       | j                  r| j                  j                  |       y y r   )r(  r  rU  r  rT  s     r>   rU  zChartParserApp.set_grammar6	  s9    W%==MM%%g. r@   c                     dj                  | j                        }d}d}t        | j                  ||| j                  |       y )NrL  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Jr@   c                 `    t        |j                               | _        | j                          y r   )r   splitr  r  )r;   rk  s     r>   rj  zChartParserApp.set_sentenceB	  s    HNN,-

r@   c                     | j                   | j                   j                          t        | j                  | j                        | _         | j                   j                  d| j                         y r   )r  r^   rT   r[   rW   r   rI  r   s     r>   r:  zChartParserApp.view_matrixJ	  sL    <<#LL  "&tzz4;;?!!(D,D,DEr@   c                     | j                   | j                   j                          t        | j                  | j                  | j
                        | _         y r   )r  r^   r!  r[   rW   r(  r   s     r>   r;  zChartParserApp.view_resultsP	  s8    ==$MM!!#(T[[$--Pr@   c                 d    d| _         | j                  | j                  j                                y r   )r   rK  r  r  r   s    r>   r>  zChartParserApp.resizeY	  s"    4::>>+,r@   c                    | j                   j                  |       | j                  j                  t	        |              | j
                  j                  t	        |              | j                  j                  t	        |              y rH  )r  rK  r'  rE  rI  r&  r(  rJ  s     r>   rK  zChartParserApp.set_font_size]	  se    t$

3t9*-  s4yj 1c$iZ0r@   c                 H    t        | j                  j                               S r   )rI  r  r  r   s    r>   rM  zChartParserApp.get_font_sizec	  s    4::>>#$$r@   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   rN  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_strategyj	  s@   ??DO 	4  ::>>I%M,E%%}Y'?&@A..0 ?HH((*&*DO%%h/$$& # HH!!(+}}  ""#&&( LLD| ) !<<LL'')==MM((* !r@   c                     d| _         y r   )r   r   s     r>   r   zChartParserApp._stop_animation	  s	    r@   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   rv  r  r  r  r[   afterrw  )r;   speeds     r>   rw  z ChartParserApp._animate_strategy	  s    ??a!-}}  "a'4::>>+;q+@}}  "a'

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

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

  T%;%;< .r@   c                 V    t        | j                        }|| j                  |       |S r   )nextr  rP  )r;   new_edges     r>   rv  zChartParserApp._apply_strategy	  s)    %)r@   c                     |d| j                   d<   y t        |      }|| j                   d<   | j                  j                         }y )Nr`  ro   )r  r^  r  rM  )r;   ruler  r"  s       r>   rN  zChartParserApp._display_rule	  sB    <')DV$t9D'+DV$88))+Dr@   c                 <    | j                  | j                  d        y r   )r|  _TD_INITr   s     r>   r  zChartParserApp.top_down_init	  s    DMM40r@   c                 D    | j                  | j                  t               y r   )r|  _TD_PREDICTr  r   s     r>   r  zChartParserApp.top_down_predict	  s    D,,.DEr@   c                 D    | j                  | j                  t               y r   )r|  _BU_RULEr  r   s     r>   r  zChartParserApp.bottom_up	  s    DMM+;<r@   c                 D    | j                  | j                  t               y r   )r|  _BU_LC_RULEr  r   s     r>   r  z#ChartParserApp.bottom_up_leftcorner	  s    D,,.HIr@   c                 D    | j                  | j                  t               y r   )r|  _FUNDAMENTALr  r   s     r>   r  zChartParserApp.fundamental	  s    D--/BCr@   c                 D    | j                  | j                  t               y r   )r|  _BU_STRATEGYr  r   s     r>   r  z!ChartParserApp.bottom_up_strategy	  s    D--/?@r@   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Mr@   c                 D    | j                  | j                  t               y r   )r|  _TD_STRATEGYr  r   s     r>   r  z ChartParserApp.top_down_strategy	  s    D--/EFr@   )zChart Parser Applicationr   )r   )@rN   rO   rP   rk   r^   rw  r  r  r  r  r  r  ro  rp  rl  r  rI  rG  rL  rP  r  r?  r  r\  rq  r  r9  r8  r  r!  rU  r"  rj  r:  r;  r>  rK  rM  r|  r   rw  rv  rN  r$   r  r%   r  r   r  r   r  r"   r  r  r  r  r  r  r  r  r  r  r  r  rR   r@   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r@   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   rn  r  r`  r  r  rw  )r1  sentr  r  s       r>   appr  	  s    nn	G  8D+D$**,F	,##%vtDzC'() &	#J	-&
 !	
897F#,,.r@   __main__r  )Br  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.treer'   	nltk.utilr(   r*   rT   r!  rY  r  r  r  r  r  r  r  r  rN   __all__rR   r@   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 'r@   